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

Side by Side Diff: lib/src/common/behavior.dart

Issue 1290643006: First cut at behaviors. This just implements the lifecycle methodsportion. We may get the rest for … (Closed) Base URL: git@github.com:dart-lang/polymer-dart.git@master
Patch Set: Created 5 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) 2014, the Dart project authors. Please see the AUTHORS file
Siggi Cherem (dart-lang) 2015/08/13 18:52:41 2015 :)
jakemac 2015/08/13 19:14:29 Acknowledged.
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 library polymer.src.common.behavior;
5
6 import 'dart:js';
7 import 'package:reflectable/reflectable.dart';
8 import 'js_proxy.dart';
9
10 // Interface for behavior annotations.
11 abstract class BehaviorAnnotation {
12 // Returns the JsObject created for this behavior.
13 JsObject getBehavior(Type type);
14 }
15
16 Map<Type, JsObject> _behaviorsByType = {};
17
18 const String _lifecycleMethodsPattern =
19 r'^created|attached|detached|attributeChanged$';
20 final RegExp _lifecycleMethodsRegex = new RegExp(_lifecycleMethodsPattern);
21
22 // Annotation class for behaviors written in dart.
23 class Behavior extends Reflectable implements BehaviorAnnotation {
24 JsObject getBehavior(Type type) {
25 return _behaviorsByType.putIfAbsent(type, () {
26 var obj = new JsObject(context['Object']);
27
28 // Add an entry for each static lifecycle method. These methods must take
29 // a `this` arg as the first argument.
30 var typeMirror = this.reflectType(type);
31 typeMirror.staticMembers.forEach((String name, MethodMirror method) {
32 if (!_lifecycleMethodsRegex.hasMatch(name)) return;
33 if (name == 'attributeChanged') {
34 obj[name] = new JsFunction.withThis(
35 (thisArg, String attributeName, Type type, value) {
36 typeMirror.invoke(
37 name, [dartValue(thisArg), attributeName, type, value]);
38 });
39 } else {
40 obj[name] = new JsFunction.withThis((thisArg) {
41 typeMirror.invoke(name, [thisArg]);
42 });
43 }
44 });
45
46 return obj;
47 });
48 }
49
50 const Behavior()
51 : super(declarationsCapability, typeCapability,
52 const StaticInvokeCapability(_lifecycleMethodsPattern));
53 }
54
55 const behavior = const Behavior();
56
57 // Annotation class for wrappers around behaviors written in javascript.
58 class BehaviorProxy implements BehaviorAnnotation {
59 // Path within js global context object to the original js behavior object.
60 final List<String> _jsPath;
61
62 // Returns the actual behavior.
63 JsObject getBehavior(Type type) {
64 return _behaviorsByType.putIfAbsent(type, () {
65 if (_jsPath.isEmpty) {
66 throw 'Invalid empty path for BehaviorProxy $_jsPath.';
67 }
68 var obj = context;
69 for (var part in _jsPath) {
70 obj = obj[part];
71 }
72 return obj;
73 });
74 }
75
76 // TODO(jakemac): Support dot separated Strings for paths?
77 const BehaviorProxy(this._jsPath);
78 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698