support start disconnected

This commit is contained in:
Kevin Jahns 2020-01-23 05:03:38 +01:00
parent d040651574
commit 4da78d07e0

View File

@ -167,9 +167,9 @@ export class WebsocketProvider extends Observable {
* @param {string} url * @param {string} url
* @param {string} roomname * @param {string} roomname
* @param {Y.Doc} doc * @param {Y.Doc} doc
* @param {{awareness:awarenessProtocol.Awareness,db:any|null}} conf * @param {{connect:boolean,awareness:awarenessProtocol.Awareness,db:any|null}} conf
*/ */
constructor (url, roomname, doc, { awareness = new awarenessProtocol.Awareness(doc), db = null } = /** @type {any} */ ({})) { constructor (url, roomname, doc, { connect = true, awareness = new awarenessProtocol.Awareness(doc), db = null } = /** @type {any} */ ({})) {
super() super()
// ensure that url is always ends with / // ensure that url is always ends with /
while (url[url.length - 1] === '/') { while (url[url.length - 1] === '/') {
@ -202,7 +202,7 @@ export class WebsocketProvider extends Observable {
* Whether to connect to other peers or not * Whether to connect to other peers or not
* @type {boolean} * @type {boolean}
*/ */
this.shouldConnect = true this.shouldConnect = connect
/** /**
* @param {ArrayBuffer} data * @param {ArrayBuffer} data
*/ */
@ -227,6 +227,7 @@ export class WebsocketProvider extends Observable {
broadcastMessage(this, encoding.toUint8Array(encoder)) broadcastMessage(this, encoding.toUint8Array(encoder))
} }
} }
this.doc.on('update', this._updateHandler)
/** /**
* @param {any} changed * @param {any} changed
* @param {any} origin * @param {any} origin
@ -249,8 +250,10 @@ export class WebsocketProvider extends Observable {
/** @type {WebSocket} */ (this.ws).close() /** @type {WebSocket} */ (this.ws).close()
} }
}) })
if (connect) {
this.connect() this.connect()
} }
}
/** /**
* @type {boolean} * @type {boolean}
*/ */
@ -267,28 +270,10 @@ export class WebsocketProvider extends Observable {
clearInterval(this._checkInterval) clearInterval(this._checkInterval)
this.disconnect() this.disconnect()
this.awareness.off('change', this._awarenessUpdateHandler) this.awareness.off('change', this._awarenessUpdateHandler)
this.doc.off('update', this._updateHandler)
super.destroy() super.destroy()
} }
disconnect () { connectBc () {
this.shouldConnect = false
// broadcast message with local awareness state set to null (indicating disconnect)
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, messageAwareness)
encoding.writeVarUint8Array(encoder, awarenessProtocol.encodeAwarenessUpdate(this.awareness, [this.doc.clientID], new Map()))
broadcastMessage(this, encoding.toUint8Array(encoder))
if (this.ws !== null) {
this.ws.close()
}
if (this.bcconnected) {
bc.unsubscribe(this.url, this._bcSubscriber)
this.bcconnected = false
}
this.doc.off('update', this._updateHandler)
}
connect () {
this.shouldConnect = true
if (!this.wsconnected && this.ws === null) {
setupWS(this)
if (!this.bcconnected) { if (!this.bcconnected) {
bc.subscribe(this.url, this._bcSubscriber) bc.subscribe(this.url, this._bcSubscriber)
this.bcconnected = true this.bcconnected = true
@ -315,7 +300,30 @@ export class WebsocketProvider extends Observable {
encoding.writeVarUint8Array(encoderAwarenessState, awarenessProtocol.encodeAwarenessUpdate(this.awareness, [this.doc.clientID])) encoding.writeVarUint8Array(encoderAwarenessState, awarenessProtocol.encodeAwarenessUpdate(this.awareness, [this.doc.clientID]))
bc.publish(this.url, encoding.toUint8Array(encoderAwarenessState)) bc.publish(this.url, encoding.toUint8Array(encoderAwarenessState))
}) })
this.doc.on('update', this._updateHandler) }
disconnectBc () {
// broadcast message with local awareness state set to null (indicating disconnect)
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, messageAwareness)
encoding.writeVarUint8Array(encoder, awarenessProtocol.encodeAwarenessUpdate(this.awareness, [this.doc.clientID], new Map()))
broadcastMessage(this, encoding.toUint8Array(encoder))
if (this.bcconnected) {
bc.unsubscribe(this.url, this._bcSubscriber)
this.bcconnected = false
}
}
disconnect () {
this.shouldConnect = false
this.disconnectBc()
if (this.ws !== null) {
this.ws.close()
}
}
connect () {
this.shouldConnect = true
if (!this.wsconnected && this.ws === null) {
setupWS(this)
this.connectBc()
} }
} }
} }