Skip to content

Commit

Permalink
Propagate input to after handlers #743
Browse files Browse the repository at this point in the history
  • Loading branch information
bpatrik committed Dec 2, 2023
1 parent 60a0bee commit aa4c8a2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion extension/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/backend/model/extension/ExtensionDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/backend/model/extension/ExtensionEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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);
}

Expand All @@ -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;
Expand Down
12 changes: 9 additions & 3 deletions src/backend/model/extension/IExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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>
},
/**
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aa4c8a2

Please sign in to comment.