Skip to content

Commit

Permalink
Cache batch accounts (#983)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin authored Dec 18, 2017
1 parent e676ca8 commit c846898
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/components/account/browse/account-list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}
Expand Down
38 changes: 36 additions & 2 deletions app/services/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class AccountService {
private _accountLoaded = new BehaviorSubject<boolean>(false);
private _currentAccountId = new BehaviorSubject<string>(null);
private _accounts = new BehaviorSubject<List<AccountResource>>(List([]));
private _accountsLoaded = new BehaviorSubject<boolean>(false);
private _accountsLoaded = new AsyncSubject<boolean>();
private _cache = new DataCache<AccountResource>();
private _getter: BasicEntityGetter<AccountResource, AccountParams>;

Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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<AccountResource>(accounts));
this._markAccountsAsLoaded();
}
} catch (e) {
this._clearCachedAccounts();
}
}

private _clearCachedAccounts() {
localStorage.removeItem(Constants.localStorageKey.batchAccounts);
}

}
5 changes: 5 additions & 0 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ export const localStorageKey = {
*/
subscriptions: "subscriptions",

/**
* Subscriptions cached
*/
batchAccounts: "batchAccounts",

/**
* Last batch account selected.
*/
Expand Down

0 comments on commit c846898

Please sign in to comment.