From c846898c563bfebf25d67f85a13ae3dcc9cab735 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 18 Dec 2017 10:31:32 +0100 Subject: [PATCH] Cache batch accounts (#983) --- .../account/browse/account-list.component.ts | 2 +- app/services/account.service.ts | 38 ++++++++++++++++++- src/common/constants.ts | 5 +++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/app/components/account/browse/account-list.component.ts b/app/components/account/browse/account-list.component.ts index 0d36461655..e0afe4e669 100644 --- a/app/components/account/browse/account-list.component.ts +++ b/app/components/account/browse/account-list.component.ts @@ -35,7 +35,7 @@ export class AccountListComponent { subscriptionService: SubscriptionService) { this._updateDisplayedAccounts(); - this.accountService.accountsLoaded.filter(x => x).first().subscribe(() => { + this.accountService.accountsLoaded.subscribe(() => { this.loadingStatus = LoadingStatus.Ready; }); } diff --git a/app/services/account.service.ts b/app/services/account.service.ts index 93030ee7f6..8cc63c7da9 100644 --- a/app/services/account.service.ts +++ b/app/services/account.service.ts @@ -58,7 +58,7 @@ export class AccountService { private _accountLoaded = new BehaviorSubject(false); private _currentAccountId = new BehaviorSubject(null); private _accounts = new BehaviorSubject>(List([])); - private _accountsLoaded = new BehaviorSubject(false); + private _accountsLoaded = new AsyncSubject(); private _cache = new DataCache(); private _getter: BasicEntityGetter; @@ -147,6 +147,7 @@ export class AccountService { } public load() { + this._loadCachedAccounts(); const obs = this.subscriptionService.subscriptions.flatMap((subscriptions) => { const accountObs = subscriptions.map((subscription) => { return this.list(subscription.subscriptionId); @@ -159,7 +160,8 @@ export class AccountService { next: (accountsPerSubscriptions) => { const accounts = accountsPerSubscriptions.map(x => x.toArray()).flatten(); this._accounts.next(List(accounts)); - this._accountsLoaded.next(true); + this._cacheAccounts(); + this._markAccountsAsLoaded(); }, error: (error) => { log.error("Error loading accounts", error); @@ -308,4 +310,36 @@ export class AccountService { private _createAccount(subscription: Subscription, data: any): AccountResource { return new AccountResource(Object.assign({}, data, { subscription })); } + + private _markAccountsAsLoaded() { + this._accountsLoaded.next(true); + this._accountsLoaded.complete(); + } + + private _cacheAccounts() { + localStorage.setItem(Constants.localStorageKey.batchAccounts, JSON.stringify(this._accounts.value.toJS())); + } + + private _loadCachedAccounts() { + const str = localStorage.getItem(Constants.localStorageKey.batchAccounts); + + try { + const data = JSON.parse(str); + + if (data.length === 0) { + this._clearCachedAccounts(); + } else { + const accounts = data.map(x => new AccountResource(x)); + this._accounts.next(List(accounts)); + this._markAccountsAsLoaded(); + } + } catch (e) { + this._clearCachedAccounts(); + } + } + + private _clearCachedAccounts() { + localStorage.removeItem(Constants.localStorageKey.batchAccounts); + } + } diff --git a/src/common/constants.ts b/src/common/constants.ts index f074ec5643..6ed79256cd 100644 --- a/src/common/constants.ts +++ b/src/common/constants.ts @@ -95,6 +95,11 @@ export const localStorageKey = { */ subscriptions: "subscriptions", + /** + * Subscriptions cached + */ + batchAccounts: "batchAccounts", + /** * Last batch account selected. */