y-websocket
Advanced tools
Comparing version
@@ -15,4 +15,4 @@ const Y = require('yjs') | ||
const CALLBACK_DEBOUNCE_WAIT = process.env.CALLBACK_DEBOUNCE_WAIT || 2000 | ||
const CALLBACK_DEBOUNCE_MAXWAIT = process.env.CALLBACK_DEBOUNCE_MAXWAIT || 10000 | ||
const CALLBACK_DEBOUNCE_WAIT = parseInt(process.env.CALLBACK_DEBOUNCE_WAIT) || 2000 | ||
const CALLBACK_DEBOUNCE_MAXWAIT = parseInt(process.env.CALLBACK_DEBOUNCE_MAXWAIT) || 10000 | ||
@@ -28,3 +28,3 @@ const wsReadyStateConnecting = 0 | ||
/** | ||
* @type {{bindState: function(string,WSSharedDoc):void, writeState:function(string,WSSharedDoc):Promise<any>}|null} | ||
* @type {{bindState: function(string,WSSharedDoc):void, writeState:function(string,WSSharedDoc):Promise<any>, provider: any}|null} | ||
*/ | ||
@@ -38,2 +38,3 @@ let persistence = null | ||
persistence = { | ||
provider: ldb, | ||
bindState: async (docName, ydoc) => { | ||
@@ -54,3 +55,3 @@ const persistedYdoc = await ldb.getYDoc(docName) | ||
* @param {{bindState: function(string,WSSharedDoc):void, | ||
* writeState:function(string,WSSharedDoc):Promise<any>}|null} persistence_ | ||
* writeState:function(string,WSSharedDoc):Promise<any>,provider:any}|null} persistence_ | ||
*/ | ||
@@ -62,2 +63,8 @@ exports.setPersistence = persistence_ => { | ||
/** | ||
* @return {null|{bindState: function(string,WSSharedDoc):void, | ||
* writeState:function(string,WSSharedDoc):Promise<any>}|null} used persistence layer | ||
*/ | ||
exports.getPersistence = () => persistence | ||
/** | ||
* @type {Map<string,WSSharedDoc>} | ||
@@ -139,2 +146,21 @@ */ | ||
/** | ||
* Gets a Y.Doc by name, whether in memory or on disk | ||
* | ||
* @param {string} docname - the name of the Y.Doc to find or create | ||
* @param {boolean} gc - whether to allow gc on the doc (applies only when created) | ||
* @return {WSSharedDoc} | ||
*/ | ||
const getYDoc = (docname, gc = true) => map.setIfUndefined(docs, docname, () => { | ||
const doc = new WSSharedDoc(docname) | ||
doc.gc = gc | ||
if (persistence !== null) { | ||
persistence.bindState(docname, doc) | ||
} | ||
docs.set(docname, doc) | ||
return doc | ||
}) | ||
exports.getYDoc = getYDoc | ||
/** | ||
* @param {any} conn | ||
@@ -212,12 +238,4 @@ * @param {WSSharedDoc} doc | ||
conn.binaryType = 'arraybuffer' | ||
// get doc, create if it does not exist yet | ||
const doc = map.setIfUndefined(docs, docName, () => { | ||
const doc = new WSSharedDoc(docName) | ||
doc.gc = gc | ||
if (persistence !== null) { | ||
persistence.bindState(docName, doc) | ||
} | ||
docs.set(docName, doc) | ||
return doc | ||
}) | ||
// get doc, initialize if it does not exist yet | ||
const doc = getYDoc(docName, gc) | ||
doc.conns.set(conn, new Set()) | ||
@@ -224,0 +242,0 @@ // listen and reply to events |
@@ -47,8 +47,2 @@ /** | ||
}; | ||
/** | ||
* @type {Object<string,Object>} | ||
*/ | ||
_localAwarenessState: { | ||
[x: string]: any; | ||
}; | ||
awareness: awarenessProtocol.Awareness; | ||
@@ -55,0 +49,0 @@ wsconnected: boolean; |
{ | ||
"name": "y-websocket", | ||
"version": "1.3.5", | ||
"version": "1.3.6", | ||
"description": "Websockets provider for Yjs", | ||
@@ -58,3 +58,3 @@ "main": "./dist/y-websocket.cjs", | ||
"typescript": "^3.9.6", | ||
"yjs": "13.0.5" | ||
"yjs": "^13.4.1" | ||
}, | ||
@@ -61,0 +61,0 @@ "peerDependenies": { |
@@ -15,3 +15,3 @@ | ||
import * as Y from 'yjs' | ||
import { WebsocketProvider } from 'yjs/provider/websocket.js' | ||
import { WebsocketProvider } from 'y-websocket' | ||
@@ -18,0 +18,0 @@ const doc = new Y.Doc() |
@@ -87,2 +87,7 @@ /* | ||
provider.synced = false | ||
provider.emit('status', [{ | ||
status: 'connecting' | ||
}]) | ||
websocket.onmessage = event => { | ||
@@ -150,3 +155,3 @@ provider.wsLastMessageReceived = time.getUnixTime() | ||
provider.mux(() => { | ||
bc.publish(provider.url, buf) | ||
bc.publish(provider.bcChannel, buf) | ||
}) | ||
@@ -193,6 +198,2 @@ } | ||
this._WS = WebSocketPolyfill | ||
/** | ||
* @type {Object<string,Object>} | ||
*/ | ||
this._localAwarenessState = {} | ||
this.awareness = awareness | ||
@@ -199,0 +200,0 @@ this.wsconnected = false |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
71143
0.67%1146
0.61%