[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
This commit is contained in:
Diego Mello 2020-08-24 16:59:55 -03:00 committed by GitHub
parent 49cdb7e844
commit 7f7bab8e03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 33 deletions

View File

@ -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);