Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mock-server): add hook to run code after initializing mocks #6478

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/zwave-js/src/mockServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,19 @@ export type MockServerNodeOptions =
& {
behaviors?: MockNodeBehavior[];
};

export type MockServerInitHook = (
controller: MockController,
nodes: MockNode[],
) => void;

export interface MockServerOptions {
interface?: string;
port?: number;
config?: {
controller?: MockServerControllerOptions;
nodes?: MockServerNodeOptions[];
onInit?: MockServerInitHook;
};
}

Expand Down Expand Up @@ -70,6 +77,11 @@ export class MockServer {
this.options.config?.nodes,
));

// Call the init hook if it is defined
if (typeof this.options.config?.onInit === "function") {
this.options.config.onInit(this.mockController, this.mockNodes);
}

// Start a TCP server, listen for connections, and forward them to the serial port
this.server = createServer((socket) => {
if (!this.serialport) {
Expand Down