Skip to content

Commit

Permalink
fix: Support passing data along with hook
Browse files Browse the repository at this point in the history
Closes #16
  • Loading branch information
thebongy committed May 7, 2020
1 parent 10c2d58 commit 79f85d5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/juno-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ export default class JunoModule {
);
}

public async triggerHook(hook: string) {
public async triggerHook(hook: string, data: any = {}) {
return this.sendRequest(
this.protocol.triggerHook(hook)
this.protocol.triggerHook(hook, data)
);
}

Expand Down Expand Up @@ -219,7 +219,7 @@ export default class JunoModule {
}
} else if (this.hookListeners[request.hook]) {
for (const listener of this.hookListeners[request.hook]) {
listener();
listener(request.data || {});
}
}
return true;
Expand Down
4 changes: 3 additions & 1 deletion src/models/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export interface RegisterHookRequest extends BaseMessage {

export interface TriggerHookRequest extends BaseMessage {
hook: string;
data: {
[type: string]: any
};
}

export interface RegisterModuleResponse extends BaseMessage {
Expand All @@ -43,7 +46,6 @@ export interface ListenHookResponse extends BaseMessage {

export interface TriggerHookResponse extends BaseMessage {
hook: string;
data?: any;
}

export interface DeclareFunctionResponse extends BaseMessage {
Expand Down
5 changes: 3 additions & 2 deletions src/protocol/base-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ export abstract class BaseProtocol {
};
}

public triggerHook(hook: string): TriggerHookRequest {
public triggerHook(hook: string, data: any): TriggerHookRequest {
return {
requestId: this.generateRequestId(),
type: RequestTypes.TriggerHook,
hook
hook,
data
};
}

Expand Down
19 changes: 18 additions & 1 deletion test/juno-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,29 @@ makeConnectionTests('Test if requests constructed correctly', function () {
});
});

it('triggerHook', function () {
it('triggerHook with no args', function () {
this.test.module.triggerHook('test_hook');
const message = this.test.getLatestSent();
expect(message).excluding('requestId').to.deep.equal({
type: 7,
hook: 'test_hook',
data: {},
});
});

it('triggerHook with args', function () {
this.test.module.triggerHook('test_hook', {
a:1,
b:2,
});
const message = this.test.getLatestSent();
expect(message).excluding('requestId').to.deep.equal({
type: 7,
hook: 'test_hook',
data: {
a: 1,
b: 2,
}
});
});
});
Expand Down

0 comments on commit 79f85d5

Please sign in to comment.