Skip to content

Commit

Permalink
Added usage of reflectStatus in findInternal
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-van committed Dec 28, 2023
1 parent 1fcff3d commit 666d774
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
34 changes: 15 additions & 19 deletions src/findInternal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import assert from 'nanoassert'
import asyncWrap from './asyncWrap.mjs'
import asyncIterableWrap from './asyncIterableWrap.mjs'
import getQueue from './getQueue.mjs'
import reflectStatus from './reflectStatus.mjs'

/**
* @ignore
Expand Down Expand Up @@ -39,11 +40,7 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
const identifier = waitListIndex
waitListIndex += 1
const p = (async () => {
try {
return [identifier, 'resolved', await fct()]
} catch (e) {
return [identifier, 'rejected', e]
}
return [identifier, await reflectStatus(fct)]
})()
assert(!waitList.has(identifier), 'waitList already contains identifier')
waitList.set(identifier, p)
Expand Down Expand Up @@ -75,12 +72,11 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
const removed = scheduledList.delete(index)
assert(removed, 'Couldn\'t find index in scheduledList for removal')

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

scheduledCount -= 1
insertInResults(index, value, state, result)
if (state === 'rejected' || (state === 'resolved' && result)) {
insertInResults(index, value, snapshot)
if (snapshot.status === 'rejected' || (snapshot.status === 'fulfilled' && snapshot.value)) {
shouldStop = true
cancelAllScheduled(ordered ? index : 0)
}
Expand All @@ -107,10 +103,10 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
const fetch = () => {
fetching = true
addToWaitList(async () => {
const [state, result] = await it.next().then((r) => ['resolved', r], (e) => ['rejected', e])
const snapshot = await reflectStatus(() => it.next())
fetching = false
if (state === 'resolved') {
const { value, done } = result
if (snapshot.status === 'fulfilled') {
const { value, done } = snapshot.value
if (!done) {
lastIndexFetched += 1
assert(fetchedValue === null, 'fetchedValue should be null')
Expand All @@ -124,19 +120,19 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
exhausted = true
lastIndexFetched += 1
const index = lastIndexFetched
insertInResults(index, undefined, state, result)
insertInResults(index, undefined, snapshot)
cancelAllScheduled(ordered ? index : 0)
}
})
}

const insertInResults = (index, value, state, result) => {
const insertInResults = (index, value, snapshot) => {
if (ordered) {
assert(index - lastIndexHandled - 1 >= 0, 'invalid index to insert')
assert(results[index - lastIndexHandled - 1] === undefined, 'already inserted result')
results[index - lastIndexHandled - 1] = { index, value, state, result }
results[index - lastIndexHandled - 1] = { index, value, snapshot }
} else {
results.push({ index, value, state, result })
results.push({ index, value, snapshot })
}
}

Expand All @@ -146,9 +142,9 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
while (results.length >= 1 && results[0] !== undefined) {
const result = results.shift()
lastIndexHandled += 1
if (result.state === 'rejected') {
throw result.result
} else if (result.result) {
if (result.snapshot.status === 'rejected') {
throw result.snapshot.reason
} else if (result.snapshot.value) {
return [result.index, result.value]
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/mapGenerator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ async function * mapGenerator (iterable, iteratee, queueOrConcurrency = 1, order
const identifier = waitListIndex
waitListIndex += 1
const p = (async () => {
const snapshot = await reflectStatus(fct)
return [identifier, snapshot]
return [identifier, await reflectStatus(fct)]
})()
assert(!waitList.has(identifier), 'waitList contains identifier')
waitList.set(identifier, p)
Expand Down

0 comments on commit 666d774

Please sign in to comment.