Skip to content

Commit

Permalink
implement resyncInterval - #19
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Jul 10, 2020
1 parent dedc7f5 commit ce66920
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/y-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ export class WebsocketProvider extends Observable {
* @param {awarenessProtocol.Awareness} [opts.awareness]
* @param {Object<string,string>} [opts.params]
* @param {typeof WebSocket} [opts.WebSocketPolyfill] Optionall provide a WebSocket polyfill
* @param {number} [opts.resyncInterval] Request server state every `resyncInterval` milliseconds
*/
constructor (serverUrl, roomname, doc, { connect = true, awareness = new awarenessProtocol.Awareness(doc), params = {}, WebSocketPolyfill = WebSocket } = {}) {
constructor (serverUrl, roomname, doc, { connect = true, awareness = new awarenessProtocol.Awareness(doc), params = {}, WebSocketPolyfill = WebSocket, resyncInterval = -1 } = {}) {
super()
// ensure that url is always ends with /
while (serverUrl[serverUrl.length - 1] === '/') {
Expand Down Expand Up @@ -211,6 +212,32 @@ export class WebsocketProvider extends Observable {
* @type {boolean}
*/
this.shouldConnect = connect

/**
* @type {NodeJS.Timeout | number}
*/
this._resyncInterval = 0
if (resyncInterval > 0) {
this._resyncInterval = setInterval(() => {
if (this.ws) {
if (!this.synced) {
alert(`
Please report that this message was shown to https://github.com/yjs/y-websocket/issues
Thank you! ❤
(Sorry for showing this message..`)
console.warn('Client was unsynced anyway')
}
// resend sync step 1
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, messageSync)
syncProtocol.writeSyncStep1(encoder, doc)
this.ws.send(encoding.toUint8Array(encoder))
}
}, resyncInterval)
}

/**
* @param {ArrayBuffer} data
*/
Expand Down Expand Up @@ -278,6 +305,9 @@ export class WebsocketProvider extends Observable {
}

destroy () {
if (this._resyncInterval !== 0) {
clearInterval(/** @type {NodeJS.Timeout} */ (this._resyncInterval))
}
clearInterval(this._checkInterval)
this.disconnect()
this.awareness.off('update', this._awarenessUpdateHandler)
Expand Down

0 comments on commit ce66920

Please sign in to comment.