OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of observe; | |
6 | |
7 /** Records a change to an [Observable]. */ | |
8 abstract class ChangeRecord { | |
Jennifer Messerly
2013/07/19 01:32:58
no changes here yet. just moved into its own file
| |
9 /** True if the change affected the given item, otherwise false. */ | |
10 bool change(key); | |
11 } | |
12 | |
13 /** A change record to a field of an observable object. */ | |
14 class PropertyChangeRecord extends ChangeRecord { | |
15 /** The field that was changed. */ | |
16 final Symbol field; | |
17 | |
18 PropertyChangeRecord(this.field); | |
19 | |
20 bool changes(key) => key is Symbol && field == key; | |
21 | |
22 String toString() => '#<PropertyChangeRecord $field>'; | |
23 } | |
24 | |
25 /** A change record for an observable list. */ | |
26 class ListChangeRecord extends ChangeRecord { | |
27 /** The starting index of the change. */ | |
28 final int index; | |
29 | |
30 /** The number of items removed. */ | |
31 final int removedCount; | |
32 | |
33 /** The number of items added. */ | |
34 final int addedCount; | |
35 | |
36 ListChangeRecord(this.index, {this.removedCount: 0, this.addedCount: 0}) { | |
37 if (addedCount == 0 && removedCount == 0) { | |
38 throw new ArgumentError('added and removed counts should not both be ' | |
39 'zero. Use 1 if this was a single item update.'); | |
40 } | |
41 } | |
42 | |
43 /** Returns true if the provided index was changed by this operation. */ | |
44 bool changes(key) { | |
45 // If key isn't an int, or before the index, then it wasn't changed. | |
46 if (key is! int || key < index) return false; | |
47 | |
48 // If this was a shift operation, anything after index is changed. | |
49 if (addedCount != removedCount) return true; | |
50 | |
51 // Otherwise, anything in the update range was changed. | |
52 return key < index + addedCount; | |
53 } | |
54 | |
55 String toString() => '#<ListChangeRecord index: $index, ' | |
56 'removed: $removedCount, addedCount: $addedCount>'; | |
57 } | |
OLD | NEW |