y-websocket
Advanced tools
Comparing version 1.1.1 to 1.2.0
{ | ||
"name": "y-websocket", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Websockets provider for Yjs", | ||
@@ -5,0 +5,0 @@ "main": "./dist/y-websocket.cjs", |
@@ -170,5 +170,5 @@ /* | ||
* @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() | ||
@@ -206,3 +206,3 @@ // ensure that url is always ends with / | ||
*/ | ||
this.shouldConnect = true | ||
this.shouldConnect = connect | ||
/** | ||
@@ -232,2 +232,3 @@ * @param {ArrayBuffer} data | ||
} | ||
this.doc.on('update', this._updateHandler) | ||
/** | ||
@@ -255,3 +256,5 @@ * @param {any} changed | ||
}) | ||
this.connect() | ||
if (connect) { | ||
this.connect() | ||
} | ||
} | ||
@@ -274,6 +277,34 @@ /** | ||
this.awareness.off('change', this._awarenessUpdateHandler) | ||
this.doc.off('update', this._updateHandler) | ||
super.destroy() | ||
} | ||
disconnect () { | ||
this.shouldConnect = false | ||
connectBc () { | ||
if (!this.bcconnected) { | ||
bc.subscribe(this.url, this._bcSubscriber) | ||
this.bcconnected = true | ||
} | ||
// send sync step1 to bc | ||
this.mux(() => { | ||
// write sync step 1 | ||
const encoderSync = encoding.createEncoder() | ||
encoding.writeVarUint(encoderSync, messageSync) | ||
syncProtocol.writeSyncStep1(encoderSync, this.doc) | ||
bc.publish(this.url, encoding.toUint8Array(encoderSync)) | ||
// broadcast local state | ||
const encoderState = encoding.createEncoder() | ||
encoding.writeVarUint(encoderState, messageSync) | ||
syncProtocol.writeSyncStep2(encoderState, this.doc) | ||
bc.publish(this.url, encoding.toUint8Array(encoderState)) | ||
// write queryAwareness | ||
const encoderAwarenessQuery = encoding.createEncoder() | ||
encoding.writeVarUint(encoderAwarenessQuery, messageQueryAwareness) | ||
bc.publish(this.url, encoding.toUint8Array(encoderAwarenessQuery)) | ||
// broadcast local awareness state | ||
const encoderAwarenessState = encoding.createEncoder() | ||
encoding.writeVarUint(encoderAwarenessState, messageAwareness) | ||
encoding.writeVarUint8Array(encoderAwarenessState, awarenessProtocol.encodeAwarenessUpdate(this.awareness, [this.doc.clientID])) | ||
bc.publish(this.url, encoding.toUint8Array(encoderAwarenessState)) | ||
}) | ||
} | ||
disconnectBc () { | ||
// broadcast message with local awareness state set to null (indicating disconnect) | ||
@@ -284,5 +315,2 @@ const encoder = encoding.createEncoder() | ||
broadcastMessage(this, encoding.toUint8Array(encoder)) | ||
if (this.ws !== null) { | ||
this.ws.close() | ||
} | ||
if (this.bcconnected) { | ||
@@ -292,4 +320,10 @@ bc.unsubscribe(this.url, this._bcSubscriber) | ||
} | ||
this.doc.off('update', this._updateHandler) | ||
} | ||
disconnect () { | ||
this.shouldConnect = false | ||
this.disconnectBc() | ||
if (this.ws !== null) { | ||
this.ws.close() | ||
} | ||
} | ||
connect () { | ||
@@ -299,31 +333,5 @@ this.shouldConnect = true | ||
setupWS(this) | ||
if (!this.bcconnected) { | ||
bc.subscribe(this.url, this._bcSubscriber) | ||
this.bcconnected = true | ||
} | ||
// send sync step1 to bc | ||
this.mux(() => { | ||
// write sync step 1 | ||
const encoderSync = encoding.createEncoder() | ||
encoding.writeVarUint(encoderSync, messageSync) | ||
syncProtocol.writeSyncStep1(encoderSync, this.doc) | ||
bc.publish(this.url, encoding.toUint8Array(encoderSync)) | ||
// broadcast local state | ||
const encoderState = encoding.createEncoder() | ||
encoding.writeVarUint(encoderState, messageSync) | ||
syncProtocol.writeSyncStep2(encoderState, this.doc) | ||
bc.publish(this.url, encoding.toUint8Array(encoderState)) | ||
// write queryAwareness | ||
const encoderAwarenessQuery = encoding.createEncoder() | ||
encoding.writeVarUint(encoderAwarenessQuery, messageQueryAwareness) | ||
bc.publish(this.url, encoding.toUint8Array(encoderAwarenessQuery)) | ||
// broadcast local awareness state | ||
const encoderAwarenessState = encoding.createEncoder() | ||
encoding.writeVarUint(encoderAwarenessState, messageAwareness) | ||
encoding.writeVarUint8Array(encoderAwarenessState, awarenessProtocol.encodeAwarenessUpdate(this.awareness, [this.doc.clientID])) | ||
bc.publish(this.url, encoding.toUint8Array(encoderAwarenessState)) | ||
}) | ||
this.doc.on('update', this._updateHandler) | ||
this.connectBc() | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
58186
875