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

Side by Side Diff: packages/observe/lib/src/observer_transform.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 years, 4 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
OLDNEW
(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 library observe.src.observer_transform;
6
7 import 'package:observe/observe.dart';
8
9 /// ObserverTransform is used to dynamically transform observed value(s).
10 ///
11 /// var obj = new ObservableBox(10);
12 /// var observer = new PathObserver(obj, 'value');
13 /// var transform = new ObserverTransform(observer,
14 /// (x) => x * 2, setValue: (x) => x ~/ 2);
15 ///
16 /// // Open returns the current value of 20.
17 /// transform.open((newValue) => print('new: $newValue'));
18 ///
19 /// obj.value = 20; // prints 'new: 40' async
20 /// new Future(() {
21 /// transform.value = 4; // obj.value will be 2
22 /// });
23 ///
24 /// ObserverTransform can also be used to reduce a set of observed values to a
25 /// single value:
26 ///
27 /// var obj = new ObservableMap.from({'a': 1, 'b': 2, 'c': 3});
28 /// var observer = new CompoundObserver()
29 /// ..addPath(obj, 'a')
30 /// ..addPath(obj, 'b')
31 /// ..addPath(obj, 'c');
32 ///
33 /// var transform = new ObserverTransform(observer,
34 /// (values) => values.fold(0, (x, y) => x + y));
35 ///
36 /// // Open returns the current value of 6.
37 /// transform.open((newValue) => print('new: $newValue'));
38 ///
39 /// obj['a'] = 2;
40 /// obj['c'] = 10; // will print 'new 14' asynchronously
41 ///
42 class ObserverTransform extends Bindable {
43 Bindable _bindable;
44 Function _getTransformer, _setTransformer;
45 Function _notifyCallback;
46 var _value;
47
48 ObserverTransform(Bindable bindable, computeValue(value), {setValue(value)})
49 : _bindable = bindable,
50 _getTransformer = computeValue,
51 _setTransformer = setValue;
52
53 open(callback) {
54 _notifyCallback = callback;
55 _value = _getTransformer(_bindable.open(_observedCallback));
56 return _value;
57 }
58
59 _observedCallback(newValue) {
60 final value = _getTransformer(newValue);
61 if (value == _value) return null;
62 _value = value;
63 return _notifyCallback(value);
64 }
65
66 void close() {
67 if (_bindable != null) _bindable.close();
68 _bindable = null;
69 _getTransformer = null;
70 _setTransformer = null;
71 _notifyCallback = null;
72 _value = null;
73 }
74
75 get value => _value = _getTransformer(_bindable.value);
76
77 set value(newValue) {
78 if (_setTransformer != null) {
79 newValue = _setTransformer(newValue);
80 }
81 _bindable.value = newValue;
82 }
83
84 deliver() => _bindable.deliver();
85 }
OLDNEW
« no previous file with comments | « packages/observe/lib/src/observable_map.dart ('k') | packages/observe/lib/src/path_observer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698