-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab.js
More file actions
142 lines (134 loc) · 3.36 KB
/
lab.js
File metadata and controls
142 lines (134 loc) · 3.36 KB
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
(function (window) {
var scriptsCache = {};
function LAB() {
var curCbs = [];
var curErrHandles = [];
var syncReqQueCbs = [];
var syncErrHandles = [];
var syncReqQue = [];
var loadingNum = 0;
var queIdx = -1;
function nonOperate() {}
function triggerCallbacks(err) {
if (err) {
errHandle(err);
} else {
try {
while (curCbs.length) {
curCbs.shift().call(null);
}
} catch (e) {
errHandle(e);
}
}
curCbs = curErrHandles = [];
curCbs.push.apply(curCbs, syncReqQueCbs.shift());
curErrHandles.push.apply(curErrHandles, syncErrHandles.shift());
(nextReqs = syncReqQue.shift()) && nextReqs.forEach(function (request) {
request();
});
function errHandle(err) {
if (!curErrHandles.length) throw new Error(err);
while (curErrHandles.length) {
curErrHandles.shift().call(null, err);
}
}
}
function _load(url, callback) {
if (scriptsCache[url] && !scriptsCache[url].loaded) return;
if (scriptsCache[url] && scriptsCache[url].loaded) {
setTimeout(function () {
callback();
});
return;
}
document = window.document;
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.charset = 'utf-8';
script.async = true;
script.src = url;
script.onerror = function () {
delete scriptsCache[url];
script.remove();
callback({
msg: 'err',
url: url
});
}
script.onload = function () {
scriptsCache[url].loaded = 1;
callback();
}
head.appendChild(script);
setTimeout(function () {
if (scriptsCache[url].loaded === 0) {
delete scriptsCache[url];
script.remove();
callback({
msg: 'timeout',
url: url
});
}
}, 5000);
scriptsCache[url] = {
loaded: 0
};
}
function load(url) {
loadingNum++;
_load(url, function (err) {
if (err) {
loadingNum = 0;
triggerCallbacks(err);
}
if (loadingNum > 0) {
loadingNum--
if (!loadingNum) {
triggerCallbacks();
}
}
});
}
return {
script: function (url) {
if (curCbs.length) {
if (queIdx === -1 || syncReqQueCbs[queIdx]) {
syncReqQue[++queIdx] = [];
}
syncReqQue[queIdx].push(function () {
load(url);
});
} else {
load(url);
}
return this;
},
wait: function (fn) {
fn = fn || nonOperate;
if (queIdx === -1) {
curCbs.push(fn);
} else {
if (!syncReqQueCbs[queIdx]) {
syncReqQueCbs[queIdx] = [];
}
syncReqQueCbs[queIdx].push(fn);
}
return this;
},
catch: function (fn) {
if (queIdx === -1) {
curErrHandles.push(fn);
} else {
if (!syncErrHandles[queIdx]) {
syncErrHandles[queIdx] = [];
}
syncErrHandles[queIdx].push(fn);
}
return this;
}
}
}
window.$LAB = LAB;
})(window);