How to prevent the event "value" on the client who issued the set?

The Firebase client-client set() will start all connected value clients - including - the original client that issued set() .

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

Is there a way for the client not to accept / prevent / ignore the value event caused by its own set() call? I considered using off / on around set() , but this could cause the client to skip value events that came at the same time but were not triggered by it.

Am I missing something obvious?

+2
javascript firebase firebase-database
source share
2 answers

As a result, I added the client identifier 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 }); 
+1
source share

Most applications treat Firebase data as their model. Therefore, when there is an update, they call ref.set() (or another mutator function), and then the update returns to their application through the on() event. React / Flux aficionados know this as a unidirectional data stream , others may know it as Segmentation of command line response requests .

But there really are cases when the model is already updated, and therefore you want to ignore the event from Firebase if you are the one who raised it.

There is no API for not receiving abstracts caused by self-issuing. Instead, you will have to โ€œrememberโ€ the data sent to Firebase and filter it in your on() handler.

An Android drawing sample from Firebase stores a list of segments that it sends to Firebase , and then ignores these segments in its onChildAdded handler . It uses push identifiers to identify line segments, and they are generated on the client side, so they can use those that track segment identification.

JavaScript Usage Example:

 var pendingChildIds = []; // Push ids of nodes we've sent to the server, but haven't received in `on()` yet // this code is in your UI event handler, or whatever triggers the needs to update your Firebase data var newChild = ref.push(); pendingChildIds.push(newChild.key()); newChild.set( { property1: 'value1', property2: 3.14 }, function(error) { // the write operation has completed, remove the child id from the list of pending writes pendingChildIds.splice(pendingChildIds.indexOf(newChild.key()); } ); // this is the event handler, using child_added in this case ref.on('child_added', function(snapshot) { if (!pendingChildIds.contains(snapshot.key())) { // this is a child that we DIDN'T generate } }); 
+4
source share

All Articles