OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012, 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 library for observing changes to expressions. */ |
| 6 library web_ui.observe.expression; |
| 7 |
| 8 import 'package:web_ui/observe.dart'; |
| 9 import 'package:web_ui/src/utils.dart' show setImmediate; |
| 10 import 'package:web_ui/src/observe/impl.dart' as impl; |
| 11 |
| 12 // TODO(jmesserly): notifyRead/notifyWrite are only used by people |
| 13 // implementating advanced observable functionality. The need to be public, but |
| 14 // ideally they would not be in the top level "observe" library. |
| 15 |
| 16 // TODO(jmesserly): ExpressionObserver, ObservableExpression, ExpressionChange |
| 17 // types need work. They're a bit long and I don't like the disconnect between |
| 18 // observing expressions and observing objects for changes. |
| 19 |
| 20 /** Callback fired when an expression changes. */ |
| 21 typedef void ExpressionObserver(ExpressionChange e); |
| 22 |
| 23 /** A function that computes a value. */ |
| 24 typedef Object ObservableExpression(); |
| 25 |
| 26 /** |
| 27 * A notification of a change to an [ObservableExpression] that is passed to a |
| 28 * [ExpressionObserver]. |
| 29 */ |
| 30 class ExpressionChange { |
| 31 |
| 32 /** Previous value seen on the watched expression. */ |
| 33 final oldValue; |
| 34 |
| 35 /** New value seen on the watched expression. */ |
| 36 final newValue; |
| 37 |
| 38 ExpressionChange(this.oldValue, this.newValue); |
| 39 |
| 40 // Note: these two methods are here mainly to make testing easier. |
| 41 bool operator ==(other) { |
| 42 return other is ExpressionChange && oldValue == other.oldValue && |
| 43 newValue == other.newValue; |
| 44 } |
| 45 |
| 46 String toString() => 'change from $oldValue to $newValue'; |
| 47 } |
| 48 |
| 49 /** |
| 50 * Observes the [expression] and delivers asynchronous notifications of changes |
| 51 * to the [callback]. |
| 52 * |
| 53 * The expression is considered to have changed if the values no longer compare |
| 54 * equal via the equality operator. |
| 55 * |
| 56 * Returns a function that can be used to stop observation. |
| 57 * Calling this makes it possible for the garbage collector to reclaim memory |
| 58 * associated with the observation and prevents further calls to [callback]. |
| 59 * |
| 60 * Because notifications are delivered asynchronously and batched, only a single |
| 61 * notification is provided for all changes that were made prior to running |
| 62 * callback. Intermediate values of the expression are not saved. Instead, |
| 63 * [ExpressionChange.oldValue] represents the value before any changes, and |
| 64 * [ExpressionChange.newValue] represents the current value of [expression] |
| 65 * at the time that [callback] is called. |
| 66 * |
| 67 * You can force a synchronous change delivery at any time by calling |
| 68 * [deliverChangesSync]. Calling this method if there are no changes has no |
| 69 * effect. If changes are delivered by deliverChangesSync, they will not be |
| 70 * delivered again asynchronously, unless the value is changed again. |
| 71 * |
| 72 * Any errors thrown by [expression] and [callback] will be caught and sent to |
| 73 * [onObserveUnhandledError]. |
| 74 */ |
| 75 ChangeUnobserver observe(expression, ExpressionObserver callback) { |
| 76 |
| 77 if (expression is Observable) { |
| 78 Observable obs = expression; |
| 79 return obs.observe((_) { |
| 80 callback(new ExpressionChange(obs, obs)); |
| 81 }); |
| 82 } |
| 83 var obs = new impl.ExpressionObserverImpl(expression, callback); |
| 84 obs.observe(); |
| 85 return obs.unobserve; |
| 86 } |
OLD | NEW |