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

This commit is contained in:
Alexis Métaireau 2024-10-25 16:53:37 +02:00
parent 4ba5bbc542
commit 67c0467e5e
No known key found for this signature in database
GPG key ID: 1C21B876828E5FF2
2 changed files with 15 additions and 2 deletions

View file

@ -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.
@ -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)
}
/**
@ -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)
}

View file

@ -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) {
let timeoutId = null;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
callback.apply(null, args);
}, wait);
};
}