From 31c785df06c67f227a6252715fc84747a5d3ba75 Mon Sep 17 00:00:00 2001 From: Steven Orvell Date: Wed, 16 Mar 2016 17:08:14 -0700 Subject: [PATCH] * turn on lazy registration via `Polymer.Settings.lazyRegister` * ensure registration finished by calling `Element.prototype.ensureRegisterFinished()` --- polymer.html | 2 +- src/lib/base.html | 28 +++++++++++++++------------- src/standard/styling.html | 7 ------- test/unit/base.html | 21 +++++++++++++++++++-- test/unit/lazy-register.html | 19 +++++++++++-------- test/unit/notify-path.html | 2 +- 6 files changed, 47 insertions(+), 32 deletions(-) diff --git a/polymer.html b/polymer.html index c156365738..21078653d7 100644 --- a/polymer.html +++ b/polymer.html @@ -36,7 +36,7 @@ this._prepStyles(); }, - _registerLazyFeatures: function() { + _finishRegisterFeatures: function() { this._prepShimStyles(); // template markup this._prepAnnotations(); diff --git a/src/lib/base.html b/src/lib/base.html index a1690205d7..8cf9a3b444 100644 --- a/src/lib/base.html +++ b/src/lib/base.html @@ -34,13 +34,13 @@ this._desugarBehaviors(); // abstract this._doBehavior('beforeRegister'); // abstract this._registerFeatures(); // abstract - if (settings.eagerRegister || this.eagerRegister) { - this.ensureRegistered(); + if (!settings.lazyRegister) { + this.ensureRegisterFinished(); } }, createdCallback: function() { - this._ensureRegistered(this.__proto__); + this._ensureRegisterFinished(this.__proto__); Polymer.telemetry.instanceCount++; this.root = this; this._doBehavior('created'); // abstract @@ -48,19 +48,21 @@ }, /** - * When called from the element's prototype, ensures that the element has - * fully registered. By default registration tasks are defered until the - * first instance of an element is created. + * As an optimization, when `Polymer.Settings.lazyRegister` is set to true + * registration tasks are deferred until the first instance of the element + * is created. If an element should not defer registration tasks until + * this time, `ensureRegisterFinished` may be called + * on the element's prototype. */ - ensureRegistered: function() { - this._ensureRegistered(this); + ensureRegisterFinished: function() { + this._ensureRegisterFinished(this); }, - _ensureRegistered: function(proto) { - if (proto.__hasRegistered !== proto.is) { - proto.__hasRegistered = proto.is; - if (proto._registerLazyFeatures) { - proto._registerLazyFeatures(); + _ensureRegisterFinished: function(proto) { + if (proto.__hasRegisterFinished !== proto.is) { + proto.__hasRegisterFinished = proto.is; + if (proto._finishRegisterFeatures) { + proto._finishRegisterFeatures(); } // registration extension point proto._doBehavior('registered'); diff --git a/src/standard/styling.html b/src/standard/styling.html index 13373fbded..3fdec1ab2f 100644 --- a/src/standard/styling.html +++ b/src/standard/styling.html @@ -63,15 +63,8 @@ // only if no custom properties are used since otherwise // styles are applied via property shimming. if (!this._needsStyleProperties() && this._styles.length) { - // NOTE: IE has css style ordering issues unless there's at least a - // space in the stylesheet. var style = styleUtil.applyCss(cssText, this.is, nativeShadow ? this._template.content : null, this._scopeStyle); - // keep track of style when in document scope (polyfill) so we can - // attach property styles after it. - if (!nativeShadow) { - this._scopeStyle = style; - } } } else { this._styles = []; diff --git a/test/unit/base.html b/test/unit/base.html index 19724949a3..9fc827d591 100644 --- a/test/unit/base.html +++ b/test/unit/base.html @@ -29,9 +29,10 @@ setup(function() { // Ensure a clean environment for each test. + /* global Base */ window.Child = Object.create(Polymer.Base); - Child._registerFeatures = function() { - }; + Child.is = 'x-child'; + Child._registerFeatures = function() {}; Child._initFeatures = function() {}; Child._setAttributeToProperty = function() {}; Child._desugarBehaviors = function() {}; @@ -53,6 +54,22 @@ }); +suite('registerCallback', function() { + + test('calls registered() after registerFeatures()', function() { + var called = []; + Child._registerFeatures = function() { + called.push('1'); + }; + Child.registered = function() { + called.push('2'); + }; + assert.deepEqual(called, []); + Child.registerCallback(); + assert.includeMembers(called, ['1', '2']); + }); + +}); suite('createdCallback', function() { diff --git a/test/unit/lazy-register.html b/test/unit/lazy-register.html index 8bba4c8cc1..aa64431c0e 100644 --- a/test/unit/lazy-register.html +++ b/test/unit/lazy-register.html @@ -19,6 +19,9 @@ @@ -80,7 +81,8 @@ assert.isTrue(window.XLazy.prototype.registered.called, 'registered not called after instance created'); }); - test('registered called at registration time if `eagerRegister` is true', function() { + test('registered when `ensureRegisterFinished()` is called', function() { + window.XEager.prototype.ensureRegisterFinished(); assert.isTrue(window.XEager.prototype.registered.called, 'registered not called before instance created'); document.createElement('x-eager'); assert.isTrue(window.XLazy.prototype.registered.calledOnce, 'registered called more than once'); @@ -92,20 +94,21 @@ assert.ok(document.querySelector('style[scope=x-lazy-style]'), 'style shimmed at first instance'); }); - test('styles shimmed at registration when `eagerRegister` is true', function() { + test('styles shimmed when `ensureRegisterFinished()` is called', function() { + window.XEagerStyle.prototype.ensureRegisterFinished(); assert.ok(document.querySelector('style[scope=x-eager-style]'), 'style shimmed before registration complete'); document.createElement('x-eager-style'); assert.equal(document.querySelectorAll('style[scope=x-eager-style]').length, 1); }); test('Polymer.Settings.eagerRegister', function() { - Polymer.Settings.eagerRegister = true; + Polymer.Settings.lazyRegister = false; var XTest1 = Polymer({ is: 'x-test1', registered: sinon.spy() }); assert.isTrue(XTest1.prototype.registered.called, 'registered not called when eagerRegister is set'); - Polymer.Settings.eagerRegister = false; + Polymer.Settings.lazyRegister = true; var XTest2 = Polymer({ is: 'x-test2', registered: sinon.spy() diff --git a/test/unit/notify-path.html b/test/unit/notify-path.html index 6f6033f3b9..d164e4fa30 100644 --- a/test/unit/notify-path.html +++ b/test/unit/notify-path.html @@ -950,7 +950,7 @@ Polymer({ is: 'x-broken', observers: ['foo(missingParenthesis'] - }).prototype.ensureRegistered(); + }).prototype.ensureRegisterFinished(); } catch (e) { assert.equal(e.message, "Malformed observer expression 'foo(missingParenthesis'"); thrown = true;