Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

tomusdrw/web3.onChange

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

In short

web3.eth.getBalance.onChange('0xbb9bc244d798123fde783fcc1c72d3bb8c189413', (err, balance) => {
  console.log(`Your new balance is: ${balance}`);
});

Full version

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 polling manually for every call
setTimeout(() => {
  web3.eth.getBalance('0xbb9bc244d798123fde783fcc1c72d3bb8c189413', (err, result) => {
    console.log(result);
  });
}, 1000);

// Just listen to changes
const off = web3.eth.getBalance.onChange('0xbb9bc244d798123fde783fcc1c72d3bb8c189413', (err, result) => {
  console.log(result);
});

// and to stop listening call
off();

// You can do the same with every web3 method.
// Also in contracts:
const off2 = web3.eth.contract(abi).at('0xbb9bc244d798123fde783fcc1c72d3bb8c189413').balance.onChange((err, result) => {
  console.log(result);
});

How it works

Library is doing a batch request each time new block is imported.

After a request is made response is compared with previous value and callback is fired only if current value is different then the previous one.

By default all queries are made (batched) only when new block is imported (eth.filter('latest') fires). By passing additional argument (number) you can specify polling interval manually.

TODO

  • - Support time-based polling (.onChange(...args, callback, 500) - polls every 500s)
  • - Support lightweight, promise-based Web3 replacement jacogr/ethapi-js
  • - Support pending-transactions polling (.onChange(...args, callback, 'pending'))
  • - Support Filter/Logs polling?
  • - Passing old value?

About

Plugin converting Web3 calls into "Event Emitters"

Resources

License

Stars

1 star

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors