implement resyncInterval - #19
This commit is contained in:
parent
dedc7f56a6
commit
ce66920e31
@ -174,8 +174,9 @@ export class WebsocketProvider extends Observable {
|
|||||||
* @param {awarenessProtocol.Awareness} [opts.awareness]
|
* @param {awarenessProtocol.Awareness} [opts.awareness]
|
||||||
* @param {Object<string,string>} [opts.params]
|
* @param {Object<string,string>} [opts.params]
|
||||||
* @param {typeof WebSocket} [opts.WebSocketPolyfill] Optionall provide a WebSocket polyfill
|
* @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()
|
super()
|
||||||
// ensure that url is always ends with /
|
// ensure that url is always ends with /
|
||||||
while (serverUrl[serverUrl.length - 1] === '/') {
|
while (serverUrl[serverUrl.length - 1] === '/') {
|
||||||
@ -211,6 +212,32 @@ export class WebsocketProvider extends Observable {
|
|||||||
* @type {boolean}
|
* @type {boolean}
|
||||||
*/
|
*/
|
||||||
this.shouldConnect = connect
|
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
|
* @param {ArrayBuffer} data
|
||||||
*/
|
*/
|
||||||
@ -278,6 +305,9 @@ export class WebsocketProvider extends Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
destroy () {
|
destroy () {
|
||||||
|
if (this._resyncInterval !== 0) {
|
||||||
|
clearInterval(/** @type {NodeJS.Timeout} */ (this._resyncInterval))
|
||||||
|
}
|
||||||
clearInterval(this._checkInterval)
|
clearInterval(this._checkInterval)
|
||||||
this.disconnect()
|
this.disconnect()
|
||||||
this.awareness.off('update', this._awarenessUpdateHandler)
|
this.awareness.off('update', this._awarenessUpdateHandler)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user