web3.onChange

0.1.4 • Public • Published

NPM version Build Status Join the chat at https://gitter.im/ethcore/parity GPLv3

web3.onChange

Wondering how often / when you should call Web3 methods again to refresh the content?

Usage

Install:

$ npm install web3.onChange --save

Use:

import {Web3OnChange} from 'web3.onChange';
import {Web3} from 'web3';
 
// Initialize web3
let web3 = typeof web3 === 'undefined' ? new Web3(new Web3.providers.HttpProvider('http://localhost:8545')) : web3;
 
// Install .onChange plugin
web3 = Web3OnChange.install(web3);
 
// Instead of:
setTimeout(() => {
  web3.eth.getBalance('0xbb9bc244d798123fde783fcc1c72d3bb8c189413', (err, result) => {
    console.log(result);
  });
}, 1000);
 
// Just do:
const off = web3.eth.getBalance.onChange('0xbb9bc244d798123fde783fcc1c72d3bb8c189413', (err, result) => {
  console.log(result);
});
// To stop watching:
off();
 
// Same with contracts:
const off2 = web3.eth.contract(abi).at('0xbb9bc244d798123fde783fcc1c72d3bb8c189413').balance.onChange((err, result) => {
  console.log(result);
});

How it works

By default all queries are made (batched) for each new block. It is planned to support other polling schemes in future.

TODO

  • - Support time-based polling (.onChange(...args, callback, 500) - would poll every 500s)
  • - Support jacogr/ethapi-js
  • - Support pending-transactions polling (.onChange(...args, callback, 'pending'))
  • - Support Filter/Logs polling?

Readme

Keywords

none

Package Sidebar

Install

npm i web3.onChange

Weekly Downloads

0

Version

0.1.4

License

GPL3

Last publish

Collaborators

  • tomusdrw