-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeferStack.src.js
61 lines (61 loc) · 2.23 KB
/
DeferStack.src.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
// The javascript promise library that allows you to defer the stack easily to a lower level
(function(){
"use strict";
// the reason I use a variable like this is because closure compiler automaticly inlines and optimizes it
///////////////////////
const DEBUGMODE = true;
///////////////////////
const persuadoStack = []; // Defered loading of promises to outermost promise scope so as to not exceed the stack level
const transPersuadoStack = []; // temporary buffer for the persuado stack
const promiseBufferLvl = 256; // length of the promise buffer for double stack buffering
var deferLevel = 0;
var isAtHigherLvl = false;
var isInsideDeferable = false; // because javascript is single threaded, we can use this to determine if there is an outermost promise
var pStackLength = 0; // the current size of the persuadoStack
var previous
/** @noCollapse */
window["DeferStack"] = function(func, start){
if (DEBUGMODE){
if (typeof func !== "function") console.error("The first argument to DeferStack must be a function.");
if (start !== undefined && typeof start !== "number") console.error("The second argument to DeferStack must be a number or undefined (for the default).");
}
if (isInsideDeferable) {
transPersuadoStack.unshift(func);
++pStackLength;
} else {
if (isAtHigherLvl) {
if (deferLevel >= promiseBufferLvl){
isInsideDeferable = true;
transPersuadoStack.unshift(func);
++pStackLength;
} else {
++deferLevel;
func();
}
} else {
deferLevel = start || 1;
isAtHigherLvl = true;
func();
if (isInsideDeferable) { // if this is the outermost promise
try {
do {
deferLevel = start || 1;
if (isInsideDeferable) {
// if some stuff got added onto the transPersuadoStack
persuadoStack.push.apply(persuadoStack, transPersuadoStack);
transPersuadoStack.length = 0;
isInsideDeferable = false;
}
persuadoStack.pop()();
} while (--pStackLength);
} catch(e) {
// reset everything so that the error doesn't permanently ruin everything
if (DEBUGMODE) console.error(e);
}
}
isInsideDeferable = isAtHigherLvl = false;
deferLevel = persuadoStack.length = pStackLength = 0;
}
}
}
})();