Skip to content

Commit

Permalink
always call close-event even if manually closed + switch to observablev2
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Dec 19, 2024
1 parent beca90d commit f606f3f
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/y-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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])
}
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit f606f3f

Please sign in to comment.