Skip to content

Commit fbc0d5d

Browse files
committed
chore: add control exceptions
1 parent 1f33649 commit fbc0d5d

File tree

5 files changed

+110
-3
lines changed

5 files changed

+110
-3
lines changed

src/nodes/Array.FindWhere.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type R = unknown;
1515

1616
export const module: ModuleDefinition<P, R> = {
1717
moduleName: 'Array / Find Where',
18-
version: '1.1.1',
18+
version: '1.1.2',
1919
description: `
2020
Returns items from the array that match specified conditions.
2121
`,

src/nodes/Flow.Catch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type R = Promise<{
1010
}>;
1111

1212
export const module: ModuleDefinition<P, R> = {
13-
version: '1.1.1',
13+
version: '1.1.2',
1414
moduleName: 'Flow / Catch',
1515
description: `
1616
Computes the value and catches the error it produces.

src/nodes/Flow.Continue.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { SYM_SKIPPED } from '@nodescript/core/runtime';
2+
import { ModuleCompute, ModuleDefinition } from '@nodescript/core/types';
3+
4+
type P = {
5+
value: unknown;
6+
skipped: unknown;
7+
token: string;
8+
};
9+
10+
type R = Promise<unknown>;
11+
12+
export const module: ModuleDefinition<P, R> = {
13+
version: '1.0.1',
14+
moduleName: 'Flow / Continue',
15+
description: `
16+
Evaluates and returns "skipped" if the evaluation of "value"
17+
was previously skipped with Flow / Skip.
18+
Otherwise, returns "value" and does not evaluate "skipped".
19+
If "token" is set, it must match the "token" used on Flow / Skip.
20+
`,
21+
params: {
22+
value: {
23+
deferred: true,
24+
schema: { type: 'any' },
25+
},
26+
skipped: {
27+
deferred: true,
28+
schema: { type: 'any' },
29+
},
30+
token: {
31+
schema: { type: 'string', default: '' },
32+
advanced: true,
33+
},
34+
},
35+
result: {
36+
async: true,
37+
schema: { type: 'any' },
38+
}
39+
};
40+
41+
export const compute: ModuleCompute<P, R> = async (params, ctx) => {
42+
const { value, skipped, token } = params;
43+
try {
44+
return await ctx.resolveDeferred(value);
45+
} catch (error: any) {
46+
if (error && error[SYM_SKIPPED]) {
47+
if (token === '' || error.token === token) {
48+
return await ctx.resolveDeferred(skipped);
49+
}
50+
}
51+
throw error;
52+
}
53+
};

src/nodes/Flow.Skip.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { ModuleCompute, ModuleDefinition } from '@nodescript/core/types';
2+
3+
type P = {
4+
condition: boolean;
5+
value: unknown;
6+
token: string;
7+
message: string;
8+
status: number;
9+
};
10+
11+
type R = Promise<unknown>;
12+
13+
export const module: ModuleDefinition<P, R> = {
14+
version: '1.0.1',
15+
moduleName: 'Flow / Skip',
16+
description: `
17+
Skips the evaluation if condition is true, otherwise returns the value.
18+
Skipped evaluation results in a control exception (by default with 204 status).
19+
The flow can subsequently be resumed with Flow / Continue.
20+
`,
21+
params: {
22+
condition: {
23+
schema: { type: 'boolean' },
24+
},
25+
value: {
26+
schema: { type: 'any' },
27+
deferred: true,
28+
},
29+
token: {
30+
schema: { type: 'string', default: '' },
31+
advanced: true,
32+
},
33+
message: {
34+
schema: { type: 'string', default: 'Evaluation skipped' },
35+
advanced: true,
36+
},
37+
status: {
38+
schema: { type: 'number', default: 204 },
39+
advanced: true,
40+
},
41+
},
42+
result: {
43+
async: true,
44+
schema: { type: 'any' },
45+
}
46+
};
47+
48+
export const compute: ModuleCompute<P, R> = async (params, ctx) => {
49+
const { condition, value, token, message, status } = params;
50+
if (condition) {
51+
return ctx.skipEvaluation(message, token, status);
52+
}
53+
return await ctx.resolveDeferred(value);
54+
};

src/nodes/Flow.Try.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type P = {
77
type R = Promise<unknown>;
88

99
export const module: ModuleDefinition<P, R> = {
10-
version: '1.2.0',
10+
version: '1.2.1',
1111
moduleName: 'Flow / Try',
1212
description: `Runs the steps one-by-one and returns the first result that doesn't throw an error.`,
1313
params: {

0 commit comments

Comments
 (0)