always call close-event even if manually closed + switch to observablev2
This commit is contained in:
parent
beca90d78d
commit
f606f3f9f9
@ -12,7 +12,7 @@ import * as decoding from 'lib0/decoding'
|
|||||||
import * as syncProtocol from 'y-protocols/sync'
|
import * as syncProtocol from 'y-protocols/sync'
|
||||||
import * as authProtocol from 'y-protocols/auth'
|
import * as authProtocol from 'y-protocols/auth'
|
||||||
import * as awarenessProtocol from 'y-protocols/awareness'
|
import * as awarenessProtocol from 'y-protocols/awareness'
|
||||||
import { Observable } from 'lib0/observable'
|
import { ObservableV2 } from 'lib0/observable'
|
||||||
import * as math from 'lib0/math'
|
import * as math from 'lib0/math'
|
||||||
import * as url from 'lib0/url'
|
import * as url from 'lib0/url'
|
||||||
import * as env from 'lib0/environment'
|
import * as env from 'lib0/environment'
|
||||||
@ -130,9 +130,11 @@ const readMessage = (provider, buf, emitSynced) => {
|
|||||||
*
|
*
|
||||||
* @param {WebsocketProvider} provider
|
* @param {WebsocketProvider} provider
|
||||||
* @param {WebSocket} ws
|
* @param {WebSocket} ws
|
||||||
|
* @param {CloseEvent | null} event
|
||||||
*/
|
*/
|
||||||
const closeWebsocketConnection = (provider, ws) => {
|
const closeWebsocketConnection = (provider, ws, event) => {
|
||||||
if (ws === provider.ws) {
|
if (ws === provider.ws) {
|
||||||
|
provider.emit('connection-close', [event, provider])
|
||||||
provider.ws = null
|
provider.ws = null
|
||||||
ws.close()
|
ws.close()
|
||||||
provider.wsconnecting = false
|
provider.wsconnecting = false
|
||||||
@ -189,8 +191,7 @@ const setupWS = (provider) => {
|
|||||||
provider.emit('connection-error', [event, provider])
|
provider.emit('connection-error', [event, provider])
|
||||||
}
|
}
|
||||||
websocket.onclose = (event) => {
|
websocket.onclose = (event) => {
|
||||||
provider.emit('connection-close', [event, provider])
|
closeWebsocketConnection(provider, websocket, event)
|
||||||
closeWebsocketConnection(provider, websocket)
|
|
||||||
}
|
}
|
||||||
websocket.onopen = () => {
|
websocket.onopen = () => {
|
||||||
provider.wsLastMessageReceived = time.getUnixTime()
|
provider.wsLastMessageReceived = time.getUnixTime()
|
||||||
@ -249,9 +250,9 @@ const broadcastMessage = (provider, buf) => {
|
|||||||
* const doc = new Y.Doc()
|
* const doc = new Y.Doc()
|
||||||
* const provider = new WebsocketProvider('http://localhost:1234', 'my-document-name', doc)
|
* const provider = new WebsocketProvider('http://localhost:1234', 'my-document-name', doc)
|
||||||
*
|
*
|
||||||
* @extends {Observable<string>}
|
* @extends {ObservableV2<{ 'connection-close': (event: CloseEvent | null, provider: WebsocketProvider) => any, 'status': (event: { status: 'connected' | 'disconnected' | 'connecting' }) => any, 'connection-error': (event: Event, provider: WebsocketProvider) => any, 'sync': (state: boolean) => any }>}
|
||||||
*/
|
*/
|
||||||
export class WebsocketProvider extends Observable {
|
export class WebsocketProvider extends ObservableV2 {
|
||||||
/**
|
/**
|
||||||
* @param {string} serverUrl
|
* @param {string} serverUrl
|
||||||
* @param {string} roomname
|
* @param {string} roomname
|
||||||
@ -391,7 +392,7 @@ export class WebsocketProvider extends Observable {
|
|||||||
) {
|
) {
|
||||||
// no message received in a long time - not even your own awareness
|
// no message received in a long time - not even your own awareness
|
||||||
// updates (which are updated every 15 seconds)
|
// updates (which are updated every 15 seconds)
|
||||||
closeWebsocketConnection(this, /** @type {WebSocket} */ (this.ws))
|
closeWebsocketConnection(this, /** @type {WebSocket} */ (this.ws), null)
|
||||||
}
|
}
|
||||||
}, messageReconnectTimeout / 10))
|
}, messageReconnectTimeout / 10))
|
||||||
if (connect) {
|
if (connect) {
|
||||||
@ -415,6 +416,7 @@ export class WebsocketProvider extends Observable {
|
|||||||
set synced (state) {
|
set synced (state) {
|
||||||
if (this._synced !== state) {
|
if (this._synced !== state) {
|
||||||
this._synced = state
|
this._synced = state
|
||||||
|
// @ts-ignore
|
||||||
this.emit('synced', [state])
|
this.emit('synced', [state])
|
||||||
this.emit('sync', [state])
|
this.emit('sync', [state])
|
||||||
}
|
}
|
||||||
@ -498,7 +500,7 @@ export class WebsocketProvider extends Observable {
|
|||||||
this.shouldConnect = false
|
this.shouldConnect = false
|
||||||
this.disconnectBc()
|
this.disconnectBc()
|
||||||
if (this.ws !== null) {
|
if (this.ws !== null) {
|
||||||
closeWebsocketConnection(this, this.ws)
|
closeWebsocketConnection(this, this.ws, null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user