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 library observe.src.path_observer; | 5 library observe.src.path_observer; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 @MirrorsUsed(metaTargets: const [Reflectable, ObservableProperty], | 8 @MirrorsUsed(metaTargets: const [Reflectable, ObservableProperty], |
9 override: 'observe.src.path_observer') | 9 override: 'observe.src.path_observer') |
10 import 'dart:mirrors'; | 10 import 'dart:mirrors'; |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 if (end == null) end = _segments.length; | 177 if (end == null) end = _segments.length; |
178 | 178 |
179 for (int i = start; i < end; i++) { | 179 for (int i = start; i < end; i++) { |
180 if (_subs[i] != null) _subs[i].cancel(); | 180 if (_subs[i] != null) _subs[i].cancel(); |
181 _observeIndex(i); | 181 _observeIndex(i); |
182 } | 182 } |
183 } | 183 } |
184 | 184 |
185 void _observeIndex(int i) { | 185 void _observeIndex(int i) { |
186 final object = _values[i]; | 186 final object = _values[i]; |
187 if (object is Observable) { | 187 final segment = _segments[i]; |
| 188 if (segment is int) { |
| 189 if (object is ObservableList) { |
| 190 _subs[i] = object.listChanges.listen((List<ListChangeRecord> records) { |
| 191 for (var record in records) { |
| 192 if (record.indexChanged(segment)) { |
| 193 _updateObservedValues(start: i); |
| 194 return; |
| 195 } |
| 196 } |
| 197 }); |
| 198 } |
| 199 } else if (object is Observable) { |
188 // TODO(jmesserly): rather than allocating a new closure for each | 200 // TODO(jmesserly): rather than allocating a new closure for each |
189 // property, we could try and have one for the entire path. However we'd | 201 // property, we could try and have one for the entire path. However we'd |
190 // need to do a linear scan to find the index as soon as we got a change. | 202 // need to do a linear scan to find the index as soon as we got a change. |
191 // Also we need to fix ListChangeRecord and MapChangeRecord to contain | 203 // Also we need to fix ListChangeRecord and MapChangeRecord to contain |
192 // the target. Not sure if it's worth it. | 204 // the target. Not sure if it's worth it. |
| 205 |
193 _subs[i] = object.changes.listen((List<ChangeRecord> records) { | 206 _subs[i] = object.changes.listen((List<ChangeRecord> records) { |
194 for (var record in records) { | 207 for (var record in records) { |
195 if (_changeRecordMatches(record, _segments[i])) { | 208 if (_changeRecordMatches(record, segment)) { |
196 _updateObservedValues(start: i); | 209 _updateObservedValues(start: i); |
197 return; | 210 return; |
198 } | 211 } |
199 } | 212 } |
200 }); | 213 }); |
201 } | 214 } |
202 } | 215 } |
203 } | 216 } |
204 | 217 |
205 bool _changeRecordMatches(record, key) { | 218 bool _changeRecordMatches(record, key) { |
206 if (record is ListChangeRecord) { | |
207 return key is int && (record as ListChangeRecord).indexChanged(key); | |
208 } | |
209 if (record is PropertyChangeRecord) { | 219 if (record is PropertyChangeRecord) { |
210 return (record as PropertyChangeRecord).name == key; | 220 return (record as PropertyChangeRecord).name == key; |
211 } | 221 } |
212 if (record is MapChangeRecord) { | 222 if (record is MapChangeRecord) { |
213 if (key is Symbol) key = MirrorSystem.getName(key); | 223 if (key is Symbol) key = MirrorSystem.getName(key); |
214 return (record as MapChangeRecord).key == key; | 224 return (record as MapChangeRecord).key == key; |
215 } | 225 } |
216 return false; | 226 return false; |
217 } | 227 } |
218 | 228 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 | 357 |
348 bool _isPathValid(String s) { | 358 bool _isPathValid(String s) { |
349 s = s.replaceAll(_spacesRegExp, ''); | 359 s = s.replaceAll(_spacesRegExp, ''); |
350 | 360 |
351 if (s == '') return true; | 361 if (s == '') return true; |
352 if (s[0] == '.') return false; | 362 if (s[0] == '.') return false; |
353 return _pathRegExp.hasMatch(s); | 363 return _pathRegExp.hasMatch(s); |
354 } | 364 } |
355 | 365 |
356 final _logger = new Logger('observe.PathObserver'); | 366 final _logger = new Logger('observe.PathObserver'); |
OLD | NEW |