This repository has been archived by the owner on May 26, 2020. It is now read-only.
forked from DynoSRC/dynosrc
/
dynoSrc-core.js
176 lines (138 loc) · 3.78 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* dynoSrc-core
*/
(function() {
var manifest = {},
plugins = <%= plugins %>,
version = '<%= version %>',
localStoragePrefix = '<%= localStoragePrefix %>',
updateCallbacks = [],
destroyCallbacks = [],
callbackCount = 0,
endpoint = '/dynoSrc',
callbackPrefix = '__dynoSrcCb';
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));
}
};
}
function fetch (name, version, cb) {
var from = this.getVersion(name) || '',
callbackName = callbackPrefix + (callbackCount++),
path = endpoint + '?',
script = document.createElement('script');
window[callbackName] = function (name, version, patchSrc) {
var updated = dynoSrc.add(name, version, patchSrc);
if (cb) {
cb(updated, patchSrc);
}
delete window[callbackName];
};
path += 'id=' + name +
'&from=' + from +
'&to=' + (version || 'HEAD') +
'&fmt=js' +
'&callback=' + callbackName;
document.head.appendChild(script);
script.src = path;
return script;
};
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, cb) {
if (!name) {
throw new Error('No module name!');
}
var src = this.storage.get(name, version) || '';
if (src) {
evalScript(src);
if (cb) cb(src);
} else {
fetch.call(this, name, version, cb);
}
},
init: function init () {
plugins.forEach(function (plugin) {
this.load(plugin, version);
}, this);
},
destroy: function destroy () {
for (var name in manifest) {
delete manifest[name];
}
destroyCallbacks.forEach(function (fn) { fn(); });
}
};
window.dynoSrc.init();
})();