Mobx debounced autorun function with immediate synchronous first call
If you need to run some heavy action (such as data fetching from server) when your observables change, you usually want to debounce your action. But you probably need the first run of your action to be synchronous (so the first fetching starts right away).
Mobx has autorunAsync, but it may not suit you for the following reasons:
- it provides throttled (rate limiting), not debounced behaviour
- it doesn't run your action immediately, so your initial server request will be delayed for
delay
miliseconds, which is probably not what you want, especially for server-side rendering
$ npm install --save mobx-autorun-async-immediate
const autorunAsyncImmediate = require('mobx-autorun-async-immediate');
const scope = {};
const delay = 100;
const dispose = autorunAsyncImmediate(() => {
// send request to fetch data from server
}, delay, scope);
// request is already sent here
MIT © dettier