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