Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: pkg/observe/lib/src/change_notifier.dart

Issue 178683003: [observe] use consistent comment style (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/observe/lib/src/bind_property.dart ('k') | pkg/observe/lib/src/change_record.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 /** 12 /// Mixin and base class for implementing an [Observable] object that performs
13 * Mixin and base class for implementing an [Observable] object that performs 13 /// 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 14 /// [Observable.dirtyCheck].
15 * [Observable.dirtyCheck]. 15 ///
16 * 16 /// 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 17 /// call [notifyPropertyChange]. See that method for an example.
18 * call [notifyPropertyChange]. See that method for an example.
19 */
20 abstract class ChangeNotifier implements Observable { 18 abstract class ChangeNotifier implements Observable {
21 StreamController _changes; 19 StreamController _changes;
22 List<ChangeRecord> _records; 20 List<ChangeRecord> _records;
23 21
24 Stream<List<ChangeRecord>> get changes { 22 Stream<List<ChangeRecord>> get changes {
25 if (_changes == null) { 23 if (_changes == null) {
26 _changes = new StreamController.broadcast(sync: true, 24 _changes = new StreamController.broadcast(sync: true,
27 onListen: observed, onCancel: unobserved); 25 onListen: observed, onCancel: unobserved);
28 } 26 }
29 return _changes.stream; 27 return _changes.stream;
30 } 28 }
31 29
32 // TODO(jmesserly): should these be public? They're useful lifecycle methods 30 // TODO(jmesserly): should these be public? They're useful lifecycle methods
33 // for subclasses. Ideally they'd be protected. 31 // for subclasses. Ideally they'd be protected.
34 /** 32 /// 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.
36 */
37 void observed() {} 33 void observed() {}
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.
42 */
43 void unobserved() { 37 void unobserved() {
44 // Free some memory 38 // Free some memory
45 _changes = null; 39 _changes = null;
46 } 40 }
47 41
48 bool deliverChanges() { 42 bool deliverChanges() {
49 var records = _records; 43 var records = _records;
50 _records = null; 44 _records = null;
51 if (hasObservers && records != null) { 45 if (hasObservers && records != null) {
52 _changes.add(new UnmodifiableListView<ChangeRecord>(records)); 46 _changes.add(new UnmodifiableListView<ChangeRecord>(records));
53 return true; 47 return true;
54 } 48 }
55 return false; 49 return false;
56 } 50 }
57 51
58 /** 52 /// True if this object has any observers, and should call
59 * True if this object has any observers, and should call 53 /// [notifyPropertyChange] for changes.
60 * [notifyPropertyChange] for changes.
61 */
62 bool get hasObservers => _changes != null && _changes.hasListener; 54 bool get hasObservers => _changes != null && _changes.hasListener;
63 55
64 /** 56 /// Notify that the field [name] of this object has been changed.
65 * Notify that the field [name] of this object has been changed. 57 ///
66 * 58 /// The [oldValue] and [newValue] are also recorded. If the two values are
67 * The [oldValue] and [newValue] are also recorded. If the two values are 59 /// equal, no change will be recorded.
68 * equal, no change will be recorded. 60 ///
69 * 61 /// For convenience this returns [newValue]. This makes it easy to use in a
70 * For convenience this returns [newValue]. This makes it easy to use in a 62 /// setter:
71 * setter: 63 ///
72 * 64 /// var _myField;
73 * var _myField; 65 /// @reflectable get myField => _myField;
74 * @reflectable get myField => _myField; 66 /// @reflectable set myField(value) {
75 * @reflectable set myField(value) { 67 /// _myField = notifyPropertyChange(#myField, _myField, value);
76 * _myField = notifyPropertyChange(#myField, _myField, value); 68 /// }
77 * }
78 */
79 notifyPropertyChange(Symbol field, Object oldValue, Object newValue) 69 notifyPropertyChange(Symbol field, Object oldValue, Object newValue)
80 => notifyPropertyChangeHelper(this, field, oldValue, newValue); 70 => notifyPropertyChangeHelper(this, field, oldValue, newValue);
81 71
82 void notifyChange(ChangeRecord record) { 72 void notifyChange(ChangeRecord record) {
83 if (!hasObservers) return; 73 if (!hasObservers) return;
84 74
85 if (_records == null) { 75 if (_records == null) {
86 _records = []; 76 _records = [];
87 scheduleMicrotask(deliverChanges); 77 scheduleMicrotask(deliverChanges);
88 } 78 }
89 _records.add(record); 79 _records.add(record);
90 } 80 }
91 } 81 }
OLDNEW
« no previous file with comments | « pkg/observe/lib/src/bind_property.dart ('k') | pkg/observe/lib/src/change_record.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698