Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Adding debounce to prevent client breaking for very high rate channels.
Browse files Browse the repository at this point in the history
  • Loading branch information
crspeller committed Nov 14, 2018
1 parent 738a048 commit be186eb
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion actions/websocket_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,50 @@ function handleChannelMemberUpdatedEvent(msg) {
dispatch({type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER, data: channelMember});
}

function handleNewPostEvent(msg) {
export function debouncePostEvent(func, wait) {
let timeout;
let queue = [];
return function fx(...args) {
// Called when timeout triggered
const triggered = () => {
timeout = null;
if (queue.length > 0) {
const posts = {};
let channelId = '';
queue.map((queuedArgs) => {
const post = JSON.parse(queuedArgs[0].data.post);
posts[post.id] = post;
channelId = post.channel_id;
return null;
});
dispatch({
type: PostTypes.RECEIVED_POSTS,
data: {posts},
channelId,
});
}
queue = [];
};
if (timeout) {
// If the timeout is going this is the second or further event so queue them up.
if (queue.push(args) > 200) {
// Don't run us out of memory, give up if the queue gets insane
queue = [];
console.log('channel broken because of too many incoming messages'); //eslint-disable-line no-console
}
clearTimeout(timeout);
timeout = setTimeout(triggered, wait);
} else {
// Apply immediatly for single event
Reflect.apply(func, this, args);
timeout = setTimeout(triggered, wait);
}
};
}

const handleNewPostEvent = debouncePostEvent(handleNewPostEventWrapped, 100);

function handleNewPostEventWrapped(msg) {
const post = JSON.parse(msg.data.post);
dispatch(handleNewPost(post, msg));

Expand Down

0 comments on commit be186eb

Please sign in to comment.