| 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 library observe.src.change_notifier; | 5 library observe.src.change_notifier; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection' show UnmodifiableListView; | 8 import 'dart:collection' show UnmodifiableListView; |
| 9 import 'package:observe/observe.dart'; | 9 import 'package:observe/observe.dart'; |
| 10 import 'package:observe/src/observable.dart' show notifyPropertyChangeHelper; | 10 import 'package:observe/src/observable.dart' show notifyPropertyChangeHelper; |
| 11 | 11 |
| 12 /// Mixin and base class for implementing an [Observable] object that performs | 12 /// Mixin and base class for implementing an [Observable] object that performs |
| 13 /// its own change notifications, and does not need to be considered by | 13 /// its own change notifications, and does not need to be considered by |
| 14 /// [Observable.dirtyCheck]. | 14 /// [Observable.dirtyCheck]. |
| 15 /// | 15 /// |
| 16 /// When a field, property, or indexable item is changed, a derived class should | 16 /// When a field, property, or indexable item is changed, a derived class should |
| 17 /// call [notifyPropertyChange]. See that method for an example. | 17 /// call [notifyPropertyChange]. See that method for an example. |
| 18 abstract class ChangeNotifier implements Observable { | 18 abstract class ChangeNotifier implements Observable { |
| 19 StreamController _changes; | 19 StreamController<List<ChangeRecord>> _changes; |
| 20 List<ChangeRecord> _records; | 20 List<ChangeRecord> _records; |
| 21 | 21 |
| 22 Stream<List<ChangeRecord>> get changes { | 22 Stream<List<ChangeRecord>> get changes { |
| 23 if (_changes == null) { | 23 if (_changes == null) { |
| 24 _changes = new StreamController.broadcast(sync: true, | 24 _changes = new StreamController.broadcast( |
| 25 onListen: observed, onCancel: unobserved); | 25 sync: true, onListen: observed, onCancel: unobserved); |
| 26 } | 26 } |
| 27 return _changes.stream; | 27 return _changes.stream; |
| 28 } | 28 } |
| 29 | 29 |
| 30 // TODO(jmesserly): should these be public? They're useful lifecycle methods | 30 // TODO(jmesserly): should these be public? They're useful lifecycle methods |
| 31 // for subclasses. Ideally they'd be protected. | 31 // for subclasses. Ideally they'd be protected. |
| 32 /// Override this method to be called when the [changes] are first observed. | 32 /// Override this method to be called when the [changes] are first observed. |
| 33 void observed() {} | 33 void observed() {} |
| 34 | 34 |
| 35 /// Override this method to be called when the [changes] are no longer being | 35 /// Override this method to be called when the [changes] are no longer being |
| (...skipping 23 matching lines...) Expand all Loading... |
| 59 /// equal, no change will be recorded. | 59 /// equal, no change will be recorded. |
| 60 /// | 60 /// |
| 61 /// For convenience this returns [newValue]. This makes it easy to use in a | 61 /// For convenience this returns [newValue]. This makes it easy to use in a |
| 62 /// setter: | 62 /// setter: |
| 63 /// | 63 /// |
| 64 /// var _myField; | 64 /// var _myField; |
| 65 /// @reflectable get myField => _myField; | 65 /// @reflectable get myField => _myField; |
| 66 /// @reflectable set myField(value) { | 66 /// @reflectable set myField(value) { |
| 67 /// _myField = notifyPropertyChange(#myField, _myField, value); | 67 /// _myField = notifyPropertyChange(#myField, _myField, value); |
| 68 /// } | 68 /// } |
| 69 notifyPropertyChange(Symbol field, Object oldValue, Object newValue) | 69 /*=T*/ notifyPropertyChange /*<T>*/ ( |
| 70 => notifyPropertyChangeHelper(this, field, oldValue, newValue); | 70 Symbol field, /*=T*/ oldValue, /*=T*/ newValue) => |
| 71 notifyPropertyChangeHelper /*<T>*/ (this, field, oldValue, newValue); |
| 71 | 72 |
| 72 void notifyChange(ChangeRecord record) { | 73 void notifyChange(ChangeRecord record) { |
| 73 if (!hasObservers) return; | 74 if (!hasObservers) return; |
| 74 | 75 |
| 75 if (_records == null) { | 76 if (_records == null) { |
| 76 _records = []; | 77 _records = []; |
| 77 scheduleMicrotask(deliverChanges); | 78 scheduleMicrotask(deliverChanges); |
| 78 } | 79 } |
| 79 _records.add(record); | 80 _records.add(record); |
| 80 } | 81 } |
| 81 } | 82 } |
| OLD | NEW |