forked from acenturyandabit/polymorph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
80 lines (78 loc) · 2.07 KB
/
utils.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
function capacitor(t, limit, send, settings = {}, checkInterval = 100) {
let options = {
fireFirst: false,
afterLast: true,
};
if (typeof(settings) == "boolean") {
options.fireFirst = settings;
} else {
Object.assign(options, settings);
}
let me = this;
let lastUID;
let lastData;
let tcount = 0;
let rqcount = 0;
let pid = undefined;
let prefire = false;
this.forceSend = function() {
send(lastUID, lastData);
rqcount = 0;
clearTimeout(pid);
pid = undefined;
}
this.checkAndUpdate = function() {
tcount -= checkInterval;
if (tcount <= 0) {
if (!prefire) {
me.forceSend();
}
} else {
pid = setTimeout(me.checkAndUpdate, checkInterval);
}
}
this.submit = function(UID, data) {
if (options.presubmit) options.presubmit();
if (lastUID != UID && lastUID) {
me.forceSend();
} else {
if (rqcount == 0 && options.fireFirst) {
lastUID = UID;
me.forceSend();
prefire = true;
} else {
prefire = false;
}
rqcount++;
if (rqcount > limit) {
me.forceSend();
rqcount = 1;
}
if (options.afterLast && pid) {
clearTimeout(pid);
pid = undefined;
}
if (!pid) {
tcount = t;
pid = setTimeout(me.checkAndUpdate, checkInterval);
}
}
lastUID = UID;
lastData = data;
}
}
function htmlwrap(html, el) {
let d = document.createElement(el || 'div');
d.innerHTML = html;
if (d.children.length == 1) {
let dd = d.children[0];
dd.remove();
return dd;
} else return d;
}
function waitForFn(property) {
let me = this;
if (!this[property]) this[property] = function(args) {
setTimeout(() => me[property].apply(me, arguments), 1000);
}
}