mobx-autorun-async-immediate

1.2.0 • Public • Published

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

Package Sidebar

Install

npm i mobx-autorun-async-immediate

Weekly Downloads

1

Version

1.2.0

License

MIT

Last publish

Collaborators

  • dettier