Skip to content

Commit

Permalink
Change forceRegister to eagerRegister and add `Polymer.Settings.e…
Browse files Browse the repository at this point in the history
…agerRegister` flag.
  • Loading branch information
Steven Orvell committed Mar 15, 2016
1 parent d53323d commit f6597ec
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
9 changes: 8 additions & 1 deletion src/lib/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="settings.html">
<script>
(function() {

'use strict';

var settings = Polymer.Settings;

Polymer.Base = {

Expand All @@ -28,7 +34,7 @@
this._desugarBehaviors(); // abstract
this._doBehavior('beforeRegister'); // abstract
this._registerFeatures(); // abstract
if (this.forceRegister) {
if (settings.eagerRegister || this.eagerRegister) {
this.ensureRegistered();
}
},
Expand Down Expand Up @@ -178,4 +184,5 @@
// TODO(sjmiles): ad hoc telemetry
Polymer.telemetry.instanceCount = 0;

})();
</script>
37 changes: 16 additions & 21 deletions src/lib/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,33 @@
// like: Polymer = {dom: 'shady'}

// via Polymer object
var user = window.Polymer || {};
var settings = window.Polymer || {};

// via url
var parts = location.search.slice(1).split('&');
for (var i=0, o; (i < parts.length) && (o=parts[i]); i++) {
o = o.split('=');
o[0] && (user[o[0]] = o[1] || true);
o[0] && (settings[o[0]] = o[1] || true);
}

var wantShadow = (user.dom === 'shadow');
var hasShadow = Boolean(Element.prototype.createShadowRoot);
var nativeShadow = hasShadow && !window.ShadowDOMPolyfill;
var useShadow = wantShadow && hasShadow;
settings.wantShadow = (settings.dom === 'shadow');
settings.hasShadow = Boolean(Element.prototype.createShadowRoot);
settings.nativeShadow = settings.hasShadow && !window.ShadowDOMPolyfill;
settings.useShadow = settings.wantShadow && settings.hasShadow;

var hasNativeImports = Boolean('import' in document.createElement('link'));
var useNativeImports = hasNativeImports;
settings.hasNativeImports =
Boolean('import' in document.createElement('link'));
settings.useNativeImports = settings.hasNativeImports;

var useNativeCustomElements = (!window.CustomElements ||
settings.useNativeCustomElements = (!window.CustomElements ||
window.CustomElements.useNative);

var usePolyfillProto = !useNativeCustomElements && !Object.__proto__;

return {
wantShadow: wantShadow,
hasShadow: hasShadow,
nativeShadow: nativeShadow,
useShadow: useShadow,
useNativeShadow: useShadow && nativeShadow,
useNativeImports: useNativeImports,
useNativeCustomElements: useNativeCustomElements,
usePolyfillProto: usePolyfillProto
};
settings.useNativeShadow = settings.useShadow && settings.nativeShadow;

settings.usePolyfillProto = !settings.useNativeCustomElements &&
!Object.__proto__;

return settings;
})()
};

Expand Down
23 changes: 19 additions & 4 deletions test/unit/lazy-register.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

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

Expand Down Expand Up @@ -65,7 +65,7 @@
HTMLImports.whenReady(function() {
Polymer({
is: 'x-eager-style',
forceRegister: true
eagerRegister: true
});
});
</script>
Expand All @@ -81,7 +81,7 @@
assert.isTrue(XLazy.prototype.registered.called, 'registered not called after instance created');
});

test('registered called at registration time if `forceRegister` is true', function() {
test('registered called at registration time if `eagerRegister` is true', function() {
assert.isTrue(XEager.prototype.registered.called, 'registered not called before instance created');
document.createElement('x-eager');
assert.isTrue(XLazy.prototype.registered.calledOnce, 'registered called more than once');
Expand All @@ -93,12 +93,27 @@
assert.ok(document.querySelector('style[scope=x-lazy-style]'), 'style shimmed at first instance');
});

test('styles shimmed at registration when `forceRegister` is true', function() {
test('styles shimmed at registration when `eagerRegister` is true', function() {
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;
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;
var XTest2 = Polymer({
is: 'x-test2',
registered: sinon.spy()
});
assert.isFalse(XTest2.prototype.registered.called, 'registered called when eagerRegister not set');
});

});

</script>
Expand Down

0 comments on commit f6597ec

Please sign in to comment.