Open History to see the last commit
1interface PoolConfig {
2 concurrency?: number
3 started?: boolean
4 tasks?: Array<() => Promise<any>>
5}
6β
7const defaultConfig: PoolConfig = {
8 concurrency: 5,
9 started: false,
10 tasks: [],
11}
12β
13export class Queue<T> {
14 private onSettles: Array<(res: any, error: any) => void> = []
15 private onErrors: Array<(error: any, task: () => Promise<any>) => void> = []
16 private onSuccesses: Array<(result: any, task: () => Promise<any>) => void> =
17 []
18 private running: boolean
19 private active: Array<() => Promise<any>> = []
20 private pending: Array<() => Promise<any>>
21 private currentConcurrency: number
22β
23 constructor(config: PoolConfig = defaultConfig) {
24 const { concurrency, started, tasks } = {
25 ...defaultConfig,
26 ...config,
27 }
28 this.running = started!
29 this.pending = tasks as Array<() => Promise<any>>
30 this.currentConcurrency = concurrency!
31 }
32β
33 private tick() {
34 if (!this.running) {
35 return
36 }
37 while (
38 this.active.length < this.currentConcurrency &&
39 this.pending.length
40 ) {
41 const nextFn = this.pending.shift()
42 if (!nextFn) {
43 throw new Error('Found task that is not a function')
44 }
45 this.active.push(nextFn)
46 ;(async () => {
47 let success = false
48 let res!: T
49 let error: any
50 try {
51 res = await nextFn()
52 success = true
53 } catch (e) {
54 error = e
55 }
56 this.active = this.active.filter((d) => d !== nextFn)
57 if (success) {
58 this.onSuccesses.forEach((d) => d(res, nextFn))
59 } else {
60 this.onErrors.forEach((d) => d(error, nextFn))
61 }
62 this.onSettles.forEach((d) => d(res, error))
63 this.tick()
64 })()
65 }
66 }
67β
68 add(fn: () => Promise<T> | T, { priority }: { priority?: boolean } = {}) {
69 return new Promise<any>((resolve, reject) => {
70 const task = () =>
71 Promise.resolve(fn())
72 .then((res) => {
73 resolve(res)
74 return res
75 })
76 .catch((err) => {
77 reject(err)
78 throw err
79 })
80 if (priority) {
81 this.pending.unshift(task)
82 } else {
83 this.pending.push(task)
84 }
85 this.tick()
86 })
87 }
88β
89 throttle(n: number) {
90 this.currentConcurrency = n
91 }
92β
93 onSettled(cb: () => void) {
94 this.onSettles.push(cb)
95 return () => {
96 this.onSettles = this.onSettles.filter((d) => d !== cb)
97 }
98 }
99β
100 onError(cb: (error: any, task: () => Promise<any>) => void) {
101 this.onErrors.push(cb)
102 return () => {
103 this.onErrors = this.onErrors.filter((d) => d !== cb)
104 }
105 }
106β
107 onSuccess(cb: (result: any, task: () => Promise<any>) => void) {
108 this.onSuccesses.push(cb)
109 return () => {
110 this.onSuccesses = this.onSuccesses.filter((d) => d !== cb)
111 }
112 }
113β
114 stop() {
115 this.running = false
116 }
117β
118 start() {
119 this.running = true
120 this.tick()
121 return new Promise<void>((resolve) => {
122 this.onSettled(() => {
123 if (this.isSettled()) {
124 resolve()
125 }
126 })
127 })
128 }
129β
130 clear() {
131 this.pending = []
132 }
133β
134 getActive() {
135 return this.active
136 }
137β
138 getPending() {
139 return this.pending
140 }
141β
142 getAll() {
143 return [...this.active, ...this.pending]
144 }
145β
146 isRunning() {
147 return this.running
148 }
149β
150 isSettled() {
151 return !this.active.length && !this.pending.length
152 }
153}