Skip to content

Commit 3b35a7c

Browse files
committed
feat: Implementation for initalize(), declareFunction(), functionCall(), registerHook() and triggerHook() requests
1 parent deeab19 commit 3b35a7c

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/main.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Protocol, Dependency } from "./protocol/protocol";
1+
import { Protocol, Dependency, FnArgs } from "./protocol/protocol";
22

3-
class Gotham {
3+
export class Gotham {
44
moduleId: string;
55
protocol: Protocol
66
constructor() {
@@ -11,4 +11,28 @@ class Gotham {
1111
this.protocol.initialize(moduleId, version, deps),
1212
);
1313
}
14+
15+
async declareFunction(fnName: string, fn: Function) {
16+
return await this.protocol.sendRequest(
17+
this.protocol.declareFunction(fnName, fn)
18+
);
19+
}
20+
21+
async functionCall(fnNmame: string, args: FnArgs) {
22+
return await this.protocol.sendRequest(
23+
this.protocol.functionCall(fnNmame, args)
24+
);
25+
}
26+
27+
async registerHook(hook: string, cb: Function) {
28+
return await this.protocol.sendRequest(
29+
this.protocol.registerHook(hook, cb)
30+
);
31+
}
32+
33+
async triggerHook(hook: string) {
34+
return await this.protocol.sendRequest(
35+
this.protocol.triggerHook(hook)
36+
);
37+
}
1438
}

src/protocol/protocol.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface Dependency {
66
[type: string]: string;
77
}
88

9-
interface FnArgs {
9+
export interface FnArgs {
1010
[type: string]: any;
1111
}
1212

@@ -115,7 +115,7 @@ export class Protocol {
115115
}
116116
}
117117

118-
registerHook(moduleId: string, hook: string, cb: Function): RegisterHookRequest {
118+
registerHook(hook: string, cb: Function): RegisterHookRequest {
119119
if (this.hookListeners[hook]) {
120120
this.hookListeners[hook] = [cb];
121121
} else {
@@ -124,7 +124,7 @@ export class Protocol {
124124
return {
125125
requestId: this.generateRequestId(),
126126
type: 'registerHook',
127-
hook: `${moduleId}-${hook}`,
127+
hook: `${this.moduleId}-${hook}`,
128128
}
129129
}
130130

@@ -162,9 +162,9 @@ export class Protocol {
162162
}
163163
}
164164

165-
functionCall(functionName: string, ...args: FnArgs[]) {
165+
functionCall(functionName: string, args: FnArgs) {
166166
if (this.functions[functionName]) {
167-
this.functions[functionName]();
167+
this.functions[functionName](...args);
168168
}
169169
}
170170

0 commit comments

Comments
 (0)