BlueWallet/events.js

30 lines
974 B
JavaScript
Raw Normal View History

2018-03-17 21:39:21 +01:00
function EV(eventName, arg) {
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);
EV.callbacks[eventName].push(arg);
2018-01-30 23:42:38 +01:00
}
}
EV.enum = {
WALLETS_COUNT_CHANGED: 'WALLETS_COUNT_CHANGED',
TRANSACTIONS_COUNT_CHANGED: 'TRANSACTIONS_COUNT_CHANGED',
2018-07-07 15:04:32 +02:00
CREATE_TRANSACTION_NEW_DESTINATION_ADDRESS: 'CREATE_TRANSACTION_NEW_DESTINATION_ADDRESS',
2018-06-25 00:22:46 +02:00
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;