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

Side by Side Diff: pkg/observe/lib/src/compound_path_observer.dart

Issue 132403010: big update to observe, template_binding, polymer (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 10 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/observe/lib/src/bindable.dart ('k') | pkg/observe/lib/src/list_diff.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 library observe.src.compound_path_observer;
6
7 import 'dart:async';
8 import 'package:observe/observe.dart';
9
10 /**
11 * CompoundPathObserver is an object which knows how to listen to multiple path
12 * values (registered via [addPath]) and invoke a function when one or more
13 * of the values have changed. The result of this function will be set into the
14 * [value] property. When any value has changed, all current values are provided
15 * to the function in the single `values` argument.
16 *
17 * For example:
18 *
19 * var binding = new CompoundPathObserver(computeValue: (values) {
20 * var combinedValue;
21 * // compute combinedValue based on the current values which are provided
22 * return combinedValue;
23 * });
24 * binding.addPath(obj1, path1);
25 * binding.addPath(obj2, path2);
26 * //...
27 * binding.addPath(objN, pathN);
28 */
29 // TODO(jmesserly): this isn't a full port of CompoundPathObserver. It is only
30 // what was needed for TemplateBinding.
31 class CompoundPathObserver extends ChangeNotifier {
32 List<PathObserver> _observers = [];
33 List<StreamSubscription> _subs = [];
34 Object _value; // the last computed value
35
36 // TODO(jmesserly): this is public in observe.js
37 final Function _computeValue;
38
39 bool _started = false;
40
41 /** True if [start] has been called, otherwise false. */
42 bool get started => _started;
43
44 bool _scheduled = false;
45
46 /**
47 * Creates a new observer, optionally proving the [computeValue] function
48 * for computing the value. You can also set [schedule] to true if you plan
49 * to invoke [resolve] manually after initial construction of the binding.
50 */
51 CompoundPathObserver({computeValue(List values)})
52 : _computeValue = computeValue;
53
54 int get length => _observers.length;
55
56 @reflectable get value => _value;
57
58 void addPath(model, String path) {
59 if (_started) {
60 throw new StateError('Cannot add more paths once started.');
61 }
62
63 _observers.add(new PathObserver(model, path));
64 }
65
66 void start() {
67 if (_started) return;
68 _started = true;
69
70 final scheduleResolve = _scheduleResolve;
71 for (var observer in _observers) {
72 _subs.add(observer.changes.listen(scheduleResolve));
73 }
74 _resolve();
75 }
76
77 // TODO(rafaelw): Is this the right processing model?
78 // TODO(rafaelw): Consider having a seperate ChangeSummary for
79 // CompoundBindings so to excess dirtyChecks.
80 void _scheduleResolve(_) {
81 if (_scheduled) return;
82 _scheduled = true;
83 scheduleMicrotask(_resolve);
84 }
85
86 void _resolve() {
87 _scheduled = false;
88 if (_observers.isEmpty) return;
89 var newValue = _observers.map((o) => o.value).toList();
90 if (_computeValue != null) newValue = _computeValue(newValue);
91 _value = notifyPropertyChange(#value, _value, newValue);
92 }
93
94 /**
95 * Closes the observer.
96 *
97 * This happens automatically if the [value] property is no longer observed,
98 * but this can also be called explicitly.
99 */
100 void close() {
101 if (_observers.isEmpty) return;
102
103 if (_started) {
104 for (StreamSubscription sub in _subs) {
105 sub.cancel();
106 }
107 }
108 _observers.clear();
109 _subs.clear();
110 _value = null;
111 }
112
113 observed() => start();
114 unobserved() => close();
115 }
OLDNEW
« no previous file with comments | « pkg/observe/lib/src/bindable.dart ('k') | pkg/observe/lib/src/list_diff.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698