Skip to content

Mobx debounced autorun function with immediate synchronous first call

License

Notifications You must be signed in to change notification settings

dettier/mobx-autorun-async-immediate

Repository files navigation

mobx-autorun-async-immediate NPM version Build Status Dependency Status Coverage percentage

Mobx debounced autorun function with immediate synchronous first call

Why use this?

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).

Why not use autorunAsync?

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

Installation

$ npm install --save mobx-autorun-async-immediate

Usage

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

License

MIT © dettier

About

Mobx debounced autorun function with immediate synchronous first call

Resources

License

Stars

Watchers

Forks

Packages

No packages published