This commit is contained in:
Kevin Jahns 2020-04-02 12:15:28 +02:00
parent 15a9c99833
commit 1ba2b7a0e7
4 changed files with 19 additions and 14 deletions

6
package-lock.json generated
View File

@ -1552,9 +1552,9 @@
}, },
"dependencies": { "dependencies": {
"minimist": { "minimist": {
"version": "1.2.0", "version": "1.2.5",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz",
"integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "integrity": "sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==",
"dev": true "dev": true
} }
} }

View File

@ -10,6 +10,7 @@
"start": "node ./bin/server.js", "start": "node ./bin/server.js",
"dist": "rm -rf dist && rollup -c && tsc", "dist": "rm -rf dist && rollup -c && tsc",
"lint": "standard && tsc", "lint": "standard && tsc",
"test": "npm run lint",
"preversion": "npm run lint && npm run dist" "preversion": "npm run lint && npm run dist"
}, },
"bin": { "bin": {
@ -40,18 +41,18 @@
] ]
}, },
"dependencies": { "dependencies": {
"lib0": "^0.2.15", "lib0": "^0.2.22",
"y-protocols": "^0.2.0" "y-protocols": "^0.2.3"
}, },
"devDependencies": { "devDependencies": {
"rollup": "^1.31.1", "rollup": "^1.31.1",
"rollup-cli": "^1.0.9", "rollup-cli": "^1.0.9",
"standard": "^12.0.1", "standard": "^12.0.1",
"typescript": "^3.7.5", "typescript": "^3.7.5",
"yjs": "13.0.4" "yjs": "13.0.5"
}, },
"peerDependenies": { "peerDependenies": {
"yjs": "^13.0.4" "yjs": "^13.0.0"
}, },
"optionalDependencies": { "optionalDependencies": {
"ws": "^6.2.1" "ws": "^6.2.1"

View File

@ -48,13 +48,14 @@ const readMessage = (provider, buf, emitSynced) => {
const encoder = encoding.createEncoder() const encoder = encoding.createEncoder()
const messageType = decoding.readVarUint(decoder) const messageType = decoding.readVarUint(decoder)
switch (messageType) { switch (messageType) {
case messageSync: case messageSync: {
encoding.writeVarUint(encoder, messageSync) encoding.writeVarUint(encoder, messageSync)
const syncMessageType = syncProtocol.readSyncMessage(decoder, encoder, provider.doc, provider) const syncMessageType = syncProtocol.readSyncMessage(decoder, encoder, provider.doc, provider)
if (emitSynced && syncMessageType === syncProtocol.messageYjsSyncStep2 && !provider.synced) { if (emitSynced && syncMessageType === syncProtocol.messageYjsSyncStep2 && !provider.synced) {
provider.synced = true provider.synced = true
} }
break break
}
case messageQueryAwareness: case messageQueryAwareness:
encoding.writeVarUint(encoder, messageAwareness) encoding.writeVarUint(encoder, messageAwareness)
encoding.writeVarUint8Array(encoder, awarenessProtocol.encodeAwarenessUpdate(provider.awareness, Array.from(provider.awareness.getStates().keys()))) encoding.writeVarUint8Array(encoder, awarenessProtocol.encodeAwarenessUpdate(provider.awareness, Array.from(provider.awareness.getStates().keys())))
@ -257,18 +258,21 @@ export class WebsocketProvider extends Observable {
this.connect() this.connect()
} }
} }
/** /**
* @type {boolean} * @type {boolean}
*/ */
get synced () { get synced () {
return this._synced return this._synced
} }
set synced (state) { set synced (state) {
if (this._synced !== state) { if (this._synced !== state) {
this._synced = state this._synced = state
this.emit('sync', [state]) this.emit('sync', [state])
} }
} }
destroy () { destroy () {
clearInterval(this._checkInterval) clearInterval(this._checkInterval)
this.disconnect() this.disconnect()
@ -276,6 +280,7 @@ export class WebsocketProvider extends Observable {
this.doc.off('update', this._updateHandler) this.doc.off('update', this._updateHandler)
super.destroy() super.destroy()
} }
connectBc () { connectBc () {
if (!this.bcconnected) { if (!this.bcconnected) {
bc.subscribe(this.bcChannel, this._bcSubscriber) bc.subscribe(this.bcChannel, this._bcSubscriber)
@ -304,6 +309,7 @@ export class WebsocketProvider extends Observable {
bc.publish(this.bcChannel, encoding.toUint8Array(encoderAwarenessState)) bc.publish(this.bcChannel, encoding.toUint8Array(encoderAwarenessState))
}) })
} }
disconnectBc () { disconnectBc () {
// broadcast message with local awareness state set to null (indicating disconnect) // broadcast message with local awareness state set to null (indicating disconnect)
const encoder = encoding.createEncoder() const encoder = encoding.createEncoder()
@ -315,6 +321,7 @@ export class WebsocketProvider extends Observable {
this.bcconnected = false this.bcconnected = false
} }
} }
disconnect () { disconnect () {
this.shouldConnect = false this.shouldConnect = false
this.disconnectBc() this.disconnectBc()
@ -322,6 +329,7 @@ export class WebsocketProvider extends Observable {
this.ws.close() this.ws.close()
} }
} }
connect () { connect () {
this.shouldConnect = true this.shouldConnect = true
if (!this.wsconnected && this.ws === null) { if (!this.wsconnected && this.ws === null) {

View File

@ -39,16 +39,12 @@
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "./", /* Base directory to resolve non-absolute module names. */ "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
"paths": { "paths": {
"yjs": ["node_modules/yjs/src/index.js"],
"lib0/*": ["node_modules/lib0/*"],
"lib0/dist/*": ["node_modules/lib0/dist/*"],
"y-protocols/*": ["node_modules/y-protocols/*"]
}, },
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */ // "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */ // "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
/* Source Map Options */ /* Source Map Options */
@ -60,7 +56,7 @@
/* Experimental Options */ /* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
"maxNodeModuleJsDepth": 5 // "maxNodeModuleJsDepth": 5
}, },
"include": ["./src/y-websocket.js"] "include": ["./src/y-websocket.js"]
} }