@oscarpalmer/atoms 0.208.0 → 0.210.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/index.d.mts +64 -37
- package/dist/index.mjs +106 -76
- package/dist/internal/models/plan.model.d.mts +22 -6
- package/dist/internal/plan.d.mts +45 -0
- package/dist/internal/plan.mjs +68 -0
- package/dist/plan/index.d.mts +13 -48
- package/dist/plan/index.mjs +45 -85
- package/dist/plan/is.d.mts +2 -0
- package/dist/plan/is.mjs +2 -0
- package/dist/plan/misc.d.mts +2 -0
- package/dist/plan/misc.mjs +2 -0
- package/dist/plan/models.d.mts +2 -2
- package/package.json +10 -2
package/dist/index.d.mts
CHANGED
|
@@ -4591,25 +4591,42 @@ export declare function setValue<Data extends PlainObject>(data: Data, path: str
|
|
|
4591
4591
|
/**
|
|
4592
4592
|
* An asynchronous plan of execution that can yield intermediate results and eventually return a result
|
|
4593
4593
|
*/
|
|
4594
|
-
type AsyncPlan<
|
|
4594
|
+
type AsyncPlan<Yielded, Returned> = {
|
|
4595
|
+
/**
|
|
4596
|
+
* Get the asynchronous generator for the plan
|
|
4597
|
+
*
|
|
4598
|
+
* @returns Asynchronous generator
|
|
4599
|
+
*/
|
|
4600
|
+
[Symbol.asyncIterator]: () => AsyncGenerator<Yielded, Returned, unknown>;
|
|
4595
4601
|
/**
|
|
4596
4602
|
* Run the plan to completion
|
|
4597
4603
|
*
|
|
4604
|
+
* @throws {PlanError<Yielded, Returned>}
|
|
4598
4605
|
* @returns Result
|
|
4599
4606
|
*/
|
|
4600
|
-
run: () => Promise<
|
|
4607
|
+
run: () => Promise<PlanResult<Returned>>;
|
|
4601
4608
|
};
|
|
4602
4609
|
/**
|
|
4603
4610
|
* A plan of execution that can yield intermediate results and eventually return a result
|
|
4604
4611
|
*/
|
|
4605
|
-
type Plan<
|
|
4612
|
+
type Plan<Yielded, Returned> = {
|
|
4613
|
+
/**
|
|
4614
|
+
* Get the generator for the plan
|
|
4615
|
+
*
|
|
4616
|
+
* @returns Generator
|
|
4617
|
+
*/
|
|
4618
|
+
[Symbol.iterator]: () => Generator<Yielded, Returned, unknown>;
|
|
4606
4619
|
/**
|
|
4607
4620
|
* Run the plan to completion
|
|
4608
4621
|
*
|
|
4622
|
+
* @throws {PlanError<Yielded, Returned>}
|
|
4609
4623
|
* @returns Result
|
|
4610
4624
|
*/
|
|
4611
|
-
run: () =>
|
|
4625
|
+
run: () => PlanResult<Returned>;
|
|
4612
4626
|
};
|
|
4627
|
+
type PlanErrorValues<Original> = Original extends Error ? Original : never;
|
|
4628
|
+
type PlanError<Yielded, Returned> = PlanErrorValues<Yielded> | PlanErrorValues<Returned> | Error;
|
|
4629
|
+
type PlanResult<Returned> = Returned extends Error ? never : Returned;
|
|
4613
4630
|
//#endregion
|
|
4614
4631
|
//#region src/plan/index.d.ts
|
|
4615
4632
|
/**
|
|
@@ -4618,28 +4635,51 @@ type Plan<Value, Error = unknown> = {
|
|
|
4618
4635
|
* @param generator Generator to plan for
|
|
4619
4636
|
* @returns Generator plan
|
|
4620
4637
|
*/
|
|
4621
|
-
export declare function
|
|
4638
|
+
export declare function plan<Yielded, Returned>(generator: () => AsyncGenerator<Yielded, Returned>): AsyncPlan<Yielded, Returned>;
|
|
4639
|
+
/**
|
|
4640
|
+
* Create a plan for a generator function
|
|
4641
|
+
*
|
|
4642
|
+
* @param generator Generator to plan for
|
|
4643
|
+
* @returns Generator plan
|
|
4644
|
+
*/
|
|
4645
|
+
export declare function plan<Yielded, Returned>(generator: () => Generator<Yielded, Returned>): Plan<Yielded, Returned>;
|
|
4622
4646
|
/**
|
|
4623
4647
|
* Run an asynchronous generator to completion
|
|
4624
4648
|
*
|
|
4625
4649
|
* @param generator Generator to run
|
|
4626
4650
|
* @returns Result
|
|
4627
4651
|
*/
|
|
4628
|
-
export declare function
|
|
4652
|
+
export declare function run<Yielded, Returned>(generator: () => AsyncGenerator<Yielded, Returned>): Promise<PlanResult<Returned> | PlanError<Yielded, Returned>>;
|
|
4629
4653
|
/**
|
|
4630
4654
|
* Run an asynchronous plan to completion
|
|
4631
4655
|
*
|
|
4632
4656
|
* @param plan Plan to run
|
|
4633
4657
|
* @returns Result
|
|
4634
4658
|
*/
|
|
4635
|
-
export declare function
|
|
4659
|
+
export declare function run<Yielded, Returned>(plan: AsyncPlan<Yielded, Returned>): Promise<PlanResult<Returned> | PlanError<Yielded, Returned>>;
|
|
4636
4660
|
/**
|
|
4637
|
-
*
|
|
4661
|
+
* Run a generator to completion
|
|
4638
4662
|
*
|
|
4639
|
-
* @param
|
|
4640
|
-
* @returns
|
|
4663
|
+
* @param generator Generator to run
|
|
4664
|
+
* @returns Result
|
|
4641
4665
|
*/
|
|
4642
|
-
export declare function
|
|
4666
|
+
export declare function run<Yielded, Returned>(generator: () => Generator<Yielded, Returned>): PlanResult<Returned> | PlanError<Yielded, Returned>;
|
|
4667
|
+
/**
|
|
4668
|
+
* Run a plan to completion
|
|
4669
|
+
*
|
|
4670
|
+
* @param plan Plan to run
|
|
4671
|
+
* @returns Result
|
|
4672
|
+
*/
|
|
4673
|
+
export declare function run<Yielded, Returned>(plan: Plan<Yielded, Returned>): PlanResult<Returned> | PlanError<Yielded, Returned>;
|
|
4674
|
+
//#endregion
|
|
4675
|
+
//#region src/internal/plan.d.ts
|
|
4676
|
+
/**
|
|
4677
|
+
* Create an immediately failing generator
|
|
4678
|
+
*
|
|
4679
|
+
* @param reason Failure reason
|
|
4680
|
+
* @returns Failing generator
|
|
4681
|
+
*/
|
|
4682
|
+
export declare function fail<Reason>(reason: Reason): Generator<never, never>;
|
|
4643
4683
|
/**
|
|
4644
4684
|
* Is the value an asynchronous generator function?
|
|
4645
4685
|
*
|
|
@@ -4647,6 +4687,13 @@ export declare function isAsyncPlan<Value = unknown, Error = unknown>(value: unk
|
|
|
4647
4687
|
* @returns `true` if the value is an asynchronous generator function, otherwise `false`
|
|
4648
4688
|
*/
|
|
4649
4689
|
export declare function isAsyncGenerator(value: unknown): value is () => AsyncGenerator;
|
|
4690
|
+
/**
|
|
4691
|
+
* Is the value an asynchronous plan?
|
|
4692
|
+
*
|
|
4693
|
+
* @param value Value to check
|
|
4694
|
+
* @returns `true` if the value is an asynchronous plan, otherwise `false`
|
|
4695
|
+
*/
|
|
4696
|
+
export declare function isAsyncPlan<Yielded = unknown, Returned = unknown>(value: unknown): value is AsyncPlan<Yielded, Returned>;
|
|
4650
4697
|
/**
|
|
4651
4698
|
* Is the value a generator function?
|
|
4652
4699
|
*
|
|
@@ -4660,34 +4707,14 @@ export declare function isGenerator(value: unknown): value is () => Generator;
|
|
|
4660
4707
|
* @param value Value to check
|
|
4661
4708
|
* @returns `true` if the value is a plan, otherwise `false`
|
|
4662
4709
|
*/
|
|
4663
|
-
export declare function isPlan<
|
|
4710
|
+
export declare function isPlan<Yielded = unknown, Returned = unknown>(value: unknown): value is Plan<Yielded, Returned>;
|
|
4664
4711
|
/**
|
|
4665
|
-
* Create
|
|
4712
|
+
* Create an immediately succeeding generator
|
|
4666
4713
|
*
|
|
4667
|
-
* @param
|
|
4668
|
-
* @returns
|
|
4714
|
+
* @param value Success value
|
|
4715
|
+
* @returns Succeeding generator
|
|
4669
4716
|
*/
|
|
4670
|
-
export declare function
|
|
4671
|
-
/**
|
|
4672
|
-
* Run a generator to completion
|
|
4673
|
-
*
|
|
4674
|
-
* @param generator Generator to run
|
|
4675
|
-
* @returns Result
|
|
4676
|
-
*/
|
|
4677
|
-
export declare function run<Value = unknown, Error = unknown>(generator: () => Generator<unknown, Value, unknown>): Result<Value, Error>;
|
|
4678
|
-
/**
|
|
4679
|
-
* Run a plan to completion
|
|
4680
|
-
*
|
|
4681
|
-
* @param plan Plan to run
|
|
4682
|
-
* @returns Result
|
|
4683
|
-
*/
|
|
4684
|
-
export declare function run<Value = unknown, Error = unknown>(plan: Plan<Value, Error>): Result<Value, Error>;
|
|
4685
|
-
export declare namespace plan {
|
|
4686
|
-
export var async: typeof asyncPlan;
|
|
4687
|
-
}
|
|
4688
|
-
export declare namespace run {
|
|
4689
|
-
export var async: typeof asyncRun;
|
|
4690
|
-
}
|
|
4717
|
+
export declare function succeed<Value>(value: Value): Generator<never, Value>;
|
|
4691
4718
|
//#endregion
|
|
4692
4719
|
//#region src/internal/aborter.d.ts
|
|
4693
4720
|
type Aborter = {
|
|
@@ -8419,4 +8446,4 @@ export declare class SizedSet<Value = unknown> extends Set<Value> {
|
|
|
8419
8446
|
get(value: Value, update?: boolean): Value | undefined;
|
|
8420
8447
|
}
|
|
8421
8448
|
//#endregion
|
|
8422
|
-
export { type AsyncLimiter, type Color, type ColorType, type ColorTypeExtended, type ColorValue, type ColorValueExtended, type HSLAColor, type HSLColor, type HWBAColor, type HWBColor, Key$1 as Key, type Limiter, Logger, type Plan, type RGBAColor, type RGBColor, type Shaken, type Smushed, type Subscription, type SubscriptionParameters, type SubscriptionProperty, type Subscriptions, type SubscriptionsParameters, type TimeLogger, type Unsmushed, push as append, filterAndMap as select, toPromise as fromResult, getRandomFloatingNumber as getRandomFloat, isLogger, isTimeLogger, mapAndFilter as reverseSelect };
|
|
8449
|
+
export { type AsyncLimiter, type AsyncPlan, type Color, type ColorType, type ColorTypeExtended, type ColorValue, type ColorValueExtended, type HSLAColor, type HSLColor, type HWBAColor, type HWBColor, Key$1 as Key, type Limiter, Logger, type Plan, type RGBAColor, type RGBColor, type Shaken, type Smushed, type Subscription, type SubscriptionParameters, type SubscriptionProperty, type Subscriptions, type SubscriptionsParameters, type TimeLogger, type Unsmushed, push as append, filterAndMap as select, toPromise as fromResult, getRandomFloatingNumber as getRandomFloat, isLogger, isTimeLogger, mapAndFilter as reverseSelect };
|
package/dist/index.mjs
CHANGED
|
@@ -2746,89 +2746,45 @@ function setValue(data, path, value, ignoreCase) {
|
|
|
2746
2746
|
return data;
|
|
2747
2747
|
}
|
|
2748
2748
|
const SET_EXPRESSION_INDEX = /^\d+$/;
|
|
2749
|
+
//#endregion
|
|
2750
|
+
//#region src/internal/models/plan.model.ts
|
|
2751
|
+
const GENERATOR_NAME_ASYNC = "AsyncGeneratorFunction";
|
|
2752
|
+
const GENERATOR_NAME_SYNC = "GeneratorFunction";
|
|
2749
2753
|
const PLAN_MESSAGE_PLAN_INPUT = "plan requires a generator function";
|
|
2750
2754
|
const PLAN_MESSAGE_RUN_INPUT = "run requires a plan or a generator function";
|
|
2751
2755
|
const PLAN_SYMBOL = Symbol("plan");
|
|
2752
2756
|
const PLAN_TYPE_PLAN_ASYNC = "asyncPlan";
|
|
2753
2757
|
const PLAN_TYPE_PLAN_SYNC = "plan";
|
|
2754
2758
|
//#endregion
|
|
2755
|
-
//#region src/plan
|
|
2756
|
-
function AsyncPlan(generator) {
|
|
2757
|
-
this[PLAN_SYMBOL] = {
|
|
2758
|
-
generator,
|
|
2759
|
-
type: PLAN_TYPE_PLAN_ASYNC
|
|
2760
|
-
};
|
|
2761
|
-
}
|
|
2762
|
-
AsyncPlan.prototype.run = runAsyncPlan;
|
|
2763
|
-
function Plan(generator) {
|
|
2764
|
-
this[PLAN_SYMBOL] = {
|
|
2765
|
-
generator,
|
|
2766
|
-
type: PLAN_TYPE_PLAN_SYNC
|
|
2767
|
-
};
|
|
2768
|
-
}
|
|
2769
|
-
Plan.prototype.run = runPlan;
|
|
2770
|
-
function asyncExecute(callback) {
|
|
2771
|
-
return asyncAttempt(async () => {
|
|
2772
|
-
const generator = callback();
|
|
2773
|
-
let lastValue;
|
|
2774
|
-
while (true) {
|
|
2775
|
-
const next = await generator.next();
|
|
2776
|
-
const { done } = next;
|
|
2777
|
-
let value = next.value;
|
|
2778
|
-
if (value instanceof Error) throw value;
|
|
2779
|
-
lastValue = value;
|
|
2780
|
-
if (done === true) break;
|
|
2781
|
-
}
|
|
2782
|
-
return lastValue;
|
|
2783
|
-
});
|
|
2784
|
-
}
|
|
2759
|
+
//#region src/internal/plan.ts
|
|
2785
2760
|
/**
|
|
2786
|
-
* Create
|
|
2761
|
+
* Create an immediately failing generator
|
|
2787
2762
|
*
|
|
2788
|
-
* @param
|
|
2789
|
-
* @returns
|
|
2763
|
+
* @param reason Failure reason
|
|
2764
|
+
* @returns Failing generator
|
|
2790
2765
|
*/
|
|
2791
|
-
function
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
}
|
|
2795
|
-
async function asyncRun(input) {
|
|
2796
|
-
if (isAsyncPlan(input)) return input.run();
|
|
2797
|
-
if (!isAsyncGenerator(input)) throw new Error(PLAN_MESSAGE_RUN_INPUT);
|
|
2798
|
-
return asyncExecute(input);
|
|
2799
|
-
}
|
|
2800
|
-
function execute(callback) {
|
|
2801
|
-
return attempt(() => {
|
|
2802
|
-
const generator = callback();
|
|
2803
|
-
let lastValue;
|
|
2804
|
-
while (true) {
|
|
2805
|
-
const next = generator.next();
|
|
2806
|
-
const { done } = next;
|
|
2807
|
-
let { value } = next;
|
|
2808
|
-
if (value instanceof Error) throw value;
|
|
2809
|
-
lastValue = value;
|
|
2810
|
-
if (done === true) break;
|
|
2811
|
-
}
|
|
2812
|
-
return lastValue;
|
|
2813
|
-
});
|
|
2766
|
+
function fail(reason) {
|
|
2767
|
+
return (function* () {
|
|
2768
|
+
throw reason;
|
|
2769
|
+
})();
|
|
2814
2770
|
}
|
|
2815
2771
|
/**
|
|
2816
|
-
* Is the value an asynchronous
|
|
2772
|
+
* Is the value an asynchronous generator function?
|
|
2817
2773
|
*
|
|
2818
2774
|
* @param value Value to check
|
|
2819
|
-
* @returns `true` if the value is an asynchronous
|
|
2775
|
+
* @returns `true` if the value is an asynchronous generator function, otherwise `false`
|
|
2820
2776
|
*/
|
|
2821
|
-
function
|
|
2822
|
-
return
|
|
2777
|
+
function isAsyncGenerator(value) {
|
|
2778
|
+
return isGeneratorInstance(GENERATOR_NAME_ASYNC, value);
|
|
2823
2779
|
}
|
|
2824
2780
|
/**
|
|
2825
|
-
* Is the value an asynchronous
|
|
2781
|
+
* Is the value an asynchronous plan?
|
|
2826
2782
|
*
|
|
2827
2783
|
* @param value Value to check
|
|
2828
|
-
* @returns `true` if the value is an asynchronous
|
|
2784
|
+
* @returns `true` if the value is an asynchronous plan, otherwise `false`
|
|
2829
2785
|
*/
|
|
2830
|
-
function
|
|
2831
|
-
return
|
|
2786
|
+
function isAsyncPlan(value) {
|
|
2787
|
+
return isPlanInstance(PLAN_TYPE_PLAN_ASYNC, value);
|
|
2832
2788
|
}
|
|
2833
2789
|
/**
|
|
2834
2790
|
* Is the value a generator function?
|
|
@@ -2837,7 +2793,10 @@ function isAsyncGenerator(value) {
|
|
|
2837
2793
|
* @returns `true` if the value is a generator function, otherwise `false`
|
|
2838
2794
|
*/
|
|
2839
2795
|
function isGenerator(value) {
|
|
2840
|
-
return
|
|
2796
|
+
return isGeneratorInstance(GENERATOR_NAME_SYNC, value);
|
|
2797
|
+
}
|
|
2798
|
+
function isGeneratorInstance(name, value) {
|
|
2799
|
+
return typeof value === "function" && value !== null && value.constructor.name === name && typeof value.prototype.next === "function" && typeof value.prototype.return === "function" && typeof value.prototype.throw === "function";
|
|
2841
2800
|
}
|
|
2842
2801
|
/**
|
|
2843
2802
|
* Is the value a plan?
|
|
@@ -2852,19 +2811,92 @@ function isPlanInstance(type, value) {
|
|
|
2852
2811
|
return typeof value === "object" && value !== null && PLAN_SYMBOL in value && value[PLAN_SYMBOL].type === type;
|
|
2853
2812
|
}
|
|
2854
2813
|
/**
|
|
2855
|
-
* Create
|
|
2814
|
+
* Create an immediately succeeding generator
|
|
2856
2815
|
*
|
|
2857
|
-
* @param
|
|
2858
|
-
* @returns
|
|
2816
|
+
* @param value Success value
|
|
2817
|
+
* @returns Succeeding generator
|
|
2859
2818
|
*/
|
|
2819
|
+
function succeed(value) {
|
|
2820
|
+
return (function* () {
|
|
2821
|
+
return value;
|
|
2822
|
+
})();
|
|
2823
|
+
}
|
|
2824
|
+
//#endregion
|
|
2825
|
+
//#region src/plan/index.ts
|
|
2826
|
+
function AsyncPlan(generator) {
|
|
2827
|
+
this[PLAN_SYMBOL] = {
|
|
2828
|
+
generator,
|
|
2829
|
+
type: PLAN_TYPE_PLAN_ASYNC
|
|
2830
|
+
};
|
|
2831
|
+
}
|
|
2832
|
+
AsyncPlan.prototype[Symbol.asyncIterator] = function() {
|
|
2833
|
+
return this[PLAN_SYMBOL].generator();
|
|
2834
|
+
};
|
|
2835
|
+
AsyncPlan.prototype.run = runAsyncPlan;
|
|
2836
|
+
function Plan(generator) {
|
|
2837
|
+
this[PLAN_SYMBOL] = {
|
|
2838
|
+
generator,
|
|
2839
|
+
type: PLAN_TYPE_PLAN_SYNC
|
|
2840
|
+
};
|
|
2841
|
+
}
|
|
2842
|
+
Plan.prototype[Symbol.iterator] = function() {
|
|
2843
|
+
return this[PLAN_SYMBOL].generator();
|
|
2844
|
+
};
|
|
2845
|
+
Plan.prototype.run = runPlan;
|
|
2846
|
+
async function asyncExecute(callback) {
|
|
2847
|
+
const generator = callback();
|
|
2848
|
+
let success = true;
|
|
2849
|
+
let lastValue;
|
|
2850
|
+
try {
|
|
2851
|
+
while (true) {
|
|
2852
|
+
const next = await generator.next(lastValue);
|
|
2853
|
+
const { done } = next;
|
|
2854
|
+
let { value } = next;
|
|
2855
|
+
if (value instanceof Error) throw value;
|
|
2856
|
+
lastValue = value;
|
|
2857
|
+
if (done === true) break;
|
|
2858
|
+
}
|
|
2859
|
+
} catch (error) {
|
|
2860
|
+
lastValue = error;
|
|
2861
|
+
success = false;
|
|
2862
|
+
} finally {
|
|
2863
|
+
generator?.return(lastValue);
|
|
2864
|
+
}
|
|
2865
|
+
if (success) return lastValue;
|
|
2866
|
+
throw lastValue;
|
|
2867
|
+
}
|
|
2868
|
+
function execute(callback) {
|
|
2869
|
+
const generator = callback();
|
|
2870
|
+
let success = true;
|
|
2871
|
+
let lastValue;
|
|
2872
|
+
try {
|
|
2873
|
+
while (true) {
|
|
2874
|
+
const next = generator.next(lastValue);
|
|
2875
|
+
const done = next.done === true;
|
|
2876
|
+
let { value } = next;
|
|
2877
|
+
if (value instanceof Error) throw value;
|
|
2878
|
+
lastValue = value;
|
|
2879
|
+
if (done) break;
|
|
2880
|
+
}
|
|
2881
|
+
} catch (error) {
|
|
2882
|
+
lastValue = error;
|
|
2883
|
+
success = false;
|
|
2884
|
+
} finally {
|
|
2885
|
+
generator.return(lastValue);
|
|
2886
|
+
}
|
|
2887
|
+
if (success) return lastValue;
|
|
2888
|
+
throw lastValue;
|
|
2889
|
+
}
|
|
2860
2890
|
function plan(generator) {
|
|
2861
|
-
if (
|
|
2862
|
-
return new Plan(generator);
|
|
2891
|
+
if (isAsyncGenerator(generator)) return new AsyncPlan(generator);
|
|
2892
|
+
if (isGenerator(generator)) return new Plan(generator);
|
|
2893
|
+
throw new Error(PLAN_MESSAGE_PLAN_INPUT);
|
|
2863
2894
|
}
|
|
2864
2895
|
function run(input) {
|
|
2865
|
-
if (isPlan(input)) return input.run();
|
|
2866
|
-
if (
|
|
2867
|
-
return execute(input);
|
|
2896
|
+
if (isAsyncPlan(input) || isPlan(input)) return input.run();
|
|
2897
|
+
if (isAsyncGenerator(input)) return asyncExecute(input);
|
|
2898
|
+
if (isGenerator(input)) return execute(input);
|
|
2899
|
+
throw new Error(PLAN_MESSAGE_RUN_INPUT);
|
|
2868
2900
|
}
|
|
2869
2901
|
function runAsyncPlan() {
|
|
2870
2902
|
return asyncExecute(this[PLAN_SYMBOL].generator);
|
|
@@ -2872,8 +2904,6 @@ function runAsyncPlan() {
|
|
|
2872
2904
|
function runPlan() {
|
|
2873
2905
|
return execute(this[PLAN_SYMBOL].generator);
|
|
2874
2906
|
}
|
|
2875
|
-
plan.async = asyncPlan;
|
|
2876
|
-
run.async = asyncRun;
|
|
2877
2907
|
//#endregion
|
|
2878
2908
|
//#region src/internal/aborter.ts
|
|
2879
2909
|
function createAborter(value, onAbort) {
|
|
@@ -6975,4 +7005,4 @@ var SizedSet = class extends Set {
|
|
|
6975
7005
|
}
|
|
6976
7006
|
};
|
|
6977
7007
|
//#endregion
|
|
6978
|
-
export { CancelablePromise, Logger, PromiseTimeoutError, QueueError, RetryError, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SizedMap, SizedSet, push as append, push, assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assertProperty, assign, asyncAttempt, asyncDebounce, asyncFlow, asyncMatchResult, asyncOnce, asyncPipe,
|
|
7008
|
+
export { CancelablePromise, Logger, PromiseTimeoutError, QueueError, RetryError, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SizedMap, SizedSet, push as append, push, assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assertProperty, assign, asyncAttempt, asyncDebounce, asyncFlow, asyncMatchResult, asyncOnce, asyncPipe, asyncThrottle, attempt, attemptAsyncFlow, attemptAsyncPipe, attemptFlow, attemptPipe, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, copy, count, debounce, deburr, dedent, delay, deregisterCloner, deregisterComparator, deregisterEqualizer, diff, difference, drop, endsWith, endsWithArray, equal, error, exclude, exists, fail, filter, filterAndMap, filterAndMap as select, find, findLast, first, firstOrDefault, flatFreeze, flatten, floor, flow, freeze, fromQuery, toPromise as fromResult, toPromise, fuzzy, fuzzyMatch, getArray, getArrayComparison, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getHwbColor, getHwbaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloatingNumber as getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getSortedIndex, getString, getTimedPromise, getUuid, getValue, groupArraysBy, groupBy, handleResult, hasValue, hasValueResult, herald, hexToHsl, hexToHsla, hexToHwb, hexToHwba, hexToRgb, hexToRgba, hslToHex, hslToHwb, hslToHwba, hslToRgb, hslToRgba, hwbToHex, hwbToHsl, hwbToHsla, hwbToRgb, hwbToRgba, ignoreKey, inMap, inSet, includes, includesArray, indexOf, indexOfArray, initializeAssigner, initializeCloner, initializeEqualizer, initializeMerger, initializeNormalizer, initializeSorter, initializeTemplater, initializeTransformer, insert, interpolate, intersection, isArrayOrPlainObject, isAsyncGenerator, isAsyncOnce, isAsyncPlan, isBeacon, isBeaconSubscription, isColor, isConstructor, isEmpty, isError, isFrozen, isFulfilled, isFuzzy, isGenerator, isHerald, isHeraldEvents, isHeraldSubscription, isHexColor, isHslColor, isHslaColor, isHwbColor, isHwbaColor, isInstanceOf, isKey, isKeyedQueue, isLogger, isNonArrayOrPlainObject, isNonConstructor, isNonEmpty, isNonInstanceOf, isNonKey, isNonNullable, isNonNullableOrEmpty, isNonNullableOrWhitespace, isNonNumber, isNonNumerical, isNonObject, isNonPlainObject, isNonPrimitive, isNonTemplateStringsArray, isNonTypedArray, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isObservable, isOk, isOnce, isPlainObject, isPlan, isPrimitive, isQueue, isQueueInstance, isRejected, isResult, isRgbColor, isRgbaColor, isSorted, isSubscription, isSubscriptions, isTemplateStringsArray, isTimeLogger, isTypedArray, join, kebabCase, keyedQueue, last, lastIndexOf, lastOrDefault, lowerCase, mapAndFilter, mapAndFilter as reverseSelect, matchResult, max, median, memoize, merge, min, move, moveIndices, moveToIndex, noop, normalize, ok, omit, once, parse, partition, pascalCase, pick, pipe, plan, promises, queue, range, registerCloner, registerComparator, registerEqualizer, resultPromises, retry, reverse, rgbToHex, rgbToHsl, rgbToHsla, rgbToHwb, rgbToHwba, round, run, setValue, settlePromise, shake, shuffle, single, slice, smush, snakeCase, sort, splice, startsWith, startsWithArray, subscriptions, succeed, sum, swap, take, template, throttle, timed, times, titleCase, toMap, toMapArrays, toQuery, toRecord, toRecordArrays, toResult, toSet, toggle, transform, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
|
|
@@ -1,15 +1,21 @@
|
|
|
1
|
-
import { Result } from "./result.model.mjs";
|
|
2
1
|
//#region src/internal/models/plan.model.d.ts
|
|
3
2
|
/**
|
|
4
3
|
* An asynchronous plan of execution that can yield intermediate results and eventually return a result
|
|
5
4
|
*/
|
|
6
|
-
export type AsyncPlan<
|
|
5
|
+
export type AsyncPlan<Yielded, Returned> = {
|
|
6
|
+
/**
|
|
7
|
+
* Get the asynchronous generator for the plan
|
|
8
|
+
*
|
|
9
|
+
* @returns Asynchronous generator
|
|
10
|
+
*/
|
|
11
|
+
[Symbol.asyncIterator]: () => AsyncGenerator<Yielded, Returned, unknown>;
|
|
7
12
|
/**
|
|
8
13
|
* Run the plan to completion
|
|
9
14
|
*
|
|
15
|
+
* @throws {PlanError<Yielded, Returned>}
|
|
10
16
|
* @returns Result
|
|
11
17
|
*/
|
|
12
|
-
run: () => Promise<
|
|
18
|
+
run: () => Promise<PlanResult<Returned>>;
|
|
13
19
|
};
|
|
14
20
|
type AsyncPlanState = {
|
|
15
21
|
generator: () => AsyncGenerator;
|
|
@@ -26,18 +32,28 @@ export type InternalPlan = {
|
|
|
26
32
|
/**
|
|
27
33
|
* A plan of execution that can yield intermediate results and eventually return a result
|
|
28
34
|
*/
|
|
29
|
-
export type Plan<
|
|
35
|
+
export type Plan<Yielded, Returned> = {
|
|
36
|
+
/**
|
|
37
|
+
* Get the generator for the plan
|
|
38
|
+
*
|
|
39
|
+
* @returns Generator
|
|
40
|
+
*/
|
|
41
|
+
[Symbol.iterator]: () => Generator<Yielded, Returned, unknown>;
|
|
30
42
|
/**
|
|
31
43
|
* Run the plan to completion
|
|
32
44
|
*
|
|
45
|
+
* @throws {PlanError<Yielded, Returned>}
|
|
33
46
|
* @returns Result
|
|
34
47
|
*/
|
|
35
|
-
run: () =>
|
|
48
|
+
run: () => PlanResult<Returned>;
|
|
36
49
|
};
|
|
50
|
+
type PlanErrorValues<Original> = Original extends Error ? Original : never;
|
|
51
|
+
export type PlanError<Yielded, Returned> = PlanErrorValues<Yielded> | PlanErrorValues<Returned> | Error;
|
|
52
|
+
export type PlanResult<Returned> = Returned extends Error ? never : Returned;
|
|
37
53
|
type PlanState = {
|
|
38
54
|
generator: () => Generator;
|
|
39
55
|
} & BaseState<typeof PLAN_TYPE_PLAN_SYNC>;
|
|
40
|
-
export type PlanType = "asyncPlan" | "plan";
|
|
56
|
+
export type PlanType = "asyncPlan" | "plan" | "stop";
|
|
41
57
|
export declare const GENERATOR_NAME_ASYNC = "AsyncGeneratorFunction";
|
|
42
58
|
export declare const GENERATOR_NAME_SYNC = "GeneratorFunction";
|
|
43
59
|
export declare const PLAN_MESSAGE_PLAN_INPUT = "plan requires a generator function";
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { AsyncPlan, Plan } from "./models/plan.model.mjs";
|
|
2
|
+
//#region src/internal/plan.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Create an immediately failing generator
|
|
5
|
+
*
|
|
6
|
+
* @param reason Failure reason
|
|
7
|
+
* @returns Failing generator
|
|
8
|
+
*/
|
|
9
|
+
export declare function fail<Reason>(reason: Reason): Generator<never, never>;
|
|
10
|
+
/**
|
|
11
|
+
* Is the value an asynchronous generator function?
|
|
12
|
+
*
|
|
13
|
+
* @param value Value to check
|
|
14
|
+
* @returns `true` if the value is an asynchronous generator function, otherwise `false`
|
|
15
|
+
*/
|
|
16
|
+
export declare function isAsyncGenerator(value: unknown): value is () => AsyncGenerator;
|
|
17
|
+
/**
|
|
18
|
+
* Is the value an asynchronous plan?
|
|
19
|
+
*
|
|
20
|
+
* @param value Value to check
|
|
21
|
+
* @returns `true` if the value is an asynchronous plan, otherwise `false`
|
|
22
|
+
*/
|
|
23
|
+
export declare function isAsyncPlan<Yielded = unknown, Returned = unknown>(value: unknown): value is AsyncPlan<Yielded, Returned>;
|
|
24
|
+
/**
|
|
25
|
+
* Is the value a generator function?
|
|
26
|
+
*
|
|
27
|
+
* @param value Value to check
|
|
28
|
+
* @returns `true` if the value is a generator function, otherwise `false`
|
|
29
|
+
*/
|
|
30
|
+
export declare function isGenerator(value: unknown): value is () => Generator;
|
|
31
|
+
/**
|
|
32
|
+
* Is the value a plan?
|
|
33
|
+
*
|
|
34
|
+
* @param value Value to check
|
|
35
|
+
* @returns `true` if the value is a plan, otherwise `false`
|
|
36
|
+
*/
|
|
37
|
+
export declare function isPlan<Yielded = unknown, Returned = unknown>(value: unknown): value is Plan<Yielded, Returned>;
|
|
38
|
+
/**
|
|
39
|
+
* Create an immediately succeeding generator
|
|
40
|
+
*
|
|
41
|
+
* @param value Success value
|
|
42
|
+
* @returns Succeeding generator
|
|
43
|
+
*/
|
|
44
|
+
export declare function succeed<Value>(value: Value): Generator<never, Value>;
|
|
45
|
+
//#endregion
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { GENERATOR_NAME_ASYNC, GENERATOR_NAME_SYNC, PLAN_SYMBOL, PLAN_TYPE_PLAN_ASYNC, PLAN_TYPE_PLAN_SYNC } from "./models/plan.model.mjs";
|
|
2
|
+
//#region src/internal/plan.ts
|
|
3
|
+
/**
|
|
4
|
+
* Create an immediately failing generator
|
|
5
|
+
*
|
|
6
|
+
* @param reason Failure reason
|
|
7
|
+
* @returns Failing generator
|
|
8
|
+
*/
|
|
9
|
+
function fail(reason) {
|
|
10
|
+
return (function* () {
|
|
11
|
+
throw reason;
|
|
12
|
+
})();
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Is the value an asynchronous generator function?
|
|
16
|
+
*
|
|
17
|
+
* @param value Value to check
|
|
18
|
+
* @returns `true` if the value is an asynchronous generator function, otherwise `false`
|
|
19
|
+
*/
|
|
20
|
+
function isAsyncGenerator(value) {
|
|
21
|
+
return isGeneratorInstance(GENERATOR_NAME_ASYNC, value);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Is the value an asynchronous plan?
|
|
25
|
+
*
|
|
26
|
+
* @param value Value to check
|
|
27
|
+
* @returns `true` if the value is an asynchronous plan, otherwise `false`
|
|
28
|
+
*/
|
|
29
|
+
function isAsyncPlan(value) {
|
|
30
|
+
return isPlanInstance(PLAN_TYPE_PLAN_ASYNC, value);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Is the value a generator function?
|
|
34
|
+
*
|
|
35
|
+
* @param value Value to check
|
|
36
|
+
* @returns `true` if the value is a generator function, otherwise `false`
|
|
37
|
+
*/
|
|
38
|
+
function isGenerator(value) {
|
|
39
|
+
return isGeneratorInstance(GENERATOR_NAME_SYNC, value);
|
|
40
|
+
}
|
|
41
|
+
function isGeneratorInstance(name, value) {
|
|
42
|
+
return typeof value === "function" && value !== null && value.constructor.name === name && typeof value.prototype.next === "function" && typeof value.prototype.return === "function" && typeof value.prototype.throw === "function";
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Is the value a plan?
|
|
46
|
+
*
|
|
47
|
+
* @param value Value to check
|
|
48
|
+
* @returns `true` if the value is a plan, otherwise `false`
|
|
49
|
+
*/
|
|
50
|
+
function isPlan(value) {
|
|
51
|
+
return isPlanInstance(PLAN_TYPE_PLAN_SYNC, value);
|
|
52
|
+
}
|
|
53
|
+
function isPlanInstance(type, value) {
|
|
54
|
+
return typeof value === "object" && value !== null && PLAN_SYMBOL in value && value[PLAN_SYMBOL].type === type;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Create an immediately succeeding generator
|
|
58
|
+
*
|
|
59
|
+
* @param value Success value
|
|
60
|
+
* @returns Succeeding generator
|
|
61
|
+
*/
|
|
62
|
+
function succeed(value) {
|
|
63
|
+
return (function* () {
|
|
64
|
+
return value;
|
|
65
|
+
})();
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
export { fail, isAsyncGenerator, isAsyncPlan, isGenerator, isPlan, succeed };
|
package/dist/plan/index.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AsyncPlan, Plan } from "../internal/models/plan.model.mjs";
|
|
1
|
+
import { AsyncPlan, Plan, PlanError, PlanResult } from "../internal/models/plan.model.mjs";
|
|
3
2
|
//#region src/plan/index.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* Create a plan for an asynchronous generator function
|
|
@@ -7,74 +6,40 @@ import { AsyncPlan, Plan } from "../internal/models/plan.model.mjs";
|
|
|
7
6
|
* @param generator Generator to plan for
|
|
8
7
|
* @returns Generator plan
|
|
9
8
|
*/
|
|
10
|
-
export declare function
|
|
9
|
+
export declare function plan<Yielded, Returned>(generator: () => AsyncGenerator<Yielded, Returned>): AsyncPlan<Yielded, Returned>;
|
|
10
|
+
/**
|
|
11
|
+
* Create a plan for a generator function
|
|
12
|
+
*
|
|
13
|
+
* @param generator Generator to plan for
|
|
14
|
+
* @returns Generator plan
|
|
15
|
+
*/
|
|
16
|
+
export declare function plan<Yielded, Returned>(generator: () => Generator<Yielded, Returned>): Plan<Yielded, Returned>;
|
|
11
17
|
/**
|
|
12
18
|
* Run an asynchronous generator to completion
|
|
13
19
|
*
|
|
14
20
|
* @param generator Generator to run
|
|
15
21
|
* @returns Result
|
|
16
22
|
*/
|
|
17
|
-
export declare function
|
|
23
|
+
export declare function run<Yielded, Returned>(generator: () => AsyncGenerator<Yielded, Returned>): Promise<PlanResult<Returned> | PlanError<Yielded, Returned>>;
|
|
18
24
|
/**
|
|
19
25
|
* Run an asynchronous plan to completion
|
|
20
26
|
*
|
|
21
27
|
* @param plan Plan to run
|
|
22
28
|
* @returns Result
|
|
23
29
|
*/
|
|
24
|
-
export declare function
|
|
25
|
-
/**
|
|
26
|
-
* Is the value an asynchronous plan?
|
|
27
|
-
*
|
|
28
|
-
* @param value Value to check
|
|
29
|
-
* @returns `true` if the value is an asynchronous plan, otherwise `false`
|
|
30
|
-
*/
|
|
31
|
-
export declare function isAsyncPlan<Value = unknown, Error = unknown>(value: unknown): value is AsyncPlan<Value, Error>;
|
|
32
|
-
/**
|
|
33
|
-
* Is the value an asynchronous generator function?
|
|
34
|
-
*
|
|
35
|
-
* @param value Value to check
|
|
36
|
-
* @returns `true` if the value is an asynchronous generator function, otherwise `false`
|
|
37
|
-
*/
|
|
38
|
-
export declare function isAsyncGenerator(value: unknown): value is () => AsyncGenerator;
|
|
39
|
-
/**
|
|
40
|
-
* Is the value a generator function?
|
|
41
|
-
*
|
|
42
|
-
* @param value Value to check
|
|
43
|
-
* @returns `true` if the value is a generator function, otherwise `false`
|
|
44
|
-
*/
|
|
45
|
-
export declare function isGenerator(value: unknown): value is () => Generator;
|
|
46
|
-
/**
|
|
47
|
-
* Is the value a plan?
|
|
48
|
-
*
|
|
49
|
-
* @param value Value to check
|
|
50
|
-
* @returns `true` if the value is a plan, otherwise `false`
|
|
51
|
-
*/
|
|
52
|
-
export declare function isPlan<Value = unknown, Error = unknown>(value: unknown): value is Plan<Value, Error>;
|
|
53
|
-
/**
|
|
54
|
-
* Create a plan for a generator function
|
|
55
|
-
*
|
|
56
|
-
* @param generator Generator to plan for
|
|
57
|
-
* @returns Generator plan
|
|
58
|
-
*/
|
|
59
|
-
export declare function plan<Value = unknown, Error = unknown>(generator: () => Generator<unknown, Value, Error>): Plan<Value, Error>;
|
|
30
|
+
export declare function run<Yielded, Returned>(plan: AsyncPlan<Yielded, Returned>): Promise<PlanResult<Returned> | PlanError<Yielded, Returned>>;
|
|
60
31
|
/**
|
|
61
32
|
* Run a generator to completion
|
|
62
33
|
*
|
|
63
34
|
* @param generator Generator to run
|
|
64
35
|
* @returns Result
|
|
65
36
|
*/
|
|
66
|
-
export declare function run<
|
|
37
|
+
export declare function run<Yielded, Returned>(generator: () => Generator<Yielded, Returned>): PlanResult<Returned> | PlanError<Yielded, Returned>;
|
|
67
38
|
/**
|
|
68
39
|
* Run a plan to completion
|
|
69
40
|
*
|
|
70
41
|
* @param plan Plan to run
|
|
71
42
|
* @returns Result
|
|
72
43
|
*/
|
|
73
|
-
export declare function run<
|
|
74
|
-
export declare namespace plan {
|
|
75
|
-
export var async: typeof asyncPlan;
|
|
76
|
-
}
|
|
77
|
-
export declare namespace run {
|
|
78
|
-
export var async: typeof asyncRun;
|
|
79
|
-
}
|
|
44
|
+
export declare function run<Yielded, Returned>(plan: Plan<Yielded, Returned>): PlanResult<Returned> | PlanError<Yielded, Returned>;
|
|
80
45
|
//#endregion
|
package/dist/plan/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { asyncAttempt, attempt } from "../internal/result/attempt.mjs";
|
|
2
1
|
import { PLAN_MESSAGE_PLAN_INPUT, PLAN_MESSAGE_RUN_INPUT, PLAN_SYMBOL, PLAN_TYPE_PLAN_ASYNC, PLAN_TYPE_PLAN_SYNC } from "../internal/models/plan.model.mjs";
|
|
2
|
+
import { isAsyncGenerator, isAsyncPlan, isGenerator, isPlan } from "../internal/plan.mjs";
|
|
3
3
|
//#region src/plan/index.ts
|
|
4
4
|
function AsyncPlan(generator) {
|
|
5
5
|
this[PLAN_SYMBOL] = {
|
|
@@ -7,6 +7,9 @@ function AsyncPlan(generator) {
|
|
|
7
7
|
type: PLAN_TYPE_PLAN_ASYNC
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
|
+
AsyncPlan.prototype[Symbol.asyncIterator] = function() {
|
|
11
|
+
return this[PLAN_SYMBOL].generator();
|
|
12
|
+
};
|
|
10
13
|
AsyncPlan.prototype.run = runAsyncPlan;
|
|
11
14
|
function Plan(generator) {
|
|
12
15
|
this[PLAN_SYMBOL] = {
|
|
@@ -14,105 +17,64 @@ function Plan(generator) {
|
|
|
14
17
|
type: PLAN_TYPE_PLAN_SYNC
|
|
15
18
|
};
|
|
16
19
|
}
|
|
20
|
+
Plan.prototype[Symbol.iterator] = function() {
|
|
21
|
+
return this[PLAN_SYMBOL].generator();
|
|
22
|
+
};
|
|
17
23
|
Plan.prototype.run = runPlan;
|
|
18
|
-
function asyncExecute(callback) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
24
|
+
async function asyncExecute(callback) {
|
|
25
|
+
const generator = callback();
|
|
26
|
+
let success = true;
|
|
27
|
+
let lastValue;
|
|
28
|
+
try {
|
|
22
29
|
while (true) {
|
|
23
|
-
const next = await generator.next();
|
|
30
|
+
const next = await generator.next(lastValue);
|
|
24
31
|
const { done } = next;
|
|
25
|
-
let value = next
|
|
32
|
+
let { value } = next;
|
|
26
33
|
if (value instanceof Error) throw value;
|
|
27
34
|
lastValue = value;
|
|
28
35
|
if (done === true) break;
|
|
29
36
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
*/
|
|
39
|
-
function asyncPlan(generator) {
|
|
40
|
-
if (!isAsyncGenerator(generator)) throw new Error(PLAN_MESSAGE_PLAN_INPUT);
|
|
41
|
-
return new AsyncPlan(generator);
|
|
42
|
-
}
|
|
43
|
-
async function asyncRun(input) {
|
|
44
|
-
if (isAsyncPlan(input)) return input.run();
|
|
45
|
-
if (!isAsyncGenerator(input)) throw new Error(PLAN_MESSAGE_RUN_INPUT);
|
|
46
|
-
return asyncExecute(input);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
lastValue = error;
|
|
39
|
+
success = false;
|
|
40
|
+
} finally {
|
|
41
|
+
generator?.return(lastValue);
|
|
42
|
+
}
|
|
43
|
+
if (success) return lastValue;
|
|
44
|
+
throw lastValue;
|
|
47
45
|
}
|
|
48
46
|
function execute(callback) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
47
|
+
const generator = callback();
|
|
48
|
+
let success = true;
|
|
49
|
+
let lastValue;
|
|
50
|
+
try {
|
|
52
51
|
while (true) {
|
|
53
|
-
const next = generator.next();
|
|
54
|
-
const
|
|
52
|
+
const next = generator.next(lastValue);
|
|
53
|
+
const done = next.done === true;
|
|
55
54
|
let { value } = next;
|
|
56
55
|
if (value instanceof Error) throw value;
|
|
57
56
|
lastValue = value;
|
|
58
|
-
if (done
|
|
57
|
+
if (done) break;
|
|
59
58
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
*/
|
|
69
|
-
function isAsyncPlan(value) {
|
|
70
|
-
return isPlanInstance(PLAN_TYPE_PLAN_ASYNC, value);
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Is the value an asynchronous generator function?
|
|
74
|
-
*
|
|
75
|
-
* @param value Value to check
|
|
76
|
-
* @returns `true` if the value is an asynchronous generator function, otherwise `false`
|
|
77
|
-
*/
|
|
78
|
-
function isAsyncGenerator(value) {
|
|
79
|
-
return typeof value === "function" && value.constructor.name === "AsyncGeneratorFunction" && typeof value.prototype.next === "function" && typeof value.prototype.return === "function" && typeof value.prototype.throw === "function";
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Is the value a generator function?
|
|
83
|
-
*
|
|
84
|
-
* @param value Value to check
|
|
85
|
-
* @returns `true` if the value is a generator function, otherwise `false`
|
|
86
|
-
*/
|
|
87
|
-
function isGenerator(value) {
|
|
88
|
-
return typeof value === "function" && value.constructor.name === "GeneratorFunction" && typeof value.prototype.next === "function" && typeof value.prototype.return === "function" && typeof value.prototype.throw === "function";
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Is the value a plan?
|
|
92
|
-
*
|
|
93
|
-
* @param value Value to check
|
|
94
|
-
* @returns `true` if the value is a plan, otherwise `false`
|
|
95
|
-
*/
|
|
96
|
-
function isPlan(value) {
|
|
97
|
-
return isPlanInstance(PLAN_TYPE_PLAN_SYNC, value);
|
|
98
|
-
}
|
|
99
|
-
function isPlanInstance(type, value) {
|
|
100
|
-
return typeof value === "object" && value !== null && PLAN_SYMBOL in value && value[PLAN_SYMBOL].type === type;
|
|
59
|
+
} catch (error) {
|
|
60
|
+
lastValue = error;
|
|
61
|
+
success = false;
|
|
62
|
+
} finally {
|
|
63
|
+
generator.return(lastValue);
|
|
64
|
+
}
|
|
65
|
+
if (success) return lastValue;
|
|
66
|
+
throw lastValue;
|
|
101
67
|
}
|
|
102
|
-
/**
|
|
103
|
-
* Create a plan for a generator function
|
|
104
|
-
*
|
|
105
|
-
* @param generator Generator to plan for
|
|
106
|
-
* @returns Generator plan
|
|
107
|
-
*/
|
|
108
68
|
function plan(generator) {
|
|
109
|
-
if (
|
|
110
|
-
return new Plan(generator);
|
|
69
|
+
if (isAsyncGenerator(generator)) return new AsyncPlan(generator);
|
|
70
|
+
if (isGenerator(generator)) return new Plan(generator);
|
|
71
|
+
throw new Error(PLAN_MESSAGE_PLAN_INPUT);
|
|
111
72
|
}
|
|
112
73
|
function run(input) {
|
|
113
|
-
if (isPlan(input)) return input.run();
|
|
114
|
-
if (
|
|
115
|
-
return execute(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);
|
|
116
78
|
}
|
|
117
79
|
function runAsyncPlan() {
|
|
118
80
|
return asyncExecute(this[PLAN_SYMBOL].generator);
|
|
@@ -120,7 +82,5 @@ function runAsyncPlan() {
|
|
|
120
82
|
function runPlan() {
|
|
121
83
|
return execute(this[PLAN_SYMBOL].generator);
|
|
122
84
|
}
|
|
123
|
-
plan.async = asyncPlan;
|
|
124
|
-
run.async = asyncRun;
|
|
125
85
|
//#endregion
|
|
126
|
-
export {
|
|
86
|
+
export { plan, run };
|
package/dist/plan/is.mjs
ADDED
package/dist/plan/models.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Plan } from "../internal/models/plan.model.mjs";
|
|
2
|
-
export type { Plan };
|
|
1
|
+
import { AsyncPlan, Plan } from "../internal/models/plan.model.mjs";
|
|
2
|
+
export type { AsyncPlan, Plan };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oscarpalmer/atoms",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.210.0",
|
|
4
4
|
"description": "Atomic utilities for making your JavaScript better.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"array",
|
|
@@ -153,6 +153,14 @@
|
|
|
153
153
|
"types": "./dist/plan/index.d.mts",
|
|
154
154
|
"default": "./dist/plan/index.mjs"
|
|
155
155
|
},
|
|
156
|
+
"./plan/is": {
|
|
157
|
+
"types": "./dist/plan/is.d.mts",
|
|
158
|
+
"default": "./dist/plan/is.mjs"
|
|
159
|
+
},
|
|
160
|
+
"./plan/misc": {
|
|
161
|
+
"types": "./dist/plan/misc.d.mts",
|
|
162
|
+
"default": "./dist/plan/misc.mjs"
|
|
163
|
+
},
|
|
156
164
|
"./plan/models": {
|
|
157
165
|
"default": "./dist/plan/models.mjs"
|
|
158
166
|
},
|
|
@@ -297,7 +305,7 @@
|
|
|
297
305
|
"devDependencies": {
|
|
298
306
|
"@types/node": "^26.6",
|
|
299
307
|
"@vitest/coverage-istanbul": "5.0.1",
|
|
300
|
-
"eslint": "^10.
|
|
308
|
+
"eslint": "^10.12",
|
|
301
309
|
"jsdom": "^30.1",
|
|
302
310
|
"tsdown": "^0.23",
|
|
303
311
|
"typescript": "^7",
|