[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:
parent
49cdb7e844
commit
7f7bab8e03
|
@ -23,10 +23,8 @@ let connectedListener;
|
||||||
let disconnectedListener;
|
let disconnectedListener;
|
||||||
let streamListener;
|
let streamListener;
|
||||||
let subServer;
|
let subServer;
|
||||||
let subQueue = {};
|
let queue = {};
|
||||||
let subTimer = null;
|
let subTimer = null;
|
||||||
let roomQueue = {};
|
|
||||||
let roomTimer = null;
|
|
||||||
const WINDOW_TIME = 500;
|
const WINDOW_TIME = 500;
|
||||||
|
|
||||||
const createOrUpdateSubscription = async(subscription, room) => {
|
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) {
|
if (!subTimer) {
|
||||||
subTimer = setTimeout(() => {
|
subTimer = setTimeout(() => {
|
||||||
const subBatch = subQueue;
|
const batch = queue;
|
||||||
subQueue = {};
|
queue = {};
|
||||||
subTimer = null;
|
subTimer = null;
|
||||||
Object.keys(subBatch).forEach((key) => {
|
Object.keys(batch).forEach((key) => {
|
||||||
InteractionManager.runAfterInteractions(() => {
|
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);
|
}, WINDOW_TIME);
|
||||||
}
|
}
|
||||||
subQueue[subscription.rid] = subscription;
|
queue[subscription.rid ? getSubQueueId(subscription.rid) : getRoomQueueId(subscription._id)] = 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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function subscribeRooms() {
|
export default function subscribeRooms() {
|
||||||
|
@ -280,12 +280,12 @@ export default function subscribeRooms() {
|
||||||
log(e);
|
log(e);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
debouncedUpdateSub(data);
|
debouncedUpdate(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (/rooms/.test(ev)) {
|
if (/rooms/.test(ev)) {
|
||||||
if (type === 'updated' || type === 'inserted') {
|
if (type === 'updated' || type === 'inserted') {
|
||||||
debouncedUpdateRoom(data);
|
debouncedUpdate(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (/message/.test(ev)) {
|
if (/message/.test(ev)) {
|
||||||
|
@ -348,16 +348,11 @@ export default function subscribeRooms() {
|
||||||
streamListener.then(removeListener);
|
streamListener.then(removeListener);
|
||||||
streamListener = false;
|
streamListener = false;
|
||||||
}
|
}
|
||||||
subQueue = {};
|
queue = {};
|
||||||
roomQueue = {};
|
|
||||||
if (subTimer) {
|
if (subTimer) {
|
||||||
clearTimeout(subTimer);
|
clearTimeout(subTimer);
|
||||||
subTimer = false;
|
subTimer = false;
|
||||||
}
|
}
|
||||||
if (roomTimer) {
|
|
||||||
clearTimeout(roomTimer);
|
|
||||||
roomTimer = false;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
connectedListener = this.sdk.onStreamData('connected', handleConnection);
|
connectedListener = this.sdk.onStreamData('connected', handleConnection);
|
||||||
|
|
Loading…
Reference in New Issue