| 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 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 import 'dart:math' show min; | 9 import 'dart:math' show min; |
| 10 | 10 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 var oldValue = _value; | 75 var oldValue = _value; |
| 76 _value = _path.getValueFrom(_object); | 76 _value = _path.getValueFrom(_object); |
| 77 if (skipChanges || _value == oldValue) return false; | 77 if (skipChanges || _value == oldValue) return false; |
| 78 | 78 |
| 79 _report(_value, oldValue); | 79 _report(_value, oldValue); |
| 80 return true; | 80 return true; |
| 81 } | 81 } |
| 82 } | 82 } |
| 83 | 83 |
| 84 /// A dot-delimieted property path such as "foo.bar" or "foo.10.bar". | 84 /// A dot-delimieted property path such as "foo.bar" or "foo.10.bar". |
| 85 /// |
| 85 /// The path specifies how to get a particular value from an object graph, where | 86 /// The path specifies how to get a particular value from an object graph, where |
| 86 /// the graph can include arrays. | 87 /// the graph can include arrays and maps. Each segment of the path describes |
| 88 /// how to take a single step in the object graph. Properties like 'foo' or |
| 89 /// 'bar' are read as properties on objects, or as keys if the object is a [Map] |
| 90 /// or a [Indexable], while integer values are read as indexes in a [List]. |
| 87 // TODO(jmesserly): consider specialized subclasses for: | 91 // TODO(jmesserly): consider specialized subclasses for: |
| 88 // * empty path | 92 // * empty path |
| 89 // * "value" | 93 // * "value" |
| 90 // * single token in path, e.g. "foo" | 94 // * single token in path, e.g. "foo" |
| 91 class PropertyPath { | 95 class PropertyPath { |
| 92 /// The segments of the path. | 96 /// The segments of the path. |
| 93 final List<Object> _segments; | 97 final List<Object> _segments; |
| 94 | 98 |
| 95 /// Creates a new [PropertyPath]. These can be stored to avoid excessive | 99 /// Creates a new [PropertyPath]. These can be stored to avoid excessive |
| 96 /// parsing of path strings. | 100 /// parsing of path strings. |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 for (int i = 0, len = _segments.length; i < len; i++) { | 177 for (int i = 0, len = _segments.length; i < len; i++) { |
| 174 hash = 0x1fffffff & (hash + _segments[i].hashCode); | 178 hash = 0x1fffffff & (hash + _segments[i].hashCode); |
| 175 hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); | 179 hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10)); |
| 176 hash = hash ^ (hash >> 6); | 180 hash = hash ^ (hash >> 6); |
| 177 } | 181 } |
| 178 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); | 182 hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3)); |
| 179 hash = hash ^ (hash >> 11); | 183 hash = hash ^ (hash >> 11); |
| 180 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); | 184 return 0x1fffffff & (hash + ((0x00003fff & hash) << 15)); |
| 181 } | 185 } |
| 182 | 186 |
| 183 /// Returns the current of the path from the provided [obj]ect. | 187 /// Returns the current value of the path from the provided [obj]ect. |
| 184 getValueFrom(Object obj) { | 188 getValueFrom(Object obj) { |
| 185 if (!isValid) return null; | 189 if (!isValid) return null; |
| 186 for (var segment in _segments) { | 190 for (var segment in _segments) { |
| 187 if (obj == null) return null; | 191 if (obj == null) return null; |
| 188 obj = _getObjectProperty(obj, segment); | 192 obj = _getObjectProperty(obj, segment); |
| 189 } | 193 } |
| 190 return obj; | 194 return obj; |
| 191 } | 195 } |
| 192 | 196 |
| 193 /// Attempts to set the [value] of the path from the provided [obj]ect. | 197 /// Attempts to set the [value] of the path from the provided [obj]ect. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 if (record is PropertyChangeRecord) { | 230 if (record is PropertyChangeRecord) { |
| 227 return (record as PropertyChangeRecord).name == key; | 231 return (record as PropertyChangeRecord).name == key; |
| 228 } | 232 } |
| 229 if (record is MapChangeRecord) { | 233 if (record is MapChangeRecord) { |
| 230 if (key is Symbol) key = smoke.symbolToName(key); | 234 if (key is Symbol) key = smoke.symbolToName(key); |
| 231 return (record as MapChangeRecord).key == key; | 235 return (record as MapChangeRecord).key == key; |
| 232 } | 236 } |
| 233 return false; | 237 return false; |
| 234 } | 238 } |
| 235 | 239 |
| 240 /// Properties in [Map] that need to be read as properties and not as keys in |
| 241 /// the map. We exclude methods ('containsValue', 'containsKey', 'putIfAbsent', |
| 242 /// 'addAll', 'remove', 'clear', 'forEach') because there is no use in reading |
| 243 /// them as part of path-observer segments. |
| 244 const _MAP_PROPERTIES = const [#keys, #values, #length, #isEmpty, #isNotEmpty]; |
| 245 |
| 236 _getObjectProperty(object, property) { | 246 _getObjectProperty(object, property) { |
| 237 if (object == null) return null; | 247 if (object == null) return null; |
| 238 | 248 |
| 239 if (property is int) { | 249 if (property is int) { |
| 240 if (object is List && property >= 0 && property < object.length) { | 250 if (object is List && property >= 0 && property < object.length) { |
| 241 return object[property]; | 251 return object[property]; |
| 242 } | 252 } |
| 243 } else if (property is Symbol) { | 253 } else if (property is Symbol) { |
| 244 final type = object.runtimeType; | 254 // Support indexer if available, e.g. Maps or polymer_expressions Scope. |
| 255 // This is the default syntax used by polymer/nodebind and |
| 256 // polymer/observe-js PathObserver. |
| 257 // TODO(sigmund): should we also support using checking dynamically for |
| 258 // whether the type practically implements the indexer API |
| 259 // (smoke.hasInstanceMethod(type, const Symbol('[]')))? |
| 260 if (object is Indexable<String, dynamic> || |
| 261 object is Map<String, dynamic> && !_MAP_PROPERTIES.contains(property)) { |
| 262 return object[smoke.symbolToName(property)]; |
| 263 } |
| 245 try { | 264 try { |
| 246 if (smoke.hasGetter(type, property) || smoke.hasNoSuchMethod(type)) { | 265 return smoke.read(object, property); |
| 247 return smoke.read(object, property); | |
| 248 } | |
| 249 // Support indexer if available, e.g. Maps or polymer_expressions Scope. | |
| 250 // This is the default syntax used by polymer/nodebind and | |
| 251 // polymer/observe-js PathObserver. | |
| 252 if (smoke.hasInstanceMethod(type, const Symbol('[]'))) { | |
| 253 return object[smoke.symbolToName(property)]; | |
| 254 } | |
| 255 } on NoSuchMethodError catch (e) { | 266 } on NoSuchMethodError catch (e) { |
| 256 // Rethrow, unless the type implements noSuchMethod, in which case we | 267 // Rethrow, unless the type implements noSuchMethod, in which case we |
| 257 // interpret the exception as a signal that the method was not found. | 268 // interpret the exception as a signal that the method was not found. |
| 258 if (!smoke.hasNoSuchMethod(type)) rethrow; | 269 // Dart note: getting invalid properties is an error, unlike in JS where |
| 270 // it returns undefined. |
| 271 if (!smoke.hasNoSuchMethod(object.runtimeType)) rethrow; |
| 259 } | 272 } |
| 260 } | 273 } |
| 261 | 274 |
| 262 if (_logger.isLoggable(Level.FINER)) { | 275 if (_logger.isLoggable(Level.FINER)) { |
| 263 _logger.finer("can't get $property in $object"); | 276 _logger.finer("can't get $property in $object"); |
| 264 } | 277 } |
| 265 return null; | 278 return null; |
| 266 } | 279 } |
| 267 | 280 |
| 268 bool _setObjectProperty(object, property, value) { | 281 bool _setObjectProperty(object, property, value) { |
| 269 if (object == null) return false; | 282 if (object == null) return false; |
| 270 | 283 |
| 271 if (property is int) { | 284 if (property is int) { |
| 272 if (object is List && property >= 0 && property < object.length) { | 285 if (object is List && property >= 0 && property < object.length) { |
| 273 object[property] = value; | 286 object[property] = value; |
| 274 return true; | 287 return true; |
| 275 } | 288 } |
| 276 } else if (property is Symbol) { | 289 } else if (property is Symbol) { |
| 277 final type = object.runtimeType; | 290 // Support indexer if available, e.g. Maps or polymer_expressions Scope. |
| 291 if (object is Indexable<String, dynamic> || |
| 292 object is Map<String, dynamic> && !_MAP_PROPERTIES.contains(property)) { |
| 293 object[smoke.symbolToName(property)] = value; |
| 294 return true; |
| 295 } |
| 278 try { | 296 try { |
| 279 if (smoke.hasSetter(type, property) || smoke.hasNoSuchMethod(type)) { | 297 smoke.write(object, property, value); |
| 280 smoke.write(object, property, value); | 298 return true; |
| 281 return true; | 299 } on NoSuchMethodError catch (e, s) { |
| 282 } | 300 if (!smoke.hasNoSuchMethod(object.runtimeType)) rethrow; |
| 283 // Support indexer if available, e.g. Maps or polymer_expressions Scope. | |
| 284 if (smoke.hasInstanceMethod(type, const Symbol('[]='))) { | |
| 285 object[smoke.symbolToName(property)] = value; | |
| 286 return true; | |
| 287 } | |
| 288 } on NoSuchMethodError catch (e) { | |
| 289 if (!smoke.hasNoSuchMethod(type)) rethrow; | |
| 290 } | 301 } |
| 291 } | 302 } |
| 292 | 303 |
| 293 if (_logger.isLoggable(Level.FINER)) { | 304 if (_logger.isLoggable(Level.FINER)) { |
| 294 _logger.finer("can't set $property in $object"); | 305 _logger.finer("can't set $property in $object"); |
| 295 } | 306 } |
| 296 return false; | 307 return false; |
| 297 } | 308 } |
| 298 | 309 |
| 299 // From: https://github.com/rafaelw/ChangeSummary/blob/master/change_summary.js | 310 // From: https://github.com/rafaelw/ChangeSummary/blob/master/change_summary.js |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 | 474 |
| 464 if (!changed) return false; | 475 if (!changed) return false; |
| 465 | 476 |
| 466 // TODO(rafaelw): Having _observed as the third callback arg here is | 477 // TODO(rafaelw): Having _observed as the third callback arg here is |
| 467 // pretty lame API. Fix. | 478 // pretty lame API. Fix. |
| 468 _report(_value, oldValues, _observed); | 479 _report(_value, oldValues, _observed); |
| 469 return true; | 480 return true; |
| 470 } | 481 } |
| 471 } | 482 } |
| 472 | 483 |
| 484 /// An object accepted by [PropertyPath] where properties are read and written |
| 485 /// as indexing operations, just like a [Map]. |
| 486 abstract class Indexable<K, V> { |
| 487 V operator [](K key); |
| 488 operator []=(K key, V value); |
| 489 } |
| 490 |
| 473 const _observerSentinel = const _ObserverSentinel(); | 491 const _observerSentinel = const _ObserverSentinel(); |
| 474 class _ObserverSentinel { const _ObserverSentinel(); } | 492 class _ObserverSentinel { const _ObserverSentinel(); } |
| 475 | 493 |
| 476 // A base class for the shared API implemented by PathObserver and | 494 // A base class for the shared API implemented by PathObserver and |
| 477 // CompoundObserver and used in _ObservedSet. | 495 // CompoundObserver and used in _ObservedSet. |
| 478 abstract class _Observer extends Bindable { | 496 abstract class _Observer extends Bindable { |
| 479 static int _nextBirthId = 0; | 497 static int _nextBirthId = 0; |
| 480 | 498 |
| 481 /// A number indicating when the object was created. | 499 /// A number indicating when the object was created. |
| 482 final int _birthId = _nextBirthId++; | 500 final int _birthId = _nextBirthId++; |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 for (var observer in _observers.values.toList(growable: false)) { | 670 for (var observer in _observers.values.toList(growable: false)) { |
| 653 if (observer._isOpen) observer._check(); | 671 if (observer._isOpen) observer._check(); |
| 654 } | 672 } |
| 655 | 673 |
| 656 _resetNeeded = true; | 674 _resetNeeded = true; |
| 657 scheduleMicrotask(reset); | 675 scheduleMicrotask(reset); |
| 658 } | 676 } |
| 659 } | 677 } |
| 660 | 678 |
| 661 const int _MAX_DIRTY_CHECK_CYCLES = 1000; | 679 const int _MAX_DIRTY_CHECK_CYCLES = 1000; |
| OLD | NEW |