Skip to content

Commit

Permalink
fix caching on key registration
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt committed Apr 15, 2021
1 parent d5e4b40 commit 9d49415
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 33 deletions.
64 changes: 48 additions & 16 deletions packages/yoroi-extension/app/stores/ada/AdaDelegationStore.js
Expand Up @@ -52,7 +52,7 @@ export default class AdaDelegationStore extends Store<StoresMap, ActionsMap> {

@observable delegationRequests: Array<AdaDelegationRequests> = [];

_recalculateDelegationInfoDisposer: void => void = () => {};
_recalculateDelegationInfoDisposer: Array<void => void> = [];

@action addObservedWallet: PublicDeriver<> => void = (
publicDeriver
Expand Down Expand Up @@ -115,8 +115,6 @@ export default class AdaDelegationStore extends Store<StoresMap, ActionsMap> {
publicDeriver,
}).promise;

delegationRequest.getDelegatedBalance.reset();
delegationRequest.getCurrentDelegation.reset();
runInAction(() => {
delegationRequest.error = undefined;
});
Expand Down Expand Up @@ -303,36 +301,70 @@ export default class AdaDelegationStore extends Store<StoresMap, ActionsMap> {

@action.bound
_startWatch: void => void = () => {
this._recalculateDelegationInfoDisposer = reaction(
const triggerRefresh = async () => {
if (!this.stores.serverConnectionStore.checkAdaServerStatus) {
// don't re-query when server goes offline -- only when it comes back online
return;
}
const selected = this.stores.wallets.selected;
if (selected == null) return;
if (!isCardanoHaskell(selected.getParent().getNetworkInfo())) {
return;
}
if (asGetStakingKey(selected) != null) {
await this.refreshDelegation(selected);
}
};
this._recalculateDelegationInfoDisposer.push(reaction(
() => [
this.stores.wallets.selected,
],
triggerRefresh,
));
this._recalculateDelegationInfoDisposer.push(reaction(
() => [
// update if tx history changes
this.stores.transactions.hash,
],
async () => {
for (const requests of this.stores.delegation.delegationRequests) {
requests.mangledAmounts.invalidate();
requests.getDelegatedBalance.invalidate();
requests.getCurrentDelegation.invalidate();
}
for (const requests of this.delegationRequests) {
requests.getRegistrationHistory.invalidate();
}
await triggerRefresh();
},
));
this._recalculateDelegationInfoDisposer = reaction(
() => [
// if query failed due to server issue, need to re-query when it comes back online
this.stores.serverConnectionStore.checkAdaServerStatus,
// reward grows every epoch so we have to refresh
this.stores.substores.ada.time.currentTime?.currentEpoch,
],
async () => {
if (!this.stores.serverConnectionStore.checkAdaServerStatus) {
// don't re-query when server goes offline -- only when it comes back online
return;
}
const selected = this.stores.wallets.selected;
if (selected == null) return;
if (!isCardanoHaskell(selected.getParent().getNetworkInfo())) {
return;
for (const requests of this.stores.delegation.delegationRequests) {
requests.mangledAmounts.invalidate();
requests.getDelegatedBalance.invalidate();
requests.getCurrentDelegation.invalidate();
requests.rewardHistory.invalidate();
}
if (asGetStakingKey(selected) != null) {
await this.refreshDelegation(selected);
for (const requests of this.delegationRequests) {
requests.getRegistrationHistory.invalidate();
}
await triggerRefresh();
},
);
}

@action.bound
reset(): void {
this._recalculateDelegationInfoDisposer();
this._recalculateDelegationInfoDisposer = () => {};
while (this._recalculateDelegationInfoDisposer.length > 0) {
const disposer = this._recalculateDelegationInfoDisposer.pop();
disposer();
}
}
}
Expand Up @@ -42,7 +42,7 @@ import type { StoresMap } from '../index';

export default class JormungandrDelegationStore extends Store<StoresMap, ActionsMap> {

_recalculateDelegationInfoDisposer: void => void = () => {};
_recalculateDelegationInfoDisposer: Array<void => void> = [];

@action addObservedWallet: PublicDeriver<> => void = (
publicDeriver
Expand Down Expand Up @@ -104,8 +104,6 @@ export default class JormungandrDelegationStore extends Store<StoresMap, Actions
publicDeriver,
}).promise;

delegationRequest.getDelegatedBalance.reset();
delegationRequest.getCurrentDelegation.reset();
runInAction(() => {
delegationRequest.error = undefined;
});
Expand Down Expand Up @@ -254,36 +252,64 @@ export default class JormungandrDelegationStore extends Store<StoresMap, Actions

@action.bound
_startWatch: void => void = () => {
this._recalculateDelegationInfoDisposer = reaction(
const triggerRefresh = async () => {
if (!this.stores.serverConnectionStore.checkAdaServerStatus) {
// don't re-query when server goes offline -- only when it comes back online
return;
}
const selected = this.stores.wallets.selected;
if (selected == null) return;
if (!isJormungandr(selected.getParent().getNetworkInfo())) {
return;
}
if (asGetStakingKey(selected) != null) {
await this.refreshDelegation(selected);
}
};
this._recalculateDelegationInfoDisposer.push(reaction(
() => [
this.stores.wallets.selected,
],
triggerRefresh,
));
this._recalculateDelegationInfoDisposer.push(reaction(
() => [
// update if tx history changes
this.stores.transactions.hash,
],
async () => {
for (const requests of this.stores.delegation.delegationRequests) {
requests.mangledAmounts.invalidate();
requests.getDelegatedBalance.invalidate();
requests.getCurrentDelegation.invalidate();
}
await triggerRefresh();
},
));
this._recalculateDelegationInfoDisposer = reaction(
() => [
// if query failed due to server issue, need to re-query when it comes back online
this.stores.serverConnectionStore.checkAdaServerStatus,
// reward grows every epoch so we have to refresh
this.stores.substores.jormungandr.time.currentTime?.currentEpoch,
],
async () => {
if (!this.stores.serverConnectionStore.checkAdaServerStatus) {
// don't re-query when server goes offline -- only when it comes back online
return;
}
const selected = this.stores.wallets.selected;
if (selected == null) return;
if (!isJormungandr(selected.getParent().getNetworkInfo())) {
return;
}
if (asGetStakingKey(selected) != null) {
await this.refreshDelegation(selected);
for (const requests of this.stores.delegation.delegationRequests) {
requests.mangledAmounts.invalidate();
requests.getDelegatedBalance.invalidate();
requests.getCurrentDelegation.invalidate();
requests.rewardHistory.invalidate();
}
await triggerRefresh();
},
);
}

@action.bound
reset(): void {
this._recalculateDelegationInfoDisposer();
this._recalculateDelegationInfoDisposer = () => {};
while (this._recalculateDelegationInfoDisposer.length > 0) {
const disposer = this._recalculateDelegationInfoDisposer.pop();
disposer();
}
}
}

0 comments on commit 9d49415

Please sign in to comment.