-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Fully featured types for logic processors
- Loading branch information
1 parent
8481547
commit 57e1e6d
Showing
9 changed files
with
116 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import { logic } from '@do-ob/logic/logic'; | ||
export * from '@do-ob/logic/process'; | ||
export * from '@do-ob/logic/logic'; | ||
|
||
export { | ||
logic | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,63 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import type { Action, ActionResult, Output, Context, ActionModule, Input } from '@do-ob/core/io'; | ||
|
||
import type { Action, Context, ActionModule, Input, Output } from '@do-ob/core'; | ||
import { outputify } from '@do-ob/core/io'; | ||
// import { OutputStatus, OutputFailureType } from '@do-ob/core/io'; | ||
// export type Process<C extends Context, A extends Action<string, unknown> = Action<string, unknown>> = (context: C, input: Input<A>) => Promise<Output>; | ||
|
||
// export type ProcessHandlers = Record<string, Process<Context>>; | ||
|
||
export type ProcessHandler< | ||
M extends ActionModule, | ||
C extends Context, | ||
R | ||
> = (input: Input<ReturnType<M['act']>>, adapter: C['adapter']) => Promise<Output<R>>; | ||
|
||
export interface Process< | ||
K extends string = string, | ||
// C extends Context = Context, | ||
C extends Context = Context, | ||
M extends ActionModule = ActionModule, | ||
R = unknown | ||
> = (input: Input<ReturnType<M['act']>>, adapter: C['adapter']) => Promise<R>; | ||
|
||
export interface Process< | ||
L extends [ActionModule, any][] = [ActionModule, any][], | ||
> { | ||
key: K; | ||
_infer_output: Record<M['type'], Output<R>>; | ||
execute: <A extends Action<string, unknown>>(input: Input<A>) => Promise<A['type'] extends M['type'] ? Output<ActionResult<A>> : undefined>; | ||
_: { | ||
output: { [ K in L[number][0]['type']]: Output<Awaited< | ||
ReturnType<Extract<L[number], [{ type: K }, ProcessHandler ]>[1]> | ||
>> }; | ||
} | ||
execute: <A extends Action<string, unknown>>(input: Input<A>) => Promise<A['type'] extends L[number][0]['type'] ? | ||
Output<Awaited<ReturnType<Extract<L[number], [{ type: A['type'] }, ProcessHandler ]>[1]>>> : | ||
undefined | ||
>; | ||
} | ||
|
||
/** | ||
* Creates a new logic processing function to handle actions and return results. | ||
*/ | ||
export function processify< | ||
K extends string, | ||
C extends Context, | ||
M extends ActionModule, | ||
R | ||
H extends ProcessHandler<C>, | ||
L extends [ActionModule, H][], | ||
>( | ||
/** | ||
* The key name of the process. | ||
* This is used to identify the process in the logic system. | ||
*/ | ||
key: K, | ||
context: C, | ||
...handler: [M, ProcessHandler<M, C, R>][] | ||
): Process<K, M, R> { | ||
const handlers: Record<M['type'], ProcessHandler<M, C, R>> = handler.reduce((acc, [ module, handler ]) => { | ||
acc[module.type as M['type']] = handler; | ||
...handler: L | ||
): Process<L> { | ||
const handlers: Record<string, ProcessHandler<C>> = handler.reduce((acc, [ module, handle ]) => { | ||
acc[module.type] = handle; | ||
return acc; | ||
}, {} as Record<M['type'], ProcessHandler<M, C, R>>); | ||
}, {} as Record<string, ProcessHandler<C>>); | ||
|
||
return { | ||
key, | ||
execute: async < | ||
A extends Action<string, unknown>, | ||
>(input: Input<A>): Promise<A['type'] extends M['type'] ? Output<ActionResult<A>> : undefined> => { | ||
const handler = handlers[input.action.type as M['type']]; | ||
const process: Process<L> = { | ||
execute: async (input) => { | ||
const handler = handlers[input.action.type]; | ||
if (!handler) { | ||
return undefined as any; | ||
} | ||
|
||
return handler(input as Input<ReturnType<M['act']>>, context.adapter) as Promise<Output<ActionResult<A>>> as any; | ||
const payload = await handler(input, context.adapter); | ||
|
||
return outputify({ | ||
status: 1, | ||
payload, | ||
}) as any; | ||
}, | ||
} as Process<K, M, R>; | ||
} as Process<L>; | ||
|
||
return process; | ||
|
||
} |