release for Yjs@13.0.0-80

This commit is contained in:
Kevin Jahns 2019-04-26 20:05:06 +02:00
parent 01519447ca
commit cb50b2fbcf
5 changed files with 134 additions and 13112 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
dist
node_modules

14
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "websocket server",
"program": "${workspaceFolder}/bin/server.js"
}
]
}

13133
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -2,7 +2,9 @@
"name": "y-websocket", "name": "y-websocket",
"version": "0.0.0", "version": "0.0.0",
"description": "Websockets provider for Yjs", "description": "Websockets provider for Yjs",
"main": "", "main": "./dist/y-websocket.js",
"module": "./src/y-websocket.js",
"sideEffects": false,
"scripts": { "scripts": {
"start": "node ./bin/server.js", "start": "node ./bin/server.js",
"dist": "rm -rf dist/* && rollup -c", "dist": "rm -rf dist/* && rollup -c",
@ -14,6 +16,9 @@
"bin": { "bin": {
"y-websocket-server": "./bin/server.js" "y-websocket-server": "./bin/server.js"
}, },
"files": [
"dist/*"
],
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/y-js/y-websocket.git" "url": "git+https://github.com/y-js/y-websocket.git"
@ -27,22 +32,19 @@
"url": "https://github.com/y-js/y-websocket/issues" "url": "https://github.com/y-js/y-websocket/issues"
}, },
"homepage": "https://github.com/y-js/y-websocket#readme", "homepage": "https://github.com/y-js/y-websocket#readme",
"dependencies": {}, "dependencies": {
"y-protocols": "0.0.2"
},
"devDependencies": { "devDependencies": {
"rollup": "^1.1.2", "rollup": "^1.1.2",
"rollup-cli": "^1.0.9", "rollup-cli": "^1.0.9",
"standard": "^12.0.1", "standard": "^12.0.1"
"lib0": "file:/../lib0",
"y-leveldb": "file:../y-leveldb",
"y-protocols": "file:../y-protocols",
"yjs": "file:../yjs"
}, },
"peerDependenies": { "peerDependenies": {
"lib0": "^0.0.1", "lib0": "*",
"yjs": "*" "yjs": "13.0.0-80"
}, },
"optionalDependencies": { "optionalDependencies": {
"ws": "^6.2.1", "ws": "^6.2.1"
"y-leveldb": "*"
} }
} }

View File

@ -41,12 +41,12 @@ const readMessage = (doc, buf) => {
switch (messageType) { switch (messageType) {
case messageSync: case messageSync:
encoding.writeVarUint(encoder, messageSync) encoding.writeVarUint(encoder, messageSync)
doc.mux(() => syncProtocol.readSyncMessage(decoder, encoder, doc, doc.ws)
syncProtocol.readSyncMessage(decoder, encoder, doc)
)
break break
case messageAwareness: case messageAwareness:
awarenessProtocol.readAwarenessMessage(decoder, doc) doc.mux(() =>
awarenessProtocol.readAwarenessMessage(decoder, doc)
)
break break
case messageAuth: case messageAuth:
authProtocol.readAuthMessage(decoder, doc, permissionDeniedHandler) authProtocol.readAuthMessage(decoder, doc, permissionDeniedHandler)
@ -86,7 +86,9 @@ const setupWS = (doc, url) => {
doc.emit('status', [{ doc.emit('status', [{
status: 'disconnected' status: 'disconnected'
}]) }])
setTimeout(setupWS, reconnectTimeout, doc, url) if (doc.shouldReconnect) {
setTimeout(setupWS, reconnectTimeout, doc, url)
}
} }
websocket.onopen = () => { websocket.onopen = () => {
doc.wsconnected = true doc.wsconnected = true
@ -108,21 +110,19 @@ const setupWS = (doc, url) => {
* @param {WebsocketsSharedDocument} y * @param {WebsocketsSharedDocument} y
*/ */
const broadcastUpdate = (transaction, y) => { const broadcastUpdate = (transaction, y) => {
if (transaction.updateMessage !== null) { if (transaction.origin !== y.ws && transaction.updateMessage !== null) {
y.mux(() => { const updateMessage = transaction.updateMessage
const updateMessage = transaction.updateMessage if (updateMessage !== null) {
if (updateMessage !== null) { const encoder = encoding.createEncoder()
const encoder = encoding.createEncoder() encoding.writeVarUint(encoder, messageSync)
encoding.writeVarUint(encoder, messageSync) syncProtocol.writeUpdate(encoder, updateMessage)
syncProtocol.writeUpdate(encoder, updateMessage) const buf = encoding.toBuffer(encoder)
const buf = encoding.toBuffer(encoder) if (y.wsconnected) {
if (y.wsconnected) { // @ts-ignore We know that wsconnected = true
// @ts-ignore We know that wsconnected = true y.ws.send(buf)
y.ws.send(buf)
}
bc.publish(y.url, buf)
} }
}) bc.publish(y.url, buf)
}
} }
} }
@ -146,8 +146,7 @@ class WebsocketsSharedDocument extends Y.Y {
* @type {WebSocket?} * @type {WebSocket?}
*/ */
this.ws = null this.ws = null
setupWS(this, url) this.shouldReconnect = true
this.on('afterTransaction', broadcastUpdate)
/** /**
* @param {ArrayBuffer} data * @param {ArrayBuffer} data
*/ */
@ -159,14 +158,30 @@ class WebsocketsSharedDocument extends Y.Y {
} }
}) })
} }
bc.subscribe(url, this._bcSubscriber) this.connect()
// send sync step1 to bc }
this.mux(() => { disconnect () {
const encoder = encoding.createEncoder() this.shouldReconnect = false
encoding.writeVarUint(encoder, messageSync) if (this.ws !== null) {
syncProtocol.writeSyncStep1(encoder, this.store) this.ws.close()
bc.publish(url, encoding.toBuffer(encoder)) bc.unsubscribe(this.url, this._bcSubscriber)
}) this.off('afterTransaction', broadcastUpdate)
}
}
connect () {
this.shouldReconnect = true
if (!this.wsconnected && this.ws === null) {
setupWS(this, this.url)
bc.subscribe(this.url, this._bcSubscriber)
// send sync step1 to bc
this.mux(() => {
const encoder = encoding.createEncoder()
encoding.writeVarUint(encoder, messageSync)
syncProtocol.writeSyncStep1(encoder, this.store)
bc.publish(this.url, encoding.toBuffer(encoder))
})
this.on('afterTransaction', broadcastUpdate)
}
} }
getLocalAwarenessInfo () { getLocalAwarenessInfo () {
return this._localAwarenessState return this._localAwarenessState