From af7eb43511becf18c67c0807492f5532df30f4bd Mon Sep 17 00:00:00 2001 From: AnthonyFuller <24512050+AnthonyFuller@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:46:58 +0000 Subject: [PATCH 1/4] feat(hooks): add priority to taps and intercepts --- components/hooksImpl.ts | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/components/hooksImpl.ts b/components/hooksImpl.ts index e8fd7cece..43959912f 100644 --- a/components/hooksImpl.ts +++ b/components/hooksImpl.ts @@ -56,7 +56,14 @@ export class HookMap { /** * The options for a hook. Will either be just the name (as a string), or an object containing the additional options. */ -export type TapOptions = string | { name: string; context: boolean } +export type TapOptions = + | string + | { + name: string + context: boolean + // Priority of the tap, higher goes first. Defaults to 0. + priority?: number + } // eslint-disable-next-line @typescript-eslint/no-explicit-any type AsArray = T extends any[] ? T : [T] @@ -68,12 +75,14 @@ interface Tap { name: string func: (...args: AsArray) => R enableContext: boolean + priority: number } /** * The structure of an intercept. * * @see name + * @see priority * @see call * @see tap */ @@ -83,6 +92,11 @@ export interface Intercept { */ name: string + /** + * The priority of the intercept, higher goes first. + */ + priority: number + /** * A function called just after the hook is called, and before all taps run. * @@ -120,6 +134,12 @@ export abstract class BaseImpl { */ public intercept(intercept: Intercept): void { this._intercepts.push(intercept) + + this._intercepts.sort((a, b) => { + if (a.priority !== a.priority) return b.priority - a.priority + + return a.name.localeCompare(b.name) + }) } /** @@ -139,6 +159,8 @@ export abstract class BaseImpl { : nameOrOptions.name const enableContext = typeof nameOrOptions === "string" ? false : nameOrOptions.context + const priority = + typeof nameOrOptions === "string" ? 0 : nameOrOptions.priority ?? 0 for (const intercept of this._intercepts) { if (intercept.tap) { @@ -150,6 +172,13 @@ export abstract class BaseImpl { name, func: consumer, enableContext, + priority, + }) + + this._taps.sort((a, b) => { + if (a.priority !== a.priority) return b.priority - a.priority + + return a.name.localeCompare(b.name) }) } From dec29dc5f9e1493c83de067de2223fd9e8a51f1c Mon Sep 17 00:00:00 2001 From: AnthonyFuller <24512050+AnthonyFuller@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:51:40 +0000 Subject: [PATCH 2/4] refactor(hooks): rename context option to enableContext and make it optional --- components/hooksImpl.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/components/hooksImpl.ts b/components/hooksImpl.ts index 43959912f..2ee6d4162 100644 --- a/components/hooksImpl.ts +++ b/components/hooksImpl.ts @@ -60,7 +60,8 @@ export type TapOptions = | string | { name: string - context: boolean + // Whether the context object (which can be modified) should be used. Defaults to false. + enableContext?: boolean // Priority of the tap, higher goes first. Defaults to 0. priority?: number } @@ -158,7 +159,9 @@ export abstract class BaseImpl { ? nameOrOptions : nameOrOptions.name const enableContext = - typeof nameOrOptions === "string" ? false : nameOrOptions.context + typeof nameOrOptions === "string" + ? false + : nameOrOptions.enableContext ?? false const priority = typeof nameOrOptions === "string" ? 0 : nameOrOptions.priority ?? 0 From 0026c70aafb530cb6e333c72385965930f863c4f Mon Sep 17 00:00:00 2001 From: AnthonyFuller <24512050+AnthonyFuller@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:55:40 +0000 Subject: [PATCH 3/4] fix(hooks): priority ordering check being typo'd --- components/hooksImpl.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/hooksImpl.ts b/components/hooksImpl.ts index 2ee6d4162..4b965ae2d 100644 --- a/components/hooksImpl.ts +++ b/components/hooksImpl.ts @@ -137,7 +137,7 @@ export abstract class BaseImpl { this._intercepts.push(intercept) this._intercepts.sort((a, b) => { - if (a.priority !== a.priority) return b.priority - a.priority + if (a.priority !== b.priority) return b.priority - a.priority return a.name.localeCompare(b.name) }) @@ -179,7 +179,7 @@ export abstract class BaseImpl { }) this._taps.sort((a, b) => { - if (a.priority !== a.priority) return b.priority - a.priority + if (a.priority !== b.priority) return b.priority - a.priority return a.name.localeCompare(b.name) }) From 914636a782a886e6b72016b7ec043bcf92b74d74 Mon Sep 17 00:00:00 2001 From: AnthonyFuller <24512050+AnthonyFuller@users.noreply.github.com> Date: Mon, 6 Jan 2025 17:28:13 +0000 Subject: [PATCH 4/4] feat(controller): add blackboard for plugins to share data --- components/controller.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/components/controller.ts b/components/controller.ts index 2224f12b1..654cb6ce0 100644 --- a/components/controller.ts +++ b/components/controller.ts @@ -380,6 +380,9 @@ export class Controller { public locationsWithETA = new Set() public parentsWithETA = new Set() + // A place for plugins to share data. + public blackboard: unknown = {} + /** * The constructor. */