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 authProtocol from 'y-protocols/auth'
|
||||
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 url from 'lib0/url'
|
||||
import * as env from 'lib0/environment'
|
||||
@ -130,9 +130,11 @@ const readMessage = (provider, buf, emitSynced) => {
|
||||
*
|
||||
* @param {WebsocketProvider} provider
|
||||
* @param {WebSocket} ws
|
||||
* @param {CloseEvent | null} event
|
||||
*/
|
||||
const closeWebsocketConnection = (provider, ws) => {
|
||||
const closeWebsocketConnection = (provider, ws, event) => {
|
||||
if (ws === provider.ws) {
|
||||
provider.emit('connection-close', [event, provider])
|
||||
provider.ws = null
|
||||
ws.close()
|
||||
provider.wsconnecting = false
|
||||
@ -189,8 +191,7 @@ const setupWS = (provider) => {
|
||||
provider.emit('connection-error', [event, provider])
|
||||
}
|
||||
websocket.onclose = (event) => {
|
||||
provider.emit('connection-close', [event, provider])
|
||||
closeWebsocketConnection(provider, websocket)
|
||||
closeWebsocketConnection(provider, websocket, event)
|
||||
}
|
||||
websocket.onopen = () => {
|
||||
provider.wsLastMessageReceived = time.getUnixTime()
|
||||
@ -249,9 +250,9 @@ const broadcastMessage = (provider, buf) => {
|
||||
* const doc = new Y.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} roomname
|
||||
@ -391,7 +392,7 @@ export class WebsocketProvider extends Observable {
|
||||
) {
|
||||
// no message received in a long time - not even your own awareness
|
||||
// updates (which are updated every 15 seconds)
|
||||
closeWebsocketConnection(this, /** @type {WebSocket} */ (this.ws))
|
||||
closeWebsocketConnection(this, /** @type {WebSocket} */ (this.ws), null)
|
||||
}
|
||||
}, messageReconnectTimeout / 10))
|
||||
if (connect) {
|
||||
@ -415,6 +416,7 @@ export class WebsocketProvider extends Observable {
|
||||
set synced (state) {
|
||||
if (this._synced !== state) {
|
||||
this._synced = state
|
||||
// @ts-ignore
|
||||
this.emit('synced', [state])
|
||||
this.emit('sync', [state])
|
||||
}
|
||||
@ -498,7 +500,7 @@ export class WebsocketProvider extends Observable {
|
||||
this.shouldConnect = false
|
||||
this.disconnectBc()
|
||||
if (this.ws !== null) {
|
||||
closeWebsocketConnection(this, this.ws)
|
||||
closeWebsocketConnection(this, this.ws, null)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user