Skip to content

Commit

Permalink
fix: Listener interface
Browse files Browse the repository at this point in the history
  • Loading branch information
taorepoara committed Feb 8, 2024
1 parent b9f1d82 commit 1ed8db3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Binary file modified example/app-lenra/bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions example/app-lenra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
"author": "Lenra",
"license": "MIT",
"dependencies": {
"@lenra/app": "../../../app-lib-js/"
"@lenra/app": "^1.2"
},
"app-lenra": {
"app": "app.ts",
"indexer": "ts"
},
"devDependencies": {
"bun-types": "^1.0.5-canary.20231007T140129"
"@types/bun": "^1.0.5"
}
}
17 changes: 11 additions & 6 deletions src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@ export type ListenerCall<T = any> = {
event?: T,
}

export interface Listener<T = any> {
(event: T): Promise<void>
readonly code: string
}

class ExtensibleFunction extends Function {
constructor(f: Function) {
super();
return Object.setPrototypeOf(f, new.target.prototype);
return Object.setPrototypeOf(f, new.target.prototype);
}
}
}

export class Listener<T = any> extends ExtensibleFunction {
class ListenerImpl<T = any> extends ExtensibleFunction {
readonly code: string;
constructor(route: LenraRoute, code: string) {
super((event?: T) => route.callListener({ code, event }));
super((event: T) => route.callListener({ code, event }));
this.code = code;
}

toJSON() {
return {code: this.code};
return { code: this.code };
}
}

Expand Down Expand Up @@ -90,7 +95,7 @@ export function parseData(route: LenraRoute, data: any): any {
switch (data._type) {
case "listener":
if ("code" in data && !("call" in data)) {
return new Listener(route, data.code);
return new ListenerImpl(route, data.code);
}
break;
// add other types here
Expand Down

0 comments on commit 1ed8db3

Please sign in to comment.