-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: workaround creating many AbortControllers
- Loading branch information
Showing
4 changed files
with
101 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { type EventEmitter } from 'events'; | ||
|
||
import { List, promiseWithResolvers } from '../../utils'; | ||
|
||
export function onData(emitter: EventEmitter, options: { signal: AbortSignal }) { | ||
const signal = options.signal; | ||
signal.throwIfAborted(); | ||
|
||
// Preparing controlling queues and variables | ||
const unconsumedEvents = new List<Buffer>(); | ||
const unconsumedPromises = new List< | ||
Omit<ReturnType<typeof promiseWithResolvers<IteratorResult<Buffer>>>, 'promise'> | ||
>(); | ||
let error: Error | null = null; | ||
let finished = false; | ||
const iterator: AsyncGenerator<Buffer> = { | ||
next() { | ||
// First, we consume all unread events | ||
const value = unconsumedEvents.shift(); | ||
if (value != null) { | ||
return Promise.resolve({ value, done: false }); | ||
} | ||
// Then we error, if an error happened | ||
// This happens one time if at all, because after 'error' | ||
// we stop listening | ||
if (error != null) { | ||
const p = Promise.reject(error); | ||
// Only the first element errors | ||
error = null; | ||
return p; | ||
} | ||
// If the iterator is finished, resolve to done | ||
if (finished) return closeHandler(); | ||
// Wait until an event happens | ||
const { promise, resolve, reject } = promiseWithResolvers<IteratorResult<Buffer>>(); | ||
unconsumedPromises.push({ resolve, reject }); | ||
return promise; | ||
}, | ||
return() { | ||
return closeHandler(); | ||
}, | ||
throw(err: Error) { | ||
errorHandler(err); | ||
return Promise.resolve({ value: undefined, done: true }); | ||
}, | ||
[Symbol.asyncIterator]() { | ||
return this; | ||
} | ||
}; | ||
// Adding event handlers | ||
emitter.on('data', eventHandler); | ||
emitter.on('error', errorHandler); | ||
signal.addEventListener('abort', abortListener, { once: true }); | ||
|
||
return iterator; | ||
|
||
function abortListener() { | ||
errorHandler(signal.reason); | ||
} | ||
|
||
function eventHandler(value: Buffer) { | ||
const promise = unconsumedPromises.shift(); | ||
if (promise != null) promise.resolve({ value, done: false }); | ||
else unconsumedEvents.push(value); | ||
} | ||
|
||
function errorHandler(err: Error) { | ||
const promise = unconsumedPromises.shift(); | ||
if (promise != null) promise.reject(err); | ||
else error = err; | ||
void closeHandler(); | ||
} | ||
|
||
function closeHandler() { | ||
// Adding event handlers | ||
emitter.off('data', eventHandler); | ||
emitter.off('error', errorHandler); | ||
signal.removeEventListener('abort', abortListener); | ||
finished = true; | ||
const doneResult = { value: undefined, done: finished } as const; | ||
|
||
for (const promise of unconsumedPromises) { | ||
promise.resolve(doneResult); | ||
} | ||
|
||
return Promise.resolve(doneResult); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters