Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: pkg/observe/lib/src/change_record.dart

Issue 27618002: package:observe fix various api issues (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 /** *Deprecated* use == [name] instead. */
Siggi Cherem (dart-lang) 2013/10/17 02:04:56 use ` ` to quote the code sample? maybe: *Depreca
Jennifer Messerly 2013/10/17 03:25:12 good idea, done
35 @deprecated
36 bool changes(key) => key is Symbol && name == key;
37
38 String toString() =>
39 '#<PropertyChangeRecord $name from: $oldValue to: $newValue>';
24 } 40 }
25 41
26 /** A change record for an observable list. */ 42 /** A change record for an observable list. */
27 class ListChangeRecord extends ChangeRecord { 43 class ListChangeRecord extends ChangeRecord {
28 /** The starting index of the change. */ 44 /** The starting index of the change. */
29 final int index; 45 final int index;
30 46
31 /** The number of items removed. */ 47 /** The number of items removed. */
32 final int removedCount; 48 final int removedCount;
33 49
34 /** The number of items added. */ 50 /** The number of items added. */
35 final int addedCount; 51 final int addedCount;
36 52
37 ListChangeRecord(this.index, {this.removedCount: 0, this.addedCount: 0}) { 53 ListChangeRecord(this.index, {this.removedCount: 0, this.addedCount: 0}) {
38 if (addedCount == 0 && removedCount == 0) { 54 if (addedCount == 0 && removedCount == 0) {
39 throw new ArgumentError('added and removed counts should not both be ' 55 throw new ArgumentError('added and removed counts should not both be '
40 'zero. Use 1 if this was a single item update.'); 56 'zero. Use 1 if this was a single item update.');
41 } 57 }
42 } 58 }
43 59
60 /**
61 * *Deprecated* use [indexChanged] instead.
62 * Returns true if the provided index was changed by this operation.
63 */
64 @deprecated
65 bool changes(value) => indexChanged(value);
66
44 /** Returns true if the provided index was changed by this operation. */ 67 /** Returns true if the provided index was changed by this operation. */
45 bool changes(key) { 68 bool indexChanged(otherIndex) {
46 // If key isn't an int, or before the index, then it wasn't changed. 69 // If key isn't an int, or before the index, then it wasn't changed.
47 if (key is! int || key < index) return false; 70 if (otherIndex is! int || otherIndex < index) return false;
48 71
49 // If this was a shift operation, anything after index is changed. 72 // If this was a shift operation, anything after index is changed.
50 if (addedCount != removedCount) return true; 73 if (addedCount != removedCount) return true;
51 74
52 // Otherwise, anything in the update range was changed. 75 // Otherwise, anything in the update range was changed.
53 return key < index + addedCount; 76 return otherIndex < index + addedCount;
54 } 77 }
55 78
56 String toString() => '#<ListChangeRecord index: $index, ' 79 String toString() => '#<ListChangeRecord index: $index, '
57 'removed: $removedCount, addedCount: $addedCount>'; 80 'removed: $removedCount, addedCount: $addedCount>';
58 } 81 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698