Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local events #543

Merged
merged 2 commits into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions scripts/composables/use_wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const useWallet = defineStore('wallet', () => {
pendingShieldBalance.value = await wallet.getPendingShieldBalance();
isSynced.value = wallet.isSynced;
};
getEventEmitter().on('shield-loaded-from-disk', () => {
wallet.onShieldLoadedFromDisk(() => {
hasShield.value = wallet.hasShield();
});
const createAndSendTransaction = lockableFunction(
Expand Down Expand Up @@ -166,7 +166,7 @@ export const useWallet = defineStore('wallet', () => {
publicMode.value = fPublicMode;
});

getEventEmitter().on('balance-update', async () => {
wallet.onBalanceUpdate(async () => {
balance.value = wallet.balance;
immatureBalance.value = wallet.immatureBalance;
immatureColdBalance.value = wallet.immatureColdBalance;
Expand All @@ -183,6 +183,22 @@ export const useWallet = defineStore('wallet', () => {
blockCount.value = rawBlockCount;
});

const onNewTx = (fun) => {
return wallet.onNewTx(fun);
};

const onTransparentSyncStatusUpdate = (fun) => {
return wallet.onTransparentSyncStatusUpdate(fun);
};

const onShieldSyncStatusUpdate = (fun) => {
return wallet.onShieldSyncStatusUpdate(fun);
};

const onShieldTransactionCreationUpdate = (fun) => {
return wallet.onShieldTransactionCreationUpdate(fun);
};

return {
publicMode,
isImported,
Expand Down Expand Up @@ -221,5 +237,9 @@ export const useWallet = defineStore('wallet', () => {
blockCount,
lockCoin,
unlockCoin,
onNewTx,
onTransparentSyncStatusUpdate,
onShieldSyncStatusUpdate,
onShieldTransactionCreationUpdate,
};
});
1 change: 0 additions & 1 deletion scripts/dashboard/Activity.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { translation } from '../i18n.js';
import { Database } from '../database.js';
import { HistoricalTx, HistoricalTxType } from '../historical_tx.js';
import { getNameOrAddress } from '../contacts-book.js';
import { getEventEmitter } from '../event_bus';

import iCheck from '../../assets/icons/icon-check.svg';
import iHourglass from '../../assets/icons/icon-hourglass.svg';
Expand Down
2 changes: 1 addition & 1 deletion scripts/dashboard/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ getEventEmitter().on('sync-status', (status) => {
if (status === 'stop') activity?.value?.update();
});

getEventEmitter().on('new-tx', () => {
wallet.onNewTx(() => {
activity?.value?.update();
});

Expand Down
51 changes: 23 additions & 28 deletions scripts/dashboard/WalletBalance.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { cChainParams, COIN } from '../chain_params.js';
import { translation, tr } from '../i18n';
import { ref, computed, toRefs } from 'vue';
import { beautifyNumber } from '../misc';
import { getEventEmitter } from '../event_bus';
import { useWallet } from '../composables/use_wallet';
import { optimiseCurrencyLocale } from '../global';
import { renderWalletBreakdown } from '../charting.js';
import { guiRenderCurrentReceiveModal } from '../contacts-book';
Expand Down Expand Up @@ -61,6 +61,8 @@ const {
publicMode,
} = toRefs(props);

const wallet = useWallet();

// Transparent sync status
const transparentSyncing = ref(false);
const percentage = ref(0.0);
Expand Down Expand Up @@ -129,35 +131,28 @@ const emit = defineEmits([
'restoreWallet',
]);

getEventEmitter().on(
'transparent-sync-status-update',
(i, totalPages, finished) => {
const str = tr(translation.syncStatusHistoryProgress, [
{ current: totalPages - i + 1 },
{ total: totalPages },
]);
const progress = ((totalPages - i) / totalPages) * 100;
syncTStr.value = str;
percentage.value = progress;
transparentSyncing.value = !finished;
}
);
wallet.onTransparentSyncStatusUpdate((i, totalPages, finished) => {
const str = tr(translation.syncStatusHistoryProgress, [
{ current: totalPages - i + 1 },
{ total: totalPages },
]);
const progress = ((totalPages - i) / totalPages) * 100;
syncTStr.value = str;
percentage.value = progress;
transparentSyncing.value = !finished;
});

getEventEmitter().on(
'shield-sync-status-update',
(bytes, totalBytes, finished) => {
percentage.value = Math.round((100 * bytes) / totalBytes);
const mb = bytes / 1_000_000;
const totalMb = totalBytes / 1_000_000;
shieldSyncingStr.value = `Syncing Shield (${mb.toFixed(
1
)}MB/${totalMb.toFixed(1)}MB)`;
shieldSyncing.value = !finished;
}
);
wallet.onShieldSyncStatusUpdate((bytes, totalBytes, finished) => {
percentage.value = Math.round((100 * bytes) / totalBytes);
const mb = bytes / 1_000_000;
const totalMb = totalBytes / 1_000_000;
shieldSyncingStr.value = `Syncing Shield (${mb.toFixed(
1
)}MB/${totalMb.toFixed(1)}MB)`;
shieldSyncing.value = !finished;
});

getEventEmitter().on(
'shield-transaction-creation-update',
wallet.onShieldTransactionCreationUpdate(
// state: 0 = loading shield params
// 1 = proving tx
// 2 = finished
Expand Down
12 changes: 10 additions & 2 deletions scripts/event_bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EventEmitter } from 'events';
* Wrapper class around EventEmitter that allow to enable/disable specific events.
* By defaults all events are enabled, and can be disabled by calling disableEvent
*/
class EventEmitterWrapper {
export class EventEmitterWrapper {
/** @type{EventEmitter} */
#internalEmitter;
/**
Expand All @@ -16,8 +16,16 @@ class EventEmitterWrapper {
this.#internalEmitter = new EventEmitter();
}

/**
* @param {string} eventName
* @param {Function} listener
* @returns {()=>void} A function that removes the attached listener when called
*/
on(eventName, listener) {
this.#internalEmitter.on(eventName, listener);
const a = this.#internalEmitter.on(eventName, listener);
return () => {
a.removeListener(eventName, listener);
};
}

emit(eventName, ...args) {
Expand Down
19 changes: 17 additions & 2 deletions scripts/mempool.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getEventEmitter } from './event_bus.js';
import { COutpoint, UTXO } from './transaction.js';

export const OutpointState = {
Expand Down Expand Up @@ -31,6 +30,22 @@ export class Mempool {
immatureColdBalance: new CachableBalance(),
};

/**
* @type{Function}
*/
#emitter;

/**
* @param {Function} emitter - Calling this function emits the balance-update event
*/
constructor(emitter = () => {}) {
this.#emitter = emitter;
}

setEmitter(emitter) {
this.#emitter = emitter;
}

/**
* Add a transaction to the mempool
* And mark the input as spent.
Expand Down Expand Up @@ -238,7 +253,7 @@ export class Mempool {
this.#balances.immatureBalance.invalidate();
this.#balances.balance.invalidate();
this.#balances.coldBalance.invalidate();
getEventEmitter().emit('balance-update');
this.#emitter();
}

/**
Expand Down
5 changes: 2 additions & 3 deletions scripts/sapling_params.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getEventEmitter } from './event_bus.js';

Check warning on line 1 in scripts/sapling_params.js

View workflow job for this annotation

GitHub Actions / Run linters

'getEventEmitter' is defined but never used
/**
* Manages fetching, caching and loading bar updates of sapling params
*/
Expand Down Expand Up @@ -39,7 +39,7 @@
/**
* @param {import('pivx-shield').PIVXShield} shield
*/
async fetch(shield) {
async fetch(shield, emitter = () => {}) {
if (await this.#getFromDatabase(shield)) return;
const streams = [
{
Expand All @@ -64,8 +64,7 @@
const { done, value } = await reader.read();
if (value) {
percentage += (100 * ratio * value.length) / totalBytes;
getEventEmitter().emit(
'shield-transaction-creation-update',
emitter(
percentage,
// state: 0 = loading shield params
// 1 = proving tx
Expand Down
Loading