@@ -194,6 +194,15 @@ define(function (require, exports, module) {
194194 }
195195 }
196196
197+ const processedMessageIDs = new Phoenix . libs . LRUCache ( {
198+ max : 1000
199+ // we dont need to set a ttl here as message ids are unique throughout lifetime. And old ids will
200+ // start getting evited from the cache. the message ids are only an issue within a fraction of a seconds when
201+ // a series of messages are sent in quick succession. Eg. user click on a div and there are 3 tabs and due to
202+ // the reflection bug, we almost immediately get 3 messages with the same id. So that will be in this cache
203+ // for a fraction of a second. so a size of 1000 should be more than enough.
204+ } ) ;
205+
197206 /**
198207 * @private
199208 * Handles a message received from the remote protocol handler via the transport.
@@ -203,12 +212,21 @@ define(function (require, exports, module) {
203212 * TODO: we should probably have a way of returning the results from all clients, not just the first?
204213 *
205214 * @param {number } clientId ID of the client that sent the message
206- * @param {string } msg The message that was sent, in JSON string format
215+ * @param {string } msgStr The message that was sent, in JSON string format
216+ * @param {string } messageID The messageID uniquely identifying a message. in browsers, since we use broadcast
217+ * channels, we get reflections echoes when there are multiple tabs open. Ideally those reflections need to
218+ * be fixed, but that was too complex to fix, so we just reply on the message id to guarantee that a message is
219+ * only processed once and not from any reflections.
207220 */
208- function _receive ( clientId , msgStr ) {
221+ function _receive ( clientId , msgStr , messageID ) {
209222 var msg = JSON . parse ( msgStr ) ,
210223 event = msg . method || "event" ,
211224 deferred ;
225+ if ( messageID && processedMessageIDs . has ( messageID ) ) {
226+ return ; // this message is already processed.
227+ } else if ( messageID ) {
228+ processedMessageIDs . set ( messageID , true ) ;
229+ }
212230 if ( msg . livePreviewEditEnabled ) {
213231 LivePreviewEdit . handleLivePreviewEditOperation ( msg ) ;
214232 }
@@ -305,7 +323,7 @@ define(function (require, exports, module) {
305323 _connect ( msg [ 0 ] , msg [ 1 ] ) ;
306324 } )
307325 . on ( "message.livedev" , function ( event , msg ) {
308- _receive ( msg [ 0 ] , msg [ 1 ] ) ;
326+ _receive ( msg [ 0 ] , msg [ 1 ] , msg [ 2 ] ) ;
309327 } )
310328 . on ( "close.livedev" , function ( event , msg ) {
311329 _close ( msg [ 0 ] ) ;
0 commit comments