Firebase syncing causes lag

回眸只為那壹抹淺笑 提交于 2019-12-08 09:35:09

问题


I am working on a project where I use Polymer in combination with Firebase. I have a knob element that is just a knob which value is stored in Firebase. The value of the knob is observed by a polymer observer, that when the value changes updates the firebase value.

The problem is the following : when the value is changed in one place it updates the firebase value. This emits a change event to all other places. In other places this makes the value in the element being set and hence the observer triggered. This observer causes the firebase value being set again. This in turn emits again a change, and so forth ... This causes laggy behaviour when adapting the knob. What can I do?

<script>

    Polymer({
        is: 'disco-ccontrol',

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

        _computeRef: function(channel, channelNumber) {

            var ref = new Firebase("https://incandescent-inferno-8405.firebaseio.com/user/"+this.channel+'/'+this.channelNumber);
            ref.on("child_changed", function(data) {
               this.midiValue = data.val();
            }.bind(this));

            return ref;
        },

        _valueChanged: function() {
            var message = { value: this.midiValue, channel: this.channel, channelNumber: this.channelNumber };
            if (this.ref) {
                this.ref.set(message);
            } 
        }

    });

</script>

回答1:


How about instead of

ref.on("child_changed", function(data) {

you use

ref.once("child_changed", function(data) {

to only get the value from firebase at startup & after that you are only updating the value in firebase.

Of course depends on your overall app architecture, but from what you show here it seems ok.



来源:https://stackoverflow.com/questions/32529499/firebase-syncing-causes-lag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!