Skip to content

Commit

Permalink
* turn on lazy registration via Polymer.Settings.lazyRegister
Browse files Browse the repository at this point in the history
* ensure registration finished by calling `Element.prototype.ensureRegisterFinished()`
  • Loading branch information
Steven Orvell committed Mar 17, 2016
1 parent 3dd1b61 commit 31c785d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 32 deletions.
2 changes: 1 addition & 1 deletion polymer.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
this._prepStyles();
},

_registerLazyFeatures: function() {
_finishRegisterFeatures: function() {
this._prepShimStyles();
// template markup
this._prepAnnotations();
Expand Down
28 changes: 15 additions & 13 deletions src/lib/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,35 @@
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
this._initFeatures(); // abstract
},

/**
* 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');
Expand Down
7 changes: 0 additions & 7 deletions src/standard/styling.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
21 changes: 19 additions & 2 deletions test/unit/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -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() {};
Expand All @@ -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() {

Expand Down
19 changes: 11 additions & 8 deletions test/unit/lazy-register.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
<script>
HTMLImports.whenReady(function() {

// set lazyRegister to true
Polymer.Settings.lazyRegister = true;

window.XLazy = Polymer({
is: 'x-lazy',
registered: sinon.spy()
});

window.XEager = Polymer({
is: 'x-eager',
eagerRegister: true,
registered: sinon.spy()
});

Expand Down Expand Up @@ -62,9 +64,8 @@
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({
is: 'x-eager-style',
eagerRegister: true
window.XEagerStyle = Polymer({
is: 'x-eager-style'
});
});
</script>
Expand All @@ -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');
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion test/unit/notify-path.html
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 31c785d

Please sign in to comment.