-
Notifications
You must be signed in to change notification settings - Fork 30
/
bootstrap.ts
123 lines (108 loc) · 3.3 KB
/
bootstrap.ts
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
/* eslint-disable no-inner-declarations */
/* eslint-disable no-undef-init */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable implicit-arrow-linebreak */
/* eslint-disable no-await-in-loop */
type RecursiveGenerator<T> = Generator<T | RecursiveGenerator<T>, T, T>
type RecursiveAsyncGenerator<T> = AsyncGenerator<T | RecursiveAsyncGenerator<T>, T, T>
/**
* @param gen 同步/异步的递归生成器.
* @param shouldBreak 是否中断.如果中断,则返回当前的结果.默认不中断.
* @param useTimeSlicing 是否使用时间片.如果使用时间片,则每64ms让出cpu一次.默认不使用.
* @returns 生成器的最终结果.
*/
async function bootStrapAsync<T>(
gen: RecursiveGenerator<T> | RecursiveAsyncGenerator<T>,
shouldBreak?: () => boolean,
useTimeSlicing?: boolean
): Promise<T> {
const stack = [gen]
let res: any = undefined
let start = Date.now()
const isAsyncGenerator = Object.prototype.toString.call(gen) === '[object AsyncGenerator]'
if (isAsyncGenerator) {
if (shouldBreak && shouldBreak()) return res
const last = stack[stack.length - 1]
const next = await (last as RecursiveAsyncGenerator<T>).next(res)
if (next.done) {
res = next.value
stack.pop()
} else {
stack.push(next.value as RecursiveAsyncGenerator<T>)
}
if (useTimeSlicing) {
const now = Date.now()
if (now - start > 64) {
await sleep()
start = Date.now()
}
}
} else {
while (stack.length) {
if (shouldBreak && shouldBreak()) return res
const last = stack[stack.length - 1]
const next = (last as RecursiveGenerator<T>).next(res)
if (next.done) {
res = next.value
stack.pop()
} else {
stack.push(next.value as RecursiveGenerator<T>)
}
if (useTimeSlicing) {
const now = Date.now()
if (now - start > 64) {
await sleep()
start = Date.now()
}
}
}
}
return res
}
function bootStrap<T>(gen: RecursiveGenerator<T>): T {
const stack = [gen]
let res = undefined as any
while (stack.length) {
const last = stack[stack.length - 1]
const next = last.next(res)
if (next.done) {
res = next.value
stack.pop()
} else {
stack.push(next.value as RecursiveGenerator<T>)
}
}
return res
}
function sleep(time = 0): Promise<void> {
return new Promise(resolve => {
setTimeout(resolve, time)
})
}
export { RecursiveGenerator, RecursiveAsyncGenerator, bootStrap, bootStrapAsync, sleep }
if (require.main === module) {
// eslint-disable-next-line no-inner-declarations
function facDfs(n: number): bigint {
if (n <= 1) return BigInt(1)
return BigInt(n) * facDfs(n - 1)
}
// eslint-disable-next-line no-inner-declarations
function* facGen(n: number): RecursiveGenerator<bigint> {
if (n <= 1) return BigInt(1)
return BigInt(n) * (yield facGen(n - 1))
}
function fooDfs(cur: number): number {
if (cur <= 1) return 1
return cur + fooDfs(cur - 1)
}
try {
fooDfs(1e5)
} catch (error: unknown) {
if (error instanceof Error) console.log(error.message)
}
function* fooDfsGen(cur: number): RecursiveGenerator<number> {
if (cur <= 1) return 1
return cur + (yield fooDfsGen(cur - 1))
}
console.log(bootStrap(fooDfsGen(1e5)))
}