如何防止发布的客户端上的“值”事件? [英] How to prevent 'value' event on the client that issued set?

查看:133
本文介绍了如何防止发布的客户端上的“值”事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个调用 set()的Firebase客户端将导致所有连接的客户端拥有被触发 - - 发布 set()的原始客户端。



在大多数情况下),发出 set()的客户端没有理由对自己调用产生的值事件做出响应。显然,它的模型是正确的,没有必要改变它(这可能是一个昂贵的操作)。

有什么办法让客户端不能接收/防止/忽略由其自己的 set()调用触发的事件。我考虑在 set()附近使用off / on,但是这可以使客户端错过 value 事件时间,但并没有触发它。



我是否缺少明显的东西?

解决方案


$ $ $ code $ var clientId =(Math.random( )* 10000000000000000).toFixed(0);

function set(data){
ref.set(JSON.stringify({clientId:clientId,data:data}));

$ b ref.on('value',function(snapshot){
var json = JSON.parse(snapshot.val());
if( !json || json.clientId === clientId)return;

var data = json.data;
//用数据更新模型
});


A Firebase client calling set() will cause all connected clients to have value triggered - including - the original client that issued the set().

In my case (and I think in most cases), there is no reason for the client that issued the set() to respond to the value event produced by its own call. Obviously its model is correct and there's no need to change it (which may be an expensive operation).

Is there any way for the client to not-receive/prevent/ignore the value event triggered by its own set() call ? I considered using off/on around set() but that can make the client miss value events that came at the same time but were not triggered by it.

Am I missing something obvious ?

解决方案

I ended up adding a client ID to the model, something like:

var clientId=(Math.random()*10000000000000000).toFixed(0);

function set(data) {
    ref.set(JSON.stringify({ clientId: clientId, data: data }));
}

ref.on('value', function(snapshot) {
    var json=JSON.parse(snapshot.val());
    if (!json || json.clientId===clientId) return;

    var data=json.data;
    // update model with data
});

这篇关于如何防止发布的客户端上的“值”事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆