-
Notifications
You must be signed in to change notification settings - Fork 0
/
PromiseMap.js
168 lines (154 loc) · 3.24 KB
/
PromiseMap.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
158
159
160
161
162
163
164
165
166
167
168
/**
* @typedef {object} PromiseExecutor
* @property {function} resolve
* @property {function} reject
* @property {NodeJS.Timeout|number|undefined} [timeout]
*/
/**
* PromiseMap
*
* Keeps track of promises using a map of promises.
*/
export class PromiseMap {
/**
* Pending promises
*
* @type {Map<any, Promise>}
*/
#promises = new Map()
/**
* Pending promise executors
*
* @type {Map<any, PromiseExecutor>}
*/
#executors = new Map()
/**
* Amount of pending promises
*
* @type {number}
*/
get size() {
return this.#promises.size
}
/**
* Creates a new promise and returns it
*
* @param {any} id Promise identifier
* @param {number} [time=Infinity] Time to wait before rejecting promise
* @returns {Promise<any>}
*/
create(id, time = Infinity) {
const promise = new Promise((resolve, reject) => {
let timeout
if (Number.isFinite(time) && time > 0) {
timeout = setTimeout(
() => {
this.#executors.delete(id)
this.#promises.delete(id)
reject(new Error('Timed out'))
},
time
)
}
this.#executors.set(id, { resolve, reject, timeout })
})
this.#promises.set(id, promise)
return promise
}
/**
* Retrieves the promise by it's identifier. This method
* removes the promise.
*
* @param {any} id Promise identifier
* @returns {Promise}
*/
#retrieve(id) {
if (!this.#executors.has(id)) {
throw new Error(`Promise "${id}" doesn't exists`)
}
const promise = this.#executors.get(id)
this.#executors.delete(id)
this.#promises.delete(id)
const { resolve, reject, timeout } = promise
if (timeout !== undefined) {
clearTimeout(timeout)
}
return { resolve, reject }
}
/**
* Returns true if the promise identified by id
* exists in the promise map.
*
* @param {any} id Promise identifier
* @returns {boolean}
*/
has(id) {
return this.#promises.has(id)
}
/**
* Returns the promise identified by the id.
*
* @param {any} id Promise identifier
* @returns {Promise}
*/
get(id) {
return this.#promises.get(id)
}
/**
* Cancels a promise.
*
* @param {any} id Promise identifier
*/
cancel(id) {
this.#retrieve(id)
}
/**
* Resolves a promise by its identifier.
*
* @param {any} id Promise identifier
* @param {any} payload
*/
resolve(id, payload) {
const { resolve } = this.#retrieve(id)
return resolve(payload)
}
/**
* Rejects a promise by its identifier.
*
* @param {any} id Promise identifier
* @param {any} payload
*/
reject(id, payload) {
const { reject } = this.#retrieve(id)
return reject(payload)
}
/**
* Cancels all promises.
*/
cancelAll() {
for (const [id] of this.#executors) {
this.cancel(id)
}
}
/**
* Resolves all promises.
*
* @param {any} payload
*/
resolveAll(payload) {
for (const [id] of this.#executors) {
this.resolve(id, payload)
}
}
/**
* Rejects all promises.
*
* @param {any} payload
*/
rejectAll(payload) {
for (const [id] of this.#executors) {
this.reject(id, payload)
}
}
}
export default PromiseMap