Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to modify a observed valued without triggering the observer callback in Polymer

Like the title says. Is there a way to modify a observed valued without triggering the observer callback in Polymer?

For example

Polymer({
        is: 'my-component',

        properties: {
            aValue: {
                type: Number,
                value: 0,
                observer: '_valueChanged',
                notify: true
            },
            ref: {
                type: Object,
                computed: '_computeRef(channel, channelNumber)'
            }
        },

        _computeRef: function(channel, channelNumber) {

            var ref = new Firebase("/*link*/");
            ref.on("child_changed", function(data) {
               this.aValue.setWithoutCallingObserver(data.val());
            }.bind(this));

            return ref;
        },

        _valueChanged: function() {
            var message = { aValue: this.aValue };
            if (this.ref) {
                this.ref.set(message);
            }
        }

    });

This would be useful because now I am experiencing lag in the following scenario:

  1. Adapting aValue in a third party app
  2. Firebase updates all clients
  3. The .on callback sets the value and triggers the observer callback
  4. Causes a .set to firebase
  5. go back to 2.

Update: The issue is not firebase related. I believe the solution is in getting control over how updates are applied to observed values in Polymer. Partly because changes to the values in the firebase store can be made by 3th party (not necessarily web) applications too.

like image 962
Jeremy Knees Avatar asked Sep 19 '15 14:09

Jeremy Knees


People also ask

Is it possible to change observed values without using an action?

[MobX] Since strict-mode is enabled, changing (observed) observable values without using an action is not allowed. Tried to modify: [email protected] Everything works as expected. How can I fix this issue? Show activity on this post.

What is the use of makeautoobservable in react?

For example, if you changing two values inside some handler without action then your component might re-render twice instead of once. makeAutoObservable wraps all methods automatically but you still need to handle async methods and Promises manually. Thanks for contributing an answer to Stack Overflow!

Can the volume of a model view controller be accessed through slider?

The ONLY thing that accesses the volume is the slider, period. So it would go like this: This basically means you cannot do a Model View Controller architecture using the new UI system without adding quite ugly code (I am currently writing said ugly code).


1 Answers

To my knowledge, there is not built in way to set a property value without triggering it's observer.

You don't have control over how/when/with what arguments the observer is called, but you do have control over the procedure body and, luckily, you are working with a shared state (this).

So, you can modify the behavior of the function based on a flag that can be accessed from within the function, but doesn't have to be passed in.

For example:

_valueChanged: function (new_val, old_val) {
   if (this._observerLock) { return; }

   var message = { aValue: this.aValue };
     if (this.ref) {
       this.ref.set(message);
     }
   }
 }, 
...

Then, you can implement the _setWithoutCallingObserver() method like:

_setWithoutCallingObserver: function (value) {
  this._observerLock = true;
  this.aValue = value;
  this._observerLock = false;
}
like image 170
Luke Avatar answered Nov 16 '22 05:11

Luke