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

Unified Diff: pkg/polymer/lib/src/mirror_loader.dart

Issue 189213003: Refactoring two pieces of polymer: (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 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/polymer/lib/src/mirror_loader.dart
diff --git a/pkg/polymer/lib/src/loader.dart b/pkg/polymer/lib/src/mirror_loader.dart
similarity index 55%
copy from pkg/polymer/lib/src/loader.dart
copy to pkg/polymer/lib/src/mirror_loader.dart
index f3dc5e86e27becce5afdfd415909524c945ceffa..c182d9c470a7e6d8431bbdb324fd2f0371d59443 100644
--- a/pkg/polymer/lib/src/loader.dart
+++ b/pkg/polymer/lib/src/mirror_loader.dart
@@ -1,81 +1,41 @@
-// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// Copyright (c) 2014, 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.
-part of polymer;
+/// Contains logic to initialize polymer apps during development. This
+/// implementation uses dart:mirrors to load each library as they are discovered
+/// through HTML imports. This is only meant to be during development in
+/// dartium, and the polymer transformers replace this implementation with
+/// codege generation in the polymer-build steps.
+library polymer.src.mirror_loader;
-/// Annotation used to automatically register polymer elements.
-class CustomTag {
- final String tagName;
- const CustomTag(this.tagName);
-}
-
-/// Metadata used to label static or top-level methods that are called
-/// automatically when loading the library of a custom element.
-const initMethod = const _InitMethodAnnotation();
-
-/// Initializes a polymer application as follows:
-/// * set up up polling for observable changes
-/// * initialize Model-Driven Views
-/// * Include some style to prevent flash of unstyled content (FOUC)
-/// * for each library included transitively from HTML and HTML imports,
-/// register custom elements declared there (labeled with [CustomTag]) and
-/// invoke the initialization method on it (top-level functions annotated with
-/// [initMethod]).
-Zone initPolymer() {
- // We use this pattern, and not the inline lazy initialization pattern, so we
- // can help dart2js detect that _discoverInitializers can be tree-shaken for
- // deployment (and hence all uses of dart:mirrors from this loading logic).
- // TODO(sigmund): fix polymer's transformers so they can replace initPolymer
- // by initPolymerOptimized.
- if (_initializers == null) _initializers = _discoverInitializers();
-
- // In deployment mode, we rely on change notifiers instead of dirty checking.
- if (!_deployMode) {
- return dirtyCheckZone()..run(initPolymerOptimized);
- }
-
- return initPolymerOptimized();
-}
+import 'dart:async';
+import 'dart:html';
-/// Same as [initPolymer], but runs the version that is optimized for deployment
-/// to the internet. The biggest difference is it omits the [Zone] that
-/// automatically invokes [Observable.dirtyCheck], and the list of initializers
-/// must be supplied instead of being dynamically searched for at runtime using
-/// mirrors.
-Zone initPolymerOptimized() {
- // TODO(sigmund): refactor this so we can replace it by codegen.
- smoke.useMirrors();
- _hookJsPolymer();
+// Technically, we shouldn't need any @MirrorsUsed, since this is for
+// development only, but our test bots don't yet run pub-build. See more details
+// on the comments of the mirrors import in `lib/polymer.dart`.
+@MirrorsUsed(metaTargets:
+ const [CustomTag, InitMethodAnnotation],
+ override: const ['smoke.mirrors', 'polymer.src.mirror_loader'])
+import 'dart:mirrors';
- for (var initializer in _initializers) {
- initializer();
- }
-
- return Zone.current;
-}
+import 'package:logging/logging.dart' show Logger;
+import 'package:polymer/polymer.dart' show
+ InitMethodAnnotation, CustomTag, initMethod, Polymer;
-/// Configures [initPolymer] making it optimized for deployment to the internet.
-/// With this setup the initializer list is supplied instead of searched for
-/// at runtime. Additionally, after this method is called [initPolymer] omits
-/// the [Zone] that automatically invokes [Observable.dirtyCheck].
-void configureForDeployment(List<Function> initializers) {
- _initializers = initializers;
- _deployMode = true;
-}
-/// List of initializers that by default will be executed when calling
-/// initPolymer. If null, initPolymer will compute the list of initializers by
-/// crawling HTML imports, searchfing for script tags, and including an
+/// Set of initializers that are invoked by `initPolymer`. This is computed the
+/// list by crawling HTML imports, searching for script tags, and including an
/// initializer for each type tagged with a [CustomTag] annotation and for each
-/// top-level method annotated with [initMethod]. The value of this field is
-/// assigned programatically by the code generated from the polymer deploy
-/// scripts.
-List<Function> _initializers;
+/// top-level method annotated with [initMethod].
+List<Function> initializers = _discoverInitializers();
/// True if we're in deployment mode.
-bool _deployMode = false;
+bool deployMode = false;
+/// Discovers what script tags are loaded from HTML pages and collects the
+/// initializers of their corresponding libraries.
List<Function> _discoverInitializers() {
var initializers = [];
var librariesToLoad = _discoverScripts(document, window.location.href);
@@ -132,7 +92,7 @@ final _libs = currentMirrorSystem().libraries;
// root library (see dartbug.com/12612)
final _rootUri = currentMirrorSystem().isolate.rootLibrary.uri;
-final Logger _loaderLog = new Logger('polymer.loader');
+final Logger _loaderLog = new Logger('polymer.src.mirror_loader');
bool _isHttpStylePackageUrl(Uri uri) {
var uriPath = uri.path;
@@ -251,63 +211,3 @@ void _addInitMethod(ObjectMirror obj, MethodMirror method,
}
initializers.add(() => obj.invoke(method.simpleName, const []));
}
-
-class _InitMethodAnnotation {
- const _InitMethodAnnotation();
-}
-
-/// To ensure Dart can interoperate with polymer-element registered by
-/// polymer.js, we need to be able to execute Dart code if we are registering
-/// a Dart class for that element. We trigger Dart logic by patching
-/// polymer-element's register function and:
-///
-/// * if it has a Dart class, run PolymerDeclaration's register.
-/// * otherwise it is a JS prototype, run polymer-element's normal register.
-void _hookJsPolymer() {
- var polymerJs = js.context['Polymer'];
- if (polymerJs == null) {
- throw new StateError('polymer.js must be loaded before polymer.dart, please'
- ' add <link rel="import" href="packages/polymer/polymer.html"> to your'
- ' <head> before any Dart scripts. Alternatively you can get a different'
- ' version of polymer.js by following the instructions at'
- ' http://www.polymer-project.org; if you do that be sure to include'
- ' the platform polyfills.');
- }
-
- // TODO(jmesserly): dart:js appears to not callback in the correct zone:
- // https://code.google.com/p/dart/issues/detail?id=17301
- var zone = Zone.current;
-
- polymerJs.callMethod('whenPolymerReady',
- [zone.bindCallback(() => Polymer._ready.complete())]);
-
- var jsPolymer = new JsObject.fromBrowserObject(
- document.createElement('polymer-element'));
-
- var proto = js.context['Object'].callMethod('getPrototypeOf', [jsPolymer]);
- if (proto is Node) {
- proto = new JsObject.fromBrowserObject(proto);
- }
-
- JsFunction originalRegister = proto['register'];
- if (originalRegister == null) {
- throw new StateError('polymer.js must expose "register" function on '
- 'polymer-element to enable polymer.dart to interoperate.');
- }
-
- registerDart(jsElem, String name, String extendee) {
- // By the time we get here, we'll know for sure if it is a Dart object
- // or not, because polymer-element will wait for us to notify that
- // the @CustomTag was found.
- final type = _getRegisteredType(name);
- if (type != null) {
- final extendsDecl = _getDeclaration(extendee);
- return zone.run(() =>
- new PolymerDeclaration(jsElem, name, type, extendsDecl).register());
- }
- // It's a JavaScript polymer element, fall back to the original register.
- return originalRegister.apply([name, extendee], thisArg: jsElem);
- }
-
- proto['register'] = new JsFunction.withThis(registerDart);
-}

Powered by Google App Engine
This is Rietveld 408576698