Chromium Code Reviews| Index: pkg/observe/lib/src/change_record.dart |
| diff --git a/pkg/observe/lib/src/change_record.dart b/pkg/observe/lib/src/change_record.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..652e89e071fbc6b7667ce9a740dc3c8c3bff3348 |
| --- /dev/null |
| +++ b/pkg/observe/lib/src/change_record.dart |
| @@ -0,0 +1,57 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +part of observe; |
| + |
| +/** Records a change to an [Observable]. */ |
| +abstract class ChangeRecord { |
|
Jennifer Messerly
2013/07/19 01:32:58
no changes here yet. just moved into its own file
|
| + /** True if the change affected the given item, otherwise false. */ |
| + bool change(key); |
| +} |
| + |
| +/** A change record to a field of an observable object. */ |
| +class PropertyChangeRecord extends ChangeRecord { |
| + /** The field that was changed. */ |
| + final Symbol field; |
| + |
| + PropertyChangeRecord(this.field); |
| + |
| + bool changes(key) => key is Symbol && field == key; |
| + |
| + String toString() => '#<PropertyChangeRecord $field>'; |
| +} |
| + |
| +/** A change record for an observable list. */ |
| +class ListChangeRecord extends ChangeRecord { |
| + /** The starting index of the change. */ |
| + final int index; |
| + |
| + /** The number of items removed. */ |
| + final int removedCount; |
| + |
| + /** The number of items added. */ |
| + final int addedCount; |
| + |
| + ListChangeRecord(this.index, {this.removedCount: 0, this.addedCount: 0}) { |
| + if (addedCount == 0 && removedCount == 0) { |
| + throw new ArgumentError('added and removed counts should not both be ' |
| + 'zero. Use 1 if this was a single item update.'); |
| + } |
| + } |
| + |
| + /** Returns true if the provided index was changed by this operation. */ |
| + bool changes(key) { |
| + // If key isn't an int, or before the index, then it wasn't changed. |
| + if (key is! int || key < index) return false; |
| + |
| + // If this was a shift operation, anything after index is changed. |
| + if (addedCount != removedCount) return true; |
| + |
| + // Otherwise, anything in the update range was changed. |
| + return key < index + addedCount; |
| + } |
| + |
| + String toString() => '#<ListChangeRecord index: $index, ' |
| + 'removed: $removedCount, addedCount: $addedCount>'; |
| +} |