repoll
Automate polling in your React components
Table of contents
Installation
$ npm i repoll --save
Usage
; ; @ state = counter: 0 ; { this; }; { <div> I will increment the counter every 5 seconds The current value is thiscounter </div> }
In the above example, the foo
function will be called every 5 seconds starting when the component's constructor
function is fired. This is the most basic usage, but there are multiple ways you can tweak how the intervals are managed.
Injected values
When the class is decorated, an instance value of repollIntervals
is injected into the class, which houses all of the intervals applied via repoll
. repollIntervals
is an object whose properties are each objects themselves reflecting a specific interval:
{
interval: Number,
name: String,
start: Function,
stop: Function
}
The interval and the name are just metadata; name
is the name of the function, whereas interval
is the ID of the interval created internally by setInterval
. The start
and stop
functions will respectively start and stop the polling of the function.
const pollingFunctions = foo: 500; @ { console; }; { console; // {foo: {interval: 0, name: 'foo', start: [Function], stop: [Function]}} return <div> Stuffz </div> ; }
You do should not need to make use of it unless you are applying advanced usages.
Additionally, the functions that are wired for polling will have the interval injected into it:
const pollingFunctions = foo: 500; @ counter = 0; { if thiscounter === 3 console; fooInterval; thiscounter++; }; { return <div> Stuffz </div> ; }
Parameters
repoll(pollingFunctions: object, options: object = {})
The decorator is a composed function that accepts up to two parameters and returns a function that extends the ReactComponent
that is passed to it.
pollingFunctions
This is a simple map of functionName: intervalInMilliseconds
, where each functionName
reflects a function instantiated in the ReactComponent
. Example:
const pollingFunctions = foo: 5000 bar: 2000; @ { console; } { console; } { return <div> I poll ths stuffz </div> ; }
As you can see, both foo
and bar
line up with the named methods in the class. This means that those methods will fire at the intervalInMilliseconds
assigned to them in the map. If a name is called out in the map but does not exist in the class, an error is fired.
options
An object that is passed to override certain default options. The options available are:
- autoStart boolean, defaults to true
- stopOnUnmount boolean, defaults to true
For example, if you want to only trigger the polling after some event:
const pollingFunctions = foo: 500;const options = autoStart: false; @ { console; }; { thisrepollIntervalsfoostart; }; { return <button onClick=thisonClickButton type="button" > Click me to start polling </button> ; }
Or if you wanted the polling to continue even after the component unmounts:
const pollingFunctions = foo: 500;const options = stopOnUnmount: false; @ { console; }; { return <div> I will poll forever... </div> ; }
Development
Standard stuff, clone the repo and npm i
to get the dependencies. npm scripts available:
compile-for-publish
=> runs thelint
andtranspile
scriptsdev
=> runs the webpack dev server for the playgroundlint
=> runs ESLint against files in thesrc
folderprepublish
=> if in publish, runscompile-for-publish
test
=> runs AVA withNODE_ENV=test
test:watch
=> runstest
but with persistent watchertranspile
=> runs Babel against files insrc
to files inlib