OLD | NEW |
---|---|
(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 part of observe; | |
6 | |
7 /** | |
8 * CompoundPathObserver is an object which knows how to listen to multiple path | |
9 * values (registered via [addPath]) and invoke a function when one or more | |
10 * of the values have changed. The result of this function will be set into the | |
11 * [value] property. When any value has changed, all current values are provided | |
12 * to the function in the single `values` argument. | |
13 * | |
14 * For example: | |
15 * | |
16 * var binding = new CompoundPathObserver(getValue: (values) { | |
17 * var combinedValue; | |
18 * // compute combinedValue based on the current values which are provided | |
19 * return combinedValue; | |
20 * }); | |
21 * binding.addPath(obj1, path1); | |
22 * binding.addPath(obj2, path2); | |
23 * //... | |
24 * binding.addPath(objN, pathN); | |
25 */ | |
26 // TODO(jmesserly): this isn't a full port of CompoundPathObserver. It is only | |
27 // what was needed for TemplateBinding. | |
28 class CompoundPathObserver extends ChangeNotifier { | |
29 List<PathObserver> _observers = []; | |
30 List<StreamSubscription> _subs = []; | |
31 Object _value; // the last computed value | |
32 | |
33 // TODO(jmesserly): this is public in observe.js | |
34 final Function _getValue; | |
35 | |
36 bool _started = false; | |
37 | |
38 /** True if [start] has been called, otherwise false. */ | |
39 bool get started => _started; | |
40 | |
41 bool _scheduled = false; | |
42 | |
43 /** | |
44 * Creates a new observer, optionally proving the [getValue] function | |
45 * for computing the value. You can also set [schedule] to true if you plan | |
46 * to invoke [resolve] manually after initial construction of the binding. | |
47 */ | |
48 CompoundPathObserver({getValue(List values)}) : _getValue = getValue; | |
Siggi Cherem (dart-lang)
2013/10/29 21:00:07
should getValue be mandatory? What would we do if
Jennifer Messerly
2013/10/29 22:35:07
it should returns a list of the values. My bad. Fi
| |
49 | |
50 int get length => _observers.length; | |
51 | |
52 @reflectable get value => _value; | |
53 | |
54 @reflectable void set value(newValue) { | |
Siggi Cherem (dart-lang)
2013/10/29 21:00:07
should the setter be public?
I was expecting the
Jennifer Messerly
2013/10/29 22:35:07
good catch. removed setter.
| |
55 _value = notifyPropertyChange(#value, _value, newValue); | |
56 } | |
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 value = _getValue(_observers.map((o) => o.value).toList()); | |
Siggi Cherem (dart-lang)
2013/10/29 21:00:07
check for _getValue == null?
Jennifer Messerly
2013/10/29 22:35:07
Done.
| |
89 } | |
90 | |
91 /** | |
92 * Closes the observer. | |
93 * | |
94 * This happens automatically if the [value] property is no longer observed, | |
95 * but this can also be called explicitly. | |
96 */ | |
97 void close() { | |
98 if (_observers.isEmpty) return; | |
99 | |
100 if (_started) { | |
101 for (StreamSubscription sub in _subs) { | |
102 sub.cancel(); | |
103 } | |
104 } | |
105 _observers.clear(); | |
106 _subs.clear(); | |
107 value = null; | |
108 } | |
109 | |
110 _observed() => start(); | |
111 _unobserved() => close(); | |
112 } | |
OLD | NEW |