From 9ecccc795716a0de406b424b01db8f56ae47ea77 Mon Sep 17 00:00:00 2001 From: Jonas Zeltner Date: Tue, 21 Jan 2025 14:26:53 +0100 Subject: [PATCH] refractor: Hooks recieve one single parameter with properties --- libs/execution/src/lib/execution-context.ts | 21 +++--- libs/execution/src/lib/hooks/hook-context.ts | 73 ++++++-------------- libs/execution/src/lib/hooks/hook.ts | 23 +++--- libs/interpreter-lib/src/interpreter.spec.ts | 6 +- 4 files changed, 50 insertions(+), 73 deletions(-) diff --git a/libs/execution/src/lib/execution-context.ts b/libs/execution/src/lib/execution-context.ts index 5463ea62..3855aeaa 100644 --- a/libs/execution/src/lib/execution-context.ts +++ b/libs/execution/src/lib/execution-context.ts @@ -155,15 +155,20 @@ export class ExecutionContext { ); if (output === undefined) { - return this.hookContext.executePreBlockHooks(blocktype, input, this); + return this.hookContext.executePreBlockHooks({ + blocktype, + input, + context: this, + }); + // eslint-disable-next-line no-else-return + } else { + return this.hookContext.executePostBlockHooks({ + blocktype, + input, + output, + context: this, + }); } - - return this.hookContext.executePostBlockHooks( - blocktype, - input, - this, - output, - ); } private getDefaultPropertyValue( diff --git a/libs/execution/src/lib/hooks/hook-context.ts b/libs/execution/src/lib/hooks/hook-context.ts index 1e05b20c..60d5dcda 100644 --- a/libs/execution/src/lib/hooks/hook-context.ts +++ b/libs/execution/src/lib/hooks/hook-context.ts @@ -2,15 +2,13 @@ // // 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 PostBlockHookArgs, type PreBlockHook, + type PreBlockHookArgs, isPreBlockHook, } from './hook'; @@ -26,16 +24,14 @@ function noop() {} async function executePreBlockHooks( hooks: HookSpec[], - blocktype: string, - input: IOTypeImplementation | null, - context: ExecutionContext, + args: PreBlockHookArgs, ) { await Promise.all( hooks.map(async ({ blocking, hook }) => { if (blocking) { - await hook(blocktype, input, context); + await hook(args); } else { - hook(blocktype, input, context).catch(noop); + hook(args).catch(noop); } }), ); @@ -43,17 +39,14 @@ async function executePreBlockHooks( async function executePostBlockHooks( hooks: HookSpec[], - blocktype: string, - input: IOTypeImplementation | null, - context: ExecutionContext, - output: Result, + args: PostBlockHookArgs, ) { await Promise.all( hooks.map(async ({ blocking, hook }) => { if (blocking) { - await hook(blocktype, input, output, context); + await hook(args); } else { - hook(blocktype, input, output, context).catch(noop); + hook(args).catch(noop); } }), ); @@ -106,54 +99,32 @@ export class HookContext { } } - public async executePreBlockHooks( - blocktype: string, - input: IOTypeImplementation | null, - context: ExecutionContext, - ) { - context.logger.logDebug(`Executing general pre-block-hooks`); - const general = executePreBlockHooks( - this.hooks.pre[AllBlocks] ?? [], - blocktype, - input, - context, - ); - context.logger.logDebug( - `Executing pre-block-hooks for blocktype ${blocktype}`, + public async executePreBlockHooks(args: PreBlockHookArgs) { + args.context.logger.logDebug(`Executing general pre-block-hooks`); + const general = executePreBlockHooks(this.hooks.pre[AllBlocks] ?? [], args); + args.context.logger.logDebug( + `Executing pre-block-hooks for blocktype ${args.blocktype}`, ); const blockSpecific = executePreBlockHooks( - this.hooks.pre[blocktype] ?? [], - blocktype, - input, - context, + this.hooks.pre[args.blocktype] ?? [], + args, ); await Promise.all([general, blockSpecific]); } - public async executePostBlockHooks( - blocktype: string, - input: IOTypeImplementation | null, - context: ExecutionContext, - output: Result, - ) { - context.logger.logDebug(`Executing general post-block-hooks`); + public async executePostBlockHooks(args: PostBlockHookArgs) { + args.context.logger.logDebug(`Executing general post-block-hooks`); const general = executePostBlockHooks( this.hooks.post[AllBlocks] ?? [], - blocktype, - input, - context, - output, + args, ); - context.logger.logDebug( - `Executing post-block-hooks for blocktype ${blocktype}`, + args.context.logger.logDebug( + `Executing post-block-hooks for blocktype ${args.blocktype}`, ); const blockSpecific = executePostBlockHooks( - this.hooks.post[blocktype] ?? [], - blocktype, - input, - context, - output, + this.hooks.post[args.blocktype] ?? [], + args, ); await Promise.all([general, blockSpecific]); diff --git a/libs/execution/src/lib/hooks/hook.ts b/libs/execution/src/lib/hooks/hook.ts index 64803658..23568e5a 100644 --- a/libs/execution/src/lib/hooks/hook.ts +++ b/libs/execution/src/lib/hooks/hook.ts @@ -16,12 +16,14 @@ export interface HookOptions { blocktypes?: string[]; } +export interface PreBlockHookArgs { + blocktype: string; + input: IOTypeImplementation | null; + context: ExecutionContext; +} + /** This function will be executed before a block.*/ -export type PreBlockHook = ( - blocktype: string, - input: IOTypeImplementation | null, - context: ExecutionContext, -) => Promise; +export type PreBlockHook = (args: PreBlockHookArgs) => Promise; export function isPreBlockHook( hook: PreBlockHook | PostBlockHook, @@ -30,13 +32,12 @@ export function isPreBlockHook( return position === 'preBlock'; } +export interface PostBlockHookArgs extends PreBlockHookArgs { + output: Result; +} + /** This function will be executed before a block.*/ -export type PostBlockHook = ( - blocktype: string, - input: IOTypeImplementation | null, - output: Result, - context: ExecutionContext, -) => Promise; +export type PostBlockHook = (args: PostBlockHookArgs) => Promise; export function isPostBlockHook( hook: PreBlockHook | PostBlockHook, diff --git a/libs/interpreter-lib/src/interpreter.spec.ts b/libs/interpreter-lib/src/interpreter.spec.ts index 401e30f4..faff5c8e 100644 --- a/libs/interpreter-lib/src/interpreter.spec.ts +++ b/libs/interpreter-lib/src/interpreter.spec.ts @@ -101,7 +101,7 @@ describe('Interpreter', () => { program.addHook( 'preBlock', - async (blocktype) => { + async ({ blocktype }) => { return sqlite_spy(blocktype); }, { blocking: true, blocktypes: ['SQLiteLoader'] }, @@ -113,7 +113,7 @@ describe('Interpreter', () => { program.addHook( 'postBlock', - async (blocktype) => { + async ({ blocktype }) => { return interpreter_spy(blocktype); }, { blocking: true, blocktypes: ['CSVFileInterpreter'] }, @@ -193,7 +193,7 @@ describe('Interpreter', () => { program.addHook( 'postBlock', - async (blocktype, input, output) => { + async ({ blocktype, input, output }) => { expect(blocktype).toBe('TableTransformer'); expect(input).not.toBeNull();