Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1057)

Side by Side Diff: lib/observe/expression.dart

Issue 12096106: work in progress: observable implementation using detailed change records (Closed) Base URL: https://github.com/dart-lang/web-ui.git@master
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/observe.dart ('k') | lib/observe/list.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « lib/observe.dart ('k') | lib/observe/list.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698