0.0.2 • Published 5 years ago

mobx-suspend v0.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

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