Add flag for determining whether to add account to top or bottom of cache.

This commit is contained in:
Spencer Ofwiti 2021-08-17 15:35:24 +03:00
parent 935999cabf
commit 19fdea084b

View File

@ -209,7 +209,7 @@ export class UserService {
); );
} }
}); });
this.addAccount(data, limit); this.addAccount(data, limit, false);
} }
}; };
worker.postMessage({ worker.postMessage({
@ -222,7 +222,7 @@ export class UserService {
'Web workers are not supported in this environment' 'Web workers are not supported in this environment'
); );
for (const accountAddress of accountAddresses.slice(offset, offset + limit)) { for (const accountAddress of accountAddresses.slice(offset, offset + limit)) {
await this.getAccountByAddress(accountAddress, limit); await this.getAccountByAddress(accountAddress, limit, false, false);
} }
} }
} catch (error) { } catch (error) {
@ -234,7 +234,8 @@ export class UserService {
async getAccountByAddress( async getAccountByAddress(
accountAddress: string, accountAddress: string,
limit: number = 100, limit: number = 100,
history: boolean = false history: boolean = false,
top: boolean = true
): Promise<Observable<AccountDetails>> { ): Promise<Observable<AccountDetails>> {
const accountSubject: Subject<any> = new Subject<any>(); const accountSubject: Subject<any> = new Subject<any>();
this.getAccountDetailsFromMeta(await User.toKey(add0x(accountAddress))) this.getAccountDetailsFromMeta(await User.toKey(add0x(accountAddress)))
@ -260,7 +261,7 @@ export class UserService {
}); });
accountInfo.vcard = vCard.parse(atob(accountInfo.vcard)); accountInfo.vcard = vCard.parse(atob(accountInfo.vcard));
await vcardValidation(accountInfo.vcard); await vcardValidation(accountInfo.vcard);
this.addAccount(accountInfo, limit); this.addAccount(accountInfo, limit, top);
accountSubject.next(accountInfo); accountSubject.next(accountInfo);
}); });
return accountSubject.asObservable(); return accountSubject.asObservable();
@ -321,12 +322,13 @@ export class UserService {
return this.httpClient.get(`${environment.cicMetaUrl}/genders`); return this.httpClient.get(`${environment.cicMetaUrl}/genders`);
} }
addAccount(account: AccountDetails, cacheSize: number): void { addAccount(account: AccountDetails, cacheSize: number, top: boolean = true): void {
const savedIndex = this.accounts.findIndex( const savedIndex = this.accounts.findIndex(
(acc) => (acc) =>
acc.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] === acc.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] ===
account.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] account.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0]
); );
if (top) {
if (savedIndex === 0) { if (savedIndex === 0) {
return; return;
} }
@ -337,6 +339,18 @@ export class UserService {
if (this.accounts.length > cacheSize) { if (this.accounts.length > cacheSize) {
this.accounts.length = Math.min(this.accounts.length, cacheSize); this.accounts.length = Math.min(this.accounts.length, cacheSize);
} }
} else {
if (
this.accounts.length >= cacheSize ||
(savedIndex !== -1 && savedIndex === this.accounts.length - 1)
) {
return;
}
if (savedIndex !== -1 && savedIndex < this.accounts.length - 1) {
this.accounts.splice(savedIndex, 1);
}
this.accounts.push(account);
}
this.accountsList.next(this.accounts); this.accountsList.next(this.accounts);
} }