Skip to content

Commit

Permalink
feat: emit onConnecting event upon receiving info (#48)
Browse files Browse the repository at this point in the history
Co-authored-by: Richard Herman <[email protected]>
  • Loading branch information
GeekyEggo and GeekyEggo authored Sep 24, 2024
1 parent c56ea7e commit 1fcc034
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/ui/__tests__/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,27 @@ describe("connection", () => {
});

/**
* Asserts `connected` is emitted once a connection has been established.
* Asserts `connecting` and `connected` are emitted.
*/
it("emits connected", async () => {
it("emits connecting and connected", async () => {
// Arrange.
const uuid = "123_emits-connected";
const connectingSpy = jest.fn();
const connectedSpy = jest.fn();

// Act.
connection.on("connecting", connectingSpy);
connection.on("connected", connectedSpy);

await window.connectElgatoStreamDeckSocket(port, uuid, "register", JSON.stringify(registrationInfo), JSON.stringify(actionInfo));

// Assert
await connection.getInfo();
expect(connectingSpy).toHaveBeenCalledTimes(1);
expect(connectingSpy).toBeCalledWith<[RegistrationInfo, ActionInfo]>(registrationInfo, actionInfo);
expect(connectedSpy).toHaveBeenCalledTimes(1);
expect(connectedSpy).toBeCalledWith<[RegistrationInfo, ActionInfo]>(registrationInfo, actionInfo);
expect(connectingSpy.mock.invocationCallOrder[0]).toBeLessThan(connectedSpy.mock.invocationCallOrder[0]); // connecting before connected
});

/**
Expand Down
29 changes: 26 additions & 3 deletions src/ui/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,37 @@ describe("streamDeck", () => {
});

/**
* Asserts {@link streamDeck.onDidConnect} is propagated when the connection emits `connected`.
* Asserts {@link streamDeck.onConnecting} is propagated when the connection emits `connecting`.
*/
it("receives onDidConnect", () => {
it("receives onConnecting", () => {
// Arrange.
const listener = jest.fn();

// Act.
const disposable = streamDeck.onDidConnect(listener);
const disposable = streamDeck.onConnecting(listener);
connection.emit("connecting", registrationInfo, actionInfo);

// Assert.
expect(listener).toHaveBeenCalledTimes(1);
expect(listener).toHaveBeenCalledWith(registrationInfo, actionInfo);

// Act (dispose).
disposable.dispose();
connection.emit("connecting", registrationInfo, actionInfo);

// Assert (dispose).
expect(listener).toHaveBeenCalledTimes(1);
});

/**
* Asserts {@link streamDeck.onConnected} is propagated when the connection emits `connected`.
*/
it("receives onConnected", () => {
// Arrange.
const listener = jest.fn();

// Act.
const disposable = streamDeck.onConnected(listener);
connection.emit("connected", registrationInfo, actionInfo);

// Assert.
Expand Down
6 changes: 6 additions & 0 deletions src/ui/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class Connection extends EventEmitter<ExtendedUIEventMap> {
private async connect(port: string, uuid: string, event: string, info: RegistrationInfo, actionInfo: ActionInfo): Promise<void> {
if (this.canConnect) {
this.canConnect = false;
this.emit("connecting", info, actionInfo);

const webSocket = new WebSocket(`ws://127.0.0.1:${port}`);
webSocket.onmessage = (ev: MessageEvent<string>): void => this.tryEmit(ev);
Expand Down Expand Up @@ -130,6 +131,11 @@ export type ConnectionInfo = {
* An extended event map that includes connection events.
*/
type ExtendedUIEventMap = UIEventMap & {
/**
* Occurs when a connecting is being established.
*/
connecting: [info: RegistrationInfo, actionInfo: ActionInfo];

/**
* Occurs when a connection is established.
*/
Expand Down
13 changes: 11 additions & 2 deletions src/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,20 @@ const streamDeck = {
system,

/**
* Occurs when the UI connects to the Stream Deck.
* Occurs before the UI has established a connection with Stream Deck.
* @param listener Event handler function.
* @returns A disposable that removes the listener when disposed.
*/
onDidConnect: <TSettings extends JsonObject = JsonObject>(listener: (info: RegistrationInfo, actionInfo: ActionInfo<TSettings>) => void): IDisposable => {
onConnecting: <TSettings extends JsonObject = JsonObject>(listener: (info: RegistrationInfo, actionInfo: ActionInfo<TSettings>) => void): IDisposable => {
return connection.disposableOn("connecting", listener);
},

/**
* Occurs when the UI has established a connection with Stream Deck.
* @param listener Event handler function.
* @returns A disposable that removes the listener when disposed.
*/
onConnected: <TSettings extends JsonObject = JsonObject>(listener: (info: RegistrationInfo, actionInfo: ActionInfo<TSettings>) => void): IDisposable => {
return connection.disposableOn("connected", listener);
}
};
Expand Down

0 comments on commit 1fcc034

Please sign in to comment.