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