Skip to content

Commit 6805670

Browse files
committed
Add more support for AbortSignal
1 parent eae8806 commit 6805670

File tree

7 files changed

+49
-17
lines changed

7 files changed

+49
-17
lines changed

library/src/actions/checkItems/checkItemsAsync.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ export function checkItemsAsync(
7878
async '~run'(dataset, config) {
7979
if (dataset.typed) {
8080
const requirementResults = await Promise.all(
81-
dataset.value.map(this.requirement)
81+
dataset.value.map((...args) =>
82+
this.requirement(...args, config.signal)
83+
)
8284
);
8385
for (let index = 0; index < dataset.value.length; index++) {
8486
if (!requirementResults[index]) {

library/src/actions/partialCheck/partialCheckAsync.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ export interface PartialCheckActionAsync<
4242
/**
4343
* The validation function.
4444
*/
45-
readonly requirement: (input: TSelection) => MaybePromise<boolean>;
45+
readonly requirement: (
46+
input: TSelection,
47+
signal?: AbortSignal
48+
) => MaybePromise<boolean>;
4649
/**
4750
* The error message.
4851
*/
@@ -63,7 +66,10 @@ export function partialCheckAsync<
6366
const TSelection extends DeepPickN<TInput, TPaths>,
6467
>(
6568
paths: ValidPaths<TInput, TPaths>,
66-
requirement: (input: TSelection) => MaybePromise<boolean>
69+
requirement: (
70+
input: TSelection,
71+
signal?: AbortSignal
72+
) => MaybePromise<boolean>
6773
): PartialCheckActionAsync<TInput, TPaths, TSelection, undefined>;
6874

6975
/**
@@ -84,14 +90,20 @@ export function partialCheckAsync<
8490
| undefined,
8591
>(
8692
paths: ValidPaths<TInput, TPaths>,
87-
requirement: (input: TSelection) => MaybePromise<boolean>,
93+
requirement: (
94+
input: TSelection,
95+
signal?: AbortSignal
96+
) => MaybePromise<boolean>,
8897
message: TMessage
8998
): PartialCheckActionAsync<TInput, TPaths, TSelection, TMessage>;
9099

91100
// @__NO_SIDE_EFFECTS__
92101
export function partialCheckAsync(
93102
paths: Paths,
94-
requirement: (input: PartialInput) => MaybePromise<boolean>,
103+
requirement: (
104+
input: PartialInput,
105+
signal?: AbortSignal
106+
) => MaybePromise<boolean>,
95107
message?: ErrorMessage<PartialCheckIssue<PartialInput>>
96108
): PartialCheckActionAsync<
97109
PartialInput,
@@ -112,7 +124,7 @@ export function partialCheckAsync(
112124
if (
113125
(dataset.typed || _isPartiallyTyped(dataset, paths)) &&
114126
// @ts-expect-error
115-
!(await this.requirement(dataset.value))
127+
!(await this.requirement(dataset.value, config.signal))
116128
) {
117129
_addIssue(this, 'input', dataset, config);
118130
}

library/src/actions/transform/transformAsync.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface TransformActionAsync<TInput, TOutput>
1919
/**
2020
* The transformation operation.
2121
*/
22-
readonly operation: (input: TInput) => Promise<TOutput>;
22+
readonly operation: (input: TInput, signal?: AbortSignal) => Promise<TOutput>;
2323
}
2424

2525
/**
@@ -31,17 +31,17 @@ export interface TransformActionAsync<TInput, TOutput>
3131
*/
3232
// @__NO_SIDE_EFFECTS__
3333
export function transformAsync<TInput, TOutput>(
34-
operation: (input: TInput) => Promise<TOutput>
34+
operation: (input: TInput, signal?: AbortSignal) => Promise<TOutput>
3535
): TransformActionAsync<TInput, TOutput> {
3636
return {
3737
kind: 'transformation',
3838
type: 'transform',
3939
reference: transformAsync,
4040
async: true,
4141
operation,
42-
async '~run'(dataset) {
42+
async '~run'(dataset, config) {
4343
// @ts-expect-error
44-
dataset.value = await this.operation(dataset.value);
44+
dataset.value = await this.operation(dataset.value, config.signal);
4545
// @ts-expect-error
4646
return dataset as SuccessDataset<TOutput>;
4747
},

library/src/actions/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export type ArrayRequirement<TInput extends ArrayInput> = (
2020
export type ArrayRequirementAsync<TInput extends ArrayInput> = (
2121
item: TInput[number],
2222
index: number,
23-
array: TInput
23+
array: TInput,
24+
signal?: AbortSignal
2425
) => MaybePromise<boolean>;
2526

2627
/**

library/src/schemas/custom/customAsync.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import type { CustomIssue } from './types.ts';
1111
/**
1212
* Check async type.
1313
*/
14-
type CheckAsync = (input: unknown) => MaybePromise<boolean>;
14+
type CheckAsync = (
15+
input: unknown,
16+
signal?: AbortSignal
17+
) => MaybePromise<boolean>;
1518

1619
/**
1720
* Custom schema async interface.
@@ -85,7 +88,7 @@ export function customAsync<TInput>(
8588
return _getStandardProps(this);
8689
},
8790
async '~run'(dataset, config) {
88-
if (await this.check(dataset.value)) {
91+
if (await this.check(dataset.value, config.signal)) {
8992
// @ts-expect-error
9093
dataset.typed = true;
9194
} else {

library/src/schemas/lazy/lazyAsync.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ describe('lazyAsync', () => {
9999
const getter = vi.fn(() => string());
100100
const dataset = { value: 'foo' };
101101
lazyAsync(getter)['~run'](dataset, {});
102-
expect(getter).toHaveBeenCalledWith(dataset.value);
102+
expect(getter).toHaveBeenCalledWith(dataset.value, undefined);
103+
});
104+
105+
test('should call getter with signal', () => {
106+
const getter = vi.fn(() => string());
107+
const dataset = { value: 'foo' };
108+
const signal = AbortSignal.abort();
109+
lazyAsync(getter)['~run'](dataset, { signal });
110+
expect(getter).toHaveBeenCalledWith(dataset.value, signal);
103111
});
104112
});

library/src/schemas/lazy/lazyAsync.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export interface LazySchemaAsync<
3737
/**
3838
* The schema getter.
3939
*/
40-
readonly getter: (input: unknown) => MaybePromise<TWrapped>;
40+
readonly getter: (
41+
input: unknown,
42+
signal?: AbortSignal
43+
) => MaybePromise<TWrapped>;
4144
}
4245

4346
/**
@@ -53,7 +56,7 @@ export function lazyAsync<
5356
| BaseSchema<unknown, unknown, BaseIssue<unknown>>
5457
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>,
5558
>(
56-
getter: (input: unknown) => MaybePromise<TWrapped>
59+
getter: (input: unknown, signal?: AbortSignal) => MaybePromise<TWrapped>
5760
): LazySchemaAsync<TWrapped> {
5861
return {
5962
kind: 'schema',
@@ -66,7 +69,10 @@ export function lazyAsync<
6669
return _getStandardProps(this);
6770
},
6871
async '~run'(dataset, config) {
69-
return (await this.getter(dataset.value))['~run'](dataset, config);
72+
return (await this.getter(dataset.value, config.signal))['~run'](
73+
dataset,
74+
config
75+
);
7076
},
7177
};
7278
}

0 commit comments

Comments
 (0)