From 024fe226d955937c720167d1acd37a5f0144625b Mon Sep 17 00:00:00 2001 From: Matt Gibson Date: Thu, 15 Dec 2022 10:12:34 -0500 Subject: [PATCH] Only sync if synced value changes Co-authored-by: Daniel James Smith --- .../session-sync-observable/session-syncer.ts | 10 ++++++++-- package-lock.json | 18 +++++++++++++++--- package.json | 2 ++ 3 files changed, 25 insertions(+), 5 deletions(-) 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 91b371ef81..7e44c93301 100644 --- a/apps/browser/src/decorators/session-sync-observable/session-syncer.ts +++ b/apps/browser/src/decorators/session-sync-observable/session-syncer.ts @@ -1,3 +1,4 @@ +import * as objectHash from "object-hash"; import { BehaviorSubject, concatMap, ReplaySubject, Subject, Subscription } from "rxjs"; import { Utils } from "@bitwarden/common/misc/utils"; @@ -10,6 +11,7 @@ import { SyncedItemMetadata } from "./sync-item-metadata"; export class SessionSyncer { subscription: Subscription; id = Utils.newGuid(); + lastHash: string; // ignore initial values private ignoreNUpdates = 0; @@ -92,8 +94,12 @@ export class SessionSyncer { } private async updateSession(value: any) { - await this.stateService.setInSessionMemory(this.metaData.sessionKey, value); - await BrowserApi.sendMessage(this.updateMessageCommand, { id: this.id }); + const thisHash = objectHash(value); + if (thisHash !== this.lastHash) { + this.lastHash = thisHash; + await this.stateService.setInSessionMemory(this.metaData.sessionKey, value); + await BrowserApi.sendMessage(this.updateMessageCommand, { id: this.id }); + } } private get updateMessageCommand() { diff --git a/package-lock.json b/package-lock.json index 73699277f2..cdc25996d2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -54,6 +54,7 @@ "node-fetch": "^2.6.7", "node-forge": "^1.3.1", "nord": "0.2.1", + "object-hash": "^3.0.0", "open": "^8.4.0", "papaparse": "^5.3.2", "popper.js": "^1.16.1", @@ -99,6 +100,7 @@ "@types/node-fetch": "^2.6.1", "@types/node-forge": "^1.0.2", "@types/node-ipc": "^9.2.0", + "@types/object-hash": "^3.0.1", "@types/papaparse": "^5.3.2", "@types/proper-lockfile": "^4.1.2", "@types/retry": "^0.12.2", @@ -13024,6 +13026,12 @@ "integrity": "sha512-WKG4gTr8przEZBiJ5r3s8ZIAoMXNbOgQ+j/d5O4X3x6kZJRLNvyUJuUK/KoG3+8BaOHPhp2m7WC6JKKeovDSzQ==", "dev": true }, + "node_modules/@types/object-hash": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@types/object-hash/-/object-hash-3.0.1.tgz", + "integrity": "sha512-AeyooJR4pesRnPLXONFEpxrdvwYVkBiq26j4Uo4QYcJO2FQhAFmHHWBfRpEzwMtItz7q9JiD50s7HDbHS7PleQ==", + "dev": true + }, "node_modules/@types/papaparse": { "version": "5.3.2", "resolved": "https://registry.npmjs.org/@types/papaparse/-/papaparse-5.3.2.tgz", @@ -32944,7 +32952,6 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz", "integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==", - "dev": true, "engines": { "node": ">= 6" } @@ -52966,6 +52973,12 @@ "integrity": "sha512-WKG4gTr8przEZBiJ5r3s8ZIAoMXNbOgQ+j/d5O4X3x6kZJRLNvyUJuUK/KoG3+8BaOHPhp2m7WC6JKKeovDSzQ==", "dev": true }, + "@types/object-hash": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@types/object-hash/-/object-hash-3.0.1.tgz", + "integrity": "sha512-AeyooJR4pesRnPLXONFEpxrdvwYVkBiq26j4Uo4QYcJO2FQhAFmHHWBfRpEzwMtItz7q9JiD50s7HDbHS7PleQ==", + "dev": true + }, "@types/papaparse": { "version": "5.3.2", "resolved": "https://registry.npmjs.org/@types/papaparse/-/papaparse-5.3.2.tgz", @@ -68490,8 +68503,7 @@ "object-hash": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz", - "integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==", - "dev": true + "integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==" }, "object-inspect": { "version": "1.12.2", diff --git a/package.json b/package.json index 9a8d1b8953..1dec830483 100644 --- a/package.json +++ b/package.json @@ -64,6 +64,7 @@ "@types/node-fetch": "^2.6.1", "@types/node-forge": "^1.0.2", "@types/node-ipc": "^9.2.0", + "@types/object-hash": "^3.0.1", "@types/papaparse": "^5.3.2", "@types/proper-lockfile": "^4.1.2", "@types/retry": "^0.12.2", @@ -176,6 +177,7 @@ "node-fetch": "^2.6.7", "node-forge": "^1.3.1", "nord": "0.2.1", + "object-hash": "^3.0.0", "open": "^8.4.0", "papaparse": "^5.3.2", "popper.js": "^1.16.1",