Last active
September 12, 2019 22:29
-
-
Save xavierlepretre/46d6cd2a51ae7860edfe3b726a5ee097 to your computer and use it in GitHub Desktop.
Convert `web3.eth` asynchronous calls into Rx observables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Rx = require('rx'); | |
module.exports = { | |
rxify: function (web3) { | |
// List synchronous functions masquerading as values. | |
var syncGetters = { | |
db: [], | |
eth: [ "accounts", "blockNumber", "coinbase", "gasPrice", "hashrate", | |
"mining", "protocolVersion", "syncing" ], | |
net: [ "listening", "peerCount" ], | |
personal: [ "listAccounts" ], | |
shh: [], | |
version: [ "ethereum", "network", "node", "whisper" ] | |
}; | |
Object.keys(syncGetters).forEach(function(group) { | |
Object.keys(web3[group]).forEach(function (method) { | |
if (syncGetters[group].indexOf(method) > -1) { | |
// Skip | |
} else if (typeof web3[group][method] === "function") { | |
web3[group][method + "Observable"] = Rx.Observable.fromNodeCallback(web3[group][method]); | |
} | |
}); | |
}); | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You use it thus: