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 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 var newValue = _observers.map((o) => o.value).toList(); |
| 89 if (_computeValue != null) newValue = _computeValue(newValue); |
| 90 _value = notifyPropertyChange(#value, _value, newValue); |
| 91 } |
| 92 |
| 93 /** |
| 94 * Closes the observer. |
| 95 * |
| 96 * This happens automatically if the [value] property is no longer observed, |
| 97 * but this can also be called explicitly. |
| 98 */ |
| 99 void close() { |
| 100 if (_observers.isEmpty) return; |
| 101 |
| 102 if (_started) { |
| 103 for (StreamSubscription sub in _subs) { |
| 104 sub.cancel(); |
| 105 } |
| 106 } |
| 107 _observers.clear(); |
| 108 _subs.clear(); |
| 109 _value = null; |
| 110 } |
| 111 |
| 112 observed() => start(); |
| 113 unobserved() => close(); |
| 114 } |
OLD | NEW |