-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
157 lines (143 loc) · 3.64 KB
/
index.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
const { log } = console
function* initTrampolineManager() {
let aPrime = undefined
let count = 0
const trackChain = () => {
count++
return () => {
count--
if (count <= 0) {
count = 0
aPrime = undefined
}
}
}
do {
let fluents = yield aPrime
if (
Object.is(Boolean(aPrime), false) &&
typeof fluents.prime === "function"
) {
aPrime = fluents.prime()
}
if (aPrime instanceof Promise && typeof fluents.chain === "function") {
aPrime.then(fluents.chain).then(trackChain())
}
} while (true)
}
const trampolinePromiseOffPrime = {
_manager: undefined,
get manager() {
if (this._manager) {
return this._manager
} else {
this._manager = initTrampolineManager()
this._manager.next()
return this._manager
}
},
prime: function(init) {
const chain = chainAble => {
return {
collect: this.manager.next({ prime: init, chain: chainAble }).value,
chain
}
}
return {
chain
}
}
}
function getTimeoutPromise(label, timeout = 1000) {
return () =>
new Promise(resolve => {
setTimeout(() => {
resolve(`"${label}" - "${timeout}"`)
}, timeout)
})
}
const Colors = {
green: 32,
orange: 33,
blue: 34,
red: 31
}
const Logger = {
_build: label => numCode => `\x1b[${numCode}m${label}\x1b[0m`,
messageText: function(text) {
const func = this._build(text)
return {
value: func(Colors._blue),
withColor: {
get orange() {
return func(Colors.orange)
},
get red() {
return func(Colors.red)
},
get blue() {
return func(Colors.blue)
},
get green() {
return func(Colors.green)
}
}
}
}
}
function getMockChainer(label) {
return promiseReponse => {
log(Logger.messageText("Log").withColor.orange)
log("\x1b[34m%s\x1b[0m", `\tChain:`, `\x1b[32m${label}\x1b[0m`)
log("\x1b[34m%s\x1b[0m", `\tPrime Resolve:`, `\x1b[32m${promiseReponse}\x1b[0m`)
log(`\x1b[31m${"Done"}\x1b[0m\n`)
}
}
log("Start")
log("Case 1")
trampolinePromiseOffPrime
.prime(getTimeoutPromise("Promise1", 1))
.chain(getMockChainer("test1"))
.chain(getMockChainer("test1"))
.chain(getMockChainer("test1"))
.chain(getMockChainer("test1")).collect
log("Case 2")
trampolinePromiseOffPrime
.prime(getTimeoutPromise("promise2", 1000))
.chain(getMockChainer("test2")).collect
log("Case 3")
trampolinePromiseOffPrime
.prime(getTimeoutPromise("Promise3", 1000))
.chain(getMockChainer("test3"))
.chain(getMockChainer("test3"))
.chain(getMockChainer("test3"))
log("Case 4")
trampolinePromiseOffPrime
.prime(getTimeoutPromise("Promise4", 1000))
.chain(getMockChainer("test4")).collect
log("SetTimeout: 1\n")
setTimeout(() => {
trampolinePromiseOffPrime
.prime(getTimeoutPromise("Promise5", 500))
.chain(getMockChainer("test5")).collect
trampolinePromiseOffPrime
.prime(getTimeoutPromise("Promise6", 1))
.chain(getMockChainer("test6"))
.chain(getMockChainer("test6"))
trampolinePromiseOffPrime
.prime(getTimeoutPromise("Promise7", 10000))
.chain(getMockChainer("test7")).collect
log("SetTimeout: 2\n")
setTimeout(() => {
trampolinePromiseOffPrime
.prime(getTimeoutPromise("Promise8", 5000))
.chain(getMockChainer("test8")).collect
trampolinePromiseOffPrime
.prime(getTimeoutPromise("Promise9", 5000))
.chain(getMockChainer("test9")).collect
trampolinePromiseOffPrime
.prime(getTimeoutPromise("Promise10", 5000))
.chain(getMockChainer("test10")).collect
}, 20000)
}, 5000)
log("End")