wallet/client: different parsing solution.

This commit is contained in:
Christopher Jeffrey 2017-10-26 16:08:44 -07:00
parent a313ac244c
commit 11ac46e051
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -13,32 +13,42 @@ const TX = require('../primitives/tx');
const hash256 = require('bcrypto/lib/hash256');
const util = require('../utils/util');
const parsers = {
'block connect': (entry, txs) => parseBlock(entry, txs),
'block disconnect': entry => [parseEntry(entry)],
'block rescan': (entry, txs) => parseBlock(entry, txs),
'chain reset': entry => [parseEntry(entry)],
'tx': tx => [TX.fromRaw(tx)],
};
class WalletClient extends NodeClient {
constructor(options) {
super(options);
}
async open() {
await super.open();
listen(event, handler) {
const parser = parsers[event];
this.parse('block connect', (entry, txs) => {
return parseBlock(entry, txs);
if (!parser) {
super.listen(event, handler);
return;
}
super.listen(event, (...args) => {
return handler(...parser(...args));
});
}
this.parse('block disconnect', (entry) => {
return [parseEntry(entry)];
});
hook(event, handler) {
const parser = parsers[event];
this.parse('block rescan', (entry, txs) => {
return parseBlock(entry, txs);
});
if (!parser) {
super.hook(event, handler);
return;
}
this.parse('chain reset', (tip) => {
return [parseEntry(tip)];
});
this.parse('tx', (tx) => {
return [TX.fromRaw(tx)];
super.hook(event, (...args) => {
return handler(...parser(...args));
});
}