Code Monkey home page Code Monkey logo

Comments (4)

solkimicreb avatar solkimicreb commented on May 19, 2024

observable is returning a wrapper Proxy around the passed object. It is not doing anything with the original object. Your above example should look like this:

const observableForm = observable(this.form)

observe(() => {
    console.log(observableForm.someField)
})

And then when you change someField the observer will automatically run.

In case you use multiple fields of the observable the observer runs if any of those are mutated.

// this runs when someField or otherField is changed
observe(() => {
    console.log(observableForm.someField + observableForm.otherField)
})

Any property access or mutation at any depth is picked up and causes the relevant observers to rerun. console.log is a special edge case. It is a method that does not use JS to access the object's properties but uses some internal code in most platforms. This means that the observer-util can not pick up property accesses by console.log. You can do this for your use case:

observe(() => {
    console.log(JSON.stringify(observableForm))
})

JSON.stringify is implemented in plain JS and the observer-util can pick up any property access that happens inside it.

If you are interested in how this library works you should read this article which explains it through a very light React wrapper.

I hope this helped.

from observer-util.

jameslkingsley avatar jameslkingsley commented on May 19, 2024

Thanks, that makes sense! What happens if I wanted to have two observable objects, like this?

const observableA = observable(a)
const observableB = observable(b)

observe(() => {
    // Is this A or B that's changed?
})

from observer-util.

solkimicreb avatar solkimicreb commented on May 19, 2024
const observableA = observable({ prop1: 1, prop2: 2 })
const observableB = observable({ prop1: 12, prop2: 'Hello' })

observe(() => {
    console.log(observableA.prop2 + observableB.prop1)
})

The observed function would run when observableA.prop2 or observableB.prop1 are changed since they are used by the observed function. It would not run when observableA.prop1 or observableB.prop2 are changed since they are not used inside the function (and wouldn't result in a different function effect).

from observer-util.

jameslkingsley avatar jameslkingsley commented on May 19, 2024

Interesting, thanks for explaining it to me!

from observer-util.

Related Issues (20)

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.