Code Monkey home page Code Monkey logo

mobx-clone's Introduction

mobx-clone

How mobx works under the hood

componentDidMount() {
  autorun(()=>{
    this.render()
  })
}

What autorun does is registers itself in some global stack array.

Then when some observable is accessed, it reads the latest entry in the global stack, and calls one of its methods: add dependency.

Now this creates the link between the observable and the observer.

Now whenever user tries to modify the observable, it triggers a call to the observer.

Now what happens when you have multiple autoruns like

autorun(()=>{
  someMethod() {
    autorun(()=>{
      someObservable.get()
    }
  }

})

Since an observable only reads the latest entry in the global stack, it will only use the second autorun. So the first auto run won't re-trigger when the observable changes.

This is how the computed method works.

function computed(thunk) {
  var current = observable(undefined)
  autorun(() => {
    current.set(thunk())
  })
  return current
}

Perhaps transactions in mobx also works this way, except that it uses an internal flag to tell the observables not to invoke the run method. Then at the end of the transaction, it notifies the autorun that something have changed.

autorun(()=>{
  transaction() {
    autorun(()=>{
      /**
       * modify observables here
       */
    }
     /**
       * notify the first autorun
       */
  }

})

JavaScript Style Guide

Install

npm install --save mobx-clone

Usage

import * as React from "react"

import MyComponent from "mobx-clone"

class Example extends React.Component {
  render() {
    return <MyComponent />
  }
}

License

MIT © codiechanel

mobx-clone's People

Contributors

codiechanel avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.