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 /** Records a change to an [Observable]. */ | 7 /** Records a change to an [Observable]. */ |
8 // TODO(jmesserly): remove this type | 8 // TODO(jmesserly): remove this type |
9 abstract class ChangeRecord {} | 9 abstract class ChangeRecord {} |
10 | 10 |
11 /** A change record to a field of an observable object. */ | 11 /** A change record to a field of an observable object. */ |
12 class PropertyChangeRecord<T> extends ChangeRecord { | 12 class PropertyChangeRecord<T> extends ChangeRecord { |
13 /** | |
14 * *Deprecated* use [name] instead. | |
15 * The field that was changed. | |
16 */ | |
17 @deprecated | |
18 Symbol get field => name; | |
19 | |
20 /** The object that changed. */ | 13 /** The object that changed. */ |
21 final object; | 14 final object; |
22 | 15 |
23 /** The name of the property that changed. */ | 16 /** The name of the property that changed. */ |
24 final Symbol name; | 17 final Symbol name; |
25 | 18 |
26 /** The previous value of the property. */ | 19 /** The previous value of the property. */ |
27 final T oldValue; | 20 final T oldValue; |
28 | 21 |
29 /** The new value of the property. */ | 22 /** The new value of the property. */ |
30 final T newValue; | 23 final T newValue; |
31 | 24 |
32 PropertyChangeRecord(this.object, this.name, this.oldValue, this.newValue); | 25 PropertyChangeRecord(this.object, this.name, this.oldValue, this.newValue); |
33 | 26 |
34 /* | |
35 * *Deprecated* instead of `record.changes(key)` simply do | |
36 * `key == record.name`. | |
37 */ | |
38 @deprecated | |
39 bool changes(key) => key is Symbol && name == key; | |
40 | |
41 String toString() => | 27 String toString() => |
42 '#<PropertyChangeRecord $name from: $oldValue to: $newValue>'; | 28 '#<PropertyChangeRecord $name from: $oldValue to: $newValue>'; |
43 } | 29 } |
44 | 30 |
45 /** A change record for an observable list. */ | 31 /** A change record for an observable list. */ |
46 class ListChangeRecord extends ChangeRecord { | 32 class ListChangeRecord extends ChangeRecord { |
47 /** The starting index of the change. */ | 33 /** The starting index of the change. */ |
48 final int index; | 34 final int index; |
49 | 35 |
50 /** The number of items removed. */ | 36 /** The number of items removed. */ |
51 final int removedCount; | 37 final int removedCount; |
52 | 38 |
53 /** The number of items added. */ | 39 /** The number of items added. */ |
54 final int addedCount; | 40 final int addedCount; |
55 | 41 |
56 ListChangeRecord(this.index, {this.removedCount: 0, this.addedCount: 0}) { | 42 ListChangeRecord(this.index, {this.removedCount: 0, this.addedCount: 0}) { |
57 if (addedCount == 0 && removedCount == 0) { | 43 if (addedCount == 0 && removedCount == 0) { |
58 throw new ArgumentError('added and removed counts should not both be ' | 44 throw new ArgumentError('added and removed counts should not both be ' |
59 'zero. Use 1 if this was a single item update.'); | 45 'zero. Use 1 if this was a single item update.'); |
60 } | 46 } |
61 } | 47 } |
62 | 48 |
63 /** | |
64 * *Deprecated* use [indexChanged] instead. | |
65 * Returns true if the provided index was changed by this operation. | |
66 */ | |
67 @deprecated | |
68 bool changes(value) => indexChanged(value); | |
69 | |
70 /** Returns true if the provided index was changed by this operation. */ | 49 /** Returns true if the provided index was changed by this operation. */ |
71 bool indexChanged(otherIndex) { | 50 bool indexChanged(otherIndex) { |
72 // If key isn't an int, or before the index, then it wasn't changed. | 51 // If key isn't an int, or before the index, then it wasn't changed. |
73 if (otherIndex is! int || otherIndex < index) return false; | 52 if (otherIndex is! int || otherIndex < index) return false; |
74 | 53 |
75 // If this was a shift operation, anything after index is changed. | 54 // If this was a shift operation, anything after index is changed. |
76 if (addedCount != removedCount) return true; | 55 if (addedCount != removedCount) return true; |
77 | 56 |
78 // Otherwise, anything in the update range was changed. | 57 // Otherwise, anything in the update range was changed. |
79 return otherIndex < index + addedCount; | 58 return otherIndex < index + addedCount; |
80 } | 59 } |
81 | 60 |
82 String toString() => '#<ListChangeRecord index: $index, ' | 61 String toString() => '#<ListChangeRecord index: $index, ' |
83 'removed: $removedCount, addedCount: $addedCount>'; | 62 'removed: $removedCount, addedCount: $addedCount>'; |
84 } | 63 } |
OLD | NEW |