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 * Interface representing an [Observable] object that performs its own change |
| 9 * notifications, and does not need to be considered by [Observable.dirtyCheck]. |
| 10 */ |
| 11 abstract class ChangeNotifier extends Observable { |
| 12 /** |
| 13 * Notify observers of a change. |
| 14 * |
| 15 * For most objects [ChangeNotifierMixin.notifyPropertyChange] is more |
| 16 * convenient, but collections sometimes deliver other types of changes such |
| 17 * as a [ListChangeRecord]. |
| 18 */ |
| 19 void notifyChange(ChangeRecord record); |
| 20 } |
| 21 |
| 22 /** |
| 23 * Base class implementing [ChangeNotifier]. |
| 24 * |
| 25 * When a field, property, or indexable item is changed, a derived class should |
| 26 * call [notifyPropertyChange]. See that method for an example. |
| 27 */ |
| 28 typedef ChangeNotifierBase = Object with ChangeNotifierMixin; |
| 29 |
| 30 /** |
| 31 * Mixin for implementing [ChangeNotifier] objects. |
| 32 * |
| 33 * When a field, property, or indexable item is changed, a derived class should |
| 34 * call [notifyPropertyChange]. See that method for an example. |
| 35 */ |
| 36 abstract class ChangeNotifierMixin implements ChangeNotifier { |
| 37 StreamController _changes; |
| 38 List<ChangeRecord> _records; |
| 39 |
| 40 Stream<List<ChangeRecord>> get changes { |
| 41 if (_changes == null) { |
| 42 _changes = new StreamController.broadcast(sync: true, |
| 43 onListen: _observed, onCancel: _unobserved); |
| 44 } |
| 45 return _changes.stream; |
| 46 } |
| 47 |
| 48 // TODO(jmesserly): should these be public? They're useful lifecycle methods |
| 49 // for subclasses. Ideally they'd be protected. |
| 50 /** |
| 51 * Override this method to be called when the [changes] are first observed. |
| 52 */ |
| 53 void _observed() {} |
| 54 |
| 55 /** |
| 56 * Override this method to be called when the [changes] are no longer being |
| 57 * observed. |
| 58 */ |
| 59 void _unobserved() {} |
| 60 |
| 61 bool deliverChanges() { |
| 62 var records = _records; |
| 63 _records = null; |
| 64 if (hasObservers && records != null) { |
| 65 // TODO(jmesserly): make "records" immutable |
| 66 _changes.add(records); |
| 67 return true; |
| 68 } |
| 69 return false; |
| 70 } |
| 71 |
| 72 /** |
| 73 * True if this object has any observers, and should call |
| 74 * [notifyPropertyChange] for changes. |
| 75 */ |
| 76 bool get hasObservers => _changes != null && _changes.hasListener; |
| 77 |
| 78 /** |
| 79 * Notify that the field [name] of this object has been changed. |
| 80 * |
| 81 * The [oldValue] and [newValue] are also recorded. If the two values are |
| 82 * identical, no change will be recorded. |
| 83 * |
| 84 * For convenience this returns [newValue]. This makes it easy to use in a |
| 85 * setter: |
| 86 * |
| 87 * var _myField; |
| 88 * get myField => _myField; |
| 89 * set myField(value) { |
| 90 * _myField = notifyPropertyChange( |
| 91 * const Symbol('myField'), _myField, value); |
| 92 * } |
| 93 */ |
| 94 // TODO(jmesserly): should this be == instead of identical, to prevent |
| 95 // spurious loops? |
| 96 notifyPropertyChange(Symbol field, Object oldValue, Object newValue) { |
| 97 if (hasObservers && !identical(oldValue, newValue)) { |
| 98 notifyChange(new PropertyChangeRecord(field)); |
| 99 } |
| 100 return newValue; |
| 101 } |
| 102 |
| 103 void notifyChange(ChangeRecord record) { |
| 104 if (!hasObservers) return; |
| 105 |
| 106 if (_records == null) { |
| 107 _records = []; |
| 108 runAsync(deliverChanges); |
| 109 } |
| 110 _records.add(record); |
| 111 } |
| 112 } |
OLD | NEW |