mobx-suspend
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

mobx-suspend

npm version build status npm downloads

mobx utilities to suspend/unsuspend reactions

mobx-suspend contains functions you can use instead of the standard mobx reaction and computed features that allows them to be suspended dynamically.

Installation

$ npm install mobx-suspend --save

or if using yarn

$ yarn add mobx-suspend

Basic Usage

Suspendable computed

    import {observable,autorun,computed} from 'mobx';
    import {suspendableComputed} from "mobx-suspend";
    
    const myComputed = suspendableComputed();
    
    class MyClass {
        @observable
        value = "initial";
        @myComputed
        get actual(){
         return `${this.value}-with-addition`
        }
    }
    const instance = new MyClass();
    autorun(()=>console.log(instance.value))
    myComputed.suspend();
    instance.value = "first change";
    // console.log is not called
    instance.value = "second change";
    //console.log is not called
    myComputed.unsuspend();

Suspendable reaction

    import {suspendableReaction} from "mobx-suspend";
    import {observable} from "mobx";
    
    const value = observable.box("initial");
    const reaction = suspendableReaction(()=>value.get(),v=>console.log(v))
    reaction.suspend();
    value.set("new value");
    //the console.log action is not called
    reaction.unsuspend()
    //the console.log action is called with "new value"

License

MIT

Dependents (0)

Package Sidebar

Install

npm i mobx-suspend

Weekly Downloads

0

Version

0.0.2

License

MIT

Unpacked Size

21 kB

Total Files

13

Last publish

Collaborators

  • alonbd