From efefa3fc6aac25c2928f4121626aec6d25264365 Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Thu, 5 Jan 2023 15:19:47 -0500 Subject: [PATCH] Revert "Ps/sync only when changed (#4245)" (#4394) This reverts commit 161ff3de28a8d869fea8529d2ba2ff048be5e83b. --- .../session-syncer.spec.ts | 22 +++----------- .../session-sync-observable/session-syncer.ts | 30 +++++-------------- 2 files changed, 11 insertions(+), 41 deletions(-) diff --git a/apps/browser/src/decorators/session-sync-observable/session-syncer.spec.ts b/apps/browser/src/decorators/session-sync-observable/session-syncer.spec.ts index 2630d9f7bb..c37b640f3f 100644 --- a/apps/browser/src/decorators/session-sync-observable/session-syncer.spec.ts +++ b/apps/browser/src/decorators/session-sync-observable/session-syncer.spec.ts @@ -32,7 +32,6 @@ describe("session syncer", () => { stateService = mock(); stateService.hasInSessionMemory.mockResolvedValue(false); sut = new SessionSyncer(behaviorSubject, stateService, metaData); - jest.spyOn(sut as any, "debounceMs", "get").mockReturnValue(0); }); afterEach(() => { @@ -89,7 +88,7 @@ describe("session syncer", () => { sut.init(); - expect(sut["ignoreNUpdates"]).toBe(1); + expect(sut["ignoreNUpdates"]).toBe(3); }); it("should ignore BehaviorSubject's initial value", () => { @@ -129,41 +128,28 @@ describe("session syncer", () => { describe("a value is emitted on the observable", () => { let sendMessageSpy: jest.SpyInstance; - beforeEach(async () => { + beforeEach(() => { sendMessageSpy = jest.spyOn(BrowserApi, "sendMessage"); sut.init(); - // allow initial value to be set - await awaitAsync(); behaviorSubject.next("test"); }); it("should update the session memory", async () => { // await finishing of fire-and-forget operation - await awaitAsync(); + await new Promise((resolve) => setTimeout(resolve, 100)); expect(stateService.setInSessionMemory).toHaveBeenCalledTimes(1); expect(stateService.setInSessionMemory).toHaveBeenCalledWith(sessionKey, "test"); }); it("should update sessionSyncers in other contexts", async () => { // await finishing of fire-and-forget operation - await awaitAsync(); + await new Promise((resolve) => setTimeout(resolve, 100)); expect(sendMessageSpy).toHaveBeenCalledTimes(1); expect(sendMessageSpy).toHaveBeenCalledWith(`${sessionKey}_update`, { id: sut.id }); }); - - it("should debounce subject updates", async () => { - behaviorSubject.next("test2"); - behaviorSubject.next("test3"); - - // await finishing of fire-and-forget operation - await awaitAsync(); - - expect(stateService.setInSessionMemory).toHaveBeenCalledTimes(1); - expect(stateService.setInSessionMemory).toHaveBeenCalledWith(sessionKey, "test3"); - }); }); describe("A message is received", () => { diff --git a/apps/browser/src/decorators/session-sync-observable/session-syncer.ts b/apps/browser/src/decorators/session-sync-observable/session-syncer.ts index 74f1857706..91b371ef81 100644 --- a/apps/browser/src/decorators/session-sync-observable/session-syncer.ts +++ b/apps/browser/src/decorators/session-sync-observable/session-syncer.ts @@ -1,11 +1,4 @@ -import { - BehaviorSubject, - concatMap, - ReplaySubject, - Subject, - Subscription, - debounceTime, -} from "rxjs"; +import { BehaviorSubject, concatMap, ReplaySubject, Subject, Subscription } from "rxjs"; import { Utils } from "@bitwarden/common/misc/utils"; @@ -20,9 +13,6 @@ export class SessionSyncer { // ignore initial values private ignoreNUpdates = 0; - private get debounceMs() { - return 500; - } constructor( private subject: Subject, @@ -40,8 +30,10 @@ export class SessionSyncer { init() { switch (this.subject.constructor) { - // ignore all updates currently in the buffer - case ReplaySubject: // N = 1 due to debounce + case ReplaySubject: + // ignore all updates currently in the buffer + this.ignoreNUpdates = (this.subject as any)._buffer.length; + break; case BehaviorSubject: this.ignoreNUpdates = 1; break; @@ -66,7 +58,6 @@ export class SessionSyncer { // contexts. If so, this is handled by destruction of the context. this.subscription = this.subject .pipe( - debounceTime(this.debounceMs), concatMap(async (next) => { if (this.ignoreNUpdates > 0) { this.ignoreNUpdates -= 1; @@ -101,15 +92,8 @@ export class SessionSyncer { } private async updateSession(value: any) { - try { - await this.stateService.setInSessionMemory(this.metaData.sessionKey, value); - await BrowserApi.sendMessage(this.updateMessageCommand, { id: this.id }); - } catch (e) { - if (e.message === "Could not establish connection. Receiving end does not exist.") { - return; - } - throw e; - } + await this.stateService.setInSessionMemory(this.metaData.sessionKey, value); + await BrowserApi.sendMessage(this.updateMessageCommand, { id: this.id }); } private get updateMessageCommand() {