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

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 library polymer.src.common.behavior;
Siggi Cherem (dart-lang) 2015/08/12 22:43:14 +copyright
jakemac 2015/08/13 17:50:45 Done.
2
3 import 'dart:js';
4 import 'package:reflectable/reflectable.dart';
5 import 'js_proxy.dart';
6 import 'polymer_descriptor.dart';
7
8 typedef void _VoidFn();
Siggi Cherem (dart-lang) 2015/08/12 22:43:14 delete? (seems unused)
jakemac 2015/08/13 17:50:45 Done.
9
10 // Interface for behavior annotations.
11 abstract class BehaviorInterface {
Siggi Cherem (dart-lang) 2015/08/12 22:43:14 how about rename to BehaviorAnnotation ?
jakemac 2015/08/13 17:50:45 Done.
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 BehaviorInterface {
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, const StaticInvokeCapabili ty(_lifecycleMethodsPattern));
Siggi Cherem (dart-lang) 2015/08/12 22:43:14 nit: 80/formatter
jakemac 2015/08/13 17:50:45 Done.
52 }
53
54 // Annotation class for wrappers around behaviors written in javascript.
55 class BehaviorProxy implements BehaviorInterface {
56 // Path within js global context object to the original js behavior object.
57 final List<String> _jsPath;
Siggi Cherem (dart-lang) 2015/08/12 22:43:14 Given how it is going to be a path with dots in JS
jakemac 2015/08/13 17:50:45 Done.
58
59 // Returns the actual behavior.
60 JsObject getBehavior(Type type) {
61 return _behaviorsByType.putIfAbsent(type, () {
62 if (_jsPath.isEmpty) {
63 throw 'Invalid empty path for BehaviorProxy $_jsPath.';
64 }
65 var obj = context;
66 for (var part in _jsPath) {
67 obj = obj[part];
68 }
69 return obj;
70 });
71 }
72
73 const BehaviorProxy(this._jsPath);
74 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698