p-transform
Advanced tools
Comparing version 4.0.1 to 4.0.2
@@ -1,15 +0,4 @@ | ||
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
if (kind === "m") throw new TypeError("Private method is not writable"); | ||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); | ||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); | ||
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; | ||
}; | ||
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); | ||
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); | ||
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); | ||
}; | ||
var _OutOfOrder_instances, _OutOfOrder_queue, _OutOfOrder_closed, _OutOfOrder_nextPromise, _OutOfOrder_resolve, _OutOfOrder_results, _OutOfOrder_transform, _OutOfOrder_add; | ||
import { Readable, Duplex } from 'node:stream'; | ||
import PQueue from 'p-queue'; | ||
// eslint-disable-next-line @typescript-eslint/promise-function-async | ||
const createPromise = () => { | ||
@@ -22,2 +11,3 @@ let resolve; | ||
}); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
return Object.assign(promise, { resolve, reject }); | ||
@@ -29,27 +19,27 @@ }; | ||
export class OutOfOrder { | ||
#queue; | ||
#closed = false; | ||
#nextPromise; | ||
#resolve; | ||
#results = []; | ||
#transform; | ||
constructor(transform, pqueueOptions) { | ||
_OutOfOrder_instances.add(this); | ||
_OutOfOrder_queue.set(this, void 0); | ||
_OutOfOrder_closed.set(this, false); | ||
_OutOfOrder_nextPromise.set(this, void 0); | ||
_OutOfOrder_resolve.set(this, void 0); | ||
_OutOfOrder_results.set(this, []); | ||
_OutOfOrder_transform.set(this, void 0); | ||
__classPrivateFieldSet(this, _OutOfOrder_queue, new PQueue(pqueueOptions), "f"); | ||
__classPrivateFieldSet(this, _OutOfOrder_resolve, createPromise(), "f"); | ||
__classPrivateFieldSet(this, _OutOfOrder_nextPromise, createPromise(), "f"); | ||
__classPrivateFieldSet(this, _OutOfOrder_transform, transform, "f"); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
this.#queue = new PQueue(pqueueOptions); | ||
this.#resolve = createPromise(); | ||
this.#nextPromise = createPromise(); | ||
this.#transform = transform; | ||
} | ||
async *[(_OutOfOrder_queue = new WeakMap(), _OutOfOrder_closed = new WeakMap(), _OutOfOrder_nextPromise = new WeakMap(), _OutOfOrder_resolve = new WeakMap(), _OutOfOrder_results = new WeakMap(), _OutOfOrder_transform = new WeakMap(), _OutOfOrder_instances = new WeakSet(), Symbol.asyncIterator)]() { | ||
while (!__classPrivateFieldGet(this, _OutOfOrder_closed, "f")) { | ||
async *[Symbol.asyncIterator]() { | ||
while (!this.#closed || this.#queue.size > 0 || this.#queue.pending > 0) { | ||
// eslint-disable-next-line no-await-in-loop | ||
await __classPrivateFieldGet(this, _OutOfOrder_nextPromise, "f"); | ||
const results = __classPrivateFieldGet(this, _OutOfOrder_results, "f"); | ||
__classPrivateFieldSet(this, _OutOfOrder_results, [], "f"); | ||
await this.#nextPromise; | ||
const results = this.#results; | ||
this.#results = []; | ||
for (const result of results) { | ||
yield result; | ||
} | ||
__classPrivateFieldSet(this, _OutOfOrder_nextPromise, createPromise(), "f"); | ||
this.#nextPromise = createPromise(); | ||
} | ||
__classPrivateFieldGet(this, _OutOfOrder_resolve, "f").resolve(); | ||
this.#resolve.resolve(); | ||
} | ||
@@ -62,4 +52,27 @@ push(chunk) { | ||
}; | ||
__classPrivateFieldGet(this, _OutOfOrder_instances, "m", _OutOfOrder_add).call(this, async () => __classPrivateFieldGet(this, _OutOfOrder_transform, "f").call(transformContext, chunk)); | ||
this.#add(async () => this.#transform.call(transformContext, chunk)); | ||
} | ||
/** | ||
* Queue the transform method. | ||
* Result is queued to be emitted. | ||
* Additional chunks can be added through `this.push` method. | ||
*/ | ||
#add(fn, options) { | ||
/* c8 ignore next 3 */ | ||
if (this.#closed) { | ||
throw new Error('Queue is already closed'); | ||
} | ||
this.#queue | ||
.add(async () => { | ||
const result = await fn(); | ||
if (result !== undefined && result !== null) { | ||
this.pushResult(result); | ||
} | ||
}, options) | ||
.then(() => { | ||
this.#nextPromise.resolve(); | ||
}, error => { | ||
this.#nextPromise.reject(error); | ||
}); | ||
} | ||
duplex() { | ||
@@ -81,35 +94,17 @@ return Duplex.from({ | ||
/* c8 ignore next 3 */ | ||
if (__classPrivateFieldGet(this, _OutOfOrder_closed, "f")) { | ||
if (this.#closed) { | ||
throw new Error('Queue is already closed'); | ||
} | ||
__classPrivateFieldGet(this, _OutOfOrder_results, "f").push(chunk); | ||
this.#results.push(chunk); | ||
} | ||
async close() { | ||
await this.flush(); | ||
__classPrivateFieldSet(this, _OutOfOrder_closed, true, "f"); | ||
__classPrivateFieldGet(this, _OutOfOrder_nextPromise, "f").resolve(); | ||
await __classPrivateFieldGet(this, _OutOfOrder_resolve, "f"); | ||
this.#closed = true; | ||
this.#nextPromise.resolve(); | ||
await this.#resolve; | ||
} | ||
async flush() { | ||
await __classPrivateFieldGet(this, _OutOfOrder_queue, "f").onIdle(); | ||
__classPrivateFieldGet(this, _OutOfOrder_nextPromise, "f").resolve(); | ||
await this.#queue.onIdle(); | ||
this.#nextPromise.resolve(); | ||
} | ||
} | ||
_OutOfOrder_add = function _OutOfOrder_add(fn, options) { | ||
/* c8 ignore next 3 */ | ||
if (__classPrivateFieldGet(this, _OutOfOrder_closed, "f")) { | ||
throw new Error('Queue is already closed'); | ||
} | ||
__classPrivateFieldGet(this, _OutOfOrder_queue, "f") | ||
.add(async () => { | ||
const result = await fn(); | ||
if (result !== undefined && result !== null) { | ||
this.pushResult(result); | ||
} | ||
}, options) | ||
.then(() => { | ||
__classPrivateFieldGet(this, _OutOfOrder_nextPromise, "f").resolve(); | ||
}, error => { | ||
__classPrivateFieldGet(this, _OutOfOrder_nextPromise, "f").reject(error); | ||
}); | ||
}; |
{ | ||
"name": "p-transform", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"bugs": "https://github.com/mshima/p-transform/issues", | ||
@@ -42,3 +42,3 @@ "repository": { | ||
"vitest": "^0.31.2", | ||
"xo": "^0.53.1" | ||
"xo": "^0.54.2" | ||
}, | ||
@@ -45,0 +45,0 @@ "engines": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22165
7
167