| 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 /** | |
| 6 * *Warning*: this library is experimental, and APIs are subject to change. | |
| 7 * | |
| 8 * This library is used to observe changes to [Observable] types. It also | |
| 9 * has helpers to implement [Observable] objects. | |
| 10 * | |
| 11 * For example: | |
| 12 * | |
| 13 * class Monster extends Object with ObservableMixin { | |
| 14 * static const _HEALTH = const Symbol('health'); | |
| 15 * | |
| 16 * int _health = 100; | |
| 17 * get health => _health; | |
| 18 * set health(value) { | |
| 19 * _health = notifyChange(_HEALTH, _health, value); | |
| 20 * } | |
| 21 * | |
| 22 * void damage(int amount) { | |
| 23 * print('$this takes $amount damage!'); | |
| 24 * health -= amount; | |
| 25 * } | |
| 26 * | |
| 27 * toString() => 'Monster with $health hit points'; | |
| 28 * | |
| 29 * // These methods are temporary until dart2js supports mirrors. | |
| 30 * getValueWorkaround(key) { | |
| 31 * if (key == _HEALTH) return health; | |
| 32 * return null; | |
| 33 * } | |
| 34 * setValueWorkaround(key, val) { | |
| 35 * if (key == _HEALTH) health = val; | |
| 36 * } | |
| 37 * } | |
| 38 * | |
| 39 * main() { | |
| 40 * var obj = new Monster(); | |
| 41 * obj.changes.listen((records) { | |
| 42 * print('Changes to $obj were: $records'); | |
| 43 * }); | |
| 44 * // Schedules asynchronous delivery of these changes | |
| 45 * obj.damage(10); | |
| 46 * obj.damage(20); | |
| 47 * print('done!'); | |
| 48 * } | |
| 49 */ | |
| 50 library mdv_observe; | |
| 51 | |
| 52 import 'dart:collection'; | |
| 53 | |
| 54 // Import and reexport the observe implementation library. It contains the types | |
| 55 // that are required to implement Model-Driven-Views in dart:html. | |
| 56 // NOTE: always use package:mdv_observe (this package) in your code! | |
| 57 // DO NOT import mdv_observe_impl; it may break unpredictably. | |
| 58 import 'dart:mdv_observe_impl'; | |
| 59 export 'dart:mdv_observe_impl'; | |
| 60 | |
| 61 part 'src/observable_box.dart'; | |
| 62 part 'src/observable_list.dart'; | |
| 63 part 'src/observable_map.dart'; | |
| 64 | |
| 65 | |
| 66 /** | |
| 67 * Converts the [Iterable] or [Map] to an [ObservableList] or [ObservableMap], | |
| 68 * respectively. This is a convenience function to make it easier to convert | |
| 69 * literals into the corresponding observable collection type. | |
| 70 * | |
| 71 * If [value] is not one of those collection types, or is already [Observable], | |
| 72 * it will be returned unmodified. | |
| 73 * | |
| 74 * If [value] is a [Map], the resulting value will use the appropriate kind of | |
| 75 * backing map: either [HashMap], [LinkedHashMap], or [SplayTreeMap]. | |
| 76 * | |
| 77 * By default this performs a deep conversion, but you can set [deep] to false | |
| 78 * for a shallow conversion. This does not handle circular data structures. | |
| 79 */ | |
| 80 // TODO(jmesserly): ObservableSet? | |
| 81 toObservable(value, {bool deep: true}) => | |
| 82 deep ? _toObservableDeep(value) : _toObservableShallow(value); | |
| 83 | |
| 84 _toObservableShallow(value) { | |
| 85 if (value is Observable) return value; | |
| 86 if (value is Map) return new ObservableMap.from(value); | |
| 87 if (value is Iterable) return new ObservableList.from(value); | |
| 88 return value; | |
| 89 } | |
| 90 | |
| 91 _toObservableDeep(value) { | |
| 92 if (value is Observable) return value; | |
| 93 if (value is Map) { | |
| 94 var result = new ObservableMap._createFromType(value); | |
| 95 value.forEach((k, v) { | |
| 96 result[_toObservableDeep(k)] = _toObservableDeep(v); | |
| 97 }); | |
| 98 return result; | |
| 99 } | |
| 100 if (value is Iterable) { | |
| 101 return new ObservableList.from(value.map(_toObservableDeep)); | |
| 102 } | |
| 103 return value; | |
| 104 } | |
| OLD | NEW |