Skip to content

Commit

Permalink
wallet: Fix node client interface for hooks.
Browse files Browse the repository at this point in the history
bsock hooks that nodeclient tries to imitate, are handlers set on
specific event. They are expected to return results to the caller.
  • Loading branch information
nodech committed Feb 14, 2024
1 parent 42c4f99 commit 75f3a42
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions lib/wallet/nodeclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class NodeClient extends AsyncEmitter {
this.network = node.network;
this.filter = null;
this.opened = false;
this.hooks = new Map();

this.init();
}
Expand Down Expand Up @@ -98,13 +99,46 @@ class NodeClient extends AsyncEmitter {
}

/**
* Add a listener.
* @param {String} type
* Add a hook.
* @param {String} event
* @param {Function} handler
*/

hook(type, handler) {
return this.on(type, handler);
hook(event, handler) {
assert(typeof event === 'string', 'Event must be a string.');
assert(typeof handler === 'function', 'Handler must be a function.');
assert(!this.hooks.has(event), 'Hook already bound.');
assert(!Object.prototype.hasOwnProperty.call(blacklist, event),

Check failure on line 111 in lib/wallet/nodeclient.js

View workflow job for this annotation

GitHub Actions / Lint & Doc

'blacklist' is not defined
'Blacklisted event.');
this.hooks.set(event, handler);
}

/**
* Remove a hook.
* @param {String} event
*/

unhook(event) {
assert(typeof event === 'string', 'Event must be a string.');
assert(!Object.prototype.hasOwnProperty.call(blacklist, event),

Check failure on line 123 in lib/wallet/nodeclient.js

View workflow job for this annotation

GitHub Actions / Lint & Doc

'blacklist' is not defined
'Blacklisted event.');
this.hooks.delete(event);
}

/**
* Call a hook.
* @param {String} event
* @param {...Object} args
* @returns {Promise}
*/

handleCall(event, ...args) {
const hook = this.hooks.get(event);

if (!hook)
throw new Error('No hook available.');

return hook(...args);
}

/**
Expand Down Expand Up @@ -215,8 +249,6 @@ class NodeClient extends AsyncEmitter {
/**
* Rescan for any missed transactions.
* @param {Number|Hash} start - Start block.
* @param {Bloom} filter
* @param {Function} iter - Iterator.
* @returns {Promise}
*/

Expand All @@ -225,7 +257,7 @@ class NodeClient extends AsyncEmitter {
return this.node.chain.reset(start);

return this.node.chain.scan(start, this.filter, (entry, txs) => {
return this.emitAsync('block rescan', entry, txs);
return this.handleCall('block rescan', entry, txs);
});
}

Expand Down

0 comments on commit 75f3a42

Please sign in to comment.