| 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 /// Support for observing changes in model-view architectures. | |
| 6 /// | |
| 7 /// **Warning:** This library is experimental, and APIs are subject to change. | |
| 8 /// | |
| 9 /// This library is used to observe changes to [Observable] types. It also | |
| 10 /// has helpers to make implementing and using [Observable] objects easy. | |
| 11 /// | |
| 12 /// You can provide an observable object in two ways. The simplest way is to | |
| 13 /// use dirty checking to discover changes automatically: | |
| 14 /// | |
| 15 /// import 'package:observe/observe.dart'; | |
| 16 /// import 'package:observe/mirrors_used.dart'; // for smaller code | |
| 17 /// | |
| 18 /// class Monster extends Unit with Observable { | |
| 19 /// @observable int health = 100; | |
| 20 /// | |
| 21 /// void damage(int amount) { | |
| 22 /// print('$this takes $amount damage!'); | |
| 23 /// health -= amount; | |
| 24 /// } | |
| 25 /// | |
| 26 /// toString() => 'Monster with $health hit points'; | |
| 27 /// } | |
| 28 /// | |
| 29 /// main() { | |
| 30 /// var obj = new Monster(); | |
| 31 /// obj.changes.listen((records) { | |
| 32 /// print('Changes to $obj were: $records'); | |
| 33 /// }); | |
| 34 /// // No changes are delivered until we check for them | |
| 35 /// obj.damage(10); | |
| 36 /// obj.damage(20); | |
| 37 /// print('dirty checking!'); | |
| 38 /// Observable.dirtyCheck(); | |
| 39 /// print('done!'); | |
| 40 /// } | |
| 41 /// | |
| 42 /// A more sophisticated approach is to implement the change notification | |
| 43 /// manually. This avoids the potentially expensive [Observable.dirtyCheck] | |
| 44 /// operation, but requires more work in the object: | |
| 45 /// | |
| 46 /// import 'package:observe/observe.dart'; | |
| 47 /// import 'package:observe/mirrors_used.dart'; // for smaller code | |
| 48 /// | |
| 49 /// class Monster extends Unit with ChangeNotifier { | |
| 50 /// int _health = 100; | |
| 51 /// @reflectable get health => _health; | |
| 52 /// @reflectable set health(val) { | |
| 53 /// _health = notifyPropertyChange(#health, _health, val); | |
| 54 /// } | |
| 55 /// | |
| 56 /// void damage(int amount) { | |
| 57 /// print('$this takes $amount damage!'); | |
| 58 /// health -= amount; | |
| 59 /// } | |
| 60 /// | |
| 61 /// toString() => 'Monster with $health hit points'; | |
| 62 /// } | |
| 63 /// | |
| 64 /// main() { | |
| 65 /// var obj = new Monster(); | |
| 66 /// obj.changes.listen((records) { | |
| 67 /// print('Changes to $obj were: $records'); | |
| 68 /// }); | |
| 69 /// // Schedules asynchronous delivery of these changes | |
| 70 /// obj.damage(10); | |
| 71 /// obj.damage(20); | |
| 72 /// print('done!'); | |
| 73 /// } | |
| 74 /// | |
| 75 /// **Note**: by default this package uses mirrors to access getters and setters | |
| 76 /// marked with `@reflectable`. Dart2js disables tree-shaking if there are any | |
| 77 /// uses of mirrors, unless you declare how mirrors are used (via the | |
| 78 /// [MirrorsUsed](https://api.dartlang.org/apidocs/channels/stable/#dart-mirrors
.MirrorsUsed) | |
| 79 /// annotation). | |
| 80 /// | |
| 81 /// As of version 0.10.0, this package doesn't declare `@MirrorsUsed`. This is | |
| 82 /// because we intend to use mirrors for development time, but assume that | |
| 83 /// frameworks and apps that use this pacakge will either generate code that | |
| 84 /// replaces the use of mirrors, or add the `@MirrorsUsed` declaration | |
| 85 /// themselves. For convenience, you can import | |
| 86 /// `package:observe/mirrors_used.dart` as shown on the first example above. | |
| 87 /// That will add a `@MirrorsUsed` annotation that preserves properties and | |
| 88 /// classes labeled with `@reflectable` and properties labeled with | |
| 89 /// `@observable`. | |
| 90 /// | |
| 91 /// If you are using the `package:observe/mirrors_used.dart` import, you can | |
| 92 /// also make use of `@reflectable` on your own classes and dart2js will | |
| 93 /// preserve all of its members for reflection. | |
| 94 /// | |
| 95 /// [Tools](https://www.dartlang.org/polymer-dart/) exist to convert the first | |
| 96 /// form into the second form automatically, to get the best of both worlds. | |
| 97 library observe; | |
| 98 | |
| 99 // This library contains code ported from observe-js: | |
| 100 // https://github.com/Polymer/observe-js/blob/0152d542350239563d0f2cad39d22d3254
bd6c2a/src/observe.js | |
| 101 // We port what is needed for data bindings. Most of the functionality is | |
| 102 // ported, except where differences are needed for Dart's Observable type. | |
| 103 | |
| 104 export 'src/bindable.dart'; | |
| 105 export 'src/bind_property.dart'; | |
| 106 export 'src/change_notifier.dart'; | |
| 107 export 'src/change_record.dart'; | |
| 108 export 'src/list_path_observer.dart'; | |
| 109 export 'src/list_diff.dart' show ListChangeRecord; | |
| 110 export 'src/metadata.dart'; | |
| 111 export 'src/observable.dart' hide notifyPropertyChangeHelper; | |
| 112 export 'src/observable_box.dart'; | |
| 113 export 'src/observable_list.dart'; | |
| 114 export 'src/observable_map.dart'; | |
| 115 export 'src/observer_transform.dart'; | |
| 116 export 'src/path_observer.dart' hide getSegmentsOfPropertyPathForTesting; | |
| 117 export 'src/to_observable.dart'; | |
| OLD | NEW |