Skip to content

Commit

Permalink
refractor: Hooks recieve one single parameter with properties
Browse files Browse the repository at this point in the history
  • Loading branch information
TungstnBallon committed Jan 21, 2025
1 parent df34ae9 commit 44b3c32
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 73 deletions.
21 changes: 13 additions & 8 deletions libs/execution/src/lib/execution-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<I extends InternalValueRepresentation>(
Expand Down
73 changes: 22 additions & 51 deletions libs/execution/src/lib/hooks/hook-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -26,34 +24,29 @@ function noop() {}

async function executePreBlockHooks(
hooks: HookSpec<PreBlockHook>[],
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);
}
}),
);
}

async function executePostBlockHooks(
hooks: HookSpec<PostBlockHook>[],
blocktype: string,
input: IOTypeImplementation | null,
context: ExecutionContext,
output: Result<IOTypeImplementation | null>,
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);
}
}),
);
Expand Down Expand Up @@ -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<IOTypeImplementation | null>,
) {
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]);
Expand Down
23 changes: 12 additions & 11 deletions libs/execution/src/lib/hooks/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
export type PreBlockHook = (args: PreBlockHookArgs) => Promise<void>;

export function isPreBlockHook(
hook: PreBlockHook | PostBlockHook,
Expand All @@ -30,13 +32,12 @@ export function isPreBlockHook(
return position === 'preBlock';
}

export interface PostBlockHookArgs extends PreBlockHookArgs {
output: Result<IOTypeImplementation | null>;
}

/** 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 type PostBlockHook = (args: PostBlockHookArgs) => Promise<void>;

export function isPostBlockHook(
hook: PreBlockHook | PostBlockHook,
Expand Down
6 changes: 3 additions & 3 deletions libs/interpreter-lib/src/interpreter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('Interpreter', () => {

program.addHook(
'preBlock',
async (blocktype) => {
async ({ blocktype }) => {
return sqlite_spy(blocktype);
},
{ blocking: true, blocktypes: ['SQLiteLoader'] },
Expand All @@ -113,7 +113,7 @@ describe('Interpreter', () => {

program.addHook(
'postBlock',
async (blocktype) => {
async ({ blocktype }) => {
return interpreter_spy(blocktype);
},
{ blocking: true, blocktypes: ['CSVFileInterpreter'] },
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 44b3c32

Please sign in to comment.