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({
@ -222,7 +222,7 @@ export class UserService {
'Web workers are not supported in this environment'
);
for (const accountAddress of accountAddresses.slice(offset, offset + limit)) {
await this.getAccountByAddress(accountAddress, limit);
await this.getAccountByAddress(accountAddress, limit, false, false);
}
}
} catch (error) {
@ -234,7 +234,8 @@ export class UserService {
async getAccountByAddress(
accountAddress: string,
limit: number = 100,
history: boolean = false
history: boolean = false,
top: boolean = true
): Promise<Observable<AccountDetails>> {
const accountSubject: Subject<any> = new Subject<any>();
this.getAccountDetailsFromMeta(await User.toKey(add0x(accountAddress)))
@ -260,7 +261,7 @@ export class UserService {
});
accountInfo.vcard = vCard.parse(atob(accountInfo.vcard));
await vcardValidation(accountInfo.vcard);
this.addAccount(accountInfo, limit);
this.addAccount(accountInfo, limit, top);
accountSubject.next(accountInfo);
});
return accountSubject.asObservable();
@ -321,21 +322,34 @@ export class UserService {
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(
(acc) =>
acc.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0] ===
account.identities.evm[`bloxberg:${environment.bloxbergChainId}`][0]
);
if (savedIndex === 0) {
return;
}
if (savedIndex > 0) {
this.accounts.splice(savedIndex, 1);
}
this.accounts.unshift(account);
if (this.accounts.length > cacheSize) {
this.accounts.length = Math.min(this.accounts.length, cacheSize);
if (top) {
if (savedIndex === 0) {
return;
}
if (savedIndex > 0) {
this.accounts.splice(savedIndex, 1);
}
this.accounts.unshift(account);
if (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);
}