Skip to content

Commit

Permalink
chore(release): v0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
MichielvdVelde committed Sep 18, 2023
1 parent 3df6922 commit b804474
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@wecandobetter/propeller",
"description": "Propeller is a hook-based extension library.",
"version": "0.0.1",
"version": "0.0.2",
"type": "module",
"license": "MIT",
"main": "dist/index.js",
Expand Down
15 changes: 15 additions & 0 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ export function createHookCollection<

return collection;
},
unregister: (name, ...hooks) => {
const existing = map.get(name as string);

if (existing) {
for (const hook of hooks) {
const index = existing.indexOf(hook as Hook<unknown>);

if (index !== -1) {
existing.splice(index, 1);
}
}
}

return collection;
},
execute: async (name, ctx) => {
const hooks = map.get(name as string);

Expand Down
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ export interface HookCollection<Hooks extends Record<string, unknown>> {
...hooks: Hook<Hooks[K]>[]
) => HookCollection<Hooks>;

/**
* Unregister a hook.
* @param name The name of the hook
* @param hooks The hooks to unregister
* @returns The updated hook collection.
*/
unregister: <K extends keyof Hooks>(
name: K,
...hooks: Hook<Hooks[K]>[]
) => HookCollection<Hooks>;

/**
* Execute a hook. Hooks are executed in the order they are registered. The function returns
* the updated context, which may have been modified or rerefenced by the hooks.
Expand Down
26 changes: 26 additions & 0 deletions tests/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ describe("Hook System", () => {
expect(updatedContext.b).toBe(99);
});

it("should unregister a hook", async () => {
// Register the "beforeExecute" hook
hooks.register("beforeExecute", beforeExecuteHook);

// Create an initial context
const context: BeforeExecuteContext = { a: 10 };

// Execute the "beforeExecute" hook
const updatedContext = await hooks.execute("beforeExecute", context);

// Assert that the hook modified and returned the context
expect(updatedContext.a).toBe(42);

// Unregister the "beforeExecute" hook
hooks.unregister("beforeExecute", beforeExecuteHook);

// Create a new initial context
const context2: BeforeExecuteContext = { a: 10 };

// Execute the "beforeExecute" hook again
const updatedContext2 = await hooks.execute("beforeExecute", context2);

// Assert that the hook did not modify the context
expect(updatedContext2.a).toBe(10);
});

it("should execute multiple hooks in the specified order, returning context", async () => {
// Register multiple "beforeExecute" hooks
hooks.register("beforeExecute", beforeExecuteHook);
Expand Down

0 comments on commit b804474

Please sign in to comment.