diff --git a/packages/lib/src/subscriptions.ts b/packages/lib/src/subscriptions.ts index 8aa03c38..6a1da6e6 100644 --- a/packages/lib/src/subscriptions.ts +++ b/packages/lib/src/subscriptions.ts @@ -598,7 +598,7 @@ export class EventSubscriber { // By delaying the unsubscription here the subscription happens // before the unsubscription, thus preventing the redundant // updates from being sent out. - setImmediate(() => { + setTimeout(() => { let index = this._callbacks.lastIndexOf(handler); if (index === -1) { return; diff --git a/test/lib/subscriptions.test.js b/test/lib/subscriptions.test.js index 46c4c7e0..dcd03a81 100644 --- a/test/lib/subscriptions.test.js +++ b/test/lib/subscriptions.test.js @@ -1045,6 +1045,24 @@ describe("lib/subscriptions", function() { )); }); + it("should unsubscribe without setImmediate, which browsers lack", async function() { + const unsubscribe = eventSubscriber.subscribe(() => {}); + await onceConnectorSend(); + + const setImmediate = globalThis.setImmediate; + delete globalThis.setImmediate; + try { + unsubscribe(); + } finally { + globalThis.setImmediate = setImmediate; + } + await onceConnectorSend(); + + assertLastMessage(new lib.SubscriptionRequest( + RegisteredEvent.name, "replace", -1, lib.SubscriptionFilters.empty() + )); + }); + it("should notify all callbacks", async function() { let called1, called2; const event = new RegisteredEvent(); @@ -1066,7 +1084,7 @@ describe("lib/subscriptions", function() { eventSubscriber.subscribe((e, s) => { called2 = [e, s]; }); unsubscribe(); - await new Promise(r => setImmediate(r)); + await new Promise(r => setTimeout(r)); await eventSubscriber._handleEvent(event); assert.deepEqual(called1, undefined); @@ -1407,7 +1425,7 @@ describe("lib/subscriptions", function() { eventSubscriber.control.sendTo = function() {}; unsubscribe(); - await new Promise(r => setImmediate(r)); + await new Promise(r => setTimeout(r)); const [,, error2] = eventSubscriber.getSnapshot(); assert.deepEqual(error2, null); });