OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of observe; | 5 part of observe; |
6 | 6 |
7 /** | 7 /** |
8 * Base class implementing [Observable] object that performs its own change | 8 * Mixin and base class for implementing an [Observable] object that performs |
9 * notifications, and does not need to be considered by [Observable.dirtyCheck]. | 9 * its own change notifications, and does not need to be considered by |
| 10 * [Observable.dirtyCheck]. |
10 * | 11 * |
11 * When a field, property, or indexable item is changed, a derived class should | 12 * When a field, property, or indexable item is changed, a derived class should |
12 * call [notifyPropertyChange]. See that method for an example. | 13 * call [notifyPropertyChange]. See that method for an example. |
13 */ | 14 */ |
14 class ChangeNotifierBase = Object with ChangeNotifierMixin; | 15 abstract class ChangeNotifier implements Observable { |
15 | |
16 /** | |
17 * Mixin implementing [Observable] object that performs its own change | |
18 * notifications, and does not need to be considered by [Observable.dirtyCheck]. | |
19 * | |
20 * When a field, property, or indexable item is changed, a derived class should | |
21 * call [notifyPropertyChange]. See that method for an example. | |
22 */ | |
23 abstract class ChangeNotifierMixin implements Observable { | |
24 StreamController _changes; | 16 StreamController _changes; |
25 List<ChangeRecord> _records; | 17 List<ChangeRecord> _records; |
26 | 18 |
27 Stream<List<ChangeRecord>> get changes { | 19 Stream<List<ChangeRecord>> get changes { |
28 if (_changes == null) { | 20 if (_changes == null) { |
29 _changes = new StreamController.broadcast(sync: true, | 21 _changes = new StreamController.broadcast(sync: true, |
30 onListen: _observed, onCancel: _unobserved); | 22 onListen: _observed, onCancel: _unobserved); |
31 } | 23 } |
32 return _changes.stream; | 24 return _changes.stream; |
33 } | 25 } |
(...skipping 24 matching lines...) Expand all Loading... |
58 /** | 50 /** |
59 * True if this object has any observers, and should call | 51 * True if this object has any observers, and should call |
60 * [notifyPropertyChange] for changes. | 52 * [notifyPropertyChange] for changes. |
61 */ | 53 */ |
62 bool get hasObservers => _changes != null && _changes.hasListener; | 54 bool get hasObservers => _changes != null && _changes.hasListener; |
63 | 55 |
64 /** | 56 /** |
65 * Notify that the field [name] of this object has been changed. | 57 * Notify that the field [name] of this object has been changed. |
66 * | 58 * |
67 * The [oldValue] and [newValue] are also recorded. If the two values are | 59 * The [oldValue] and [newValue] are also recorded. If the two values are |
68 * identical, no change will be recorded. | 60 * equal, no change will be recorded. |
69 * | 61 * |
70 * For convenience this returns [newValue]. This makes it easy to use in a | 62 * For convenience this returns [newValue]. This makes it easy to use in a |
71 * setter: | 63 * setter: |
72 * | 64 * |
73 * var _myField; | 65 * var _myField; |
74 * @reflectable get myField => _myField; | 66 * @reflectable get myField => _myField; |
75 * @reflectable set myField(value) { | 67 * @reflectable set myField(value) { |
76 * _myField = notifyPropertyChange(#myField, _myField, value); | 68 * _myField = notifyPropertyChange(#myField, _myField, value); |
77 * } | 69 * } |
78 */ | 70 */ |
79 notifyPropertyChange(Symbol field, Object oldValue, Object newValue) | 71 notifyPropertyChange(Symbol field, Object oldValue, Object newValue) |
80 => _notifyPropertyChange(this, field, oldValue, newValue); | 72 => _notifyPropertyChange(this, field, oldValue, newValue); |
81 | 73 |
82 void notifyChange(ChangeRecord record) { | 74 void notifyChange(ChangeRecord record) { |
83 if (!hasObservers) return; | 75 if (!hasObservers) return; |
84 | 76 |
85 if (_records == null) { | 77 if (_records == null) { |
86 _records = []; | 78 _records = []; |
87 scheduleMicrotask(deliverChanges); | 79 scheduleMicrotask(deliverChanges); |
88 } | 80 } |
89 _records.add(record); | 81 _records.add(record); |
90 } | 82 } |
91 } | 83 } |
OLD | NEW |