Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions core/src/neighbourhood/NeighbourhoodClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ export class NeighbourhoodClient {
let handlersForPerspective = this.#signalHandlers.get(perspectiveUUID)
if (!handlersForPerspective) {
handlersForPerspective = []
this.#signalHandlers.set(perspectiveUUID, handlersForPerspective)
await this.subscribeToSignals(perspectiveUUID)
}
handlersForPerspective.push(handler)
this.#signalHandlers.set(perspectiveUUID, handlersForPerspective)
}

removeSignalHandler(perspectiveUUID: string, handler: TelepresenceSignalCallback): void {
Expand All @@ -273,8 +273,10 @@ export class NeighbourhoodClient {
const index = handlersForPerspective.indexOf(handler)
if (index > -1) {
handlersForPerspective.splice(index, 1)
if (!handlersForPerspective.length) {
this.#signalHandlers.delete(perspectiveUUID)
}
}
}
this.#signalHandlers.set(perspectiveUUID, handlersForPerspective)
}
}
45 changes: 45 additions & 0 deletions core/src/neighbourhood/NeighbourhoodProxy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ApolloClient } from "@apollo/client";
import { LinkExpression } from "../links/Links";
import { NeighbourhoodClient } from "./NeighbourhoodClient";
import { NeighbourhoodProxy } from "./NeighbourhoodProxy";

describe("NeighbourhoodProxy", () => {
it("should add multiple signal handlers", async () => {
const neighbourhoodURI = "did://123";

const mockApolloClient = {
subscribe: () => ({
subscribe: () =>
new Promise((resolve) => {
setTimeout(() => {
resolve({});
}, 10);
}),
}),
} as any;

const neighbourhoodClient = new NeighbourhoodClient(mockApolloClient);
const neighbourhoodProxy = new NeighbourhoodProxy(
neighbourhoodClient,
neighbourhoodURI
);

let callbacks = 0;

const handler1 = () => {
callbacks++;
};
const handler2 = () => {
callbacks++;
};

// Add multiple signal handlers in paralell
const promise = neighbourhoodProxy.addSignalHandler(handler1);
neighbourhoodProxy.addSignalHandler(handler2);
await promise;

neighbourhoodClient.dispatchSignal(neighbourhoodURI, true);

expect(callbacks).toBe(2);
});
});