mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-14 22:30:55 -05:00
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
|
// @ts-nocheck
|
||
|
/**
|
||
|
* A polyfill for `ReadableStream.protototype[Symbol.asyncIterator]`,
|
||
|
* aligning as closely as possible to the specification.
|
||
|
*
|
||
|
* from https://gist.github.com/MattiasBuelens/496fc1d37adb50a733edd43853f2f60e
|
||
|
*
|
||
|
* @see https://streams.spec.whatwg.org/#rs-asynciterator
|
||
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#async_iteration
|
||
|
*/
|
||
|
|
||
|
ReadableStream.prototype.values ??= function ({ preventCancel = false } = {}) {
|
||
|
const reader = this.getReader()
|
||
|
return {
|
||
|
async next() {
|
||
|
try {
|
||
|
const result = await reader.read()
|
||
|
if (result.done) {
|
||
|
reader.releaseLock()
|
||
|
}
|
||
|
return result
|
||
|
} catch (e) {
|
||
|
reader.releaseLock()
|
||
|
throw e
|
||
|
}
|
||
|
},
|
||
|
async return(value) {
|
||
|
if (!preventCancel) {
|
||
|
const cancelPromise = reader.cancel(value)
|
||
|
reader.releaseLock()
|
||
|
await cancelPromise
|
||
|
} else {
|
||
|
reader.releaseLock()
|
||
|
}
|
||
|
return { done: true, value }
|
||
|
},
|
||
|
[Symbol.asyncIterator]() {
|
||
|
return this
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ReadableStream.prototype[Symbol.asyncIterator] ??=
|
||
|
ReadableStream.prototype.values
|