Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: pkg/polymer/lib/observe.dart

Issue 23224003: move polymer.dart into dart svn (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: add --deploy to todomvc sample Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/polymer/lib/dwc.dart ('k') | pkg/polymer/lib/observe_html.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 /**
6 * Helpers for observable objects.
7 * Intended for use with `package:observe`.
8 */
9 library polymer.observe;
10
11 import 'dart:async';
12 import 'package:observe/observe.dart';
13
14 const _VALUE = const Symbol('value');
15
16 /**
17 * Forwards an observable property from one object to another. For example:
18 *
19 * class MyModel extends ObservableBase {
20 * StreamSubscription _sub;
21 * MyOtherModel _otherModel;
22 *
23 * MyModel() {
24 * ...
25 * _sub = bindProperty(_otherModel, const Symbol('value'),
26 * () => notifyProperty(this, const Symbol('prop'));
27 * }
28 *
29 * String get prop => _otherModel.value;
30 * set prop(String value) { _otherModel.value = value; }
31 * }
32 *
33 * See also [notifyProperty].
34 */
35 StreamSubscription bindProperty(Observable source, Symbol sourceName,
36 void callback()) {
37 return source.changes.listen((records) {
38 for (var record in records) {
39 if (record.changes(sourceName)) {
40 callback();
41 }
42 }
43 });
44 }
45
46 /**
47 * Notify the property change. Shorthand for:
48 *
49 * target.notifyChange(new PropertyChangeRecord(targetName));
50 */
51 void notifyProperty(Observable target, Symbol targetName) {
52 target.notifyChange(new PropertyChangeRecord(targetName));
53 }
54
55
56 // Inspired by ArrayReduction at:
57 // https://raw.github.com/rafaelw/ChangeSummary/master/util/array_reduction.js
58 // The main difference is we support anything on the rich Dart Iterable API.
59
60 /**
61 * Observes a path starting from each item in the list.
62 */
63 class ListPathObserver<E, P> extends ChangeNotifierBase {
64 final ObservableList<E> list;
65 final String _itemPath;
66 final List<PathObserver> _observers = <PathObserver>[];
67 final List<StreamSubscription> _subs = <StreamSubscription>[];
68 StreamSubscription _sub;
69 bool _scheduled = false;
70 Iterable<P> _value;
71
72 ListPathObserver(this.list, String path)
73 : _itemPath = path {
74
75 _sub = list.changes.listen((records) {
76 for (var record in records) {
77 if (record is ListChangeRecord) {
78 _observeItems(record.addedCount - record.removedCount);
79 }
80 }
81 _scheduleReduce(null);
82 });
83
84 _observeItems(list.length);
85 _reduce();
86 }
87
88 Iterable<P> get value => _value;
89
90 void dispose() {
91 if (_sub != null) _sub.cancel();
92 _subs.forEach((s) => s.cancel());
93 _subs.clear();
94 }
95
96 void _reduce() {
97 _scheduled = false;
98 _value = _observers.map((o) => o.value);
99 notifyChange(new PropertyChangeRecord(_VALUE));
100 }
101
102 void _scheduleReduce(_) {
103 if (_scheduled) return;
104 _scheduled = true;
105 runAsync(_reduce);
106 }
107
108 void _observeItems(int lengthAdjust) {
109 if (lengthAdjust > 0) {
110 for (int i = 0; i < lengthAdjust; i++) {
111 int len = _observers.length;
112 var pathObs = new PathObserver(list, '$len.$_itemPath');
113 _subs.add(pathObs.changes.listen(_scheduleReduce));
114 _observers.add(pathObs);
115 }
116 } else if (lengthAdjust < 0) {
117 for (int i = 0; i < -lengthAdjust; i++) {
118 _subs.removeLast().cancel();
119 }
120 int len = _observers.length;
121 _observers.removeRange(len + lengthAdjust, len);
122 }
123 }
124 }
OLDNEW
« no previous file with comments | « pkg/polymer/lib/dwc.dart ('k') | pkg/polymer/lib/observe_html.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698