Skip to content

Commit

Permalink
Add AsyncInterval class (#423)
Browse files Browse the repository at this point in the history
* Add an async alternative to setInterval()

* Use AsyncInterval for shield progress bar progress

* Use AsyncInterval for the global chain data handler

* Simplify recursion with a while loop

* Do not wait before clearing interval

* Add mock function to PIVXShield getTxStatus

* test that getTxStatus gets called during a shielded tx
  • Loading branch information
panleone authored Oct 14, 2024
1 parent 4ad5a0c commit 09c6aff
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
23 changes: 23 additions & 0 deletions scripts/async_interval.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { sleep } from './utils.js';

/**
* Async alternative to setInterval() and clearInterval().
*/
export class AsyncInterval {
#active = true;

constructor(cb, timeOut) {
this.#setInterval(cb, timeOut);
}
async #setInterval(cb, timeOut) {
while (this.#active) {
await cb();
await sleep(timeOut);
}
}
clearInterval(timeOut) {
setTimeout(() => {
this.#active = false;
}, timeOut);
}
}
8 changes: 4 additions & 4 deletions scripts/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { cOracle } from './prices.js';
import pIconCopy from '../assets/icons/icon-copy.svg';
import pIconCheck from '../assets/icons/icon-check.svg';
import SideNavbar from './SideNavbar.vue';
import { AsyncInterval } from './async_interval.js';

/** A flag showing if base MPW is fully loaded or not */
export let fIsLoaded = false;
Expand Down Expand Up @@ -261,13 +262,12 @@ export async function start() {
await refreshChainData();
// Load the price manager
cOracle.load();

setInterval(() => {
new AsyncInterval(async () => {
// Refresh blockchain data
refreshChainData();
await refreshChainData();

// Fetch the PIVX prices
refreshPriceDisplay();
await refreshPriceDisplay();
}, 15000);

// Check for recent upgrades, display the changelog
Expand Down
7 changes: 3 additions & 4 deletions scripts/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
import { PIVXShield } from 'pivx-shield';
import { guiToggleReceiveType } from './contacts-book.js';
import { TransactionBuilder } from './transaction_builder.js';

import { AsyncInterval } from './async_interval.js';
/**
* Class Wallet, at the moment it is just a "realization" of Masterkey with a given nAccount
* it also remembers which addresses we generated.
Expand Down Expand Up @@ -1060,8 +1060,7 @@ export class Wallet {
'trying to create a shield transaction without having shield enable'
);
}

const periodicFunction = setInterval(async () => {
const periodicFunction = new AsyncInterval(async () => {
const percentage = (await this.#shield.getTxStatus()) * 100;
getEventEmitter().emit(
'shield-transaction-creation-update',
Expand Down Expand Up @@ -1090,7 +1089,7 @@ export class Wallet {
await sleep(500);
throw e;
} finally {
clearInterval(periodicFunction);
await periodicFunction.clearInterval();
getEventEmitter().emit(
'shield-transaction-creation-update',
0.0,
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/wallet/signature.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ describe('Wallet signature tests', () => {
},
],
});
expect(PIVXShield.prototype.getTxStatus).toHaveBeenCalled();
});
it('signs a s->t tx correctly', async () => {
const tx = new Transaction({
Expand Down Expand Up @@ -127,6 +128,7 @@ describe('Wallet signature tests', () => {
},
],
});
expect(PIVXShield.prototype.getTxStatus).toHaveBeenCalled();
});
it('signs a t->s tx correctly', async () => {
const tx = new Transaction({
Expand Down Expand Up @@ -169,5 +171,6 @@ describe('Wallet signature tests', () => {
},
],
});
expect(PIVXShield.prototype.getTxStatus).toHaveBeenCalled();
});
});
1 change: 1 addition & 0 deletions tests/utils/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ PIVXShield.prototype.getLastSyncedBlock = vi.fn(() => {
});
PIVXShield.prototype.getBalance = vi.fn(() => 40 * 10 ** 8);

PIVXShield.prototype.getTxStatus = vi.fn(() => 1.0);
/**
* set up and sync a wallet
* @param {MasterKey} masterKey - masterKey of the wallet
Expand Down

0 comments on commit 09c6aff

Please sign in to comment.