@@ -15,6 +15,7 @@ import * as awarenessProtocol from 'y-protocols/awareness'
15
15
import { Observable } from 'lib0/observable'
16
16
import * as math from 'lib0/math'
17
17
import * as url from 'lib0/url'
18
+ import * as env from 'lib0/environment'
18
19
19
20
export const messageSync = 0
20
21
export const messageQueryAwareness = 3
@@ -350,17 +351,15 @@ export class WebsocketProvider extends Observable {
350
351
)
351
352
broadcastMessage ( this , encoding . toUint8Array ( encoder ) )
352
353
}
353
- this . _unloadHandler = ( ) => {
354
+ this . _exitHandler = ( ) => {
354
355
awarenessProtocol . removeAwarenessStates (
355
356
this . awareness ,
356
357
[ doc . clientID ] ,
357
- 'window unload '
358
+ 'app closed '
358
359
)
359
360
}
360
- if ( typeof window !== 'undefined' ) {
361
- window . addEventListener ( 'unload' , this . _unloadHandler )
362
- } else if ( typeof process !== 'undefined' ) {
363
- process . on ( 'exit' , this . _unloadHandler )
361
+ if ( env . isNode && typeof process !== 'undefined' ) {
362
+ process . on ( 'exit' , this . _exitHandler )
364
363
}
365
364
awareness . on ( 'update' , this . _awarenessUpdateHandler )
366
365
this . _checkInterval = /** @type {any } */ ( setInterval ( ( ) => {
@@ -400,10 +399,8 @@ export class WebsocketProvider extends Observable {
400
399
}
401
400
clearInterval ( this . _checkInterval )
402
401
this . disconnect ( )
403
- if ( typeof window !== 'undefined' ) {
404
- window . removeEventListener ( 'unload' , this . _unloadHandler )
405
- } else if ( typeof process !== 'undefined' ) {
406
- process . off ( 'exit' , this . _unloadHandler )
402
+ if ( typeof process !== 'undefined' ) {
403
+ process . off ( 'exit' , this . _exitHandler )
407
404
}
408
405
this . awareness . off ( 'update' , this . _awarenessUpdateHandler )
409
406
this . doc . off ( 'update' , this . _updateHandler )
0 commit comments