Fix VisitsStorageApi retrieveAll method to return all visits synchronously

Signed-off-by: Benjamin <benjamin.mccain@onepeloton.com>
This commit is contained in:
Benjamin
2023-10-24 10:55:25 -04:00
parent 12683b2a85
commit d86b2acec4
2 changed files with 32 additions and 11 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-home': patch
---
Fix bug where `retrieveAll` method wasn't fetching visits
+27 -11
View File
@@ -117,23 +117,39 @@ export class VisitsStorageApi implements VisitsApi {
}
private async persistAll(visits: Array<Visit>) {
const { userEntityRef } = await this.identityApi.getBackstageIdentity();
const storageKey = `${this.storageKeyPrefix}:${userEntityRef}`;
const storageKey = await this.getStorageKey();
return this.storageApi.set<Array<Visit>>(storageKey, visits);
}
private async retrieveAll(): Promise<Array<Visit>> {
const storageKey = await this.getStorageKey();
// Handles for case when snapshot is and is not referenced per storaged type used
const snapshot = this.storageApi.snapshot<Array<Visit>>(storageKey);
if (snapshot?.presence !== 'unknown') {
return snapshot?.value ?? [];
}
return new Promise((resolve, reject) => {
const subsription = this.storageApi
.observe$<Visit[]>(storageKey)
.subscribe({
next: next => {
const visits = next.value ?? [];
subsription.unsubscribe();
resolve(visits);
},
error: err => {
subsription.unsubscribe();
reject(err);
},
});
});
}
private async getStorageKey(): Promise<string> {
const { userEntityRef } = await this.identityApi.getBackstageIdentity();
const storageKey = `${this.storageKeyPrefix}:${userEntityRef}`;
let visits: Array<Visit>;
try {
visits = this.storageApi.snapshot<Array<Visit>>(storageKey).value ?? [];
} catch {
visits = [];
}
return visits;
return storageKey;
}
// This assumes Visit fields are either numbers or strings