From 7f7bab8e03d54f47b574bdfdc68202117ea98142 Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Mon, 24 Aug 2020 16:59:55 -0300 Subject: [PATCH] [IMPROVEMENT] Add subscription and room events on the same batch queue (#2423) * [IMPROVEMENT] Add subscription and room events on the same batch queue * Send both params * Unused var --- app/lib/methods/subscriptions/rooms.js | 61 ++++++++++++-------------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/app/lib/methods/subscriptions/rooms.js b/app/lib/methods/subscriptions/rooms.js index fc76c11a..700a713b 100644 --- a/app/lib/methods/subscriptions/rooms.js +++ b/app/lib/methods/subscriptions/rooms.js @@ -23,10 +23,8 @@ let connectedListener; let disconnectedListener; let streamListener; let subServer; -let subQueue = {}; +let queue = {}; let subTimer = null; -let roomQueue = {}; -let roomTimer = null; const WINDOW_TIME = 500; const createOrUpdateSubscription = async(subscription, room) => { @@ -193,36 +191,38 @@ const createOrUpdateSubscription = async(subscription, room) => { } }; -const debouncedUpdateSub = (subscription) => { +const getSubQueueId = rid => `SUB-${ rid }`; + +const getRoomQueueId = rid => `ROOM-${ rid }`; + +const debouncedUpdate = (subscription) => { if (!subTimer) { subTimer = setTimeout(() => { - const subBatch = subQueue; - subQueue = {}; + const batch = queue; + queue = {}; subTimer = null; - Object.keys(subBatch).forEach((key) => { + Object.keys(batch).forEach((key) => { InteractionManager.runAfterInteractions(() => { - createOrUpdateSubscription(subBatch[key]); + if (batch[key]) { + if (/SUB/.test(key)) { + const sub = batch[key]; + const roomQueueId = getRoomQueueId(sub.rid); + const room = batch[roomQueueId]; + delete batch[roomQueueId]; + createOrUpdateSubscription(sub, room); + } else { + const room = batch[key]; + const subQueueId = getSubQueueId(room._id); + const sub = batch[subQueueId]; + delete batch[subQueueId]; + createOrUpdateSubscription(sub, room); + } + } }); }); }, WINDOW_TIME); } - subQueue[subscription.rid] = subscription; -}; - -const debouncedUpdateRoom = (room) => { - if (!roomTimer) { - roomTimer = setTimeout(() => { - const roomBatch = roomQueue; - roomQueue = {}; - roomTimer = null; - Object.keys(roomBatch).forEach((key) => { - InteractionManager.runAfterInteractions(() => { - createOrUpdateSubscription(null, roomBatch[key]); - }); - }); - }, WINDOW_TIME); - } - roomQueue[room._id] = room; + queue[subscription.rid ? getSubQueueId(subscription.rid) : getRoomQueueId(subscription._id)] = subscription; }; export default function subscribeRooms() { @@ -280,12 +280,12 @@ export default function subscribeRooms() { log(e); } } else { - debouncedUpdateSub(data); + debouncedUpdate(data); } } if (/rooms/.test(ev)) { if (type === 'updated' || type === 'inserted') { - debouncedUpdateRoom(data); + debouncedUpdate(data); } } if (/message/.test(ev)) { @@ -348,16 +348,11 @@ export default function subscribeRooms() { streamListener.then(removeListener); streamListener = false; } - subQueue = {}; - roomQueue = {}; + queue = {}; if (subTimer) { clearTimeout(subTimer); subTimer = false; } - if (roomTimer) { - clearTimeout(roomTimer); - roomTimer = false; - } }; connectedListener = this.sdk.onStreamData('connected', handleConnection);