0.1.4 • Published 8 years ago

web3.onChange v0.1.4

Weekly downloads
1
License
GPL3
Repository
-
Last release
8 years ago

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 pending-transactions polling (.onChange(...args, callback, 'pending'))
    • Support Filter/Logs polling?
0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago