Skip to content

Commit 79f85d5

Browse files
committed
fix: Support passing data along with hook
Closes #16
1 parent 10c2d58 commit 79f85d5

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

src/juno-node.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ export default class JunoModule {
110110
);
111111
}
112112

113-
public async triggerHook(hook: string) {
113+
public async triggerHook(hook: string, data: any = {}) {
114114
return this.sendRequest(
115-
this.protocol.triggerHook(hook)
115+
this.protocol.triggerHook(hook, data)
116116
);
117117
}
118118

@@ -219,7 +219,7 @@ export default class JunoModule {
219219
}
220220
} else if (this.hookListeners[request.hook]) {
221221
for (const listener of this.hookListeners[request.hook]) {
222-
listener();
222+
listener(request.data || {});
223223
}
224224
}
225225
return true;

src/models/messages.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export interface RegisterHookRequest extends BaseMessage {
2828

2929
export interface TriggerHookRequest extends BaseMessage {
3030
hook: string;
31+
data: {
32+
[type: string]: any
33+
};
3134
}
3235

3336
export interface RegisterModuleResponse extends BaseMessage {
@@ -43,7 +46,6 @@ export interface ListenHookResponse extends BaseMessage {
4346

4447
export interface TriggerHookResponse extends BaseMessage {
4548
hook: string;
46-
data?: any;
4749
}
4850

4951
export interface DeclareFunctionResponse extends BaseMessage {

src/protocol/base-protocol.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ export abstract class BaseProtocol {
4242
};
4343
}
4444

45-
public triggerHook(hook: string): TriggerHookRequest {
45+
public triggerHook(hook: string, data: any): TriggerHookRequest {
4646
return {
4747
requestId: this.generateRequestId(),
4848
type: RequestTypes.TriggerHook,
49-
hook
49+
hook,
50+
data
5051
};
5152
}
5253

test/juno-node.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,29 @@ makeConnectionTests('Test if requests constructed correctly', function () {
106106
});
107107
});
108108

109-
it('triggerHook', function () {
109+
it('triggerHook with no args', function () {
110110
this.test.module.triggerHook('test_hook');
111111
const message = this.test.getLatestSent();
112112
expect(message).excluding('requestId').to.deep.equal({
113113
type: 7,
114114
hook: 'test_hook',
115+
data: {},
116+
});
117+
});
118+
119+
it('triggerHook with args', function () {
120+
this.test.module.triggerHook('test_hook', {
121+
a:1,
122+
b:2,
123+
});
124+
const message = this.test.getLatestSent();
125+
expect(message).excluding('requestId').to.deep.equal({
126+
type: 7,
127+
hook: 'test_hook',
128+
data: {
129+
a: 1,
130+
b: 2,
131+
}
115132
});
116133
});
117134
});

0 commit comments

Comments
 (0)