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

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

Issue 50203004: port TemplateBinding to ed3266266e751b5ab1f75f8e0509d0d5f0ef35d8 (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: round 1 Created 7 years, 1 month 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
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(getValue: (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 _getValue;
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 [getValue] 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({getValue(List values)}) : _getValue = getValue;
52
53 int get length => _observers.length;
54
55 @reflectable get value => _value;
56
57 void addPath(model, String path) {
58 if (_started) {
59 throw new StateError('Cannot add more paths once started.');
60 }
61
62 _observers.add(new PathObserver(model, path));
63 }
64
65 void start() {
66 if (_started) return;
67 _started = true;
68
69 final scheduleResolve = _scheduleResolve;
70 for (var observer in _observers) {
71 _subs.add(observer.changes.listen(scheduleResolve));
72 }
73 _resolve();
74 }
75
76 // TODO(rafaelw): Is this the right processing model?
77 // TODO(rafaelw): Consider having a seperate ChangeSummary for
78 // CompoundBindings so to excess dirtyChecks.
79 void _scheduleResolve(_) {
80 if (_scheduled) return;
81 _scheduled = true;
82 scheduleMicrotask(_resolve);
83 }
84
85 void _resolve() {
86 _scheduled = false;
87 var newValue = _observers.map((o) => o.value).toList();
88 if (_getValue != null) newValue = _getValue(newValue);
89 _value = notifyPropertyChange(#value, _value, newValue);
90 }
91
92 /**
93 * Closes the observer.
94 *
95 * This happens automatically if the [value] property is no longer observed,
96 * but this can also be called explicitly.
97 */
98 void close() {
99 if (_observers.isEmpty) return;
100
101 if (_started) {
102 for (StreamSubscription sub in _subs) {
103 sub.cancel();
104 }
105 }
106 _observers.clear();
107 _subs.clear();
108 _value = null;
109 }
110
111 observed() => start();
112 unobserved() => close();
113 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698