【问题标题】:Firebase syncing causes lagFirebase 同步导致延迟
【发布时间】:2015-09-11 18:00:03
【问题描述】:

我正在开展一个项目,我将 Polymer 与 Firebase 结合使用。我有一个旋钮元素,它只是一个旋钮,其值存储在 Firebase 中。旋钮的值由聚合物观察者观察,当值变化时会更新 firebase 值。

问题如下:当值在一个地方更改时,它会更新 firebase 值。这会向所有其他地方发出更改事件。在其他地方,这使得元素中的值被设置,因此观察者被触发。这个观察者会导致再次设置火力值。这反过来又会再次发出变化,依此类推……这会导致调整旋钮时出现滞后行为。我能做什么?

<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>

【问题讨论】:

标签: firebase polymer


【解决方案1】:

如何代替

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

你使用

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

仅在启动时从 firebase 获取值,之后您仅更新 firebase 中的值。

当然取决于您的整体应用架构,但从您在此处显示的内容看来还可以。

【讨论】:

  • 好吧,我不确定是否有足够的时间离开这里来提出一个好的建议。除了也许你可以转向一些不那么紧密耦合的东西来找出一个可行的解决方案,然后再优化代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
  • 2021-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多