OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, 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 /** A change to an observable instance. */ | |
6 class ChangeEvent { | |
7 // TODO(sigmund): capture language issues around enums & create a cannonical | |
8 // Dart enum design. | |
9 /** Type denoting an in-place update event. */ | |
10 static final UPDATE = 0; | |
11 | |
12 /** Type denoting an insertion event. */ | |
13 static final INSERT = 1; | |
14 | |
15 /** Type denoting a single-remove event. */ | |
16 static final REMOVE = 2; | |
17 | |
18 /** | |
19 * Type denoting events that affect the entire observable instance. For | |
20 * example, a list operation like clear or sort. | |
21 */ | |
22 static final GLOBAL = 3; | |
23 | |
24 /** The observable instance that changed. */ | |
25 final Observable target; | |
26 | |
27 /** Whether the change was an [INSERT], [REMOVE], or [UPDATE]. */ | |
28 final int type; | |
29 | |
30 /** The value after the change (or inserted value in a list). */ | |
31 final newValue = null; | |
32 | |
33 /** The value before the change (or removed value from a list). */ | |
34 final oldValue = null; | |
35 | |
36 /** Property that changed (null for list changes). */ | |
37 final String propertyName = null; | |
38 | |
39 /** | |
40 * Index of the list operation. Insertions prepend in front of the given | |
41 * index (insert at 0 means an insertion at the beginning of the list). | |
42 */ | |
43 final int index = null; | |
44 | |
45 /** Factory constructor for property change events. */ | |
46 ChangeEvent.property( | |
47 this.target, this.propertyName, this.newValue, this.oldValue) | |
48 : type = UPDATE; | |
49 | |
50 /** Factory constructor for list change events. */ | |
51 ChangeEvent.list( | |
52 this.target, this.type, this.index, this.newValue, this.oldValue); | |
53 | |
54 /** Factory constructor for [GLOBAL] change events. */ | |
55 ChangeEvent.global(this.target) : type = GLOBAL; | |
56 } | |
57 | |
58 /** A collection of change events on a single observable instance. */ | |
59 class EventSummary { | |
60 final Observable target; | |
61 | |
62 // TODO(sigmund): evolve this to track changes per property. | |
63 List<ChangeEvent> events; | |
64 | |
65 EventSummary(this.target) : events = new List<ChangeEvent>(); | |
66 | |
67 void addEvent(ChangeEvent e) { | |
68 events.add(e); | |
69 } | |
70 | |
71 /** Notify listeners of [target] and parents of [target] about all changes. */ | |
72 void notify() { | |
73 if (!events.isEmpty()) { | |
74 for (Observable obj = target; obj != null; obj = obj.parent) { | |
75 for (final listener in obj.listeners) { | |
76 listener(this); | |
77 } | |
78 } | |
79 } | |
80 } | |
81 } | |
82 | |
83 /** A listener of change events. */ | |
84 typedef void ChangeListener(EventSummary events); | |
OLD | NEW |