@duplojs/utils 1.5.15 → 1.6.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/clean/primitive/operations/equal.d.ts +3 -3
- package/dist/common/kind.cjs +2 -1
- package/dist/common/kind.d.ts +1 -0
- package/dist/common/kind.mjs +2 -1
- package/dist/flow/breakIf.cjs +14 -0
- package/dist/flow/breakIf.d.ts +50 -0
- package/dist/flow/breakIf.mjs +12 -0
- package/dist/flow/defer.cjs +12 -0
- package/dist/flow/defer.d.ts +47 -0
- package/dist/flow/defer.mjs +10 -0
- package/dist/flow/exec.cjs +125 -0
- package/dist/flow/exec.d.ts +83 -0
- package/dist/flow/exec.mjs +123 -0
- package/dist/flow/exitIf.cjs +14 -0
- package/dist/flow/exitIf.d.ts +67 -0
- package/dist/flow/exitIf.mjs +12 -0
- package/dist/flow/finalizer.cjs +12 -0
- package/dist/flow/finalizer.d.ts +47 -0
- package/dist/flow/finalizer.mjs +10 -0
- package/dist/flow/index.cjs +50 -0
- package/dist/flow/index.d.ts +12 -0
- package/dist/flow/index.mjs +18 -0
- package/dist/flow/initializer.cjs +38 -0
- package/dist/flow/initializer.d.ts +75 -0
- package/dist/flow/initializer.mjs +36 -0
- package/dist/flow/inject.cjs +19 -0
- package/dist/flow/inject.d.ts +46 -0
- package/dist/flow/inject.mjs +17 -0
- package/dist/flow/kind.cjs +9 -0
- package/dist/flow/kind.d.ts +1 -0
- package/dist/flow/kind.mjs +7 -0
- package/dist/flow/run.cjs +139 -0
- package/dist/flow/run.d.ts +80 -0
- package/dist/flow/run.mjs +136 -0
- package/dist/flow/step.cjs +23 -0
- package/dist/flow/step.d.ts +39 -0
- package/dist/flow/step.mjs +21 -0
- package/dist/flow/theFlow/break.cjs +11 -0
- package/dist/flow/theFlow/break.d.ts +7 -0
- package/dist/flow/theFlow/break.mjs +8 -0
- package/dist/flow/theFlow/defer.cjs +11 -0
- package/dist/flow/theFlow/defer.d.ts +5 -0
- package/dist/flow/theFlow/defer.mjs +8 -0
- package/dist/flow/theFlow/dependence.cjs +17 -0
- package/dist/flow/theFlow/dependence.d.ts +50 -0
- package/dist/flow/theFlow/dependence.mjs +14 -0
- package/dist/flow/theFlow/exit.cjs +11 -0
- package/dist/flow/theFlow/exit.d.ts +7 -0
- package/dist/flow/theFlow/exit.mjs +8 -0
- package/dist/flow/theFlow/finalizer.cjs +11 -0
- package/dist/flow/theFlow/finalizer.d.ts +5 -0
- package/dist/flow/theFlow/finalizer.mjs +8 -0
- package/dist/flow/theFlow/index.cjs +14 -0
- package/dist/flow/theFlow/index.d.ts +85 -0
- package/dist/flow/theFlow/index.mjs +11 -0
- package/dist/flow/theFlow/injection.cjs +11 -0
- package/dist/flow/theFlow/injection.d.ts +10 -0
- package/dist/flow/theFlow/injection.mjs +8 -0
- package/dist/flow/theFlow/step.cjs +11 -0
- package/dist/flow/theFlow/step.d.ts +5 -0
- package/dist/flow/theFlow/step.mjs +8 -0
- package/dist/flow/types/index.d.ts +1 -0
- package/dist/index.cjs +3 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +3 -0
- package/dist/metadata.json +189 -0
- package/dist/object/types/requireAtLeastOne.d.ts +2 -1
- package/package.json +6 -1
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { theFlowKind } from './theFlow/index.mjs';
|
|
2
|
+
import { deferKind } from './theFlow/defer.mjs';
|
|
3
|
+
import { finalizerKind } from './theFlow/finalizer.mjs';
|
|
4
|
+
import { createFlowKind } from './kind.mjs';
|
|
5
|
+
import { dependenceHandlerKind } from './theFlow/dependence.mjs';
|
|
6
|
+
import { breakKind } from './theFlow/break.mjs';
|
|
7
|
+
import { exitKind } from './theFlow/exit.mjs';
|
|
8
|
+
import { stepKind } from './theFlow/step.mjs';
|
|
9
|
+
import { injectionKind } from './theFlow/injection.mjs';
|
|
10
|
+
import { justExec } from '../common/justExec.mjs';
|
|
11
|
+
import { kindHeritage } from '../common/kind.mjs';
|
|
12
|
+
|
|
13
|
+
class MissingDependenceError extends kindHeritage("missing-dependence-error", createFlowKind("missing-dependence-error"), Error) {
|
|
14
|
+
dependenceHandler;
|
|
15
|
+
constructor(dependenceHandler) {
|
|
16
|
+
super({}, [`Missing dependence : ${dependenceHandlerKind.getValue(dependenceHandler)}`]);
|
|
17
|
+
this.dependenceHandler = dependenceHandler;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* {@include flow/run/index.md}
|
|
22
|
+
*/
|
|
23
|
+
function run(theFlow, ...[params]) {
|
|
24
|
+
let result = undefined;
|
|
25
|
+
let deferFunctions = undefined;
|
|
26
|
+
let steps = undefined;
|
|
27
|
+
const generator = typeof theFlow === "function"
|
|
28
|
+
? theFlow(params?.input)
|
|
29
|
+
: theFlowKind.getValue(theFlow).run(params?.input);
|
|
30
|
+
if (Symbol.asyncIterator in generator) {
|
|
31
|
+
return (async function () {
|
|
32
|
+
try {
|
|
33
|
+
do {
|
|
34
|
+
result = await generator.next();
|
|
35
|
+
if (result.done === true) {
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
else if (breakKind.has(result.value)) {
|
|
39
|
+
result = await generator.return(breakKind.getValue(result.value).value);
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
else if (exitKind.has(result.value)) {
|
|
43
|
+
result = await generator.return(exitKind.getValue(result.value).value);
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
else if (deferKind.has(result.value)) {
|
|
47
|
+
deferFunctions ??= [];
|
|
48
|
+
deferFunctions.push(deferKind.getValue(result.value));
|
|
49
|
+
}
|
|
50
|
+
else if (finalizerKind.has(result.value)) {
|
|
51
|
+
deferFunctions ??= [];
|
|
52
|
+
deferFunctions.push(finalizerKind.getValue(result.value));
|
|
53
|
+
}
|
|
54
|
+
else if (params?.includeDetails === true
|
|
55
|
+
&& stepKind.has(result.value)) {
|
|
56
|
+
steps ??= [];
|
|
57
|
+
steps.push(stepKind.getValue(result.value));
|
|
58
|
+
}
|
|
59
|
+
else if (injectionKind.has(result.value)) {
|
|
60
|
+
const injectionProperties = injectionKind.getValue(result.value);
|
|
61
|
+
const dependenceName = dependenceHandlerKind.getValue(injectionProperties.dependenceHandler);
|
|
62
|
+
if (!params?.dependencies
|
|
63
|
+
|| !(dependenceName in params.dependencies)) {
|
|
64
|
+
throw new MissingDependenceError(injectionProperties.dependenceHandler);
|
|
65
|
+
}
|
|
66
|
+
injectionProperties.inject(params.dependencies[dependenceName]);
|
|
67
|
+
}
|
|
68
|
+
} while (true);
|
|
69
|
+
return params?.includeDetails === true
|
|
70
|
+
? {
|
|
71
|
+
result: result.value,
|
|
72
|
+
steps: steps ?? [],
|
|
73
|
+
}
|
|
74
|
+
: result.value;
|
|
75
|
+
}
|
|
76
|
+
finally {
|
|
77
|
+
await generator.return(undefined);
|
|
78
|
+
if (deferFunctions) {
|
|
79
|
+
await Promise.all(deferFunctions.map(justExec));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
})();
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
do {
|
|
86
|
+
result = generator.next();
|
|
87
|
+
if (result.done === true) {
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
else if (breakKind.has(result.value)) {
|
|
91
|
+
result = generator.return(breakKind.getValue(result.value).value);
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
else if (exitKind.has(result.value)) {
|
|
95
|
+
result = generator.return(exitKind.getValue(result.value).value);
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
else if (deferKind.has(result.value)) {
|
|
99
|
+
deferFunctions ??= [];
|
|
100
|
+
deferFunctions.push(deferKind.getValue(result.value));
|
|
101
|
+
}
|
|
102
|
+
else if (finalizerKind.has(result.value)) {
|
|
103
|
+
deferFunctions ??= [];
|
|
104
|
+
deferFunctions.push(finalizerKind.getValue(result.value));
|
|
105
|
+
}
|
|
106
|
+
else if (params?.includeDetails === true
|
|
107
|
+
&& stepKind.has(result.value)) {
|
|
108
|
+
steps ??= [];
|
|
109
|
+
steps.push(stepKind.getValue(result.value));
|
|
110
|
+
}
|
|
111
|
+
else if (injectionKind.has(result.value)) {
|
|
112
|
+
const injectionProperties = injectionKind.getValue(result.value);
|
|
113
|
+
const dependenceName = dependenceHandlerKind.getValue(injectionProperties.dependenceHandler);
|
|
114
|
+
if (!params?.dependencies
|
|
115
|
+
|| !(dependenceName in params.dependencies)) {
|
|
116
|
+
throw new MissingDependenceError(injectionProperties.dependenceHandler);
|
|
117
|
+
}
|
|
118
|
+
injectionProperties.inject(params.dependencies[dependenceName]);
|
|
119
|
+
}
|
|
120
|
+
} while (true);
|
|
121
|
+
return (params?.includeDetails === true
|
|
122
|
+
? {
|
|
123
|
+
result: result.value,
|
|
124
|
+
steps: steps ?? [],
|
|
125
|
+
}
|
|
126
|
+
: result.value);
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
generator.return(undefined);
|
|
130
|
+
if (deferFunctions) {
|
|
131
|
+
deferFunctions.map(justExec);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export { MissingDependenceError, run };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var step$1 = require('./theFlow/step.cjs');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* {@include flow/step/index.md}
|
|
7
|
+
*/
|
|
8
|
+
function step(name, theFunction) {
|
|
9
|
+
const result = theFunction?.();
|
|
10
|
+
if (result instanceof Promise) {
|
|
11
|
+
return (async function* () {
|
|
12
|
+
yield step$1.createStep(name);
|
|
13
|
+
const awaitedResult = await result;
|
|
14
|
+
return awaitedResult;
|
|
15
|
+
})();
|
|
16
|
+
}
|
|
17
|
+
return (function* () {
|
|
18
|
+
yield step$1.createStep(name);
|
|
19
|
+
return result;
|
|
20
|
+
})();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
exports.step = step;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type Step } from "./theFlow";
|
|
2
|
+
/**
|
|
3
|
+
* Registers a named step in a flow and can optionally compute a value.
|
|
4
|
+
*
|
|
5
|
+
* **Supported call styles:**
|
|
6
|
+
* - Classic without callback: `step(name)` -> yields a step effect and returns `undefined`
|
|
7
|
+
* - Classic with callback: `step(name, theFunction)` -> yields a step effect and returns the callback result
|
|
8
|
+
*
|
|
9
|
+
* `step` records a named execution step that can be collected by `F.run(...)` when `includeDetails` is enabled.
|
|
10
|
+
* It can also wrap a synchronous or asynchronous callback and return its result while still emitting the step.
|
|
11
|
+
* Use it to make a flow easier to observe without changing its control flow.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* F.run(
|
|
15
|
+
* function *() {
|
|
16
|
+
* yield *F.step("load config");
|
|
17
|
+
* return "done";
|
|
18
|
+
* },
|
|
19
|
+
* { includeDetails: true },
|
|
20
|
+
* ); // { result: "done", steps: ["load config"] }
|
|
21
|
+
*
|
|
22
|
+
* F.run(
|
|
23
|
+
* function *() {
|
|
24
|
+
* const user = yield *F.step("read cache", () => "user-1");
|
|
25
|
+
* return user;
|
|
26
|
+
* },
|
|
27
|
+
* ); // "user-1"
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @remarks
|
|
31
|
+
* - Steps are only collected in the final result when `includeDetails` is enabled
|
|
32
|
+
*
|
|
33
|
+
* @see [`F.run`](https://utils.duplojs.dev/en/v1/api/flow/run) For collecting step details
|
|
34
|
+
* @see https://utils.duplojs.dev/en/v1/api/flow/step
|
|
35
|
+
*
|
|
36
|
+
* @namespace F
|
|
37
|
+
*
|
|
38
|
+
*/
|
|
39
|
+
export declare function step<GenericName extends string, GenericOutput extends unknown = void>(name: GenericName, theFunction?: () => GenericOutput): (GenericOutput extends Promise<unknown> ? AsyncGenerator<Step<GenericName>, Awaited<GenericOutput>> : Generator<Step<GenericName>, GenericOutput>);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { createStep } from './theFlow/step.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* {@include flow/step/index.md}
|
|
5
|
+
*/
|
|
6
|
+
function step(name, theFunction) {
|
|
7
|
+
const result = theFunction?.();
|
|
8
|
+
if (result instanceof Promise) {
|
|
9
|
+
return (async function* () {
|
|
10
|
+
yield createStep(name);
|
|
11
|
+
const awaitedResult = await result;
|
|
12
|
+
return awaitedResult;
|
|
13
|
+
})();
|
|
14
|
+
}
|
|
15
|
+
return (function* () {
|
|
16
|
+
yield createStep(name);
|
|
17
|
+
return result;
|
|
18
|
+
})();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { step };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var kind = require('../kind.cjs');
|
|
4
|
+
|
|
5
|
+
const breakKind = kind.createFlowKind("break");
|
|
6
|
+
function createBreak(value = undefined) {
|
|
7
|
+
return breakKind.setTo({}, { value });
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
exports.breakKind = breakKind;
|
|
11
|
+
exports.createBreak = createBreak;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Kind } from "../../common";
|
|
2
|
+
export declare const breakKind: import("../../common").KindHandler<import("../../common").KindDefinition<"@DuplojsUtilsFlow/break", unknown>>;
|
|
3
|
+
export interface Break<GenericValue extends unknown = unknown> extends Kind<typeof breakKind.definition, {
|
|
4
|
+
value: GenericValue;
|
|
5
|
+
}> {
|
|
6
|
+
}
|
|
7
|
+
export declare function createBreak<GenericValue extends unknown = undefined>(value?: GenericValue): Break<GenericValue>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type AnyFunction, type Kind } from "../../common";
|
|
2
|
+
export declare const deferKind: import("../../common").KindHandler<import("../../common").KindDefinition<"@DuplojsUtilsFlow/defer", AnyFunction<[], unknown>>>;
|
|
3
|
+
export interface Defer<GenericValue extends unknown = unknown> extends Kind<typeof deferKind.definition, () => GenericValue> {
|
|
4
|
+
}
|
|
5
|
+
export declare function createDefer<GenericOutput extends unknown>(value: () => GenericOutput): Defer<GenericOutput>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var kind = require('../kind.cjs');
|
|
4
|
+
|
|
5
|
+
const dependenceHandlerKind = kind.createFlowKind("dependence-handler");
|
|
6
|
+
/**
|
|
7
|
+
* {@include flow/createDependence/index.md}
|
|
8
|
+
*/
|
|
9
|
+
function createDependence(name) {
|
|
10
|
+
const dependenceHandler = function (implementation) {
|
|
11
|
+
return implementation;
|
|
12
|
+
};
|
|
13
|
+
return dependenceHandlerKind.setTo(dependenceHandler, name);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
exports.createDependence = createDependence;
|
|
17
|
+
exports.dependenceHandlerKind = dependenceHandlerKind;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { type Kind } from "../../common";
|
|
2
|
+
export declare const dependenceHandlerKind: import("../../common").KindHandler<import("../../common").KindDefinition<"@DuplojsUtilsFlow/dependence-handler", string>>;
|
|
3
|
+
export interface DependenceHandlerKind<GenericName extends string = string> extends Kind<typeof dependenceHandlerKind.definition, GenericName> {
|
|
4
|
+
}
|
|
5
|
+
export type DependenceHandler<GenericName extends string = string, GenericType extends any = any> = (DependenceHandlerKind<GenericName> & ((implementation: GenericType) => GenericType));
|
|
6
|
+
export type DependenceHandlerDefinition<GenericName extends string = string> = (DependenceHandlerKind<GenericName> & (<GenericType extends unknown>(implementation: GenericType) => GenericType));
|
|
7
|
+
/**
|
|
8
|
+
* Creates a typed dependency handler for the flow system.
|
|
9
|
+
*
|
|
10
|
+
* **Supported call styles:**
|
|
11
|
+
* - Classic: `createDependence(name)` -> returns a typed dependence handler definition
|
|
12
|
+
*
|
|
13
|
+
* `createDependence` creates a dependency descriptor identified by a string name.
|
|
14
|
+
* The returned handler is used with `inject(...)` and lets `run(...)` or `exec(...)` map a dependency bag to strongly typed values.
|
|
15
|
+
* At runtime, the handler also behaves like an identity function for the injected implementation.
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* const database = F.createDependence("database")<string>;
|
|
19
|
+
*
|
|
20
|
+
* F.run(
|
|
21
|
+
* function *() {
|
|
22
|
+
* const connection = yield *F.inject(database);
|
|
23
|
+
* return connection;
|
|
24
|
+
* },
|
|
25
|
+
* { dependencies: { database: database("main-db") } },
|
|
26
|
+
* ); // "main-db"
|
|
27
|
+
*
|
|
28
|
+
* const apiClient = F.createDependence("apiClient")<{ baseUrl: string }>;
|
|
29
|
+
*
|
|
30
|
+
* F.run(
|
|
31
|
+
* function *() {
|
|
32
|
+
* const client = yield *F.inject(apiClient);
|
|
33
|
+
* return client.baseUrl;
|
|
34
|
+
* },
|
|
35
|
+
* { dependencies: { apiClient: apiClient({ baseUrl: "/api" }) } },
|
|
36
|
+
* ); // "/api"
|
|
37
|
+
*
|
|
38
|
+
* database("replica-db"); // "replica-db"
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @remarks
|
|
42
|
+
* - Use the returned dependence handler together with `inject(...)`
|
|
43
|
+
*
|
|
44
|
+
* @see [`F.inject`](https://utils.duplojs.dev/en/v1/api/flow/inject) To request the dependency inside a flow
|
|
45
|
+
* @see https://utils.duplojs.dev/en/v1/api/flow/createDependence
|
|
46
|
+
*
|
|
47
|
+
* @namespace F
|
|
48
|
+
*
|
|
49
|
+
*/
|
|
50
|
+
export declare function createDependence<GenericName extends string>(name: GenericName): DependenceHandlerDefinition<GenericName>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createFlowKind } from '../kind.mjs';
|
|
2
|
+
|
|
3
|
+
const dependenceHandlerKind = createFlowKind("dependence-handler");
|
|
4
|
+
/**
|
|
5
|
+
* {@include flow/createDependence/index.md}
|
|
6
|
+
*/
|
|
7
|
+
function createDependence(name) {
|
|
8
|
+
const dependenceHandler = function (implementation) {
|
|
9
|
+
return implementation;
|
|
10
|
+
};
|
|
11
|
+
return dependenceHandlerKind.setTo(dependenceHandler, name);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { createDependence, dependenceHandlerKind };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var kind = require('../kind.cjs');
|
|
4
|
+
|
|
5
|
+
const exitKind = kind.createFlowKind("exit");
|
|
6
|
+
function createExit(value = undefined) {
|
|
7
|
+
return exitKind.setTo({}, { value });
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
exports.createExit = createExit;
|
|
11
|
+
exports.exitKind = exitKind;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Kind } from "../../common";
|
|
2
|
+
export declare const exitKind: import("../../common").KindHandler<import("../../common").KindDefinition<"@DuplojsUtilsFlow/exit", unknown>>;
|
|
3
|
+
export interface Exit<GenericValue extends unknown = unknown> extends Kind<typeof exitKind.definition, {
|
|
4
|
+
value: GenericValue;
|
|
5
|
+
}> {
|
|
6
|
+
}
|
|
7
|
+
export declare function createExit<GenericValue extends unknown = undefined>(value?: GenericValue): Exit<GenericValue>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var kind = require('../kind.cjs');
|
|
4
|
+
|
|
5
|
+
const finalizerKind = kind.createFlowKind("finalizer");
|
|
6
|
+
function createFinalizer(value) {
|
|
7
|
+
return finalizerKind.setTo({}, value);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
exports.createFinalizer = createFinalizer;
|
|
11
|
+
exports.finalizerKind = finalizerKind;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type AnyFunction, type Kind } from "../../common";
|
|
2
|
+
export declare const finalizerKind: import("../../common").KindHandler<import("../../common").KindDefinition<"@DuplojsUtilsFlow/finalizer", AnyFunction<[], unknown>>>;
|
|
3
|
+
export interface Finalizer<GenericValue extends unknown = unknown> extends Kind<typeof finalizerKind.definition, () => GenericValue> {
|
|
4
|
+
}
|
|
5
|
+
export declare function createFinalizer<GenericOutput extends unknown>(value: () => GenericOutput): Finalizer<GenericOutput>;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var kind = require('../kind.cjs');
|
|
4
|
+
|
|
5
|
+
const theFlowKind = kind.createFlowKind("the-flow");
|
|
6
|
+
/**
|
|
7
|
+
* {@include flow/create/index.md}
|
|
8
|
+
*/
|
|
9
|
+
function create(theFunction) {
|
|
10
|
+
return theFlowKind.setTo({}, { run: theFunction });
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
exports.create = create;
|
|
14
|
+
exports.theFlowKind = theFlowKind;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { type GetKindValue, type Kind } from "../../common";
|
|
2
|
+
import { type Step } from "./step";
|
|
3
|
+
import { type Exit } from "./exit";
|
|
4
|
+
import { type Injection } from "./injection";
|
|
5
|
+
import { type Break } from "./break";
|
|
6
|
+
import { type Defer } from "./defer";
|
|
7
|
+
import { type Finalizer } from "./finalizer";
|
|
8
|
+
import { type DependenceHandler, type dependenceHandlerKind } from "./dependence";
|
|
9
|
+
export * from "./step";
|
|
10
|
+
export * from "./exit";
|
|
11
|
+
export * from "./break";
|
|
12
|
+
export * from "./injection";
|
|
13
|
+
export * from "./defer";
|
|
14
|
+
export * from "./finalizer";
|
|
15
|
+
export * from "./dependence";
|
|
16
|
+
export type Effect = (Injection | Step | Exit | Break | Defer | Finalizer);
|
|
17
|
+
export type TheFlowGenerator<GenericOutput extends unknown = unknown, GenericEffect extends Effect = Effect> = (Generator<GenericEffect, GenericOutput> | AsyncGenerator<GenericEffect, GenericOutput>);
|
|
18
|
+
export type TheFlowFunction<GenericInput extends any = any, GenericGenerator extends TheFlowGenerator = TheFlowGenerator> = (input: GenericInput) => GenericGenerator;
|
|
19
|
+
export interface TheFlowProperties<GenericFunction extends TheFlowFunction = TheFlowFunction> {
|
|
20
|
+
run: GenericFunction;
|
|
21
|
+
}
|
|
22
|
+
export declare const theFlowKind: import("../../common").KindHandler<import("../../common").KindDefinition<"@DuplojsUtilsFlow/the-flow", TheFlowProperties<TheFlowFunction<any, TheFlowGenerator<unknown, Effect>>>>>;
|
|
23
|
+
export interface TheFlow<GenericFunction extends TheFlowFunction = TheFlowFunction> extends Kind<typeof theFlowKind.definition, TheFlowProperties<GenericFunction>> {
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Creates a reusable flow object from a flow function.
|
|
27
|
+
*
|
|
28
|
+
* **Supported call styles:**
|
|
29
|
+
* - Classic: `create(theFunction)` -> returns a flow instance that can be passed to `F.run(...)` or `F.exec(...)`
|
|
30
|
+
*
|
|
31
|
+
* `create` wraps a generator-based flow function into a flow object understood by the flow runtime.
|
|
32
|
+
* The returned flow can be executed multiple times with different inputs and can be composed with `F.exec(...)`.
|
|
33
|
+
* Use it to name, share, and reuse flow definitions without executing them immediately.
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* const greetingFlow = F.create(
|
|
37
|
+
* function *(name: string) {
|
|
38
|
+
* return `hello ${name}`;
|
|
39
|
+
* },
|
|
40
|
+
* );
|
|
41
|
+
*
|
|
42
|
+
* F.run(greetingFlow, { input: "Ada" }); // "hello Ada"
|
|
43
|
+
*
|
|
44
|
+
* const breakableFlow = F.create(
|
|
45
|
+
* function *(value: number) {
|
|
46
|
+
* yield *F.breakIf(value, (current) => current === 0);
|
|
47
|
+
* return value * 2;
|
|
48
|
+
* },
|
|
49
|
+
* );
|
|
50
|
+
*
|
|
51
|
+
* F.run(breakableFlow, { input: 0 }); // 0
|
|
52
|
+
*
|
|
53
|
+
* F.run(
|
|
54
|
+
* function *() {
|
|
55
|
+
* return yield *F.exec(greetingFlow, { input: "Linus" });
|
|
56
|
+
* },
|
|
57
|
+
* ); // "hello Linus"
|
|
58
|
+
*
|
|
59
|
+
* const asyncFlow = F.create(
|
|
60
|
+
* async function *(name: string) {
|
|
61
|
+
* const value = await name.toUpperCase();
|
|
62
|
+
* return value;
|
|
63
|
+
* },
|
|
64
|
+
* );
|
|
65
|
+
*
|
|
66
|
+
* await F.run(asyncFlow, { input: "flow" }); // Promise<"FLOW">
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* @remarks
|
|
70
|
+
* - `create` does not execute the flow, it only wraps it for later use
|
|
71
|
+
*
|
|
72
|
+
* @see [`F.run`](https://utils.duplojs.dev/en/v1/api/flow/run) To execute a created flow
|
|
73
|
+
* @see [`F.exec`](https://utils.duplojs.dev/en/v1/api/flow/exec) To execute a created flow inside another flow
|
|
74
|
+
* @see https://utils.duplojs.dev/en/v1/api/flow/create
|
|
75
|
+
*
|
|
76
|
+
* @namespace F
|
|
77
|
+
*
|
|
78
|
+
*/
|
|
79
|
+
export declare function create<GenericTheFlowFunction extends TheFlowFunction>(theFunction: GenericTheFlowFunction): TheFlow<GenericTheFlowFunction>;
|
|
80
|
+
export type FlowInput<GenericFlow extends TheFlow> = GenericFlow extends TheFlow<infer InferredFunction> ? InferredFunction extends TheFlowFunction<infer InferredInput> ? InferredInput : never : never;
|
|
81
|
+
export type WrapFlow<GenericFlow extends (TheFlow | TheFlowFunction | TheFlowGenerator)> = GenericFlow extends TheFlowGenerator ? TheFlow<TheFlowFunction<unknown, GenericFlow>> : GenericFlow extends TheFlowFunction ? TheFlow<GenericFlow> : GenericFlow;
|
|
82
|
+
export type FlowDependencies<GenericFlow extends TheFlow> = (ExtractFlowGenerator<GenericFlow> extends TheFlowGenerator<any, infer InferredEffect> ? InferredEffect extends Injection<infer InferredDependenceHandler> ? InferredDependenceHandler : never : never) extends infer InferredDependenceHandler extends DependenceHandler ? {
|
|
83
|
+
[Dependence in InferredDependenceHandler as Extract<GetKindValue<typeof dependenceHandlerKind, Dependence>, string>]: ReturnType<InferredDependenceHandler>;
|
|
84
|
+
} : never;
|
|
85
|
+
export type ExtractFlowGenerator<GenericFlow extends TheFlow> = GenericFlow extends TheFlow<infer InferredFunction> ? InferredFunction extends TheFlowFunction<any, infer InferredGenerator> ? InferredGenerator : never : never;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createFlowKind } from '../kind.mjs';
|
|
2
|
+
|
|
3
|
+
const theFlowKind = createFlowKind("the-flow");
|
|
4
|
+
/**
|
|
5
|
+
* {@include flow/create/index.md}
|
|
6
|
+
*/
|
|
7
|
+
function create(theFunction) {
|
|
8
|
+
return theFlowKind.setTo({}, { run: theFunction });
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { create, theFlowKind };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var kind = require('../kind.cjs');
|
|
4
|
+
|
|
5
|
+
const injectionKind = kind.createFlowKind("injection");
|
|
6
|
+
function createInjection(properties) {
|
|
7
|
+
return injectionKind.setTo({}, properties);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
exports.createInjection = createInjection;
|
|
11
|
+
exports.injectionKind = injectionKind;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Kind } from "../../common";
|
|
2
|
+
import { type DependenceHandler } from "./dependence";
|
|
3
|
+
export interface InjectionProperties<GenericDependenceHandler extends DependenceHandler = DependenceHandler> {
|
|
4
|
+
dependenceHandler: GenericDependenceHandler;
|
|
5
|
+
inject(value: ReturnType<GenericDependenceHandler>): void;
|
|
6
|
+
}
|
|
7
|
+
export declare const injectionKind: import("../../common").KindHandler<import("../../common").KindDefinition<"@DuplojsUtilsFlow/injection", InjectionProperties<DependenceHandler>>>;
|
|
8
|
+
export interface Injection<GenericDependenceHandler extends DependenceHandler = DependenceHandler> extends Kind<typeof injectionKind.definition, InjectionProperties<GenericDependenceHandler>> {
|
|
9
|
+
}
|
|
10
|
+
export declare function createInjection<GenericDependenceHandler extends DependenceHandler = DependenceHandler>(properties: InjectionProperties<GenericDependenceHandler>): Injection<GenericDependenceHandler>;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type Kind } from "../../common";
|
|
2
|
+
export declare const stepKind: import("../../common").KindHandler<import("../../common").KindDefinition<"@DuplojsUtilsFlow/step", string>>;
|
|
3
|
+
export interface Step<GenericName extends string = string> extends Kind<typeof stepKind.definition, GenericName> {
|
|
4
|
+
}
|
|
5
|
+
export declare function createStep<GenericName extends string>(name: GenericName): Step<GenericName>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.cjs
CHANGED
|
@@ -12,6 +12,7 @@ var index$8 = require('./dataParser/parsers/coerce/index.cjs');
|
|
|
12
12
|
var index$9 = require('./dataParser/extended/index.cjs');
|
|
13
13
|
var index$a = require('./date/index.cjs');
|
|
14
14
|
var index$b = require('./clean/index.cjs');
|
|
15
|
+
var index$c = require('./flow/index.cjs');
|
|
15
16
|
var addWrappedProperties = require('./common/addWrappedProperties.cjs');
|
|
16
17
|
var asyncPipe = require('./common/asyncPipe.cjs');
|
|
17
18
|
var clone = require('./common/clone.cjs');
|
|
@@ -93,6 +94,8 @@ exports.D = index$a;
|
|
|
93
94
|
exports.DDate = index$a;
|
|
94
95
|
exports.C = index$b;
|
|
95
96
|
exports.DClean = index$b;
|
|
97
|
+
exports.DFlow = index$c;
|
|
98
|
+
exports.F = index$c;
|
|
96
99
|
exports.addWrappedProperties = addWrappedProperties.addWrappedProperties;
|
|
97
100
|
exports.asyncPipe = asyncPipe.asyncPipe;
|
|
98
101
|
exports.clone = clone.clone;
|
package/dist/index.d.ts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -34,6 +34,9 @@ export { index$a as DDate };
|
|
|
34
34
|
import * as index$b from './clean/index.mjs';
|
|
35
35
|
export { index$b as C };
|
|
36
36
|
export { index$b as DClean };
|
|
37
|
+
import * as index$c from './flow/index.mjs';
|
|
38
|
+
export { index$c as DFlow };
|
|
39
|
+
export { index$c as F };
|
|
37
40
|
export { addWrappedProperties } from './common/addWrappedProperties.mjs';
|
|
38
41
|
export { asyncPipe } from './common/asyncPipe.mjs';
|
|
39
42
|
export { clone } from './common/clone.mjs';
|