This commit is contained in:
Kevin Jahns 2019-07-31 18:44:59 +02:00
parent f4eb236d8a
commit d2daa58db2
2 changed files with 12 additions and 4 deletions

View File

@ -33,6 +33,12 @@
"url": "https://github.com/y-js/y-websocket/issues"
},
"homepage": "https://github.com/y-js/y-websocket#readme",
"standard": {
"ignore": [
"/dist",
"/node_modules"
]
},
"dependencies": {
"y-protocols": "0.0.6",
"lib0": "0.0.5"

View File

@ -43,12 +43,10 @@ export class WebsocketProvider extends Observable {
* @param {string} url
* @param {string} roomname
* @param {Y.Doc} doc
* @param {{awareness:awarenessProtocol.Awareness,db:any|null}} conf
*/
constructor (url, roomname, doc, awareness = new awarenessProtocol.Awareness(doc)) {
constructor (url, roomname, doc, { awareness = new awarenessProtocol.Awareness(doc), db = null } = /** @type {any} */ ({})) {
super()
window.addEventListener('beforeunload', () => {
awarenessProtocol.removeAwarenessStates(this.awareness, [this.doc.clientID], null)
})
// ensure that url is always ends with /
while (url[url.length - 1] === '/') {
url = url.slice(0, url.length - 1)
@ -60,6 +58,7 @@ export class WebsocketProvider extends Observable {
* @type {Object<string,Object>}
*/
this._localAwarenessState = {}
this.db = db
this.awareness = awareness
this.wsconnected = false
this.mux = mutex.createMutex()
@ -119,6 +118,9 @@ export class WebsocketProvider extends Observable {
})
}
}
window.addEventListener('beforeunload', () => {
awarenessProtocol.removeAwarenessStates(awareness, [this.doc.clientID], null)
})
awareness.on('change', this._awarenessUpdateHandler)
this.connect()
}