Last active
March 7, 2018 19:36
-
-
Save stramel/efe669639c428e11fd02c3a1d9f92108 to your computer and use it in GitHub Desktop.
Attempt at refactor for performance gainz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
@license | |
Copyright (c) 2016 The Polymer Project Authors. All rights reserved. | |
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | |
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | |
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | |
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 | |
--> | |
<script> | |
(function() { | |
'use strict'; | |
// var __importPromises = new Map(); | |
const __lazyImports = new Map(); | |
function requestImport(link, fromElement, importStatuses) { | |
const href = link.getAttribute('href'); | |
link.promise = new Promise(function(resolve, reject) { | |
const importHref = Polymer.importHref || fromElement.importHref; | |
importHref(fromElement.resolveUrl(href), resolve, reject); | |
}); | |
return link.promise.then( | |
function onLoad() { | |
return importStatuses.loaded.push({loaded: href}); | |
}, | |
function onFail() { | |
return importStatuses.failed.push({failed: href}); | |
} | |
) | |
} | |
/** | |
* Calls `Polymer.Base.importHref` for each `<link rel="lazy-import"` in | |
* the `dom-module` where the calling element is defined, where the | |
* "group" attribute matches the provided "group". If no group is | |
* provided, Lazy HTML Imports with no group will be imported. | |
* | |
* `importHref` is only called once per globally-unique URL. | |
* | |
* @param {HTMLElement} fromElement The element to search for lazy imports | |
* within. | |
* @param {string=} group The group attribute to filter on. | |
* @param {object=} options Accepts a retry flag which will force a | |
* retry of a previously failed import. | |
* @return {Promise} | |
*/ | |
Polymer.__importLazyGroup = function(fromElement, group, options) { | |
// var groupAttribute = group ? '[group="' + group + '"]' : ':not([group])'; | |
const query = | |
'link[rel=\'lazy-import\']' + (group ? '[group]' : ':not([group])') + '[href]:not([href=\'\'])'; | |
if (!__lazyImports.get(fromElement)) { | |
const links = | |
Array.from(Polymer.DomModule.import(fromElement.localName).querySelectorAll(query)); | |
__lazyImports.set(fromElement, links.reduce((obj, link) => { | |
const group = link.getAttribute('group'); | |
if (!obj[group]) { | |
obj[group] = []; | |
} | |
obj[group].push(link); | |
return obj; | |
}, {})); | |
} | |
const groupLinks = __lazyImports.get(fromElement)[group]; | |
if (groupLinks.length < 1) { | |
return Promise.reject( | |
new Error('No imports found in the specified group.')); | |
} | |
const __importStatuses = {loaded: [], failed: []}; | |
const requestPromises = groupLinks.map(function(link) { | |
let promise; | |
if (link.promise) { | |
promise = link.promise; | |
if (options && options.retry) { | |
promise = promise.catch(requestImport.bind(this, link, fromElement, __importStatuses)); | |
} | |
} else { | |
promise = requestImport(link, fromElement, __importStatuses); | |
} | |
return promise; | |
}); | |
return Promise.all(requestPromises).then(function() { | |
if (__importStatuses.failed.length > 0) { | |
const error = new Error('One or more imports failed to load.'); | |
error.importStatuses = __importStatuses; | |
throw error; | |
} else { | |
return __importStatuses; | |
} | |
}); | |
}; | |
})(); | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
@license | |
Copyright (c) 2016 The Polymer Project Authors. All rights reserved. | |
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | |
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | |
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | |
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 | |
--> | |
<script> | |
(function() { | |
'use strict'; | |
const __lazyImports = new Map(); | |
function requestImport(link, fromElement, importStatuses) { | |
performance.mark('requestImport'); | |
const href = link.getAttribute('href'); | |
link.promise = new Promise(function(resolve, reject) { | |
const importHref = Polymer.importHref || fromElement.importHref; | |
importHref(fromElement.resolveUrl(href), resolve, reject); | |
}); | |
return link.promise.then( | |
function onLoad() { | |
performance.mark('loaded'); | |
return importStatuses.loaded.push({loaded: href}); | |
}, | |
function onFail() { | |
performance.mark('failed'); | |
return importStatuses.failed.push({failed: href}); | |
} | |
) | |
} | |
/** | |
* Calls `Polymer.Base.importHref` for each `<link rel="lazy-import"` in | |
* the `dom-module` where the calling element is defined, where the | |
* "group" attribute matches the provided "group". If no group is | |
* provided, Lazy HTML Imports with no group will be imported. | |
* | |
* `importHref` is only called once per globally-unique URL. | |
* | |
* @param {HTMLElement} fromElement The element to search for lazy imports | |
* within. | |
* @param {string=} group The group attribute to filter on. | |
* @param {object=} options Accepts a retry flag which will force a | |
* retry of a previously failed import. | |
* @return {Promise} | |
*/ | |
Polymer.__importLazyGroup = function(fromElement, group, options) { | |
performance.mark('__importLazyGroup-START'); | |
const query = | |
'link[rel=\'lazy-import\']' + (group ? '[group]' : ':not([group])') + '[href]:not([href=\'\'])'; | |
if (!__lazyImports.get(fromElement)) { | |
performance.mark('initializeLazyGroups'); | |
const links = | |
Array.from(Polymer.DomModule.import(fromElement.localName).querySelectorAll(query)); | |
__lazyImports.set(fromElement, links.reduce((obj, link) => { | |
const group = link.getAttribute('group'); | |
if (!obj[group]) { | |
obj[group] = []; | |
} | |
obj[group].push(link); | |
return obj; | |
}, {})); | |
performance.mark('endLazyGroupInit'); | |
} | |
const groupLinks = __lazyImports.get(fromElement)[group]; | |
if (groupLinks.length < 1) { | |
return Promise.reject( | |
new Error('No imports found in the specified group.')); | |
} | |
const __importStatuses = {loaded: [], failed: []}; | |
performance.mark('Start-promise-map'); | |
const requestPromises = groupLinks.map(function(link) { | |
let promise; | |
if (link.promise) { | |
performance.mark('Promise-exists'); | |
promise = link.promise; | |
if (options && options.retry) { | |
promise = promise.catch(requestImport.bind(this, link, fromElement, __importStatuses)); | |
} | |
} else { | |
performance.mark('new Promise'); | |
promise = requestImport(link, fromElement, __importStatuses); | |
} | |
return promise; | |
}); | |
performance.mark('End-promise-map'); | |
return Promise.all(requestPromises).then(function() { | |
if (__importStatuses.failed.length > 0) { | |
const error = new Error('One or more imports failed to load.'); | |
error.importStatuses = __importStatuses; | |
throw error; | |
} else { | |
performance.mark('__importLazyGroup-END'); | |
performance.measure('Total import', '__importLazyGroup-START', '__importLazyGroup-END'); | |
performance.measure('init', 'initializeLazyGroups', 'endLazyGroupInit'); | |
performance.measure('map', 'Start-promise-map', 'End-promise-map'); | |
try { | |
performance.measure('Promise exists', 'Promise-exists', '__importLazyGroup-END'); | |
} catch (ex) {} | |
performance.measure('New Promise', 'new Promise', '__importLazyGroup-END'); | |
performance.measure('requestImport', 'requestImport', 'loaded'); | |
return __importStatuses; | |
} | |
}); | |
}; | |
})(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment