This repository has been archived by the owner on May 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
dynoSrc-core.js
137 lines (106 loc) · 3.01 KB
/
dynoSrc-core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* dynoSrc-core
*/
(function() {
var manifest = {},
plugins = [<% _.map(plugins, function (plugin) { return "'dynoSrc-" + plugin + "'" }).join(',') %>],
localStoragePrefix = '<%= localStoragePrefix %>' || 'dynosrc.',
updateCallbacks = [],
destroyCallbacks = [];
function evalScript (src) {
var script = document.createElement('script');
document.head.appendChild(script);
script.appendChild(document.createTextNode(src));
return script;
}
function makeStorage (ls) {
var getItem = ls.getItem.bind(ls),
setItem = ls.setItem.bind(ls);
function deserialize (lsValue) {
if (! lsValue) {
return null;
}
var splitIndex = lsValue.indexOf(':');
return {
version: lsValue.substring(0, splitIndex),
src: lsValue.substring(splitIndex + 1)
};
}
function serialize (version, src) {
return version + ':' + src;
}
function wrapUpdateHandler (fn) {
return function (event) {
var name = event.key,
updated = deserialize(event.newValue),
last = deserialize(event.oldValue);
return fn(name, last && last.version, updated.version, updated.src);
};
}
return {
getVersion: function getVersion (name) {
var stored = deserialize(getItem(localStoragePrefix + name));
return stored && stored.version;
},
get: function get (name, version) {
var stored = deserialize(getItem(localStoragePrefix + name));
return stored && (version && version === stored.version) && stored.src;
},
set: function storeModule (name, version, src) {
return setItem(localStoragePrefix + name, serialize(version, src));
}
};
}
window.dynoSrc = {
manifest: manifest,
storage: makeStorage(window.localStorage),
getVersion: function getVersion (name) {
if (! manifest[name]) {
manifest[name] = this.storage.getVersion(name);
}
return manifest[name];
},
add: function add (name, version, src, andEval) {
if (!name) {
throw new Error('No module name!');
}
if (!version) {
throw new Error('No revision hash!');
}
if (!src) {
throw new Error('No module source!');
}
var oldVersion = manifest[name];
manifest[name] = version;
this.storage.set(name, version, src);
updateCallbacks.forEach(function (fn) {
fn(name, oldVersion, version, src);
});
if (andEval !== false) evalScript(src);
return src;
},
onUpdate: function onUpdate (fn) {
updateCallbacks.push(fn);
},
onDestroy: function onDestroy (fn) {
destroyCallbacks.push(fn);
},
load: function load (name, version) {
if (!name) {
throw new Error('No module name!');
}
var src = this.storage.get(name, version) || '';
return src ? evalScript(src) : false;
},
init: function init () {
plugins.forEach(this.load.bind(this));
},
destroy: function destroy () {
for (var name in manifest) {
delete manifest[name];
}
destroyCallbacks.forEach(function (fn) { fn(); });
}
};
window.dynoSrc.init();
})();