Skip to content

Commit

Permalink
Fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-van committed Dec 28, 2023
1 parent a1f3de4 commit 1fcff3d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/findInternal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
let lastIndexHandled = -1
const results = []

let waitListIndex = 0
const waitList = new Map()
const addToWaitList = (identifier, fct) => {
const addToWaitList = (fct) => {
const identifier = waitListIndex
waitListIndex += 1
const p = (async () => {
try {
return [identifier, 'resolved', await fct()]
} catch (e) {
return [identifier, 'rejected', e]
}
})()
assert(!waitList.has('identifier'), 'waitList already contains identifier')
assert(!waitList.has(identifier), 'waitList already contains identifier')
waitList.set(identifier, p)
}
const raceWaitList = async () => {
Expand All @@ -63,7 +66,7 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
state: null
}
scheduledList.set(index, task)
addToWaitList(task.index, async () => {
addToWaitList(async () => {
const p = queue.exec(async () => {
if (task.state === 'cancelled') {
throw new CustomCancelledError()
Expand Down Expand Up @@ -103,7 +106,7 @@ async function findInternal (iterable, iteratee, queueOrConcurrency, ordered) {
}
const fetch = () => {
fetching = true
addToWaitList('next', async () => {
addToWaitList(async () => {
const [state, result] = await it.next().then((r) => ['resolved', r], (e) => ['rejected', e])
fetching = false
if (state === 'resolved') {
Expand Down
11 changes: 7 additions & 4 deletions src/mapGenerator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,16 @@ async function * mapGenerator (iterable, iteratee, queueOrConcurrency = 1, order
let lastIndexHandled = -1
const results = []

let waitListIndex = 0
const waitList = new Map()
const addToWaitList = (identifier, fct) => {
const addToWaitList = (fct) => {
const identifier = waitListIndex
waitListIndex += 1
const p = (async () => {
const snapshot = await reflectStatus(fct)
return [identifier, snapshot]
})()
assert(!waitList.has(identifier), 'waitList already contains identifier')
assert(!waitList.has(identifier), 'waitList contains identifier')
waitList.set(identifier, p)
}
const raceWaitList = async () => {
Expand All @@ -99,7 +102,7 @@ async function * mapGenerator (iterable, iteratee, queueOrConcurrency = 1, order
state: null
}
scheduledList.set(index, task)
addToWaitList(task.index, async () => {
addToWaitList(async () => {
const p = queue.exec(async () => {
if (task.state === 'cancelled') {
throw new CustomCancelledError()
Expand Down Expand Up @@ -138,7 +141,7 @@ async function * mapGenerator (iterable, iteratee, queueOrConcurrency = 1, order
}
const fetch = () => {
fetching = true
addToWaitList('next', async () => {
addToWaitList(async () => {
const snapshot = await reflectStatus(() => it.next())
fetching = false
if (snapshot.status === 'fulfilled') {
Expand Down

0 comments on commit 1fcff3d

Please sign in to comment.