Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function() {
- var exports = {};
- window.module = {
- export: function(data) {
- var _ = this;
- var exportUrl = document.currentScript.getAttributeNode('src');
- var moduleHash = _.hash(exportUrl.value);
- exports[moduleHash] = data;
- },
- import: function(url) {
- var _ = this
- var moduleHash = _.hash(url)
- if (result = exports[moduleHash]) {
- console.log('cache hit!')
- return Promise.resolve(result)
- }
- return new Promise(function(resolve, reject) {
- var script = document.createElement('script');
- var src = document.createAttribute('src');
- src.value = url
- script.setAttributeNode(src)
- script.addEventListener('load',function() {
- resolve(exports[moduleHash])
- })
- script.addEventListener('error', function(err) {
- reject(err)
- })
- document.body.appendChild(script)
- })
- },
- hash: function(str){
- var hash = 0;
- if (str.length == 0) return hash;
- for (i = 0; i < str.length; i++) {
- char = str.charCodeAt(i);
- hash = ((hash<<5)-hash)+char;
- hash = hash & hash; // Convert to 32bit integer
- }
- return hash;
- }
- }
- })()
Advertisement
Add Comment
Please, Sign In to add comment