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:
parent
60a0beeb1e
commit
aa4c8a2e35
@ -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",
|
||||
|
@ -14,7 +14,7 @@ export const ExtensionDecorator = <I extends unknown[], O>(fn: (ee: IExtensionEv
|
||||
return (
|
||||
target: unknown,
|
||||
propertyName: string,
|
||||
descriptor: TypedPropertyDescriptor<(...args:I)=>Promise<O>>
|
||||
descriptor: TypedPropertyDescriptor<(...args: I) => Promise<O>>
|
||||
) => {
|
||||
|
||||
const targetMethod = descriptor.value;
|
||||
@ -32,7 +32,7 @@ export const ExtensionDecorator = <I extends unknown[], O>(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;
|
||||
|
@ -2,7 +2,7 @@ import {IExtensionAfterEventHandler, IExtensionBeforeEventHandler, IExtensionEve
|
||||
|
||||
export class ExtensionEvent<I extends unknown[], O> implements IExtensionEvent<I, O> {
|
||||
protected beforeHandlers: IExtensionBeforeEventHandler<I, O>[] = [];
|
||||
protected afterHandlers: IExtensionAfterEventHandler<O>[] = [];
|
||||
protected afterHandlers: IExtensionAfterEventHandler<I, O>[] = [];
|
||||
|
||||
public before(handler: IExtensionBeforeEventHandler<I, O>): void {
|
||||
if (typeof handler !== 'function') {
|
||||
@ -11,14 +11,14 @@ export class ExtensionEvent<I extends unknown[], O> implements IExtensionEvent<I
|
||||
this.beforeHandlers.push(handler);
|
||||
}
|
||||
|
||||
public after(handler: IExtensionAfterEventHandler<O>): void {
|
||||
public after(handler: IExtensionAfterEventHandler<I, O>): void {
|
||||
if (typeof handler !== 'function') {
|
||||
throw new Error('ExtensionEvent::after: Handler is not a function');
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
@ -41,11 +41,11 @@ export class ExtensionEvent<I extends unknown[], O> implements IExtensionEvent<I
|
||||
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) {
|
||||
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;
|
||||
|
@ -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 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> {
|
||||
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
|
||||
*/
|
||||
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<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.
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
|
Loading…
Reference in New Issue
Block a user