support start disconnected
This commit is contained in:
parent
d040651574
commit
4da78d07e0
@ -167,9 +167,9 @@ export class WebsocketProvider extends Observable {
|
||||
* @param {string} url
|
||||
* @param {string} roomname
|
||||
* @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()
|
||||
// ensure that url is always ends with /
|
||||
while (url[url.length - 1] === '/') {
|
||||
@ -202,7 +202,7 @@ export class WebsocketProvider extends Observable {
|
||||
* Whether to connect to other peers or not
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.shouldConnect = true
|
||||
this.shouldConnect = connect
|
||||
/**
|
||||
* @param {ArrayBuffer} data
|
||||
*/
|
||||
@ -227,6 +227,7 @@ export class WebsocketProvider extends Observable {
|
||||
broadcastMessage(this, encoding.toUint8Array(encoder))
|
||||
}
|
||||
}
|
||||
this.doc.on('update', this._updateHandler)
|
||||
/**
|
||||
* @param {any} changed
|
||||
* @param {any} origin
|
||||
@ -249,8 +250,10 @@ export class WebsocketProvider extends Observable {
|
||||
/** @type {WebSocket} */ (this.ws).close()
|
||||
}
|
||||
})
|
||||
if (connect) {
|
||||
this.connect()
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @type {boolean}
|
||||
*/
|
||||
@ -267,28 +270,10 @@ export class WebsocketProvider extends Observable {
|
||||
clearInterval(this._checkInterval)
|
||||
this.disconnect()
|
||||
this.awareness.off('change', this._awarenessUpdateHandler)
|
||||
this.doc.off('update', this._updateHandler)
|
||||
super.destroy()
|
||||
}
|
||||
disconnect () {
|
||||
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)
|
||||
connectBc () {
|
||||
if (!this.bcconnected) {
|
||||
bc.subscribe(this.url, this._bcSubscriber)
|
||||
this.bcconnected = true
|
||||
@ -315,7 +300,30 @@ export class WebsocketProvider extends Observable {
|
||||
encoding.writeVarUint8Array(encoderAwarenessState, awarenessProtocol.encodeAwarenessUpdate(this.awareness, [this.doc.clientID]))
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user