Merge pull request #688 from webcomponents/no-double-wcready

Fire WebComponentsReady once. Remove HTMLImports stub
This commit is contained in:
Daniel Freedman
2017-02-16 12:05:52 -08:00
committed by GitHub
5 changed files with 18 additions and 45 deletions

View File

@@ -26,7 +26,7 @@
// order expected
assert.deepEqual(a1DocsList, ['a1-instance.html', 'a1-reference.html']);
// style applied at upgrade time
if (!HTMLImports.useNative) {
if (window.HTMLImports) {
assert.isTrue(styleAppliedToDocument);
}
done();

View File

@@ -26,8 +26,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
link.rel = 'import';
link.href = 'imports/element-import.html';
document.head.appendChild(link);
HTMLImports.whenReady(function() {
link.addEventListener('load', function() {
assert.isTrue(xfoo.isCreated, 'element in main document, registered in dynamic import is upgraded');
var ix = link.import.querySelector('x-foo');
assert.isTrue(ix.isCreated, 'element in import, registered in dynamic import is upgraded');

View File

@@ -38,7 +38,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
}
test('upgraded document tree', function(done) {
if (CustomElements.useNative || HTMLImports.useNative) {
if (CustomElements.useNative || !window.HTMLImports) {
return done();
} else {
window.addEventListener('WebComponentsReady', function() {

View File

@@ -16,8 +16,9 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
window.addEventListener('HTMLImportsLoaded', function() {
window.importsOk = true;
});
window.webComponentsReadyCount = 0;
window.addEventListener('WebComponentsReady', function() {
window.webComponentsReady = true;
window.webComponentsReadyCount++;
});
</script>
<script src="../webcomponents-loader.js"></script>
@@ -28,8 +29,8 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
<script>
suite('Loader', function() {
test('expected boot', function() {
assert.ok(window.webComponentsReady, 'failed to fire WebComponentsReady');
if (!window.HTMLImports.useNative) {
assert.equal(window.webComponentsReadyCount, 1, 'failed to fire WebComponentsReady');
if (window.HTMLImports) {
assert.ok(window.importsOk, 'WebComponentsReady without HTMLImportsLoaded');
}
assert.ok(window.importTest, 'import failed to set global value');

View File

@@ -11,39 +11,9 @@
(function() {
// Feature detect which polyfill needs to be imported.
let polyfills = [];
let useNativeImports = ('import' in document.createElement('link'));
if (!useNativeImports) {
if (!('import' in document.createElement('link'))) {
polyfills.push('hi');
}
// Stub out HTMLImports if we're using native imports
window.HTMLImports = {
useNative: useNativeImports,
whenReady: function(callback) {
if (useNativeImports) {
// When native imports boot, the are "ready" the first rAF after
// the document becomes interactive, so wait for the correct state change.
if (document.readyState !== 'interactive') {
function once() {
document.removeEventListener('readystatechange', once);
window.HTMLImports.whenReady(callback);
}
document.addEventListener('readystatechange', once);
} else {
// TODO(sorvell): Ideally `whenReady` should return synchronously
// when imports are not pending but this would require a more
// robust implementation that should probably be a small complementary
// library available via the html-imports polyfill.
requestAnimationFrame(function() {
callback();
});
}
} else {
window.addEventListener('HTMLImportsLoaded', callback);
}
}
};
if (!('attachShadow' in Element.prototype) || (window.ShadyDOM && window.ShadyDOM.force)) {
polyfills.push('sd');
}
@@ -59,7 +29,7 @@
// https://github.com/webcomponents/shadycss/issues/46.
if (!polyfills.length) {
polyfills.push('none');
} else if (polyfills.length === 4) { // hi-ce-sd-pf is actually called lite.
} else if (polyfills.length === 4) { // hi-ce-sd-pf is actually called lite.
polyfills = ['lite'];
}
@@ -68,14 +38,17 @@
let newScript = document.createElement('script');
// Load it from the right place.
var url = script.src.replace(
'webcomponents-loader.js', `webcomponents-${polyfills.join('-')}.js`);
'webcomponents-loader.js', `webcomponents-${polyfills.join('-')}.js`);
newScript.src = url;
document.head.appendChild(newScript);
}
HTMLImports.whenReady(function() {
// Ensure `WebComponentsReady` is fired also when there are no polyfills loaded.
// TODO(valdrin): only check for `!polyfills.length` once 'none' bundle
// is removed. Addressing this is blocked on
// https://github.com/webcomponents/shadycss/issues/46.
if (polyfills[0] === 'none') {
requestAnimationFrame(function() {
window.dispatchEvent(new CustomEvent('WebComponentsReady'));
})
});
})();
});
}
})();