Created
June 21, 2017 02:50
-
-
Save stramel/15c69eb58559d95523df00482c2dc60e to your computer and use it in GitHub Desktop.
A couple options
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
Polymer.__importLazyGroup = function( | |
callingElement, group, fromElement, options) { | |
if (!fromElement) { | |
fromElement = Polymer.DomModule.import(callingElement.localName); | |
} | |
var importStatuses = {loaded: [], failed: []}; | |
var groupAttribute = group ? '[group=' + group + ']' : ':not([group])'; | |
var query = | |
'link' +groupAttribute+ '[rel=\'lazy-import\'][href]:not([href=\'\'])'; | |
var links = fromElement.querySelectorAll(query); | |
if (links.length < 1) { | |
return Promise.resolve(importStatuses); | |
} | |
var promises = Array.from(links).map(function(link) { | |
var href = link.getAttribute('href'); | |
var resolvedUrl = callingElement.resolveUrl(href); | |
var reflect = function(state) { | |
return state; | |
}; | |
var getPromise = function() { | |
if (__importPromises.hasOwnProperty(resolvedUrl)) { | |
if (options && !options.retry) { | |
return __importPromises[resolvedUrl].catch(reflect); | |
} else { | |
return __importPromises[resolvedUrl]; | |
} | |
} else { | |
return Promise.reject(); | |
} | |
}; | |
return getPromise().catch(function() { | |
return __importPromises[resolvedUrl] = | |
new Promise(function(resolve, reject) { | |
var onLoad = function() { | |
return resolve({loaded: href}); | |
}; | |
var onFail = function() { | |
return reject({failed: href}); | |
}; | |
var importHref = Polymer.importHref || callingElement.importHref; | |
importHref(resolvedUrl, onLoad, onFail); | |
}); | |
}).catch(reflect); | |
}); | |
return Promise.all(promises).then(function(importRequests) { | |
return importRequests.reduce(function(requests, nextRequest) { | |
if (nextRequest.loaded) { | |
requests.loaded.push(nextRequest.loaded); | |
} | |
if (nextRequest.failed) { | |
requests.failed.push(nextRequest.failed); | |
} | |
return requests; | |
}, importStatuses); | |
}); | |
}; |
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
Polymer.__importLazyGroup = function( | |
callingElement, group, fromElement, options) { | |
if (!fromElement) { | |
fromElement = Polymer.DomModule.import(callingElement.localName); | |
} | |
var importStatuses = {loaded: [], failed: []}; | |
var groupAttribute = group ? '[group=' + group + ']' : ':not([group])'; | |
var query = | |
'link' +groupAttribute+ '[rel=\'lazy-import\'][href]:not([href=\'\'])'; | |
var links = fromElement.querySelectorAll(query); | |
if (links.length < 1) { | |
return Promise.resolve(importStatuses); | |
} | |
var promises = Array.from(links).map(function(link) { | |
var promise; | |
var href = link.getAttribute('href'); | |
var resolvedUrl = callingElement.resolveUrl(href); | |
var requestImport = function() { | |
return new Promise(function(resolve, reject) { | |
var importHref = Polymer.importHref || callingElement.importHref; | |
importHref(resolvedUrl, resolve, reject); | |
}); | |
}; | |
if (__importPromises.hasOwnProperty(resolvedUrl)) { | |
promise = __importPromises[resolvedUrl]; | |
if (options && options.retry) { | |
promise = promise.catch(requestImport); | |
} | |
} else { | |
promise = requestImport(); | |
} | |
__importPromises[resolvedUrl] = promise; | |
return promise.then( | |
function onLoad() { | |
return {loaded: href}; | |
}, | |
function onFail() { | |
return {failed: href}; | |
} | |
); | |
}); | |
return Promise.all(promises).then(function(importRequests) { | |
return importRequests.reduce(function(requests, nextRequest) { | |
if (nextRequest.loaded) { | |
requests.loaded.push(nextRequest.loaded); | |
} | |
if (nextRequest.failed) { | |
requests.failed.push(nextRequest.failed); | |
} | |
return requests; | |
}, importStatuses); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment