From b8044745d910ac674467247cbb50a7a6eb146f20 Mon Sep 17 00:00:00 2001 From: Michiel van der Velde Date: Mon, 18 Sep 2023 17:11:59 +0200 Subject: [PATCH] chore(release): v0.0.2 --- package-lock.json | 4 ++-- package.json | 2 +- src/hooks.ts | 15 +++++++++++++++ src/types.ts | 11 +++++++++++ tests/hooks.ts | 26 ++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 980aae0..fb7bbf1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@wecandobetter/propeller", - "version": "0.0.1", + "version": "0.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@wecandobetter/propeller", - "version": "0.0.1", + "version": "0.0.2", "license": "MIT", "devDependencies": { "@types/jest": "^29.5.5", diff --git a/package.json b/package.json index eddbe1a..224f234 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/hooks.ts b/src/hooks.ts index 669f9e8..fd21dc7 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -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); + + if (index !== -1) { + existing.splice(index, 1); + } + } + } + + return collection; + }, execute: async (name, ctx) => { const hooks = map.get(name as string); diff --git a/src/types.ts b/src/types.ts index b34159d..5d11a4c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -30,6 +30,17 @@ export interface HookCollection> { ...hooks: Hook[] ) => HookCollection; + /** + * Unregister a hook. + * @param name The name of the hook + * @param hooks The hooks to unregister + * @returns The updated hook collection. + */ + unregister: ( + name: K, + ...hooks: Hook[] + ) => HookCollection; + /** * 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. diff --git a/tests/hooks.ts b/tests/hooks.ts index 265aba7..6970783 100644 --- a/tests/hooks.ts +++ b/tests/hooks.ts @@ -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);