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

Unified Diff: pkg/observe/lib/src/observer_transform.dart

Issue 132403010: big update to observe, template_binding, polymer (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: pkg/observe/lib/src/observer_transform.dart
diff --git a/pkg/observe/lib/src/observer_transform.dart b/pkg/observe/lib/src/observer_transform.dart
new file mode 100644
index 0000000000000000000000000000000000000000..75558631abef9618589b629dbb3e700015c11fab
--- /dev/null
+++ b/pkg/observe/lib/src/observer_transform.dart
@@ -0,0 +1,84 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library observe.src.observer_transform;
+
+import 'package:observe/observe.dart';
+
+/// ObserverTransform is used to dynamically transform observed value(s).
+///
+/// var obj = new ObservableBox(10);
+/// var observer = new PathObserver(obj, 'value');
+/// var transform = new ObserverTransform(observer,
+/// (x) => x * 2, setValue: (x) => x ~/ 2);
+///
+/// // Open returns the current value of 20.
+/// transform.open((newValue) => print('new: $newValue'));
+///
+/// obj.value = 20; // prints 'new: 40' async
+/// new Future(() {
+/// transform.value = 4; // obj.value will be 2
+/// });
+///
+/// ObserverTransform can also be used to reduce a set of observed values to a=
Siggi Cherem (dart-lang) 2014/02/03 22:52:48 remove "="
Jennifer Messerly 2014/02/04 00:33:06 Done.
+/// single value:
+///
+/// var obj = new ObservableMap.from({'a': 1, 'b': 2, 'c': 3});
+/// var observer = new CompoundObserver()
+/// ..addPath(obj, 'a')
+/// ..addPath(obj, 'b')
+/// ..addPath(obj, 'c');
+///
+/// var transform = new ObserverTransform(observer,
+/// (values) => values.fold(0, (x, y) => x + y));
+///
+/// // Open returns the current value of 6.
+/// transform.open((newValue) => print('new: $newValue'));
+///
+/// obj['a'] = 2;
+/// obj['c'] = 10; // will print 'new 14' asynchronously
+///
+class ObserverTransform extends Bindable {
+ Bindable _bindable;
+ Function _getValue, _setValue;
+ Function _notifyCallback;
+ var _value;
+
+ ObserverTransform(Bindable bindable, computeValue(value), {setValue(value)})
+ : _bindable = bindable, _getValue = computeValue, _setValue = setValue;
+
+ open(callback(value)) {
+ _notifyCallback = callback;
+ _value = _getValue(_bindable.open(_observedCallback));
+ return _value;
+ }
+
+ _observedCallback(value) {
+ value = _getValue(value);
Siggi Cherem (dart-lang) 2014/02/03 22:52:48 Consider introducing a new variable here: var tra
Jennifer Messerly 2014/02/04 00:33:06 sure
+ if (value == _value) return null;
+ _value = value;
+ return _notifyCallback(value);
+ }
+
+ void close() {
+ if (_bindable != null) _bindable.close();
+ _bindable = null;
+ _getValue = null;
+ _setValue = null;
+ _notifyCallback = null;
+ _value = null;
+ }
+
+ get value {
Siggi Cherem (dart-lang) 2014/02/03 22:52:48 not sure if I like this suggestion, but here it go
Jennifer Messerly 2014/02/04 00:33:06 Done.
+ _value = _getValue(_bindable.value);
+ return _value;
+ }
+
+ set value(newValue) {
+ if (_setValue != null) {
+ newValue = _setValue(newValue);
+ }
Siggi Cherem (dart-lang) 2014/02/03 22:52:48 mmm, I was expecting that if _setValue == null, th
Jennifer Messerly 2014/02/04 00:33:06 we still want to pass on the value, just we don't
+ _bindable.value = newValue;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698