-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #635 from jvalue/hooks
[FEATURE] Add custom hook support
- Loading branch information
Showing
10 changed files
with
604 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// SPDX-FileCopyrightText: 2025 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import { type Result } from '../blocks'; | ||
import { type ExecutionContext } from '../execution-context'; | ||
import { type IOTypeImplementation } from '../types'; | ||
|
||
import { | ||
type HookOptions, | ||
type HookPosition, | ||
type PostBlockHook, | ||
type PreBlockHook, | ||
isPreBlockHook, | ||
} from './hook'; | ||
|
||
const AllBlocks = '*'; | ||
|
||
interface HookSpec<H extends PreBlockHook | PostBlockHook> { | ||
blocking: boolean; | ||
hook: H; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
function noop() {} | ||
|
||
async function executePreBlockHooks( | ||
hooks: HookSpec<PreBlockHook>[], | ||
blocktype: string, | ||
input: IOTypeImplementation | null, | ||
context: ExecutionContext, | ||
) { | ||
await Promise.all( | ||
hooks.map(async ({ blocking, hook }) => { | ||
if (blocking) { | ||
await hook(blocktype, input, context); | ||
} else { | ||
hook(blocktype, input, context).catch(noop); | ||
} | ||
}), | ||
); | ||
} | ||
|
||
async function executePostBlockHooks( | ||
hooks: HookSpec<PostBlockHook>[], | ||
blocktype: string, | ||
input: IOTypeImplementation | null, | ||
context: ExecutionContext, | ||
output: Result<IOTypeImplementation | null>, | ||
) { | ||
await Promise.all( | ||
hooks.map(async ({ blocking, hook }) => { | ||
if (blocking) { | ||
await hook(blocktype, input, output, context); | ||
} else { | ||
hook(blocktype, input, output, context).catch(noop); | ||
} | ||
}), | ||
); | ||
} | ||
|
||
export class HookContext { | ||
private hooks: { | ||
pre: Record<string, HookSpec<PreBlockHook>[]>; | ||
post: Record<string, HookSpec<PostBlockHook>[]>; | ||
} = { pre: {}, post: {} }; | ||
|
||
public addHook( | ||
position: 'preBlock', | ||
hook: PreBlockHook, | ||
opts: HookOptions, | ||
): void; | ||
public addHook( | ||
position: 'postBlock', | ||
hook: PostBlockHook, | ||
opts: HookOptions, | ||
): void; | ||
public addHook( | ||
position: HookPosition, | ||
hook: PreBlockHook | PostBlockHook, | ||
opts: HookOptions, | ||
): void; | ||
public addHook( | ||
position: HookPosition, | ||
hook: PreBlockHook | PostBlockHook, | ||
opts: HookOptions, | ||
) { | ||
for (const blocktype of opts.blocktypes ?? [AllBlocks]) { | ||
if (isPreBlockHook(hook, position)) { | ||
if (this.hooks.pre[blocktype] === undefined) { | ||
this.hooks.pre[blocktype] = []; | ||
} | ||
this.hooks.pre[blocktype].push({ | ||
blocking: opts.blocking ?? false, | ||
hook, | ||
}); | ||
} else { | ||
if (this.hooks.post[blocktype] === undefined) { | ||
this.hooks.post[blocktype] = []; | ||
} | ||
this.hooks.post[blocktype].push({ | ||
blocking: opts.blocking ?? false, | ||
hook, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
public async executePreBlockHooks( | ||
blocktype: string, | ||
input: IOTypeImplementation | null, | ||
context: ExecutionContext, | ||
) { | ||
context.logger.logInfo(`Executing general pre-block-hooks`); | ||
const general = executePreBlockHooks( | ||
this.hooks.pre[AllBlocks] ?? [], | ||
blocktype, | ||
input, | ||
context, | ||
); | ||
context.logger.logInfo( | ||
`Executing pre-block-hooks for blocktype ${blocktype}`, | ||
); | ||
const blockSpecific = executePreBlockHooks( | ||
this.hooks.pre[blocktype] ?? [], | ||
blocktype, | ||
input, | ||
context, | ||
); | ||
|
||
await Promise.all([general, blockSpecific]); | ||
} | ||
|
||
public async executePostBlockHooks( | ||
blocktype: string, | ||
input: IOTypeImplementation | null, | ||
context: ExecutionContext, | ||
output: Result<IOTypeImplementation | null>, | ||
) { | ||
context.logger.logInfo(`Executing general post-block-hooks`); | ||
const general = executePostBlockHooks( | ||
this.hooks.post[AllBlocks] ?? [], | ||
blocktype, | ||
input, | ||
context, | ||
output, | ||
); | ||
context.logger.logInfo( | ||
`Executing post-block-hooks for blocktype ${blocktype}`, | ||
); | ||
const blockSpecific = executePostBlockHooks( | ||
this.hooks.post[blocktype] ?? [], | ||
blocktype, | ||
input, | ||
context, | ||
output, | ||
); | ||
|
||
await Promise.all([general, blockSpecific]); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// SPDX-FileCopyrightText: 2025 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
import { type Result } from '../blocks'; | ||
import { type ExecutionContext } from '../execution-context'; | ||
import { type IOTypeImplementation } from '../types'; | ||
|
||
/** When to execute the hook.*/ | ||
export type HookPosition = 'preBlock' | 'postBlock'; | ||
|
||
export interface HookOptions { | ||
/** Whether the pipeline should await the hooks completion. `false` if omitted.*/ | ||
blocking?: boolean; | ||
/** Optionally specify one or more blocks to limit this hook to. If omitted, the hook will be executed on all blocks*/ | ||
blocktypes?: string[]; | ||
} | ||
|
||
/** This function will be executed before a block.*/ | ||
export type PreBlockHook = ( | ||
blocktype: string, | ||
input: IOTypeImplementation | null, | ||
context: ExecutionContext, | ||
) => Promise<void>; | ||
|
||
export function isPreBlockHook( | ||
hook: PreBlockHook | PostBlockHook, | ||
position: HookPosition, | ||
): hook is PreBlockHook { | ||
return position === 'preBlock'; | ||
} | ||
|
||
/** This function will be executed before a block.*/ | ||
export type PostBlockHook = ( | ||
blocktype: string, | ||
input: IOTypeImplementation | null, | ||
output: Result<IOTypeImplementation | null>, | ||
context: ExecutionContext, | ||
) => Promise<void>; | ||
|
||
export function isPostBlockHook( | ||
hook: PreBlockHook | PostBlockHook, | ||
position: HookPosition, | ||
): hook is PostBlockHook { | ||
return position === 'postBlock'; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// SPDX-FileCopyrightText: 2025 Friedrich-Alexander-Universitat Erlangen-Nurnberg | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
export * from './hook'; | ||
export * from './hook-context'; |
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
Oops, something went wrong.