Skip to content

Commit df55341

Browse files
authored
Make alert only show on the current wallet being synced (#583)
1 parent 75b5311 commit df55341

File tree

5 files changed

+29
-20
lines changed

5 files changed

+29
-20
lines changed

scripts/composables/use_wallet.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ function addWallet(wallet) {
6666
const currency = ref('USD');
6767
const price = ref(0.0);
6868
const sync = async () => {
69-
await wallet.sync();
69+
const result = await wallet.sync();
7070
balance.value = wallet.balance;
7171
shieldBalance.value = await wallet.getShieldBalance();
7272
pendingShieldBalance.value = await wallet.getPendingShieldBalance();
7373
isSynced.value = wallet.isSynced;
74+
return result;
7475
};
7576
wallet.onShieldLoadedFromDisk(() => {
7677
hasShield.value = wallet.hasShield();

scripts/dashboard/Dashboard.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ const { createAlert } = useAlerts();
5050
const wallets = useWallets();
5151
const { activeWallet, activeVault } = storeToRefs(wallets);
5252
53+
watch(activeWallet, async (currentWallet) => {
54+
const success = await currentWallet.sync();
55+
if (success && activeWallet.value === currentWallet)
56+
createAlert('success', translation.syncStatusFinished, 12500);
57+
});
58+
5359
const needsToEncrypt = computed(() => {
5460
if (activeWallet.value.isHardwareWallet) {
5561
return false;
@@ -177,9 +183,7 @@ async function importWallet({
177183
}
178184
179185
// Start syncing in the background
180-
activeWallet.value.sync().then(() => {
181-
createAlert('success', translation.syncStatusFinished, 12500);
182-
});
186+
activeWallet.value.sync();
183187
getEventEmitter().emit('wallet-import');
184188
return true;
185189
}

scripts/lock.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,16 @@
55
* @returns {T & { isLocked: () => bool }}
66
*/
77
export const lockableFunction = (f) => {
8-
let lock = false;
8+
let promise = null;
99

1010
const g = async (...args) => {
11-
try {
12-
if (!lock) {
13-
lock = true;
14-
return await f(...args);
15-
}
16-
} finally {
17-
lock = false;
11+
if (!promise) {
12+
promise = f(...args).finally(() => {
13+
promise = null;
14+
});
1815
}
16+
return await promise;
1917
};
20-
g.isLocked = () => lock;
18+
g.isLocked = () => !!promise;
2119
return g;
2220
};

scripts/wallet.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -830,9 +830,7 @@ export class Wallet {
830830
return this.#historicalTxs.get();
831831
}
832832
sync = lockableFunction(async () => {
833-
if (this.#isSynced) {
834-
throw new Error('Attempting to sync when already synced');
835-
}
833+
if (!this.#masterKey || this.#isSynced) return false;
836834
// While syncing the wallet ( DB read + network sync) disable the event balance-update
837835
// This is done to avoid a huge spam of event.
838836
this.#eventEmitter.disableEvent('balance-update');
@@ -862,6 +860,7 @@ export class Wallet {
862860
this.#eventEmitter.enableEvent('new-tx');
863861
this.#eventEmitter.emit('balance-update');
864862
this.#eventEmitter.emit('new-tx');
863+
return true;
865864
});
866865

867866
async #transparentSync() {

tests/unit/lockable_function.spec.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ describe('Lockable function tests', () => {
66
let test_function;
77
const sleep_time = 1000;
88
beforeEach(() => {
9+
let counter = 0;
910
test_function = lockableFunction(async (str_input) => {
1011
await sleep(sleep_time);
11-
return str_input;
12+
return `${str_input}-${counter++}`;
1213
});
1314
});
1415
it('Lockable function returns the correct value', async () => {
15-
expect(await test_function('test_locks')).toBe('test_locks');
16+
expect(await test_function('test_locks')).toBe('test_locks-0');
1617
});
1718
it('Lockable function gives the correct value for the lock', async () => {
1819
// At the beginning there is no lock
@@ -26,7 +27,13 @@ describe('Lockable function tests', () => {
2627
expect(test_function.isLocked()).toBeFalsy();
2728
});
2829
it("Calling when locked doesn't make the function run twice", async () => {
29-
test_function('test_locks');
30-
expect(await test_function('test_locks')).toBeUndefined();
30+
const result = test_function('test_locks');
31+
const result2 = test_function('test_locks');
32+
expect(await Promise.all([result, result2])).toStrictEqual([
33+
'test_locks-0',
34+
'test_locks-0',
35+
]);
36+
const result3 = await test_function('test_locks');
37+
expect(result3).toBe('test_locks-1');
3138
});
3239
});

0 commit comments

Comments
 (0)