From d86b2acec47bf571883f3b71faab451b873834ed Mon Sep 17 00:00:00 2001 From: Benjamin Date: Tue, 24 Oct 2023 10:55:25 -0400 Subject: [PATCH] Fix VisitsStorageApi retrieveAll method to return all visits synchronously Signed-off-by: Benjamin --- .changeset/curvy-carrots-thank.md | 5 ++++ plugins/home/src/api/VisitsStorageApi.ts | 38 +++++++++++++++++------- 2 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 .changeset/curvy-carrots-thank.md diff --git a/.changeset/curvy-carrots-thank.md b/.changeset/curvy-carrots-thank.md new file mode 100644 index 0000000000..6303125314 --- /dev/null +++ b/.changeset/curvy-carrots-thank.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-home': patch +--- + +Fix bug where `retrieveAll` method wasn't fetching visits diff --git a/plugins/home/src/api/VisitsStorageApi.ts b/plugins/home/src/api/VisitsStorageApi.ts index daf24d716b..c72d4c5d64 100644 --- a/plugins/home/src/api/VisitsStorageApi.ts +++ b/plugins/home/src/api/VisitsStorageApi.ts @@ -117,23 +117,39 @@ export class VisitsStorageApi implements VisitsApi { } private async persistAll(visits: Array) { - const { userEntityRef } = await this.identityApi.getBackstageIdentity(); - const storageKey = `${this.storageKeyPrefix}:${userEntityRef}`; - + const storageKey = await this.getStorageKey(); return this.storageApi.set>(storageKey, visits); } private async retrieveAll(): Promise> { + const storageKey = await this.getStorageKey(); + // Handles for case when snapshot is and is not referenced per storaged type used + const snapshot = this.storageApi.snapshot>(storageKey); + if (snapshot?.presence !== 'unknown') { + return snapshot?.value ?? []; + } + + return new Promise((resolve, reject) => { + const subsription = this.storageApi + .observe$(storageKey) + .subscribe({ + next: next => { + const visits = next.value ?? []; + subsription.unsubscribe(); + resolve(visits); + }, + error: err => { + subsription.unsubscribe(); + reject(err); + }, + }); + }); + } + + private async getStorageKey(): Promise { const { userEntityRef } = await this.identityApi.getBackstageIdentity(); const storageKey = `${this.storageKeyPrefix}:${userEntityRef}`; - let visits: Array; - - try { - visits = this.storageApi.snapshot>(storageKey).value ?? []; - } catch { - visits = []; - } - return visits; + return storageKey; } // This assumes Visit fields are either numbers or strings