Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions docs/typescript-examples/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,97 @@ const block10 = de.func({
de.run(block10, {
params: {},
});

export type BlockResultOut<
Result = never,
Before = never,
After = never,
> =
[ After ] extends [ never ] ?
[ Before ] extends [ never ] ?
Result :
Before | Result :
After;

class Block<
ResultOut extends BlockResultOut<Result, Before, After>,
Result = never,
Before = never,
After = never,
> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
constructor(options: Options<Result, Before, After>) {

}

extend<
ResultOut2 extends BlockResultOut<BlockResult2, Before2, After2>,
BlockResult2 = ResultOut,
Before2 = never,
After2 = never,
>(options: Options<BlockResult2, Before2, After2>) {
return new Block<ResultOut2, BlockResult2, Before2, After2>(options);
};

run() {
return 1 as ResultOut;
}
}

export type InferResultOrResult<Result> = Result extends Block<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
infer ResultOut, infer After, infer Before, infer Result
> ? InferResultOrResult<ResultOut> : Result;

type Options<
Result = never,
Before = never,
After = never,
> = {
block?: () => Result;
before?: () => Before;
after?: (
res: [ Before ] extends [ never ] ?
InferResultOrResult<Result> : InferResultOrResult<Before> | InferResultOrResult<Result>
) => After;
};

const block = <
ResultOut extends BlockResultOut<Result, Before, After>,
Result = never,
Before = never,
After = never,
>(options: Options<Result, Before, After>) => {
return new Block<ResultOut, Result, Before, After>(options);
};

type Block11Result = {
data: { somefield: number } | null;
errors: Array<string> | null;
};

const b1 = block({
block: (): Block11Result => {
return { data: { somefield: 42 }, errors: null };
},

after: (res) => res,
});

const b2 = b1.extend({
after(res) {
return res.data?.somefield;
},
});

const b3 = b2.extend({
after(res) {
return res;
},
});

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const r1 = b3.run();

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type R = typeof r1; // оно должно стать number | undefined
12 changes: 6 additions & 6 deletions lib/arrayBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
0: never;
1: [ InferResultFromBlock<First<T>> | DescriptError ];
2: [ InferResultFromBlock<First<T>> | DescriptError, ...GetArrayBlockResult<Tail<T>> ];
}[ T extends [] ? 0 : T extends ((readonly [ any ]) | [ any ]) ? 1 : 2 ];

Check warning on line 22 in lib/arrayBlock.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

Check warning on line 22 in lib/arrayBlock.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

export type GetArrayBlockParamsUnion<T extends ReadonlyArray<unknown>> = {
0: never;
1: First<T>;
2: First<T> & GetArrayBlockParamsUnion<Tail<T>>;
}[ T extends [] ? 0 : T extends ((readonly [ any ]) | [ any ]) ? 1 : 2 ];

Check warning on line 28 in lib/arrayBlock.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

Check warning on line 28 in lib/arrayBlock.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type

type GetArrayBlockParamsMap<T extends ReadonlyArray<unknown>> = {
[ P in keyof T ]: InferParamsInFromBlock<T[ P ]>;
Expand Down Expand Up @@ -53,9 +53,9 @@
ParamsOut = GetArrayBlockParams<Block>,
BlockResult = GetArrayBlockResult<Block>,

BeforeResultOut = undefined,
AfterResultOut = undefined,
ErrorResultOut = undefined,
BeforeResultOut = unknown,
AfterResultOut = unknown,
ErrorResultOut = unknown,
Params = GetArrayBlockParams<Block>,
> extends CompositeBlock<
Context,
Expand All @@ -78,9 +78,9 @@
ExtendedParamsOut extends Params = Params,
ExtendedParams = Params,
ExtendedBlockResult = ResultOut,
ExtendedBeforeResultOut = undefined,
ExtendedAfterResultOut = undefined,
ExtendedErrorResultOut = undefined,
ExtendedBeforeResultOut = unknown,
ExtendedAfterResultOut = unknown,
ExtendedErrorResultOut = unknown,
>({ options }: {
options: DescriptBlockOptions<
Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
Expand Down
70 changes: 13 additions & 57 deletions lib/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,16 @@
'before' | 'after' | 'error' | 'params' | 'deps'
>>;

interface BlockConstructor<
Context,
ParamsOut,
BlockResult,
BeforeResultOut,
AfterResultOut,
ErrorResultOut,
Params,
ClassType,
CustomBlock,
> {
new ({ block, options }: {
block?: CustomBlock;
options: DescriptBlockOptions<Context, ParamsOut, BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut, Params>;
}): ClassType;
}

abstract class BaseBlock<
Context,
CustomBlock,
ParamsOut,
ResultOut extends BlockResultOut<BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut>,
IntermediateResult,
BlockResult,
BeforeResultOut = undefined,
AfterResultOut = undefined,
ErrorResultOut = undefined,
BeforeResultOut = unknown,
AfterResultOut = unknown,
ErrorResultOut = unknown,
Params = ParamsOut,
> {
protected block: CustomBlock;
Expand All @@ -80,39 +63,6 @@
this.initOptions(options);
}

protected extendClass<
ClassType,
ExtendedBlockResult,
ExtendedParamsOut extends Params = Params,
ExtendedParams = Params,
ExtendedBeforeResultOut = void,
ExtendedAfterResultOut = void,
ExtendedErrorResultOut = void,
ExtendedCustomBlock extends CustomBlock = CustomBlock,
>({ block, options }: {
block?: ExtendedCustomBlock;
options?:
DescriptBlockOptions<
Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
>;
}): ClassType {

return new (<BlockConstructor<
Context,
ExtendedParamsOut,
ExtendedBlockResult,
ExtendedBeforeResultOut,
ExtendedAfterResultOut,
ExtendedErrorResultOut,
ExtendedParams,
ClassType,
ExtendedCustomBlock
>> this.constructor)({
block: this.extendBlock(block) as ExtendedCustomBlock,
options: this.extendOptions(this.options, options),
});
}

abstract extend<

// ExtendedResultOut extends
Expand All @@ -121,13 +71,19 @@
ExtendedParams = Params,
// ExtendedCustomBlock = CustomBlock,
ExtendedBlockResult = ResultOut,
ExtendedBeforeResultOut = void,
ExtendedAfterResultOut = void,
ExtendedErrorResultOut = void,
ExtendedBeforeResultOut = unknown,
ExtendedAfterResultOut = unknown,
ExtendedErrorResultOut = unknown,
>({ block, options }: {
block?: CustomBlock;
options?: DescriptBlockOptions<
Context, ParamsOut & ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
Context,
ParamsOut & ExtendedParamsOut,
ExtendedBlockResult,
ExtendedBeforeResultOut,
ExtendedAfterResultOut,
ExtendedErrorResultOut,
ExtendedParams
>;
}): unknown;

Expand Down Expand Up @@ -482,7 +438,7 @@
blockCancel.throwIfCancelled();

if (typeof step.after === 'function') {
resultAfter = await step.after({ cancel, params, context, deps, result: (resultBefore || resultBlock) as any });

Check warning on line 441 in lib/block.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type
blockCancel.throwIfCancelled();

if (resultAfter instanceof BaseBlock) {
Expand Down
6 changes: 3 additions & 3 deletions lib/compositeBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ abstract class CompositeBlock<
IntermediateResult,
BlockResultInt,

BeforeResultOut = undefined,
AfterResultOut = undefined,
ErrorResultOut = undefined,
BeforeResultOut = unknown,
AfterResultOut = unknown,
ErrorResultOut = unknown,
Params = ParamsOut,
> extends BaseBlock<
Context,
Expand Down
12 changes: 6 additions & 6 deletions lib/firstBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ class FirstBlock<
ParamsOut = GetFirstBlockParams<Block>,
BlockResult = GetFirstBlockResult<Block>,

BeforeResultOut = undefined,
AfterResultOut = undefined,
ErrorResultOut = undefined,
BeforeResultOut = unknown,
AfterResultOut = unknown,
ErrorResultOut = unknown,
Params = GetFirstBlockParams<Block>,
> extends CompositeBlock<
Context,
Expand All @@ -90,9 +90,9 @@ class FirstBlock<
ExtendedParamsOut extends Params = Params,
ExtendedParams = Params,
ExtendedBlockResult = ResultOut,
ExtendedBeforeResultOut = undefined,
ExtendedAfterResultOut = undefined,
ExtendedErrorResultOut = undefined,
ExtendedBeforeResultOut = unknown,
ExtendedAfterResultOut = unknown,
ExtendedErrorResultOut = unknown,
>({ options }: {
options: DescriptBlockOptions<
Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
Expand Down
20 changes: 13 additions & 7 deletions lib/functionBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class FunctionBlock<
ParamsOut,
BlockResult,
ResultOut extends BlockResultOut<BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut>,
BeforeResultOut = undefined,
AfterResultOut = undefined,
ErrorResultOut = undefined,
BeforeResultOut = unknown,
AfterResultOut = unknown,
ErrorResultOut = unknown,
Params = never extends InferParamsOutFromBlock<BlockResult> ? ParamsOut : InferParamsOutFromBlock<BlockResult>,
> extends BaseBlock<
Context,
Expand Down Expand Up @@ -99,12 +99,18 @@ class FunctionBlock<
// ExtendedCustomBlock = DescriptHttpBlockDescription<ExtendedParamsOut, Context, HttpResult>,

ExtendedBlockResult = ResultOut,
ExtendedBeforeResultOut = undefined,
ExtendedAfterResultOut = undefined,
ExtendedErrorResultOut = undefined,
ExtendedBeforeResultOut = unknown,
ExtendedAfterResultOut = unknown,
ExtendedErrorResultOut = unknown,
>({ options }: {
options: DescriptBlockOptions<
Context, ExtendedParamsOut, ExtendedBlockResult, ExtendedBeforeResultOut, ExtendedAfterResultOut, ExtendedErrorResultOut, ExtendedParams
Context,
ExtendedParamsOut,
ExtendedBlockResult,
ExtendedBeforeResultOut,
ExtendedAfterResultOut,
ExtendedErrorResultOut,
ExtendedParams
>;
}) {
return new FunctionBlock({
Expand Down
12 changes: 6 additions & 6 deletions lib/httpBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ class HttpBlock<
HttpResult,
ResultOut extends BlockResultOut<BlockResult, BeforeResultOut, AfterResultOut, ErrorResultOut>,
BlockResult = DescriptHttpBlockResult<HttpResult>,
BeforeResultOut = undefined,
AfterResultOut = undefined,
ErrorResultOut = undefined,
BeforeResultOut = unknown,
AfterResultOut = unknown,
ErrorResultOut = unknown,
Params = ParamsOut,
> extends Block<
Context,
Expand All @@ -205,9 +205,9 @@ class HttpBlock<
// ExtendedCustomBlock = DescriptHttpBlockDescription<ExtendedParamsOut, Context, HttpResult>,

ExtendedBlockResult = ResultOut,
ExtendedBeforeResultOut = undefined,
ExtendedAfterResultOut = undefined,
ExtendedErrorResultOut = undefined,
ExtendedBeforeResultOut = unknown,
ExtendedAfterResultOut = unknown,
ExtendedErrorResultOut = unknown,
>({ options, block }: {
block?: DescriptHttpBlockDescription<ParamsOut & ExtendedParamsOut, Context, HttpResult>;
options?: DescriptBlockOptions<
Expand Down
Loading
Loading