From aa4c8a2e354b4fe625e1bd83128d3e755c1d472c Mon Sep 17 00:00:00 2001 From: "Patrik J. Braun" Date: Sat, 2 Dec 2023 09:58:02 +0100 Subject: [PATCH] Propagate input to after handlers #743 --- extension/package.json | 2 +- src/backend/model/extension/ExtensionDecorator.ts | 4 ++-- src/backend/model/extension/ExtensionEvent.ts | 10 +++++----- src/backend/model/extension/IExtension.ts | 12 +++++++++--- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/extension/package.json b/extension/package.json index 09bd6fe5..e925055a 100644 --- a/extension/package.json +++ b/extension/package.json @@ -1,6 +1,6 @@ { "name": "pigallery2-extension-kit", - "version": "2.0.3-edge3", + "version": "2.0.3-edge4", "description": "Interfaces for developing extensions for pigallery2", "author": "Patrik J. Braun", "homepage": "https://github.com/bpatrik/pigallery2", diff --git a/src/backend/model/extension/ExtensionDecorator.ts b/src/backend/model/extension/ExtensionDecorator.ts index 28784596..2846bec3 100644 --- a/src/backend/model/extension/ExtensionDecorator.ts +++ b/src/backend/model/extension/ExtensionDecorator.ts @@ -14,7 +14,7 @@ export const ExtensionDecorator = (fn: (ee: IExtensionEv return ( target: unknown, propertyName: string, - descriptor: TypedPropertyDescriptor<(...args:I)=>Promise> + descriptor: TypedPropertyDescriptor<(...args: I) => Promise> ) => { const targetMethod = descriptor.value; @@ -32,7 +32,7 @@ export const ExtensionDecorator = (fn: (ee: IExtensionEv return input as O; } const out = await targetMethod.apply(this, input); - return await event.triggerAfter(out); + return await event.triggerAfter(input as I, out); }; return descriptor; diff --git a/src/backend/model/extension/ExtensionEvent.ts b/src/backend/model/extension/ExtensionEvent.ts index 526ed409..489e4a7a 100644 --- a/src/backend/model/extension/ExtensionEvent.ts +++ b/src/backend/model/extension/ExtensionEvent.ts @@ -2,7 +2,7 @@ import {IExtensionAfterEventHandler, IExtensionBeforeEventHandler, IExtensionEve export class ExtensionEvent implements IExtensionEvent { protected beforeHandlers: IExtensionBeforeEventHandler[] = []; - protected afterHandlers: IExtensionAfterEventHandler[] = []; + protected afterHandlers: IExtensionAfterEventHandler[] = []; public before(handler: IExtensionBeforeEventHandler): void { if (typeof handler !== 'function') { @@ -11,14 +11,14 @@ export class ExtensionEvent implements IExtensionEvent): void { + public after(handler: IExtensionAfterEventHandler): void { if (typeof handler !== 'function') { throw new Error('ExtensionEvent::after: Handler is not a function'); } this.afterHandlers.push(handler); } - public offAfter(handler: IExtensionAfterEventHandler): void { + public offAfter(handler: IExtensionAfterEventHandler): void { this.afterHandlers = this.afterHandlers.filter((h) => h !== handler); } @@ -41,11 +41,11 @@ export class ExtensionEvent implements IExtensionEvent { + public async triggerAfter(input: I, output: O): Promise { if (this.afterHandlers && this.afterHandlers.length > 0) { const s = this.afterHandlers.slice(0); for (let i = 0; i < s.length; ++i) { - output = await s[i](output); + output = await s[i]({input, output}); } } return output; diff --git a/src/backend/model/extension/IExtension.ts b/src/backend/model/extension/IExtension.ts index 3ef10049..ea8da419 100644 --- a/src/backend/model/extension/IExtension.ts +++ b/src/backend/model/extension/IExtension.ts @@ -19,12 +19,16 @@ import {DirectoryScanSettings} from '../fileaccess/DiskManager'; export type IExtensionBeforeEventHandler = (input: I, event: { stopPropagation: boolean }) => Promise; -export type IExtensionAfterEventHandler = (output: O) => Promise; +/** + * input: is the original input: this is output of all before handler. This value was also piped to app's function + * output: is the output of the app's function or the previous after handler + */ +export type IExtensionAfterEventHandler = (data: { input: I, output: O }) => Promise; export interface IExtensionEvent { before: (handler: IExtensionBeforeEventHandler) => void; - after: (handler: IExtensionAfterEventHandler) => void; + after: (handler: IExtensionAfterEventHandler) => void; } /** @@ -59,7 +63,9 @@ export interface IExtensionEvents { * Reads exif, iptc, etc.. metadata for photos/videos */ MetadataLoader: { + // input: file path loadVideoMetadata: IExtensionEvent<[string], VideoMetadata>, + // input: file path loadPhotoMetadata: IExtensionEvent<[string], PhotoMetadata> }, /** @@ -122,7 +128,7 @@ export interface IExtensionDB { setExtensionTables(tables: Function[]): Promise; /** - * Exposes all tables. You can use this if you van to have a foreign key to a built in table. + * Exposes all tables. You can use this if you van to have a foreign key to a built-in table. * Use with caution. This exposes the app's internal working. */ // eslint-disable-next-line @typescript-eslint/ban-types