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

Unified Diff: pkg/mdv_observe/lib/mdv_observe.dart

Issue 14732003: Implement Model-Driven-Views spec for Dart (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: try upload again Created 7 years, 7 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/mdv_observe/lib/mdv_observe.dart
diff --git a/pkg/mdv_observe/lib/mdv_observe.dart b/pkg/mdv_observe/lib/mdv_observe.dart
new file mode 100644
index 0000000000000000000000000000000000000000..8864b36de67548dd5960b91e56a0f83a4dc03456
--- /dev/null
+++ b/pkg/mdv_observe/lib/mdv_observe.dart
@@ -0,0 +1,104 @@
+// 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.
+
+/**
+ * *Warning*: this library is experimental, and APIs are subject to change.
+ *
+ * This library is used to observe changes to [Observable] types. It also
+ * has helpers to implement [Observable] objects.
+ *
+ * For example:
+ *
+ * class Monster extends Object with ObservableMixin {
+ * static const _HEALTH = const Symbol('health');
+ *
+ * int _health = 100;
+ * get health => _health;
+ * set health(value) {
+ * _health = notifyChange(_HEALTH, _health, value);
+ * }
+ *
+ * void damage(int amount) {
+ * print('$this takes $amount damage!');
+ * health -= amount;
+ * }
+ *
+ * toString() => 'Monster with $health hit points';
+ *
+ * // These methods are temporary until dart2js supports mirrors.
+ * getValueWorkaround(key) {
+ * if (key == _HEALTH) return health;
+ * return null;
+ * }
+ * setValueWorkaround(key, val) {
+ * if (key == _HEALTH) health = val;
+ * }
+ * }
+ *
+ * main() {
+ * var obj = new Monster();
+ * obj.changes.listen((records) {
+ * print('Changes to $obj were: $records');
+ * });
+ * // Schedules asynchronous delivery of these changes
+ * obj.damage(10);
+ * obj.damage(20);
+ * print('done!');
+ * }
+ */
+library mdv_observe;
+
+import 'dart:collection';
+
+// Import and reexport the observe implementation library. It contains the types
+// that are required to implement Model-Driven-Views in dart:html.
+// NOTE: always use package:mdv_observe (this package) in your code!
+// DO NOT import mdv_observe_impl; it may break unpredictably.
+import 'dart:mdv_observe_impl';
+export 'dart:mdv_observe_impl';
+
+part 'src/observable_box.dart';
+part 'src/observable_list.dart';
+part 'src/observable_map.dart';
+
+
+/**
+ * Converts the [Iterable] or [Map] to an [ObservableList] or [ObservableMap],
+ * respectively. This is a convenience function to make it easier to convert
+ * literals into the corresponding observable collection type.
+ *
+ * If [value] is not one of those collection types, or is already [Observable],
+ * it will be returned unmodified.
+ *
+ * If [value] is a [Map], the resulting value will use the appropriate kind of
+ * backing map: either [HashMap], [LinkedHashMap], or [SplayTreeMap].
+ *
+ * By default this performs a deep conversion, but you can set [deep] to false
+ * for a shallow conversion. This does not handle circular data structures.
+ */
+// TODO(jmesserly): ObservableSet?
+toObservable(value, {bool deep: true}) =>
+ deep ? _toObservableDeep(value) : _toObservableShallow(value);
+
+_toObservableShallow(value) {
+ if (value is Observable) return value;
+ if (value is Map) return new ObservableMap.from(value);
+ if (value is Iterable) return new ObservableList.from(value);
+ return value;
+}
+
+_toObservableDeep(value) {
+ if (value is Observable) return value;
+ if (value is Map) {
+ var result = new ObservableMap._createFromType(value);
+ value.forEach((k, v) {
+ result[_toObservableDeep(k)] = _toObservableDeep(v);
+ });
+ return result;
+ }
+ if (value is Iterable) {
+ return new ObservableList.from(value.map(_toObservableDeep));
+ }
+ return value;
+}
« no previous file with comments | « no previous file | pkg/mdv_observe/lib/src/observable_box.dart » ('j') | tests/compiler/dart2js/analyze_api_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698