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