Async Throttler
This utility will delay the resolution of a promise until a delay has elapsed. It also exposes state of the passed promise so that ui events can be keyed off that.
Usage - AsyncThrottler
create
Pass in your state update handler so that you can receive throttler updates. Returns a throttler instance
import asyncThrottler from '@scoir/async-throttler';
class ExampleComp extends React.Component {
componentWillMount () {
this.throttler = asyncThrottler.create(state => this.setState(state))
}
...
}
state update handler
When the throttler state changes, it will execute the callback that was passed into the create function. That object looks like this:
{
isProcessing: <boolean>,
isProcessed: <boolean>,
isComplete: <boolean>,
isError: <boolean>,
}
Usage - throttler instance
execute
Pass in the promise you would like to throttle and an optional delay (default is 700ms)
this.throttler.execute(this.doAsync());
this.throttler.execute(this.doAsync(), 1000);
reset
Restores the throttler instance state back to its initial state.
this.throttler.reset();
{
isProcessing: false,
isProcessed: false,
isComplete: false,
isError: false,
}