OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /// Contains logic to initialize polymer apps during development. This | 5 /// Contains logic to initialize polymer apps during development. This |
6 /// implementation uses dart:mirrors to load each library as they are discovered | 6 /// implementation uses dart:mirrors to load each library as they are discovered |
7 /// through HTML imports. This is only meant to be during development in | 7 /// through HTML imports. This is only meant to be during development in |
8 /// dartium, and the polymer transformers replace this implementation with | 8 /// dartium, and the polymer transformers replace this implementation with |
9 /// codege generation in the polymer-build steps. | 9 /// codege generation in the polymer-build steps. |
10 library polymer.src.mirror_loader; | 10 library polymer.src.mirror_loader; |
11 | 11 |
12 import 'dart:async'; | 12 import 'dart:async'; |
13 import 'dart:html'; | 13 import 'dart:html'; |
14 import 'dart:collection' show LinkedHashMap; | 14 import 'dart:collection' show LinkedHashMap; |
15 | 15 |
16 // Technically, we shouldn't need any @MirrorsUsed, since this is for | 16 // Technically, we shouldn't need any @MirrorsUsed, since this is for |
17 // development only, but our test bots don't yet run pub-build. See more details | 17 // development only, but our test bots don't yet run pub-build. See more details |
18 // on the comments of the mirrors import in `lib/polymer.dart`. | 18 // on the comments of the mirrors import in `lib/polymer.dart`. |
19 @MirrorsUsed(metaTargets: | 19 @MirrorsUsed(metaTargets: |
20 const [CustomTag, InitMethodAnnotation], | 20 const [CustomTag, InitMethodAnnotation], |
21 override: const ['smoke.mirrors', 'polymer.src.mirror_loader']) | 21 override: const ['smoke.mirrors', 'polymer.src.mirror_loader']) |
22 import 'dart:mirrors'; | 22 import 'dart:mirrors'; |
23 | 23 |
24 import 'package:logging/logging.dart' show Logger; | 24 import 'package:logging/logging.dart' show Logger; |
25 import 'package:observe/src/dirty_check.dart'; | |
26 import 'package:polymer/boot.dart' show discoverScripts, ScriptInfo; | |
Siggi Cherem (dart-lang)
2014/05/22 21:10:41
Q: I wonder if I should just move the code that is
Jennifer Messerly
2014/05/23 07:20:06
I guess the main reason is if you want it to be pu
Siggi Cherem (dart-lang)
2014/05/23 17:56:33
Done. Moved it here. At least now it is structured
| |
25 import 'package:polymer/polymer.dart'; | 27 import 'package:polymer/polymer.dart'; |
26 import 'package:observe/src/dirty_check.dart'; | |
27 | 28 |
28 | 29 |
30 /// Used by code generated from the experimental polymer bootstrap in boot.js. | |
29 void startPolymerInDevelopment(List<String> librariesToLoad) { | 31 void startPolymerInDevelopment(List<String> librariesToLoad) { |
30 dirtyCheckZone()..run(() { | 32 dirtyCheckZone()..run(() { |
31 startPolymer(discoverInitializers(librariesToLoad), false); | 33 startPolymer(discoverInitializers(librariesToLoad), false); |
32 }); | 34 }); |
33 } | 35 } |
34 | 36 |
35 /// Set of initializers that are invoked by `initPolymer`. This is computed the | 37 /// Set of initializers that are invoked by `initPolymer`. This is computed the |
36 /// list by crawling HTML imports, searching for script tags, and including an | 38 /// list by crawling HTML imports, searching for script tags, and including an |
37 /// initializer for each type tagged with a [CustomTag] annotation and for each | 39 /// initializer for each type tagged with a [CustomTag] annotation and for each |
38 /// top-level method annotated with [initMethod]. | 40 /// top-level method annotated with [initMethod]. |
41 List<Function> initializers = discoverInitializers( | |
42 discoverLibrariesToLoad(document, window.location.href)); | |
43 | |
44 /// True if we're in deployment mode. | |
45 bool deployMode = false; | |
39 | 46 |
40 /// Discovers what script tags are loaded from HTML pages and collects the | 47 /// Discovers what script tags are loaded from HTML pages and collects the |
41 /// initializers of their corresponding libraries. | 48 /// initializers of their corresponding libraries. |
42 // Visible for testing only. | 49 // Visible for testing only. |
43 List<Function> discoverInitializers(List<String> librariesToLoad) { | 50 List<Function> discoverInitializers(Iterable<String> librariesToLoad) { |
44 var initializers = []; | 51 var initializers = []; |
45 for (var lib in librariesToLoad) { | 52 for (var lib in librariesToLoad) { |
46 try { | 53 try { |
47 _loadLibrary(lib, initializers); | 54 _loadLibrary(lib, initializers); |
48 } catch (e, s) { | 55 } catch (e, s) { |
49 // Deliver errors async, so if a single library fails it doesn't prevent | 56 // Deliver errors async, so if a single library fails it doesn't prevent |
50 // other things from loading. | 57 // other things from loading. |
51 new Completer().completeError(e, s); | 58 new Completer().completeError(e, s); |
52 } | 59 } |
53 } | 60 } |
54 return initializers; | 61 return initializers; |
55 } | 62 } |
56 | 63 |
64 Iterable<String> discoverLibrariesToLoad(Document doc, String baseUri) => | |
65 discoverScripts(doc, baseUri).map( | |
66 (info) => _hasPackageUrl(info) ? info.packageUrl : info.resolvedUrl); | |
67 | |
68 bool _hasPackageUrl(ScriptInfo info) => | |
69 info.isPackage && _libs[Uri.parse(info.packageUrl)] != null; | |
70 | |
57 /// All libraries in the current isolate. | 71 /// All libraries in the current isolate. |
58 final _libs = currentMirrorSystem().libraries; | 72 final _libs = currentMirrorSystem().libraries; |
59 | 73 |
60 final Logger _loaderLog = new Logger('polymer.src.mirror_loader'); | 74 final Logger _loaderLog = new Logger('polymer.src.mirror_loader'); |
61 | 75 |
62 /// Reads the library at [uriString] (which can be an absolute URI or a relative | 76 /// Reads the library at [uriString] (which can be an absolute URI or a relative |
63 /// URI from the root library), and: | 77 /// URI from the root library), and: |
64 /// | 78 /// |
65 /// * If present, invokes any top-level and static functions marked | 79 /// * If present, invokes any top-level and static functions marked |
66 /// with the [initMethod] annotation (in the order they appear). | 80 /// with the [initMethod] annotation (in the order they appear). |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 " ${method.simpleName} is not."); | 167 " ${method.simpleName} is not."); |
154 return; | 168 return; |
155 } | 169 } |
156 if (!method.parameters.where((p) => !p.isOptional).isEmpty) { | 170 if (!method.parameters.where((p) => !p.isOptional).isEmpty) { |
157 print("warning: methods marked with @initMethod should take no " | 171 print("warning: methods marked with @initMethod should take no " |
158 "arguments, ${method.simpleName} expects some."); | 172 "arguments, ${method.simpleName} expects some."); |
159 return; | 173 return; |
160 } | 174 } |
161 initializers.add(() => obj.invoke(method.simpleName, const [])); | 175 initializers.add(() => obj.invoke(method.simpleName, const [])); |
162 } | 176 } |
OLD | NEW |