Skip to content

Commit 0aa588f

Browse files
committed
Renamed refectStatus + added its usage in mapGenerator + corrected
sleepCancellable test
1 parent 6f50f1c commit 0aa588f

File tree

5 files changed

+25
-48
lines changed

5 files changed

+25
-48
lines changed

src/mapGenerator.mjs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import asyncWrap from './asyncWrap.mjs'
44
import asyncIterableWrap from './asyncIterableWrap.mjs'
55
import getQueue from './getQueue.mjs'
66
import Queue from './Queue.mjs'
7+
import reflectStatus from './reflectStatus.mjs'
78

89
/**
910
* Produces a an async iterator that will return each value or `iterable` after having processed them through
@@ -74,11 +75,7 @@ async function * mapGenerator (iterable, iteratee, queueOrConcurrency = 1, order
7475
const waitList = new Map()
7576
const addToWaitList = (identifier, fct) => {
7677
const p = (async () => {
77-
try {
78-
return [identifier, 'resolved', await fct()]
79-
} catch (e) {
80-
return [identifier, 'rejected', e]
81-
}
78+
return [identifier, await reflectStatus(fct)]
8279
})()
8380
assert(!waitList.has('identifier'), 'waitList already contains identifier')
8481
waitList.set(identifier, p)
@@ -110,12 +107,11 @@ async function * mapGenerator (iterable, iteratee, queueOrConcurrency = 1, order
110107
const removed = scheduledList.delete(index)
111108
assert(removed, 'Couldn\'t find index in scheduledList for removal')
112109

113-
const [state, result] = await iteratee(value, index, iterable)
114-
.then((r) => ['resolved', r], (e) => ['rejected', e])
110+
const snapshot = await reflectStatus(() => iteratee(value, index, iterable))
115111

116112
scheduledCount -= 1
117-
insertInResults(index, value, state, result)
118-
if (state === 'rejected') {
113+
insertInResults(index, value, snapshot)
114+
if (snapshot.status === 'rejected') {
119115
shouldStop = true
120116
cancelAllScheduled(ordered ? index : 0)
121117
}
@@ -142,10 +138,10 @@ async function * mapGenerator (iterable, iteratee, queueOrConcurrency = 1, order
142138
const fetch = () => {
143139
fetching = true
144140
addToWaitList('next', async () => {
145-
const [state, result] = await it.next().then((r) => ['resolved', r], (e) => ['rejected', e])
141+
const snapshot = await reflectStatus(() => it.next())
146142
fetching = false
147-
if (state === 'resolved') {
148-
const { value, done } = result
143+
if (snapshot.status === 'fulfilled') {
144+
const { value, done } = snapshot.value
149145
if (!done) {
150146
lastIndexFetched += 1
151147
assert(fetchedValue === null, 'fetchedValue should be null')
@@ -159,19 +155,19 @@ async function * mapGenerator (iterable, iteratee, queueOrConcurrency = 1, order
159155
exhausted = true
160156
lastIndexFetched += 1
161157
const index = lastIndexFetched
162-
insertInResults(index, undefined, state, result)
158+
insertInResults(index, undefined, snapshot)
163159
cancelAllScheduled(ordered ? index : 0)
164160
}
165161
})
166162
}
167163

168-
const insertInResults = (index, value, state, result) => {
164+
const insertInResults = (index, value, snapshot) => {
169165
if (ordered) {
170166
assert(index - lastIndexHandled - 1 >= 0, 'invalid index to insert')
171167
assert(results[index - lastIndexHandled - 1] === undefined, 'already inserted result')
172-
results[index - lastIndexHandled - 1] = { index, value, state, result }
168+
results[index - lastIndexHandled - 1] = { index, value, snapshot }
173169
} else {
174-
results.push({ index, value, state, result })
170+
results.push({ index, value, snapshot })
175171
}
176172
}
177173

@@ -181,10 +177,10 @@ async function * mapGenerator (iterable, iteratee, queueOrConcurrency = 1, order
181177
while (results.length >= 1 && results[0] !== undefined) {
182178
const result = results.shift()
183179
lastIndexHandled += 1
184-
if (result.state === 'rejected') {
185-
throw result.result
180+
if (result.snapshot.status === 'rejected') {
181+
throw result.snapshot.reason
186182
} else {
187-
yield result.result
183+
yield result.snapshot.value
188184
}
189185
}
190186
if (exhausted && lastIndexFetched === lastIndexHandled) {

src/modern-async.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ export { default as timeout } from './timeout.mjs'
4343
export { default as TimeoutError } from './TimeoutError.mjs'
4444
export { default as timeoutPrecise } from './timeoutPrecise.mjs'
4545
export { default as toArray } from './toArray.mjs'
46-
export { default as snapshot } from './snapshot.mjs'
46+
export { default as reflectStatus } from './reflectStatus.mjs'

src/snapshot.mjs renamed to src/reflectStatus.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
* @returns {Promise<any>} A promise that will always be resolved with an object containing
2323
* a snapshot of the original promise state.
2424
* @example
25-
* import { snapshot, map, sleep } from 'modern-async'
25+
* import { reflectStatus, map, sleep } from 'modern-async'
2626
*
2727
* const array = [1, 2, 3]
2828
*
29-
* const result = await map(array, (v) => snapshot(async () => {
29+
* const result = await map(array, (v) => reflectStatus(async () => {
3030
* await sleep(10) // waits 10ms
3131
* if (v % 2 === 0) { // throws error on some values
3232
* throw Error("error")
@@ -42,7 +42,7 @@
4242
* // { status: 'fulfilled', value: 3 }
4343
* // ]
4444
*/
45-
async function snapshot (fct) {
45+
async function reflectStatus (fct) {
4646
try {
4747
const res = await fct()
4848
return {
@@ -57,4 +57,4 @@ async function snapshot (fct) {
5757
}
5858
}
5959

60-
export default snapshot
60+
export default reflectStatus

src/snapshot.test.mjs renamed to src/reflectStatus.test.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11

22
import { expect, test } from '@jest/globals'
3-
import snapshot from './snapshot.mjs'
3+
import reflectStatus from './reflectStatus.mjs'
44
import delay from './delay.mjs'
55

6-
test('snapshot base test', async () => {
7-
const res1 = await snapshot(async () => {
6+
test('reflectStatus base test', async () => {
7+
const res1 = await reflectStatus(async () => {
88
await delay()
99
return 3
1010
})
@@ -15,8 +15,8 @@ test('snapshot base test', async () => {
1515
expect(res1.reason).toBeUndefined()
1616
})
1717

18-
test('snapshot falure', async () => {
19-
const res1 = await snapshot(async () => {
18+
test('reflectStatus falure', async () => {
19+
const res1 = await reflectStatus(async () => {
2020
await delay()
2121
throw new Error('error')
2222
})

src/sleepCancellable.test.mjs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,12 @@ test('sleepCancellable base', async () => {
1212
})
1313

1414
test('sleepCancellable cancel', async () => {
15-
const start = new Date().getTime()
1615
const [p, cancel] = sleepCancellable(100)
1716
expect(cancel()).toBe(true)
1817
try {
1918
await p
2019
expect(true).toBe(false)
2120
} catch (e) {
22-
const end = new Date().getTime()
23-
expect(end - start).toBeLessThan(50)
24-
expect(e).toBeInstanceOf(CancelledError)
25-
}
26-
})
27-
28-
test('sleepCancellable async cancel', async () => {
29-
const start = new Date().getTime()
30-
const [p, cancel] = sleepCancellable(100)
31-
setTimeout(() => {
32-
expect(cancel()).toBe(true)
33-
}, 10)
34-
try {
35-
await p
36-
expect(true).toBe(false)
37-
} catch (e) {
38-
const end = new Date().getTime()
39-
expect(end - start).toBeLessThan(50)
4021
expect(e).toBeInstanceOf(CancelledError)
4122
}
4223
})

0 commit comments

Comments
 (0)