Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sync): Only send sync events after one second of inactivity #2238

Open
wants to merge 1 commit into
base: websocket-reconnect
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions umap/static/umap/js/modules/sync/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { WebSocketTransport } from './websocket.js'
const RECONNECT_DELAY = 2000;
const RECONNECT_DELAY_FACTOR = 2;
const MAX_RECONNECT_DELAY = 32000;
const DEBOUNCE_DELAY = 1000;

/**
* The syncEngine exposes an API to sync messages between peers over the network.
Expand Down Expand Up @@ -64,6 +65,7 @@ export class SyncEngine {
this._reconnectTimeout = null;
this._reconnectDelay = RECONNECT_DELAY;
this.websocketConnected = false;
this._send = Utils.debounce(this.__send.bind(this), DEBOUNCE_DELAY)
}

/**
Expand Down Expand Up @@ -121,10 +123,9 @@ export class SyncEngine {
this._send({ verb: 'delete', subject, metadata, key })
}

_send(inputMessage) {
__send(inputMessage) {
const message = this._operations.addLocal(inputMessage)

if (this.offline) return
if (this.transport) {
this.transport.send('OperationMessage', message)
}
Expand Down
12 changes: 12 additions & 0 deletions umap/static/umap/js/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,15 @@ export function deepEqual(object1, object2) {
export function slugify(str) {
return (str || 'data').replace(/[^a-z0-9]/gi, '_').toLowerCase()
}

export function debounce(callback, wait) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let timeoutId = null;

return (...args) => {
clearTimeout(timeoutId);

timeoutId = setTimeout(() => {
callback.apply(null, args);
}, wait);
};
}