Update checked-in dependencies
This commit is contained in:
parent
7fdbca3ba3
commit
357e0ceaa9
360 changed files with 25673 additions and 917 deletions
191
node_modules/@open-draft/deferred-promise/README.md
generated
vendored
Normal file
191
node_modules/@open-draft/deferred-promise/README.md
generated
vendored
Normal file
|
|
@ -0,0 +1,191 @@
|
|||
# Deferred Promise
|
||||
|
||||
The `DeferredPromise` class is a Promise-compatible abstraction that defers resolving/rejecting promises to another closure. This class is primarily useful when one part of your system establishes as promise but another part of your system fulfills it.
|
||||
|
||||
> This class is conceptually inspired by the [`createDeferredPromise()`](https://github.com/nodejs/node/blob/696fd4b14fc34cc2d01497a3abd9bb441b89be50/lib/internal/util.js#L468-L477) internal utility in Node.js. Unlike the Node.js implementation, however, `DeferredProimse` _extends_ a native `Promise`, allowing the consumer to handle deferred promises like regular promises (no `.promise` instance nesting).
|
||||
|
||||
## Getting started
|
||||
|
||||
```sh
|
||||
npm install @open-draft/deferred-promise
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
- [**`createDeferredExecutor()`**](#createdeferredexecutor)
|
||||
- [`DeferredExecutor.state`](#deferredexecutorstate)
|
||||
- [`DeferredExecutor.resolve()`](#deferredexecutorresolve)
|
||||
- [`DeferredExecutor.reject()`](#deferredexecutorreject)
|
||||
- [`DeferredExecutor.rejectionReason`](#deferredexecutorrejectionreason)
|
||||
- [**Class: `DeferredPromise`**](#class-deferredpromise)
|
||||
- [`new DeferredPromise()`](#new-defferedpromise)
|
||||
- [`deferredPromise.state`](#deferredpromisestate)
|
||||
- [`deferredPromise.resolve()`](#deferredpromiseresolve)
|
||||
- [`deferredPromise.reject()`](#deferredpromisereject)
|
||||
- [`deferredPromise.rejectionReason`](#deferredpromiserejectionreason)
|
||||
|
||||
---
|
||||
|
||||
## `createDeferredExecutor()`
|
||||
|
||||
Creates a Promise executor function that delegates its resolution to the current scope.
|
||||
|
||||
```js
|
||||
import { createDeferredExecutor } from '@open-draft/deferred-promise'
|
||||
|
||||
const executor = createDeferredExecutor()
|
||||
const promise = new Promise(executor)
|
||||
|
||||
executor.resolve('hello')
|
||||
// executor.reject(new Error('Reason'))
|
||||
```
|
||||
|
||||
Deferred executor allows you to control any promise remotely and doesn't affect the Promise instance in any way. Similar to the [`DeferredPromise`](#class-deferredpromise) instance, the deferred executor exposes additional promise properties like `state`, `rejectionReason`, `resolve`, and `reject`. In fact, the `DeferredPromise` class is implemented on top of the deferred executor.
|
||||
|
||||
```js
|
||||
const executor = createDeferredExecutor()
|
||||
const promise = new Promise(executor)
|
||||
|
||||
executor.reject('reason')
|
||||
|
||||
nextTick(() => {
|
||||
console.log(executor.rejectionReason) // "reason"
|
||||
})
|
||||
```
|
||||
|
||||
### `DeferredExecutor.state`
|
||||
|
||||
- `<"pending" | "fulfilled" | "rejected">` **Default:** `"pending"`
|
||||
|
||||
```js
|
||||
const executor = createDeferredExecutor()
|
||||
const promise = new Promise(executor)
|
||||
|
||||
console.log(executor.state) // "pending"
|
||||
```
|
||||
|
||||
Calling [`resolve()`](#deferredexecutorresolve) and [`reject()`](#deferredexecutorreject) methods of the executor transitions the state to "fulfilled" and "rejected" respectively.
|
||||
|
||||
### `DeferredExecutor.resolve()`
|
||||
|
||||
Resolves the promise with a given value.
|
||||
|
||||
```js
|
||||
const executor = createDeferredExecutor()
|
||||
const promise = new Promise(executor)
|
||||
|
||||
console.log(executor.state) // "pending"
|
||||
|
||||
executor.resolve()
|
||||
|
||||
// The promise state is still "pending"
|
||||
// because promises are settled in the next microtask.
|
||||
console.log(executor.state) // "pending"
|
||||
|
||||
nextTick(() => {
|
||||
// In the next microtask, the promise's state is resolved.
|
||||
console.log(executor.state) // "fulfilled"
|
||||
})
|
||||
```
|
||||
|
||||
### `DeferredExecutor.reject()`
|
||||
|
||||
Rejects the promise with a given reason.
|
||||
|
||||
```js
|
||||
const executor = createDeferredExecutor()
|
||||
const promise = new Promise(executor)
|
||||
|
||||
executor.reject(new Error('Failed to fetch'))
|
||||
|
||||
nextTick(() => {
|
||||
console.log(executor.state) // "rejected"
|
||||
console.log(executor.rejectionReason) // Error("Failed to fetch")
|
||||
})
|
||||
```
|
||||
|
||||
You can access the rejection reason of the promise at any time by the [`rejectionReason`](#deferredexecutorrejectionreason) property of the deferred executor.
|
||||
|
||||
### `DeferredExecutor.rejectionReason`
|
||||
|
||||
Returns the reason of the promise rejection. If no reason has been provided to the `reject()` call, `undefined` is returned instead.
|
||||
|
||||
```js
|
||||
const executor = createDeferredExecutor()
|
||||
const promise = new Promise(executor)
|
||||
|
||||
promise.reject(new Error('Internal Server Error'))
|
||||
|
||||
nextTick(() => {
|
||||
console.log(promise.rejectionReason) // Error("Internal Server Error")
|
||||
})
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Class: `DeferredPromise`
|
||||
|
||||
### `new DefferedPromise()`
|
||||
|
||||
Creates a new instance of a deferred promise.
|
||||
|
||||
```js
|
||||
import { DeferredPromise } from '@open-draft/deferred-promise'
|
||||
|
||||
const promise = new DeferredPromise()
|
||||
```
|
||||
|
||||
A deferred promise is a Promise-compatible class that constructs a regular Promise instance under the hood, controlling it via the [deferred executor](#createdeferredexecutor).
|
||||
|
||||
A deferred promise is fully compatible with the regular Promise, both type- and runtime-wise, e.g. a deferred promise can be chained and awaited normally.
|
||||
|
||||
```js
|
||||
const promise = new DefferredPromise()
|
||||
.then((value) => value.toUpperCase())
|
||||
.then((value) => value.substring(0, 2))
|
||||
.catch((error) => console.error(error))
|
||||
|
||||
await promise
|
||||
```
|
||||
|
||||
Unlike the regular Promise, however, a deferred promise doesn't accept the `executor` function as the constructor argument. Instead, the resolution of the deferred promise is deferred to the current scope (thus the name).
|
||||
|
||||
```js
|
||||
function getPort() {
|
||||
// Notice that you don't provide any executor function
|
||||
// when constructing a deferred promise.
|
||||
const portPromise = new DeferredPromise()
|
||||
|
||||
port.on('open', (port) => {
|
||||
// Resolve the deferred promise whenever necessary.
|
||||
portPromise.resolve(port)
|
||||
})
|
||||
|
||||
// Return the deferred promise immediately.
|
||||
return portPromise
|
||||
}
|
||||
```
|
||||
|
||||
Use the [`resolve()`](#deferredpromiseresolve) and [`reject()`](#deferredpromisereject) methods of the deferred promise instance to resolve and reject that promise respectively.
|
||||
|
||||
### `deferredPromise.state`
|
||||
|
||||
See [`DeferredExecutor.state`](#deferredexecutorstate)
|
||||
|
||||
### `deferredPromise.resolve()`
|
||||
|
||||
See [`DeferredExecutor.resolve()`](#deferredexecutorresolve)
|
||||
|
||||
### `deferredPromise.reject()`
|
||||
|
||||
See [`DeferredExecutor.reject()`](#deferredexecutorreject)
|
||||
|
||||
### `deferredPromise.rejectionReason`
|
||||
|
||||
See [`DeferredExecutor.rejectionReason`](#deferredexecutorrejectionreason)
|
||||
|
||||
---
|
||||
|
||||
## Mentions
|
||||
|
||||
- [Jonas Kuske](https://github.com/jonaskuske) for the phenomenal work around improving Promise-compliance.
|
||||
27
node_modules/@open-draft/deferred-promise/build/index.d.mts
generated
vendored
Normal file
27
node_modules/@open-draft/deferred-promise/build/index.d.mts
generated
vendored
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
declare type PromiseState = 'pending' | 'fulfilled' | 'rejected';
|
||||
declare type Executor<Value> = ConstructorParameters<typeof Promise<Value>>[0];
|
||||
declare type ResolveFunction<Value> = Parameters<Executor<Value>>[0];
|
||||
declare type RejectFunction<Reason> = Parameters<Executor<Reason>>[1];
|
||||
declare type DeferredPromiseExecutor<Input = never, Output = Input> = {
|
||||
(resolve?: ResolveFunction<Input>, reject?: RejectFunction<any>): void;
|
||||
resolve: ResolveFunction<Input>;
|
||||
reject: RejectFunction<any>;
|
||||
result?: Output;
|
||||
state: PromiseState;
|
||||
rejectionReason?: unknown;
|
||||
};
|
||||
declare function createDeferredExecutor<Input = never, Output = Input>(): DeferredPromiseExecutor<Input, Output>;
|
||||
|
||||
declare class DeferredPromise<Input, Output = Input> extends Promise<Input> {
|
||||
#private;
|
||||
resolve: ResolveFunction<Output>;
|
||||
reject: RejectFunction<Output>;
|
||||
constructor(executor?: Executor<Input> | null);
|
||||
get state(): PromiseState;
|
||||
get rejectionReason(): unknown;
|
||||
then<ThenResult = Input, CatchResult = never>(onFulfilled?: (value: Input) => ThenResult | PromiseLike<ThenResult>, onRejected?: (reason: any) => CatchResult | PromiseLike<CatchResult>): DeferredPromise<ThenResult | CatchResult, Output>;
|
||||
catch<CatchResult = never>(onRejected?: (reason: any) => CatchResult | PromiseLike<CatchResult>): DeferredPromise<Input | CatchResult, Output>;
|
||||
finally(onfinally?: () => void | Promise<any>): DeferredPromise<Input, Output>;
|
||||
}
|
||||
|
||||
export { DeferredPromise, DeferredPromiseExecutor, Executor, PromiseState, RejectFunction, ResolveFunction, createDeferredExecutor };
|
||||
27
node_modules/@open-draft/deferred-promise/build/index.d.ts
generated
vendored
Normal file
27
node_modules/@open-draft/deferred-promise/build/index.d.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
declare type PromiseState = 'pending' | 'fulfilled' | 'rejected';
|
||||
declare type Executor<Value> = ConstructorParameters<typeof Promise<Value>>[0];
|
||||
declare type ResolveFunction<Value> = Parameters<Executor<Value>>[0];
|
||||
declare type RejectFunction<Reason> = Parameters<Executor<Reason>>[1];
|
||||
declare type DeferredPromiseExecutor<Input = never, Output = Input> = {
|
||||
(resolve?: ResolveFunction<Input>, reject?: RejectFunction<any>): void;
|
||||
resolve: ResolveFunction<Input>;
|
||||
reject: RejectFunction<any>;
|
||||
result?: Output;
|
||||
state: PromiseState;
|
||||
rejectionReason?: unknown;
|
||||
};
|
||||
declare function createDeferredExecutor<Input = never, Output = Input>(): DeferredPromiseExecutor<Input, Output>;
|
||||
|
||||
declare class DeferredPromise<Input, Output = Input> extends Promise<Input> {
|
||||
#private;
|
||||
resolve: ResolveFunction<Output>;
|
||||
reject: RejectFunction<Output>;
|
||||
constructor(executor?: Executor<Input> | null);
|
||||
get state(): PromiseState;
|
||||
get rejectionReason(): unknown;
|
||||
then<ThenResult = Input, CatchResult = never>(onFulfilled?: (value: Input) => ThenResult | PromiseLike<ThenResult>, onRejected?: (reason: any) => CatchResult | PromiseLike<CatchResult>): DeferredPromise<ThenResult | CatchResult, Output>;
|
||||
catch<CatchResult = never>(onRejected?: (reason: any) => CatchResult | PromiseLike<CatchResult>): DeferredPromise<Input | CatchResult, Output>;
|
||||
finally(onfinally?: () => void | Promise<any>): DeferredPromise<Input, Output>;
|
||||
}
|
||||
|
||||
export { DeferredPromise, DeferredPromiseExecutor, Executor, PromiseState, RejectFunction, ResolveFunction, createDeferredExecutor };
|
||||
99
node_modules/@open-draft/deferred-promise/build/index.js
generated
vendored
Normal file
99
node_modules/@open-draft/deferred-promise/build/index.js
generated
vendored
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||
var __export = (target, all) => {
|
||||
for (var name in all)
|
||||
__defProp(target, name, { get: all[name], enumerable: true });
|
||||
};
|
||||
var __copyProps = (to, from, except, desc) => {
|
||||
if (from && typeof from === "object" || typeof from === "function") {
|
||||
for (let key of __getOwnPropNames(from))
|
||||
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||
}
|
||||
return to;
|
||||
};
|
||||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||
|
||||
// src/index.ts
|
||||
var src_exports = {};
|
||||
__export(src_exports, {
|
||||
DeferredPromise: () => DeferredPromise,
|
||||
createDeferredExecutor: () => createDeferredExecutor
|
||||
});
|
||||
module.exports = __toCommonJS(src_exports);
|
||||
|
||||
// src/createDeferredExecutor.ts
|
||||
function createDeferredExecutor() {
|
||||
const executor = (resolve, reject) => {
|
||||
executor.state = "pending";
|
||||
executor.resolve = (data) => {
|
||||
if (executor.state !== "pending") {
|
||||
return;
|
||||
}
|
||||
executor.result = data;
|
||||
const onFulfilled = (value) => {
|
||||
executor.state = "fulfilled";
|
||||
return value;
|
||||
};
|
||||
return resolve(
|
||||
data instanceof Promise ? data : Promise.resolve(data).then(onFulfilled)
|
||||
);
|
||||
};
|
||||
executor.reject = (reason) => {
|
||||
if (executor.state !== "pending") {
|
||||
return;
|
||||
}
|
||||
queueMicrotask(() => {
|
||||
executor.state = "rejected";
|
||||
});
|
||||
return reject(executor.rejectionReason = reason);
|
||||
};
|
||||
};
|
||||
return executor;
|
||||
}
|
||||
|
||||
// src/DeferredPromise.ts
|
||||
var DeferredPromise = class extends Promise {
|
||||
#executor;
|
||||
resolve;
|
||||
reject;
|
||||
constructor(executor = null) {
|
||||
const deferredExecutor = createDeferredExecutor();
|
||||
super((originalResolve, originalReject) => {
|
||||
deferredExecutor(originalResolve, originalReject);
|
||||
executor?.(deferredExecutor.resolve, deferredExecutor.reject);
|
||||
});
|
||||
this.#executor = deferredExecutor;
|
||||
this.resolve = this.#executor.resolve;
|
||||
this.reject = this.#executor.reject;
|
||||
}
|
||||
get state() {
|
||||
return this.#executor.state;
|
||||
}
|
||||
get rejectionReason() {
|
||||
return this.#executor.rejectionReason;
|
||||
}
|
||||
then(onFulfilled, onRejected) {
|
||||
return this.#decorate(super.then(onFulfilled, onRejected));
|
||||
}
|
||||
catch(onRejected) {
|
||||
return this.#decorate(super.catch(onRejected));
|
||||
}
|
||||
finally(onfinally) {
|
||||
return this.#decorate(super.finally(onfinally));
|
||||
}
|
||||
#decorate(promise) {
|
||||
return Object.defineProperties(promise, {
|
||||
resolve: { configurable: true, value: this.resolve },
|
||||
reject: { configurable: true, value: this.reject }
|
||||
});
|
||||
}
|
||||
};
|
||||
// Annotate the CommonJS export names for ESM import in node:
|
||||
0 && (module.exports = {
|
||||
DeferredPromise,
|
||||
createDeferredExecutor
|
||||
});
|
||||
//# sourceMappingURL=index.js.map
|
||||
1
node_modules/@open-draft/deferred-promise/build/index.js.map
generated
vendored
Normal file
1
node_modules/@open-draft/deferred-promise/build/index.js.map
generated
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"sources":["../src/index.ts","../src/createDeferredExecutor.ts","../src/DeferredPromise.ts"],"sourcesContent":["export * from './createDeferredExecutor'\nexport * from './DeferredPromise'\n","export type PromiseState = 'pending' | 'fulfilled' | 'rejected'\n\nexport type Executor<Value> = ConstructorParameters<typeof Promise<Value>>[0]\nexport type ResolveFunction<Value> = Parameters<Executor<Value>>[0]\nexport type RejectFunction<Reason> = Parameters<Executor<Reason>>[1]\n\nexport type DeferredPromiseExecutor<Input = never, Output = Input> = {\n (resolve?: ResolveFunction<Input>, reject?: RejectFunction<any>): void\n\n resolve: ResolveFunction<Input>\n reject: RejectFunction<any>\n result?: Output\n state: PromiseState\n rejectionReason?: unknown\n}\nexport function createDeferredExecutor<\n Input = never,\n Output = Input\n>(): DeferredPromiseExecutor<Input, Output> {\n const executor = <DeferredPromiseExecutor<Input, Output>>((\n resolve,\n reject\n ) => {\n executor.state = 'pending'\n\n executor.resolve = (data) => {\n if (executor.state !== 'pending') {\n return\n }\n\n executor.result = data as Output\n\n const onFulfilled = <Value>(value: Value) => {\n executor.state = 'fulfilled'\n return value\n }\n\n return resolve(\n data instanceof Promise ? data : Promise.resolve(data).then(onFulfilled)\n )\n }\n\n executor.reject = (reason) => {\n if (executor.state !== 'pending') {\n return\n }\n\n queueMicrotask(() => {\n executor.state = 'rejected'\n })\n\n return reject((executor.rejectionReason = reason))\n }\n })\n\n return executor\n}\n","import {\n type Executor,\n type RejectFunction,\n type ResolveFunction,\n type DeferredPromiseExecutor,\n createDeferredExecutor,\n} from './createDeferredExecutor'\n\nexport class DeferredPromise<Input, Output = Input> extends Promise<Input> {\n #executor: DeferredPromiseExecutor\n\n public resolve: ResolveFunction<Output>\n public reject: RejectFunction<Output>\n\n constructor(executor: Executor<Input> | null = null) {\n const deferredExecutor = createDeferredExecutor()\n super((originalResolve, originalReject) => {\n deferredExecutor(originalResolve, originalReject)\n executor?.(deferredExecutor.resolve, deferredExecutor.reject)\n })\n\n this.#executor = deferredExecutor\n this.resolve = this.#executor.resolve\n this.reject = this.#executor.reject\n }\n\n public get state() {\n return this.#executor.state\n }\n\n public get rejectionReason() {\n return this.#executor.rejectionReason\n }\n\n public then<ThenResult = Input, CatchResult = never>(\n onFulfilled?: (value: Input) => ThenResult | PromiseLike<ThenResult>,\n onRejected?: (reason: any) => CatchResult | PromiseLike<CatchResult>\n ) {\n return this.#decorate(super.then(onFulfilled, onRejected))\n }\n\n public catch<CatchResult = never>(\n onRejected?: (reason: any) => CatchResult | PromiseLike<CatchResult>\n ) {\n return this.#decorate(super.catch(onRejected))\n }\n\n public finally(onfinally?: () => void | Promise<any>) {\n return this.#decorate(super.finally(onfinally))\n }\n\n #decorate<ChildInput>(\n promise: Promise<ChildInput>\n ): DeferredPromise<ChildInput, Output> {\n return Object.defineProperties(promise, {\n resolve: { configurable: true, value: this.resolve },\n reject: { configurable: true, value: this.reject },\n }) as DeferredPromise<ChildInput, Output>\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACeO,SAAS,yBAG4B;AAC1C,QAAM,WAAoD,CACxD,SACA,WACG;AACH,aAAS,QAAQ;AAEjB,aAAS,UAAU,CAAC,SAAS;AAC3B,UAAI,SAAS,UAAU,WAAW;AAChC;AAAA,MACF;AAEA,eAAS,SAAS;AAElB,YAAM,cAAc,CAAQ,UAAiB;AAC3C,iBAAS,QAAQ;AACjB,eAAO;AAAA,MACT;AAEA,aAAO;AAAA,QACL,gBAAgB,UAAU,OAAO,QAAQ,QAAQ,IAAI,EAAE,KAAK,WAAW;AAAA,MACzE;AAAA,IACF;AAEA,aAAS,SAAS,CAAC,WAAW;AAC5B,UAAI,SAAS,UAAU,WAAW;AAChC;AAAA,MACF;AAEA,qBAAe,MAAM;AACnB,iBAAS,QAAQ;AAAA,MACnB,CAAC;AAED,aAAO,OAAQ,SAAS,kBAAkB,MAAO;AAAA,IACnD;AAAA,EACF;AAEA,SAAO;AACT;;;AChDO,IAAM,kBAAN,cAAqD,QAAe;AAAA,EACzE;AAAA,EAEO;AAAA,EACA;AAAA,EAEP,YAAY,WAAmC,MAAM;AACnD,UAAM,mBAAmB,uBAAuB;AAChD,UAAM,CAAC,iBAAiB,mBAAmB;AACzC,uBAAiB,iBAAiB,cAAc;AAChD,iBAAW,iBAAiB,SAAS,iBAAiB,MAAM;AAAA,IAC9D,CAAC;AAED,SAAK,YAAY;AACjB,SAAK,UAAU,KAAK,UAAU;AAC9B,SAAK,SAAS,KAAK,UAAU;AAAA,EAC/B;AAAA,EAEA,IAAW,QAAQ;AACjB,WAAO,KAAK,UAAU;AAAA,EACxB;AAAA,EAEA,IAAW,kBAAkB;AAC3B,WAAO,KAAK,UAAU;AAAA,EACxB;AAAA,EAEO,KACL,aACA,YACA;AACA,WAAO,KAAK,UAAU,MAAM,KAAK,aAAa,UAAU,CAAC;AAAA,EAC3D;AAAA,EAEO,MACL,YACA;AACA,WAAO,KAAK,UAAU,MAAM,MAAM,UAAU,CAAC;AAAA,EAC/C;AAAA,EAEO,QAAQ,WAAuC;AACpD,WAAO,KAAK,UAAU,MAAM,QAAQ,SAAS,CAAC;AAAA,EAChD;AAAA,EAEA,UACE,SACqC;AACrC,WAAO,OAAO,iBAAiB,SAAS;AAAA,MACtC,SAAS,EAAE,cAAc,MAAM,OAAO,KAAK,QAAQ;AAAA,MACnD,QAAQ,EAAE,cAAc,MAAM,OAAO,KAAK,OAAO;AAAA,IACnD,CAAC;AAAA,EACH;AACF;","names":[]}
|
||||
72
node_modules/@open-draft/deferred-promise/build/index.mjs
generated
vendored
Normal file
72
node_modules/@open-draft/deferred-promise/build/index.mjs
generated
vendored
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
// src/createDeferredExecutor.ts
|
||||
function createDeferredExecutor() {
|
||||
const executor = (resolve, reject) => {
|
||||
executor.state = "pending";
|
||||
executor.resolve = (data) => {
|
||||
if (executor.state !== "pending") {
|
||||
return;
|
||||
}
|
||||
executor.result = data;
|
||||
const onFulfilled = (value) => {
|
||||
executor.state = "fulfilled";
|
||||
return value;
|
||||
};
|
||||
return resolve(
|
||||
data instanceof Promise ? data : Promise.resolve(data).then(onFulfilled)
|
||||
);
|
||||
};
|
||||
executor.reject = (reason) => {
|
||||
if (executor.state !== "pending") {
|
||||
return;
|
||||
}
|
||||
queueMicrotask(() => {
|
||||
executor.state = "rejected";
|
||||
});
|
||||
return reject(executor.rejectionReason = reason);
|
||||
};
|
||||
};
|
||||
return executor;
|
||||
}
|
||||
|
||||
// src/DeferredPromise.ts
|
||||
var DeferredPromise = class extends Promise {
|
||||
#executor;
|
||||
resolve;
|
||||
reject;
|
||||
constructor(executor = null) {
|
||||
const deferredExecutor = createDeferredExecutor();
|
||||
super((originalResolve, originalReject) => {
|
||||
deferredExecutor(originalResolve, originalReject);
|
||||
executor?.(deferredExecutor.resolve, deferredExecutor.reject);
|
||||
});
|
||||
this.#executor = deferredExecutor;
|
||||
this.resolve = this.#executor.resolve;
|
||||
this.reject = this.#executor.reject;
|
||||
}
|
||||
get state() {
|
||||
return this.#executor.state;
|
||||
}
|
||||
get rejectionReason() {
|
||||
return this.#executor.rejectionReason;
|
||||
}
|
||||
then(onFulfilled, onRejected) {
|
||||
return this.#decorate(super.then(onFulfilled, onRejected));
|
||||
}
|
||||
catch(onRejected) {
|
||||
return this.#decorate(super.catch(onRejected));
|
||||
}
|
||||
finally(onfinally) {
|
||||
return this.#decorate(super.finally(onfinally));
|
||||
}
|
||||
#decorate(promise) {
|
||||
return Object.defineProperties(promise, {
|
||||
resolve: { configurable: true, value: this.resolve },
|
||||
reject: { configurable: true, value: this.reject }
|
||||
});
|
||||
}
|
||||
};
|
||||
export {
|
||||
DeferredPromise,
|
||||
createDeferredExecutor
|
||||
};
|
||||
//# sourceMappingURL=index.mjs.map
|
||||
1
node_modules/@open-draft/deferred-promise/build/index.mjs.map
generated
vendored
Normal file
1
node_modules/@open-draft/deferred-promise/build/index.mjs.map
generated
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"version":3,"sources":["../src/createDeferredExecutor.ts","../src/DeferredPromise.ts"],"sourcesContent":["export type PromiseState = 'pending' | 'fulfilled' | 'rejected'\n\nexport type Executor<Value> = ConstructorParameters<typeof Promise<Value>>[0]\nexport type ResolveFunction<Value> = Parameters<Executor<Value>>[0]\nexport type RejectFunction<Reason> = Parameters<Executor<Reason>>[1]\n\nexport type DeferredPromiseExecutor<Input = never, Output = Input> = {\n (resolve?: ResolveFunction<Input>, reject?: RejectFunction<any>): void\n\n resolve: ResolveFunction<Input>\n reject: RejectFunction<any>\n result?: Output\n state: PromiseState\n rejectionReason?: unknown\n}\nexport function createDeferredExecutor<\n Input = never,\n Output = Input\n>(): DeferredPromiseExecutor<Input, Output> {\n const executor = <DeferredPromiseExecutor<Input, Output>>((\n resolve,\n reject\n ) => {\n executor.state = 'pending'\n\n executor.resolve = (data) => {\n if (executor.state !== 'pending') {\n return\n }\n\n executor.result = data as Output\n\n const onFulfilled = <Value>(value: Value) => {\n executor.state = 'fulfilled'\n return value\n }\n\n return resolve(\n data instanceof Promise ? data : Promise.resolve(data).then(onFulfilled)\n )\n }\n\n executor.reject = (reason) => {\n if (executor.state !== 'pending') {\n return\n }\n\n queueMicrotask(() => {\n executor.state = 'rejected'\n })\n\n return reject((executor.rejectionReason = reason))\n }\n })\n\n return executor\n}\n","import {\n type Executor,\n type RejectFunction,\n type ResolveFunction,\n type DeferredPromiseExecutor,\n createDeferredExecutor,\n} from './createDeferredExecutor'\n\nexport class DeferredPromise<Input, Output = Input> extends Promise<Input> {\n #executor: DeferredPromiseExecutor\n\n public resolve: ResolveFunction<Output>\n public reject: RejectFunction<Output>\n\n constructor(executor: Executor<Input> | null = null) {\n const deferredExecutor = createDeferredExecutor()\n super((originalResolve, originalReject) => {\n deferredExecutor(originalResolve, originalReject)\n executor?.(deferredExecutor.resolve, deferredExecutor.reject)\n })\n\n this.#executor = deferredExecutor\n this.resolve = this.#executor.resolve\n this.reject = this.#executor.reject\n }\n\n public get state() {\n return this.#executor.state\n }\n\n public get rejectionReason() {\n return this.#executor.rejectionReason\n }\n\n public then<ThenResult = Input, CatchResult = never>(\n onFulfilled?: (value: Input) => ThenResult | PromiseLike<ThenResult>,\n onRejected?: (reason: any) => CatchResult | PromiseLike<CatchResult>\n ) {\n return this.#decorate(super.then(onFulfilled, onRejected))\n }\n\n public catch<CatchResult = never>(\n onRejected?: (reason: any) => CatchResult | PromiseLike<CatchResult>\n ) {\n return this.#decorate(super.catch(onRejected))\n }\n\n public finally(onfinally?: () => void | Promise<any>) {\n return this.#decorate(super.finally(onfinally))\n }\n\n #decorate<ChildInput>(\n promise: Promise<ChildInput>\n ): DeferredPromise<ChildInput, Output> {\n return Object.defineProperties(promise, {\n resolve: { configurable: true, value: this.resolve },\n reject: { configurable: true, value: this.reject },\n }) as DeferredPromise<ChildInput, Output>\n }\n}\n"],"mappings":";AAeO,SAAS,yBAG4B;AAC1C,QAAM,WAAoD,CACxD,SACA,WACG;AACH,aAAS,QAAQ;AAEjB,aAAS,UAAU,CAAC,SAAS;AAC3B,UAAI,SAAS,UAAU,WAAW;AAChC;AAAA,MACF;AAEA,eAAS,SAAS;AAElB,YAAM,cAAc,CAAQ,UAAiB;AAC3C,iBAAS,QAAQ;AACjB,eAAO;AAAA,MACT;AAEA,aAAO;AAAA,QACL,gBAAgB,UAAU,OAAO,QAAQ,QAAQ,IAAI,EAAE,KAAK,WAAW;AAAA,MACzE;AAAA,IACF;AAEA,aAAS,SAAS,CAAC,WAAW;AAC5B,UAAI,SAAS,UAAU,WAAW;AAChC;AAAA,MACF;AAEA,qBAAe,MAAM;AACnB,iBAAS,QAAQ;AAAA,MACnB,CAAC;AAED,aAAO,OAAQ,SAAS,kBAAkB,MAAO;AAAA,IACnD;AAAA,EACF;AAEA,SAAO;AACT;;;AChDO,IAAM,kBAAN,cAAqD,QAAe;AAAA,EACzE;AAAA,EAEO;AAAA,EACA;AAAA,EAEP,YAAY,WAAmC,MAAM;AACnD,UAAM,mBAAmB,uBAAuB;AAChD,UAAM,CAAC,iBAAiB,mBAAmB;AACzC,uBAAiB,iBAAiB,cAAc;AAChD,iBAAW,iBAAiB,SAAS,iBAAiB,MAAM;AAAA,IAC9D,CAAC;AAED,SAAK,YAAY;AACjB,SAAK,UAAU,KAAK,UAAU;AAC9B,SAAK,SAAS,KAAK,UAAU;AAAA,EAC/B;AAAA,EAEA,IAAW,QAAQ;AACjB,WAAO,KAAK,UAAU;AAAA,EACxB;AAAA,EAEA,IAAW,kBAAkB;AAC3B,WAAO,KAAK,UAAU;AAAA,EACxB;AAAA,EAEO,KACL,aACA,YACA;AACA,WAAO,KAAK,UAAU,MAAM,KAAK,aAAa,UAAU,CAAC;AAAA,EAC3D;AAAA,EAEO,MACL,YACA;AACA,WAAO,KAAK,UAAU,MAAM,MAAM,UAAU,CAAC;AAAA,EAC/C;AAAA,EAEO,QAAQ,WAAuC;AACpD,WAAO,KAAK,UAAU,MAAM,QAAQ,SAAS,CAAC;AAAA,EAChD;AAAA,EAEA,UACE,SACqC;AACrC,WAAO,OAAO,iBAAiB,SAAS;AAAA,MACtC,SAAS,EAAE,cAAc,MAAM,OAAO,KAAK,QAAQ;AAAA,MACnD,QAAQ,EAAE,cAAc,MAAM,OAAO,KAAK,OAAO;AAAA,IACnD,CAAC;AAAA,EACH;AACF;","names":[]}
|
||||
49
node_modules/@open-draft/deferred-promise/package.json
generated
vendored
Normal file
49
node_modules/@open-draft/deferred-promise/package.json
generated
vendored
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
{
|
||||
"name": "@open-draft/deferred-promise",
|
||||
"version": "2.2.0",
|
||||
"description": "A Promise-compatible abstraction that defers resolving/rejecting promises to another closure.",
|
||||
"main": "./build/index.js",
|
||||
"types": "./build/index.d.ts",
|
||||
"module": "./build/index.mjs",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./build/index.d.ts",
|
||||
"require": "./build/index.js",
|
||||
"default": "./build/index.mjs"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"test": "jest",
|
||||
"test:compliance": "export NODE_OPTIONS=--loader=tsx || set NODE_OPTIONS=--loader=tsx&& npx -y promises-aplus-tests ./test/aplus-tests-adapter.ts",
|
||||
"prebuild": "rimraf ./build",
|
||||
"build": "tsup",
|
||||
"release": "release publish"
|
||||
},
|
||||
"files": [
|
||||
"./build"
|
||||
],
|
||||
"keywords": [
|
||||
"promise",
|
||||
"defer",
|
||||
"deferred",
|
||||
"resolve",
|
||||
"reject",
|
||||
"executor"
|
||||
],
|
||||
"author": "Artem Zakharchenko",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/open-draft/deferred-promise"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@ossjs/release": "^0.7.2",
|
||||
"@types/jest": "^29.0.1",
|
||||
"jest": "^29.0.3",
|
||||
"rimraf": "^3.0.2",
|
||||
"ts-jest": "^29.0.0",
|
||||
"tsup": "^7.2.0",
|
||||
"tsx": "^3.12.1",
|
||||
"typescript": "^4.8.3"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue