question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Optimization] Don't trigger observers if prev value and new value are same

See original GitHub issue

For this snippet -

const observer  = require("@risingstack/nx-observe");

let context = observer.observable({"name" : "a"});

const signal = observer.observe(()=>console.log(context.name));

let name = context.name;

context.name = "a";

The observer is triggered twice. Wouldn’t it help to not trigger observers if the value hasn’t changed?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:45 (23 by maintainers)

github_iconTop GitHub Comments

2reactions
solkimicrebcommented, Dec 21, 2016

nx-observe 3.0.0 is published. It comes with all of the improvements mentioned above.

The examples you mentioned above has the following result with v3.0.0.

1, code:

const observer  = require("@risingstack/nx-observe");

let context = observer.observable({"person" : {name:"a"}});

const signal = observer.observe(()=>console.log(context.person));

setTimeout(()=>context.person.name = "a", 100);
setTimeout(()=>context.person.name = "a", 500);
setTimeout(()=>context.person.name = "a", 1000);
setTimeout(()=>context.person.name = "b", 2000);
setTimeout(()=>context.person.name = "c", 3000);

output:

{ name: 'a' }
{ name: 'b' }
{ name: 'c' }

2, code:

const observer  = require("@risingstack/nx-observe");

let trails = [];

let context = observer.observable({"candidates" : {"first" : {"id" : "first", "name":"Amy"}}});

const signal = observer.observe(()=>trails.push(JSON.stringify(context.candidates)));

setTimeout(()=>context.candidates.second = {"id" : "second", "name":"Tracy"}, 100);
setTimeout(()=>context.candidates.third = {"id" : "third", "name":"Joe"}, 500);

setTimeout(()=>console.log(trails), 2000);

output:

[ '{"first":{"id":"first","name":"Amy"}}',
  '{"first":{"id":"first","name":"Amy"},"second":{"id":"second","name":"Tracy"}}',
  '{"first":{"id":"first","name":"Amy"},"second":{"id":"second","name":"Tracy"},"third":{"id":"third","name":"Joe"}}' ]
1reaction
SukantGujarcommented, Dec 29, 2016

@ippa So far the integration has been good 😃, @solkimicreb has been very helpful and has helped us a lot by fixing issues that came along the way. The implementation is very much like with mobx and design considerations are also similar. The project is not open source yet and I have been working on other challenges with it. But I will try to share a simple example soon on the discussion which @solkimicreb spun from this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

LiveData prevent receive the last value when start observing
I don't think it is possible to prevent LiveData from receiving the last value when start observing if you are using them as...
Read more >
Observers and computed properties - Polymer Project
Observers do not receive old values as arguments, only new values. Only single-property observers defined in the properties object receive both old and...
Read more >
Swift Property Observers - NSHipster
When you declare a stored property, you have the option to define property observers with blocks of code to be executed when a...
Read more >
RxJS: Observables, Observers and Operators Introduction
When an Observable produces values, it then informs the observer, calling .next() when a new value was successfully captured and .error() ...
Read more >
The 10 most used observables operators - Christian Lüdemann
When observers register the ReplaySubject will “replay” previous values to ... are arrays/steams of data, a lot of the Observable operators work the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found