@youngspe/async-scope 0.1.0-dev.0 → 0.1.0-dev.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cancel.d.ts +1 -1
- package/dist/cancel.js +1 -2
- package/dist/commonResources.d.ts +3 -0
- package/dist/commonResources.js +3 -0
- package/dist/error.d.ts +2 -1
- package/dist/error.js +26 -4
- package/dist/events/cancel.d.ts +2 -1
- package/dist/events/cancel.js +59 -29
- package/dist/events/generic.d.ts +18 -14
- package/dist/events/generic.js +36 -26
- package/dist/events/sub.d.ts +3 -2
- package/dist/events/sub.js +5 -1
- package/dist/events/utils.d.ts +19 -0
- package/dist/events/utils.js +56 -0
- package/dist/events.d.ts +2 -1
- package/dist/events.js +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +3 -0
- package/dist/lock.d.ts +18 -0
- package/dist/lock.js +200 -0
- package/dist/promise.d.ts +5 -0
- package/dist/promise.js +38 -0
- package/dist/scope/base.d.ts +99 -14
- package/dist/scope/base.js +163 -41
- package/dist/scope/context.d.ts +19 -0
- package/dist/scope/context.js +71 -0
- package/dist/scope/from.d.ts +1 -1
- package/dist/scope/from.js +32 -11
- package/dist/scope/standard.d.ts +4 -1
- package/dist/scope/standard.js +6 -0
- package/dist/scope.d.ts +1 -1
- package/dist/scopedResource.js +1 -2
- package/dist/timers.d.ts +23 -0
- package/dist/{timer.js → timers.js} +2 -4
- package/dist/token/base.d.ts +29 -16
- package/dist/token/base.js +39 -23
- package/dist/token.d.ts +1 -1
- package/dist/types.d.ts +19 -84
- package/package.json +6 -1
- package/dist/join.d.ts +0 -16
- package/dist/join.js +0 -85
- package/dist/timer.d.ts +0 -13
- package/dist/utils.d.ts +0 -51
- package/dist/utils.js +0 -20
package/package.json
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@youngspe/async-scope",
|
|
3
|
-
"version": "0.1.0-dev.
|
|
3
|
+
"version": "0.1.0-dev.1",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dist/index.js",
|
|
7
7
|
"./events": "./dist/events.js"
|
|
8
8
|
},
|
|
9
9
|
"type": "module",
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@youngspe/async-iter-utils": "0.1.0-dev.0",
|
|
12
|
+
"@youngspe/async-scope-common": "0.1.0-dev.0",
|
|
13
|
+
"@youngspe/common-async-utils": "0.1.0-dev.0"
|
|
14
|
+
},
|
|
10
15
|
"devDependencies": {
|
|
11
16
|
"knip": "^6.3.0"
|
|
12
17
|
},
|
package/dist/join.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { Awaitable, Falsy, OptionalUndefinedParams } from './types.ts';
|
|
2
|
-
type Operation<T, U> = ((value: T, index: number) => Awaitable<U>) | ([T] extends [U] ? null | undefined : never);
|
|
3
|
-
export declare function joinPromises<T, U = T, Out = never, Out2 = never>(src: Iterable<Awaitable<T>> | AsyncIterable<T>, ...[operation, onResolve, onReject]: OptionalUndefinedParams<[
|
|
4
|
-
operation: Operation<T, U>,
|
|
5
|
-
onResolve: ((value: U, index: number) => Awaitable<Out | Falsy>) | null | undefined,
|
|
6
|
-
onReject: ((reason: unknown, index: number) => Awaitable<Out2 | Falsy>) | null | undefined
|
|
7
|
-
]>): Promise<NonNullable<Out | Out2> | undefined>;
|
|
8
|
-
/**
|
|
9
|
-
* Like `Promise.allSettled()`, this waits until all promises have settled. Like `Promise.all()`, this rejects if any promise rejects.
|
|
10
|
-
*/
|
|
11
|
-
export declare function whenAllSettled<T, U = T, Out = never>(src: Iterable<Awaitable<T>> | AsyncIterable<T>, ...[operation, onResolve]: OptionalUndefinedParams<[
|
|
12
|
-
operation: Operation<T, U>,
|
|
13
|
-
onResolve: ((value: U, index: number) => Awaitable<Out | Falsy>) | null | undefined
|
|
14
|
-
]>): Promise<Out | undefined>;
|
|
15
|
-
export {};
|
|
16
|
-
//# sourceMappingURL=join.d.ts.map
|
package/dist/join.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { isIterable, isPromiseLike } from './utils.js';
|
|
2
|
-
export async function joinPromises(src, ...[operation, onResolve, onReject]) {
|
|
3
|
-
return new Promise((_resolve, _reject) => {
|
|
4
|
-
let done = false;
|
|
5
|
-
let count = 1;
|
|
6
|
-
const resolve = (value) => {
|
|
7
|
-
if (value || --count === 0) {
|
|
8
|
-
done = true;
|
|
9
|
-
_resolve(value || undefined);
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
const reject = (e) => ((done = true), _reject(e));
|
|
13
|
-
const processItem = async (_value, index) => {
|
|
14
|
-
try {
|
|
15
|
-
if (done)
|
|
16
|
-
return;
|
|
17
|
-
let awaited;
|
|
18
|
-
try {
|
|
19
|
-
const value = isPromiseLike(_value) ? await _value : _value;
|
|
20
|
-
if (operation) {
|
|
21
|
-
if (done)
|
|
22
|
-
return;
|
|
23
|
-
const ret = operation(value, index);
|
|
24
|
-
awaited = isPromiseLike(ret) ? await ret : ret;
|
|
25
|
-
if (done)
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
awaited = value;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
catch (e) {
|
|
33
|
-
if (!onReject)
|
|
34
|
-
return reject(e);
|
|
35
|
-
if (done)
|
|
36
|
-
return;
|
|
37
|
-
return resolve(await onReject(e, index));
|
|
38
|
-
}
|
|
39
|
-
return resolve(await onResolve?.(awaited, index));
|
|
40
|
-
}
|
|
41
|
-
catch (e) {
|
|
42
|
-
return reject(e);
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
void (async () => {
|
|
46
|
-
try {
|
|
47
|
-
let i = 0;
|
|
48
|
-
if (isIterable(src)) {
|
|
49
|
-
for (const item of src) {
|
|
50
|
-
++count;
|
|
51
|
-
void processItem(item, i++);
|
|
52
|
-
if (done)
|
|
53
|
-
break;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
for await (const item of src) {
|
|
58
|
-
++count;
|
|
59
|
-
void processItem(item, i++);
|
|
60
|
-
if (done)
|
|
61
|
-
break;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
return resolve(undefined);
|
|
65
|
-
}
|
|
66
|
-
catch (e) {
|
|
67
|
-
return reject(e);
|
|
68
|
-
}
|
|
69
|
-
})();
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Like `Promise.allSettled()`, this waits until all promises have settled. Like `Promise.all()`, this rejects if any promise rejects.
|
|
74
|
-
*/
|
|
75
|
-
export async function whenAllSettled(src, ...[operation, onResolve]) {
|
|
76
|
-
const errors = new Set();
|
|
77
|
-
const out = await joinPromises(src, operation, onResolve, e => void errors.add(e));
|
|
78
|
-
if (out !== undefined)
|
|
79
|
-
return out;
|
|
80
|
-
if (errors.size > 1)
|
|
81
|
-
throw new AggregateError(errors);
|
|
82
|
-
if (errors.size === 1)
|
|
83
|
-
throw errors.values().next().value;
|
|
84
|
-
}
|
|
85
|
-
//# sourceMappingURL=join.js.map
|
package/dist/timer.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { CancellableOptions } from './cancel.ts';
|
|
2
|
-
interface Clock {
|
|
3
|
-
setTimeout: typeof globalThis.setTimeout;
|
|
4
|
-
setInterval: typeof globalThis.setInterval;
|
|
5
|
-
clearTimeout: typeof globalThis.clearTimeout;
|
|
6
|
-
clearInterval: typeof globalThis.clearInterval;
|
|
7
|
-
}
|
|
8
|
-
interface TimerOptions extends CancellableOptions {
|
|
9
|
-
clock?: Clock | undefined;
|
|
10
|
-
}
|
|
11
|
-
export declare function delay(ms: number, options?: TimerOptions): Promise<void>;
|
|
12
|
-
export {};
|
|
13
|
-
//# sourceMappingURL=timer.d.ts.map
|
package/dist/utils.d.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import type { Awaitable, UnlessNeverElse } from './types.ts';
|
|
2
|
-
/**
|
|
3
|
-
* Gets the base {@link Iterable} types for each constituent of union type `I` that is an iterable.
|
|
4
|
-
* Discards all non-iterable constituents.
|
|
5
|
-
*
|
|
6
|
-
* This is a helper for {@linkcode AsIterable}.
|
|
7
|
-
*/
|
|
8
|
-
type ExtractIterable<I> = I extends Iterable<infer X, infer Y, infer Z> ? Iterable<X, Y, Z> : never;
|
|
9
|
-
/**
|
|
10
|
-
* If `I` or any of its union constituents extend {@link Iterable}, evaluates to the base iterable
|
|
11
|
-
* type of `I` or its constituents.
|
|
12
|
-
* Otherwise, evaluates to {@linkcode Iterable|Iterable<unknown, unknown, any>}.
|
|
13
|
-
*
|
|
14
|
-
* This is used in the predicate type of {@linkcode isIterable} to narrow a type down to
|
|
15
|
-
* {@link Iterable} while preserving the most likely `yield`, `return`, and `next` types.
|
|
16
|
-
*/
|
|
17
|
-
type AsIterable<I> = UnlessNeverElse<ExtractIterable<I>, Iterable<unknown, unknown, any>>;
|
|
18
|
-
export declare function isIterable<T>(value: T | AsIterable<T> | null | undefined): value is AsIterable<T>;
|
|
19
|
-
export declare function isPromiseLike<T>(value: Awaitable<T> | null | undefined): value is PromiseLike<T>;
|
|
20
|
-
/** @returns `true` if `value` has a {@linkcode Symbol.dispose} method. */
|
|
21
|
-
export declare function isDisposable(value: unknown): value is Disposable;
|
|
22
|
-
/** @returns `true` if `value` has a {@linkcode Symbol.asyncDispose} method. */
|
|
23
|
-
export declare function isAsyncDisposable(value: unknown): value is AsyncDisposable;
|
|
24
|
-
/**
|
|
25
|
-
* Gets the base {@link Array} types for each constituent of union type `I` that is an array.
|
|
26
|
-
* Discards all non-array constituents.
|
|
27
|
-
*
|
|
28
|
-
* This is a helper for {@linkcode AsArray}.
|
|
29
|
-
*/
|
|
30
|
-
type ExtractArray<I> = I extends Array<infer X> ? X[] : I extends ReadonlyArray<infer X> ? readonly X[] : never;
|
|
31
|
-
/**
|
|
32
|
-
* If `I` or any of its union constituents extend {@link Array} or {@link ReadonlyArray}, evaluates
|
|
33
|
-
* to the base array type of `I` or its constituents.
|
|
34
|
-
* Otherwise, evaluates to `unknown[]`.
|
|
35
|
-
*
|
|
36
|
-
* This is used in the predicate type of {@linkcode isArray} to narrow a type down to {@link Array}
|
|
37
|
-
* while preserving the most likely element type.
|
|
38
|
-
*/
|
|
39
|
-
type AsArray<I> = UnlessNeverElse<ExtractArray<I>, unknown[]>;
|
|
40
|
-
interface IsArrayFunction {
|
|
41
|
-
<I>(value: I | AsArray<I> | null | undefined): value is AsArray<I>;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* @returns `true` if {@link value} is an array.
|
|
45
|
-
*
|
|
46
|
-
* @remarks
|
|
47
|
-
* This is an alias for {@link Array.isArray} but the types work out better for readonly arrays.
|
|
48
|
-
*/
|
|
49
|
-
export declare const isArray: IsArrayFunction;
|
|
50
|
-
export {};
|
|
51
|
-
//# sourceMappingURL=utils.d.ts.map
|
package/dist/utils.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export function isIterable(value) {
|
|
2
|
-
return typeof value?.[Symbol.iterator] === 'function';
|
|
3
|
-
}
|
|
4
|
-
export function isPromiseLike(value) {
|
|
5
|
-
return typeof value?.then === 'function';
|
|
6
|
-
}
|
|
7
|
-
export function isDisposable(value) {
|
|
8
|
-
return typeof value?.[Symbol.dispose] === 'function';
|
|
9
|
-
}
|
|
10
|
-
export function isAsyncDisposable(value) {
|
|
11
|
-
return typeof value?.[Symbol.asyncDispose] === 'function';
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* @returns `true` if {@link value} is an array.
|
|
15
|
-
*
|
|
16
|
-
* @remarks
|
|
17
|
-
* This is an alias for {@link Array.isArray} but the types work out better for readonly arrays.
|
|
18
|
-
*/
|
|
19
|
-
export const isArray = Array.isArray.bind(Array);
|
|
20
|
-
//# sourceMappingURL=utils.js.map
|