1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00

Propagate input to after handlers #743

This commit is contained in:
Patrik J. Braun 2023-12-02 09:58:02 +01:00
parent 60a0beeb1e
commit aa4c8a2e35
4 changed files with 17 additions and 11 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "pigallery2-extension-kit", "name": "pigallery2-extension-kit",
"version": "2.0.3-edge3", "version": "2.0.3-edge4",
"description": "Interfaces for developing extensions for pigallery2", "description": "Interfaces for developing extensions for pigallery2",
"author": "Patrik J. Braun", "author": "Patrik J. Braun",
"homepage": "https://github.com/bpatrik/pigallery2", "homepage": "https://github.com/bpatrik/pigallery2",

View File

@ -32,7 +32,7 @@ export const ExtensionDecorator = <I extends unknown[], O>(fn: (ee: IExtensionEv
return input as O; return input as O;
} }
const out = await targetMethod.apply(this, input); const out = await targetMethod.apply(this, input);
return await event.triggerAfter(out); return await event.triggerAfter(input as I, out);
}; };
return descriptor; return descriptor;

View File

@ -2,7 +2,7 @@ import {IExtensionAfterEventHandler, IExtensionBeforeEventHandler, IExtensionEve
export class ExtensionEvent<I extends unknown[], O> implements IExtensionEvent<I, O> { export class ExtensionEvent<I extends unknown[], O> implements IExtensionEvent<I, O> {
protected beforeHandlers: IExtensionBeforeEventHandler<I, O>[] = []; protected beforeHandlers: IExtensionBeforeEventHandler<I, O>[] = [];
protected afterHandlers: IExtensionAfterEventHandler<O>[] = []; protected afterHandlers: IExtensionAfterEventHandler<I, O>[] = [];
public before(handler: IExtensionBeforeEventHandler<I, O>): void { public before(handler: IExtensionBeforeEventHandler<I, O>): void {
if (typeof handler !== 'function') { if (typeof handler !== 'function') {
@ -11,14 +11,14 @@ export class ExtensionEvent<I extends unknown[], O> implements IExtensionEvent<I
this.beforeHandlers.push(handler); this.beforeHandlers.push(handler);
} }
public after(handler: IExtensionAfterEventHandler<O>): void { public after(handler: IExtensionAfterEventHandler<I, O>): void {
if (typeof handler !== 'function') { if (typeof handler !== 'function') {
throw new Error('ExtensionEvent::after: Handler is not a function'); throw new Error('ExtensionEvent::after: Handler is not a function');
} }
this.afterHandlers.push(handler); this.afterHandlers.push(handler);
} }
public offAfter(handler: IExtensionAfterEventHandler<O>): void { public offAfter(handler: IExtensionAfterEventHandler<I, O>): void {
this.afterHandlers = this.afterHandlers.filter((h) => h !== handler); this.afterHandlers = this.afterHandlers.filter((h) => h !== handler);
} }
@ -41,11 +41,11 @@ export class ExtensionEvent<I extends unknown[], O> implements IExtensionEvent<I
return pipe; return pipe;
} }
public async triggerAfter(output: O): Promise<O> { public async triggerAfter(input: I, output: O): Promise<O> {
if (this.afterHandlers && this.afterHandlers.length > 0) { if (this.afterHandlers && this.afterHandlers.length > 0) {
const s = this.afterHandlers.slice(0); const s = this.afterHandlers.slice(0);
for (let i = 0; i < s.length; ++i) { for (let i = 0; i < s.length; ++i) {
output = await s[i](output); output = await s[i]({input, output});
} }
} }
return output; return output;

View File

@ -19,12 +19,16 @@ import {DirectoryScanSettings} from '../fileaccess/DiskManager';
export type IExtensionBeforeEventHandler<I extends unknown[], O> = (input: I, event: { stopPropagation: boolean }) => Promise<I | O>; export type IExtensionBeforeEventHandler<I extends unknown[], O> = (input: I, event: { stopPropagation: boolean }) => Promise<I | O>;
export type IExtensionAfterEventHandler<O> = (output: O) => Promise<O>; /**
* 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<I extends unknown[], O> = (data: { input: I, output: O }) => Promise<O>;
export interface IExtensionEvent<I extends unknown[], O> { export interface IExtensionEvent<I extends unknown[], O> {
before: (handler: IExtensionBeforeEventHandler<I, O>) => void; before: (handler: IExtensionBeforeEventHandler<I, O>) => void;
after: (handler: IExtensionAfterEventHandler<O>) => void; after: (handler: IExtensionAfterEventHandler<I, O>) => void;
} }
/** /**
@ -59,7 +63,9 @@ export interface IExtensionEvents {
* Reads exif, iptc, etc.. metadata for photos/videos * Reads exif, iptc, etc.. metadata for photos/videos
*/ */
MetadataLoader: { MetadataLoader: {
// input: file path
loadVideoMetadata: IExtensionEvent<[string], VideoMetadata>, loadVideoMetadata: IExtensionEvent<[string], VideoMetadata>,
// input: file path
loadPhotoMetadata: IExtensionEvent<[string], PhotoMetadata> loadPhotoMetadata: IExtensionEvent<[string], PhotoMetadata>
}, },
/** /**
@ -122,7 +128,7 @@ export interface IExtensionDB {
setExtensionTables(tables: Function[]): Promise<void>; setExtensionTables(tables: Function[]): Promise<void>;
/** /**
* 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. * Use with caution. This exposes the app's internal working.
*/ */
// eslint-disable-next-line @typescript-eslint/ban-types // eslint-disable-next-line @typescript-eslint/ban-types