BlueWallet/events.js

43 lines
1.5 KiB
JavaScript
Raw Normal View History

function EV(eventName, arg, isExclusive) {
2018-01-30 23:42:38 +01:00
if (Object.values(EV.enum).indexOf(eventName) === -1) {
2018-07-07 15:04:32 +02:00
return console.warn('Unregistered event', eventName, 'registered events:', EV.enum);
2018-01-30 23:42:38 +01:00
}
2018-03-17 21:39:21 +01:00
EV.callbacks = EV.callbacks || {}; // static variable
EV.callbacks[eventName] = EV.callbacks[eventName] || [];
2018-01-30 23:42:38 +01:00
2018-03-17 21:39:21 +01:00
if (typeof arg !== 'function') {
// then its an argument
console.log('got event', eventName, '...');
2018-01-30 23:42:38 +01:00
for (let cc of EV.callbacks[eventName]) {
2018-03-17 21:39:21 +01:00
console.log('dispatching event', eventName);
cc(arg);
2018-01-30 23:42:38 +01:00
}
2018-03-17 21:39:21 +01:00
} else {
// its a callback. subscribe it to event
console.log('someone subscribed to', eventName);
if (isExclusive) {
// if it'a an excluside subscribe - other subscribers should be terminated
console.log('exclusive subscribe');
EV.callbacks[eventName] = [];
}
2018-03-17 21:39:21 +01:00
EV.callbacks[eventName].push(arg);
2018-01-30 23:42:38 +01:00
}
}
EV.enum = {
// emitted when local wallet created or deleted, so one must redraw wallets carousel
2018-01-30 23:42:38 +01:00
WALLETS_COUNT_CHANGED: 'WALLETS_COUNT_CHANGED',
// emitted when local wallet (usually current wallet) has changed number of transactions
// so one must redraw main screen transactions list and WalletTransactions screen tx list
2018-01-30 23:42:38 +01:00
TRANSACTIONS_COUNT_CHANGED: 'TRANSACTIONS_COUNT_CHANGED',
// emitted when we know for sure that on remote server tx list
// changed (usually for current wallet)
REMOTE_TRANSACTIONS_COUNT_CHANGED: 'REMOTE_TRANSACTIONS_COUNT_CHANGED',
// RECEIVE_ADDRESS_CHANGED: 'RECEIVE_ADDRESS_CHANGED',
2018-03-17 21:39:21 +01:00
};
2018-01-30 23:42:38 +01:00
2018-03-17 21:39:21 +01:00
module.exports = EV;