@oscarpalmer/atoms 0.210.0 → 0.212.0
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/array/sort.d.mts +1 -1
- package/dist/color/models.d.mts +11 -11
- package/dist/herald.d.mts +2 -1
- package/dist/index.d.mts +142 -78
- package/dist/index.mjs +90 -52
- package/dist/internal/aborter.d.mts +1 -1
- package/dist/internal/herald.d.mts +2 -38
- package/dist/internal/herald.mjs +4 -14
- package/dist/internal/models/herald.model.d.mts +60 -0
- package/dist/internal/models/herald.model.mjs +15 -0
- package/dist/internal/models/plan.model.d.mts +27 -10
- package/dist/internal/models/result.model.d.mts +2 -2
- package/dist/internal/plan.d.mts +4 -4
- package/dist/internal/plan.mjs +2 -0
- package/dist/internal/subscription.d.mts +2 -2
- package/dist/internal/value/handlers.d.mts +3 -3
- package/dist/internal/value/set.mjs +1 -1
- package/dist/models.d.mts +3 -3
- package/dist/plan/generate.d.mts +4 -0
- package/dist/plan/generate.mjs +50 -0
- package/dist/plan/index.d.mts +7 -32
- package/dist/plan/index.mjs +7 -59
- package/dist/plan/result.d.mts +41 -0
- package/dist/plan/result.mjs +24 -0
- package/dist/plan/run.d.mts +41 -0
- package/dist/plan/run.mjs +26 -0
- package/dist/promise/models.d.mts +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { isError } from "../internal/result/misc.mjs";
|
|
2
|
+
//#region src/plan/generate.ts
|
|
3
|
+
async function asyncGenerate(callback, parameters) {
|
|
4
|
+
const generator = callback(...parameters);
|
|
5
|
+
let success = true;
|
|
6
|
+
let lastValue;
|
|
7
|
+
try {
|
|
8
|
+
while (true) {
|
|
9
|
+
const next = await generator.next(lastValue);
|
|
10
|
+
const { done } = next;
|
|
11
|
+
let { value } = next;
|
|
12
|
+
if (value instanceof Error) throw value;
|
|
13
|
+
if (isError(value)) throw value.error;
|
|
14
|
+
lastValue = value;
|
|
15
|
+
if (done === true) break;
|
|
16
|
+
}
|
|
17
|
+
} catch (error) {
|
|
18
|
+
lastValue = error;
|
|
19
|
+
success = false;
|
|
20
|
+
} finally {
|
|
21
|
+
await generator?.return(lastValue);
|
|
22
|
+
}
|
|
23
|
+
if (success) return lastValue;
|
|
24
|
+
throw lastValue;
|
|
25
|
+
}
|
|
26
|
+
function generate(callback, parameters) {
|
|
27
|
+
const generator = callback(...parameters);
|
|
28
|
+
let success = true;
|
|
29
|
+
let lastValue;
|
|
30
|
+
try {
|
|
31
|
+
while (true) {
|
|
32
|
+
const next = generator.next(lastValue);
|
|
33
|
+
const done = next.done === true;
|
|
34
|
+
const { value } = next;
|
|
35
|
+
if (value instanceof Error) throw value;
|
|
36
|
+
if (isError(value)) throw value.error;
|
|
37
|
+
lastValue = value;
|
|
38
|
+
if (done) break;
|
|
39
|
+
}
|
|
40
|
+
} catch (error) {
|
|
41
|
+
lastValue = error;
|
|
42
|
+
success = false;
|
|
43
|
+
} finally {
|
|
44
|
+
generator.return(lastValue);
|
|
45
|
+
}
|
|
46
|
+
if (success) return lastValue;
|
|
47
|
+
throw lastValue;
|
|
48
|
+
}
|
|
49
|
+
//#endregion
|
|
50
|
+
export { asyncGenerate, generate };
|
package/dist/plan/index.d.mts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { AsyncPlan, Plan
|
|
1
|
+
import { AsyncPlan, Plan } from "../internal/models/plan.model.mjs";
|
|
2
|
+
import { asyncAttemptRun, attemptRun } from "./result.mjs";
|
|
3
|
+
import { asyncRun, run } from "./run.mjs";
|
|
2
4
|
//#region src/plan/index.d.ts
|
|
3
5
|
/**
|
|
4
6
|
* Create a plan for an asynchronous generator function
|
|
@@ -6,40 +8,13 @@ import { AsyncPlan, Plan, PlanError, PlanResult } from "../internal/models/plan.
|
|
|
6
8
|
* @param generator Generator to plan for
|
|
7
9
|
* @returns Generator plan
|
|
8
10
|
*/
|
|
9
|
-
export declare function plan<Yielded, Returned>(generator: () => AsyncGenerator<Yielded, Returned>): AsyncPlan<Yielded, Returned>;
|
|
11
|
+
export declare function plan<Yielded, Returned, Parameters extends unknown[]>(generator: (...parameters: Parameters) => AsyncGenerator<Yielded, Returned>): AsyncPlan<Yielded, Returned, Parameters>;
|
|
10
12
|
/**
|
|
11
13
|
* Create a plan for a generator function
|
|
12
14
|
*
|
|
13
15
|
* @param generator Generator to plan for
|
|
14
16
|
* @returns Generator plan
|
|
15
17
|
*/
|
|
16
|
-
export declare function plan<Yielded, Returned>(generator: () => Generator<Yielded, Returned>): Plan<Yielded, Returned>;
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
*
|
|
20
|
-
* @param generator Generator to run
|
|
21
|
-
* @returns Result
|
|
22
|
-
*/
|
|
23
|
-
export declare function run<Yielded, Returned>(generator: () => AsyncGenerator<Yielded, Returned>): Promise<PlanResult<Returned> | PlanError<Yielded, Returned>>;
|
|
24
|
-
/**
|
|
25
|
-
* Run an asynchronous plan to completion
|
|
26
|
-
*
|
|
27
|
-
* @param plan Plan to run
|
|
28
|
-
* @returns Result
|
|
29
|
-
*/
|
|
30
|
-
export declare function run<Yielded, Returned>(plan: AsyncPlan<Yielded, Returned>): Promise<PlanResult<Returned> | PlanError<Yielded, Returned>>;
|
|
31
|
-
/**
|
|
32
|
-
* Run a generator to completion
|
|
33
|
-
*
|
|
34
|
-
* @param generator Generator to run
|
|
35
|
-
* @returns Result
|
|
36
|
-
*/
|
|
37
|
-
export declare function run<Yielded, Returned>(generator: () => Generator<Yielded, Returned>): PlanResult<Returned> | PlanError<Yielded, Returned>;
|
|
38
|
-
/**
|
|
39
|
-
* Run a plan to completion
|
|
40
|
-
*
|
|
41
|
-
* @param plan Plan to run
|
|
42
|
-
* @returns Result
|
|
43
|
-
*/
|
|
44
|
-
export declare function run<Yielded, Returned>(plan: Plan<Yielded, Returned>): PlanResult<Returned> | PlanError<Yielded, Returned>;
|
|
45
|
-
//#endregion
|
|
18
|
+
export declare function plan<Yielded, Returned, Parameters extends unknown[]>(generator: (...parameters: Parameters) => Generator<Yielded, Returned>): Plan<Yielded, Returned, Parameters>;
|
|
19
|
+
//#endregion
|
|
20
|
+
export { asyncAttemptRun, asyncRun, attemptRun, run };
|
package/dist/plan/index.mjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { PLAN_MESSAGE_PLAN_INPUT,
|
|
2
|
-
import { isAsyncGenerator,
|
|
1
|
+
import { PLAN_MESSAGE_PLAN_INPUT, PLAN_SYMBOL, PLAN_TYPE_PLAN_ASYNC, PLAN_TYPE_PLAN_SYNC } from "../internal/models/plan.model.mjs";
|
|
2
|
+
import { isAsyncGenerator, isGenerator } from "../internal/plan.mjs";
|
|
3
|
+
import { asyncAttemptRun, attemptRun, attemptRunAsyncPlan, attemptRunPlan } from "./result.mjs";
|
|
4
|
+
import { asyncRun, run, runAsyncPlan, runPlan } from "./run.mjs";
|
|
3
5
|
//#region src/plan/index.ts
|
|
4
6
|
function AsyncPlan(generator) {
|
|
5
7
|
this[PLAN_SYMBOL] = {
|
|
@@ -10,6 +12,7 @@ function AsyncPlan(generator) {
|
|
|
10
12
|
AsyncPlan.prototype[Symbol.asyncIterator] = function() {
|
|
11
13
|
return this[PLAN_SYMBOL].generator();
|
|
12
14
|
};
|
|
15
|
+
AsyncPlan.prototype.attempt = attemptRunAsyncPlan;
|
|
13
16
|
AsyncPlan.prototype.run = runAsyncPlan;
|
|
14
17
|
function Plan(generator) {
|
|
15
18
|
this[PLAN_SYMBOL] = {
|
|
@@ -20,67 +23,12 @@ function Plan(generator) {
|
|
|
20
23
|
Plan.prototype[Symbol.iterator] = function() {
|
|
21
24
|
return this[PLAN_SYMBOL].generator();
|
|
22
25
|
};
|
|
26
|
+
Plan.prototype.attempt = attemptRunPlan;
|
|
23
27
|
Plan.prototype.run = runPlan;
|
|
24
|
-
async function asyncExecute(callback) {
|
|
25
|
-
const generator = callback();
|
|
26
|
-
let success = true;
|
|
27
|
-
let lastValue;
|
|
28
|
-
try {
|
|
29
|
-
while (true) {
|
|
30
|
-
const next = await generator.next(lastValue);
|
|
31
|
-
const { done } = next;
|
|
32
|
-
let { value } = next;
|
|
33
|
-
if (value instanceof Error) throw value;
|
|
34
|
-
lastValue = value;
|
|
35
|
-
if (done === true) break;
|
|
36
|
-
}
|
|
37
|
-
} catch (error) {
|
|
38
|
-
lastValue = error;
|
|
39
|
-
success = false;
|
|
40
|
-
} finally {
|
|
41
|
-
generator?.return(lastValue);
|
|
42
|
-
}
|
|
43
|
-
if (success) return lastValue;
|
|
44
|
-
throw lastValue;
|
|
45
|
-
}
|
|
46
|
-
function execute(callback) {
|
|
47
|
-
const generator = callback();
|
|
48
|
-
let success = true;
|
|
49
|
-
let lastValue;
|
|
50
|
-
try {
|
|
51
|
-
while (true) {
|
|
52
|
-
const next = generator.next(lastValue);
|
|
53
|
-
const done = next.done === true;
|
|
54
|
-
let { value } = next;
|
|
55
|
-
if (value instanceof Error) throw value;
|
|
56
|
-
lastValue = value;
|
|
57
|
-
if (done) break;
|
|
58
|
-
}
|
|
59
|
-
} catch (error) {
|
|
60
|
-
lastValue = error;
|
|
61
|
-
success = false;
|
|
62
|
-
} finally {
|
|
63
|
-
generator.return(lastValue);
|
|
64
|
-
}
|
|
65
|
-
if (success) return lastValue;
|
|
66
|
-
throw lastValue;
|
|
67
|
-
}
|
|
68
28
|
function plan(generator) {
|
|
69
29
|
if (isAsyncGenerator(generator)) return new AsyncPlan(generator);
|
|
70
30
|
if (isGenerator(generator)) return new Plan(generator);
|
|
71
31
|
throw new Error(PLAN_MESSAGE_PLAN_INPUT);
|
|
72
32
|
}
|
|
73
|
-
function run(input) {
|
|
74
|
-
if (isAsyncPlan(input) || isPlan(input)) return input.run();
|
|
75
|
-
if (isAsyncGenerator(input)) return asyncExecute(input);
|
|
76
|
-
if (isGenerator(input)) return execute(input);
|
|
77
|
-
throw new Error(PLAN_MESSAGE_RUN_INPUT);
|
|
78
|
-
}
|
|
79
|
-
function runAsyncPlan() {
|
|
80
|
-
return asyncExecute(this[PLAN_SYMBOL].generator);
|
|
81
|
-
}
|
|
82
|
-
function runPlan() {
|
|
83
|
-
return execute(this[PLAN_SYMBOL].generator);
|
|
84
|
-
}
|
|
85
33
|
//#endregion
|
|
86
|
-
export { plan, run };
|
|
34
|
+
export { asyncAttemptRun, asyncRun, attemptRun, plan, run };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Result } from "../internal/models/result.model.mjs";
|
|
2
|
+
import { AsyncPlan, InternalPlan, Plan, PlanError, PlanResult } from "../internal/models/plan.model.mjs";
|
|
3
|
+
//#region src/plan/result.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Attempt to run the generator to completion
|
|
6
|
+
*
|
|
7
|
+
* _Returns a promised {@link Result} instead of a raw value or throwing an error_
|
|
8
|
+
*
|
|
9
|
+
* @returns Promised result
|
|
10
|
+
*/
|
|
11
|
+
export declare function asyncAttemptRun<Yielded, Returned, Parameters extends unknown[]>(generator: (...parameters: Parameters) => AsyncGenerator<Yielded, Returned, unknown>, ...parameters: Parameters): Promise<Result<PlanResult<Returned>, PlanError<Yielded, Returned>>>;
|
|
12
|
+
/**
|
|
13
|
+
* Attempt to run the plan to completion
|
|
14
|
+
*
|
|
15
|
+
* _Returns a promised {@link Result} instead of a raw value or throwing an error_
|
|
16
|
+
*
|
|
17
|
+
* @returns Promised result
|
|
18
|
+
*/
|
|
19
|
+
export declare function asyncAttemptRun<Yielded, Returned, Parameters extends unknown[]>(plan: AsyncPlan<Yielded, Returned, Parameters>, ...parameters: Parameters): Promise<Result<PlanResult<Returned>, PlanError<Yielded, Returned>>>;
|
|
20
|
+
/**
|
|
21
|
+
* Attempt to run the generator to completion
|
|
22
|
+
*
|
|
23
|
+
* _Returns a {@link Result} instead of a raw value or throwing an error_
|
|
24
|
+
*
|
|
25
|
+
* @returns Result
|
|
26
|
+
*/
|
|
27
|
+
export declare function attemptRun<Yielded, Returned>(generator: () => Generator<Yielded, Returned, unknown>, ...parameters: Parameters<typeof generator>): Result<PlanResult<Returned>, PlanError<Yielded, Returned>>;
|
|
28
|
+
/**
|
|
29
|
+
* Attempt to run the plan to completion
|
|
30
|
+
*
|
|
31
|
+
* _Returns a {@link Result} instead of a raw value or throwing an error_
|
|
32
|
+
*
|
|
33
|
+
* @returns Result
|
|
34
|
+
*/
|
|
35
|
+
export declare function attemptRun<Yielded, Returned, Parameters extends unknown[]>(plan: Plan<Yielded, Returned, Parameters>, ...parameters: Parameters): Result<PlanResult<Returned>, PlanError<Yielded, Returned>>;
|
|
36
|
+
export declare function attemptRunAsyncPlan(this: InternalPlan, ...parameters: unknown[]): Promise<unknown>;
|
|
37
|
+
export declare function attemptRunPlan(this: InternalPlan, ...parameters: unknown[]): unknown;
|
|
38
|
+
export declare namespace attemptRun {
|
|
39
|
+
export var async: typeof asyncAttemptRun;
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { asyncAttempt, attempt } from "../internal/result/attempt.mjs";
|
|
2
|
+
import { PLAN_MESSAGE_RUN_INPUT } from "../internal/models/plan.model.mjs";
|
|
3
|
+
import { isAsyncGenerator, isAsyncPlan, isGenerator, isPlan } from "../internal/plan.mjs";
|
|
4
|
+
import { asyncGenerate, generate } from "./generate.mjs";
|
|
5
|
+
//#region src/plan/result.ts
|
|
6
|
+
function asyncAttemptRun(input, ...parameters) {
|
|
7
|
+
if (isAsyncPlan(input)) return input.attempt(...parameters);
|
|
8
|
+
if (isAsyncGenerator(input)) return asyncAttempt(() => asyncGenerate(input, parameters));
|
|
9
|
+
throw new Error(PLAN_MESSAGE_RUN_INPUT);
|
|
10
|
+
}
|
|
11
|
+
function attemptRun(input, ...parameters) {
|
|
12
|
+
if (isPlan(input)) return input.attempt(...parameters);
|
|
13
|
+
if (isGenerator(input)) return attempt(() => generate(input, parameters));
|
|
14
|
+
throw new Error(PLAN_MESSAGE_RUN_INPUT);
|
|
15
|
+
}
|
|
16
|
+
function attemptRunAsyncPlan(...parameters) {
|
|
17
|
+
return asyncAttempt(() => this.run(...parameters));
|
|
18
|
+
}
|
|
19
|
+
function attemptRunPlan(...parameters) {
|
|
20
|
+
return attempt(() => this.run(...parameters));
|
|
21
|
+
}
|
|
22
|
+
attemptRun.async = asyncAttemptRun;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { asyncAttemptRun, attemptRun, attemptRunAsyncPlan, attemptRunPlan };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { AsyncPlan, InternalAsyncPlan, InternalPlan, Plan, PlanError, PlanResult } from "../internal/models/plan.model.mjs";
|
|
2
|
+
import { asyncAttemptRun, attemptRun } from "./result.mjs";
|
|
3
|
+
//#region src/plan/run.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Run an asynchronous generator to completion
|
|
6
|
+
*
|
|
7
|
+
* @param generator Generator to run
|
|
8
|
+
* @returns Result
|
|
9
|
+
*/
|
|
10
|
+
export declare function asyncRun<Yielded, Returned, Parameters extends unknown[]>(generator: (...parameters: Parameters) => AsyncGenerator<Yielded, Returned, unknown>, ...parameters: Parameters): Promise<PlanResult<Returned>>;
|
|
11
|
+
/**
|
|
12
|
+
* Run an asynchronous plan to completion
|
|
13
|
+
*
|
|
14
|
+
* @param plan Plan to run
|
|
15
|
+
* @returns Result
|
|
16
|
+
*/
|
|
17
|
+
export declare function asyncRun<Yielded, Returned, Parameters extends unknown[]>(plan: AsyncPlan<Yielded, Returned, Parameters>, ...parameters: Parameters): Promise<PlanResult<Returned>>;
|
|
18
|
+
/**
|
|
19
|
+
* Run a generator to completion
|
|
20
|
+
*
|
|
21
|
+
* @param generator Generator to run
|
|
22
|
+
* @returns Result
|
|
23
|
+
*/
|
|
24
|
+
export declare function run<Yielded, Returned, Parameters extends unknown[]>(generator: (...parameters: Parameters) => Generator<Yielded, Returned, unknown>, ...parameters: Parameters): PlanResult<Returned> | PlanError<Yielded, Returned>;
|
|
25
|
+
/**
|
|
26
|
+
* Run a plan to completion
|
|
27
|
+
*
|
|
28
|
+
* @param plan Plan to run
|
|
29
|
+
* @returns Result
|
|
30
|
+
*/
|
|
31
|
+
export declare function run<Yielded, Returned, Parameters extends unknown[]>(plan: Plan<Yielded, Returned, Parameters>, ...parameters: Parameters): PlanResult<Returned> | PlanError<Yielded, Returned>;
|
|
32
|
+
export declare function runAsyncPlan(this: InternalAsyncPlan, ...parameters: unknown[]): Promise<unknown>;
|
|
33
|
+
export declare function runPlan(this: InternalPlan, ...parameters: unknown[]): unknown;
|
|
34
|
+
export declare namespace asyncRun {
|
|
35
|
+
export var attempt: typeof asyncAttemptRun;
|
|
36
|
+
}
|
|
37
|
+
export declare namespace run {
|
|
38
|
+
export var async: typeof asyncRun;
|
|
39
|
+
export var attempt: typeof attemptRun;
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { PLAN_MESSAGE_RUN_INPUT, PLAN_SYMBOL } from "../internal/models/plan.model.mjs";
|
|
2
|
+
import { isAsyncGenerator, isAsyncPlan, isGenerator, isPlan } from "../internal/plan.mjs";
|
|
3
|
+
import { asyncGenerate, generate } from "./generate.mjs";
|
|
4
|
+
import { asyncAttemptRun, attemptRun } from "./result.mjs";
|
|
5
|
+
//#region src/plan/run.ts
|
|
6
|
+
function asyncRun(input, ...parameters) {
|
|
7
|
+
if (isAsyncPlan(input)) return input.run(...parameters);
|
|
8
|
+
if (isAsyncGenerator(input)) return asyncGenerate(input, parameters);
|
|
9
|
+
throw new Error(PLAN_MESSAGE_RUN_INPUT);
|
|
10
|
+
}
|
|
11
|
+
function run(input, ...parameters) {
|
|
12
|
+
if (isPlan(input)) return input.run(...parameters);
|
|
13
|
+
if (isGenerator(input)) return generate(input, parameters);
|
|
14
|
+
throw new Error(PLAN_MESSAGE_RUN_INPUT);
|
|
15
|
+
}
|
|
16
|
+
function runAsyncPlan(...parameters) {
|
|
17
|
+
return asyncGenerate(this[PLAN_SYMBOL].generator, parameters);
|
|
18
|
+
}
|
|
19
|
+
function runPlan(...parameters) {
|
|
20
|
+
return generate(this[PLAN_SYMBOL].generator, parameters);
|
|
21
|
+
}
|
|
22
|
+
asyncRun.attempt = asyncAttemptRun;
|
|
23
|
+
run.async = asyncRun;
|
|
24
|
+
run.attempt = attemptRun;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { asyncRun, run, runAsyncPlan, runPlan };
|
|
@@ -34,8 +34,8 @@ export type PromiseData = {
|
|
|
34
34
|
result: ArrayOrPlainObject;
|
|
35
35
|
};
|
|
36
36
|
export type PromiseHandlers = {
|
|
37
|
-
resolve
|
|
38
|
-
reject
|
|
37
|
+
resolve(value: unknown[]): void;
|
|
38
|
+
reject(reason: unknown): void;
|
|
39
39
|
};
|
|
40
40
|
/**
|
|
41
41
|
* Options for a _Promise_-handling function
|