1
- /*
2
- Implementation from yjs-websocket/bin/utils.js
3
- Copied here for safe referencing
4
- */
5
- import * as Y from 'yjs' ;
6
- import * as syncProtocol from 'y-protocols/sync' ;
7
1
import * as awarenessProtocol from 'y-protocols/awareness' ;
2
+ import * as syncProtocol from 'y-protocols/sync' ;
8
3
9
- import * as encoding from 'lib0/encoding' ;
10
4
import * as decoding from 'lib0/decoding' ;
5
+ import * as encoding from 'lib0/encoding' ;
11
6
import * as map from 'lib0/map' ;
12
7
13
- import { IPersistence , IWSSharedDoc } from '@/types/interfaces' ;
14
-
15
- const wsReadyStateConnecting = 0 ;
16
- const wsReadyStateOpen = 1 ;
17
- const wsReadyStateClosing = 2 ; // eslint-disable-line
18
- const wsReadyStateClosed = 3 ; // eslint-disable-line
8
+ import type { IPersistence , IWSSharedDoc } from '@/types/interfaces' ;
19
9
20
- // disable gc when using snapshots!
21
- const gcEnabled = process . env . GC !== 'false' && process . env . GC !== '0' ;
10
+ import {
11
+ messageAwareness ,
12
+ messageSync ,
13
+ pingTimeout ,
14
+ wsReadyStateConnecting ,
15
+ wsReadyStateOpen ,
16
+ } from './constants' ;
17
+ import { WSSharedDoc } from './ws-shared-doc' ;
22
18
23
19
let persistence : IPersistence | null = null ;
24
20
@@ -31,73 +27,6 @@ export const getPersistence = () => persistence;
31
27
// exporting docs so that others can use it
32
28
export const docs = new Map < string , IWSSharedDoc > ( ) ;
33
29
34
- const messageSync = 0 ;
35
- const messageAwareness = 1 ;
36
- // const messageAuth = 2
37
-
38
- const updateHandler = ( update : Uint8Array , origin : any , doc : Y . Doc ) => {
39
- const sharedDoc = doc as IWSSharedDoc ;
40
-
41
- const encoder = encoding . createEncoder ( ) ;
42
- encoding . writeVarUint ( encoder , messageSync ) ;
43
- syncProtocol . writeUpdate ( encoder , update ) ;
44
- const message = encoding . toUint8Array ( encoder ) ;
45
- sharedDoc . conns . forEach ( ( _ , conn ) => send ( sharedDoc , conn , message ) ) ;
46
- } ;
47
-
48
- class WSSharedDoc extends Y . Doc implements IWSSharedDoc {
49
- name : string ;
50
- conns : Map < object , Set < number > > ;
51
- awareness : awarenessProtocol . Awareness ;
52
-
53
- constructor ( name : string ) {
54
- super ( { gc : gcEnabled } ) ;
55
- this . name = name ;
56
- this . conns = new Map ( ) ;
57
- this . awareness = new awarenessProtocol . Awareness ( this ) ;
58
- this . awareness . setLocalState ( null ) ;
59
-
60
- const awarenessChangeHandler = (
61
- {
62
- added,
63
- updated,
64
- removed,
65
- } : {
66
- added : Array < number > ;
67
- updated : Array < number > ;
68
- removed : Array < number > ;
69
- } ,
70
- conn : object | null
71
- ) => {
72
- const changedClients = added . concat ( updated , removed ) ;
73
- if ( conn !== null ) {
74
- const connControlledIDs = /** @type {Set<number> } */ this . conns . get ( conn ) ;
75
- if ( connControlledIDs !== undefined ) {
76
- added . forEach ( ( clientID ) => {
77
- connControlledIDs . add ( clientID ) ;
78
- } ) ;
79
- removed . forEach ( ( clientID ) => {
80
- connControlledIDs . delete ( clientID ) ;
81
- } ) ;
82
- }
83
- }
84
- // broadcast awareness update
85
- const encoder = encoding . createEncoder ( ) ;
86
- encoding . writeVarUint ( encoder , messageAwareness ) ;
87
- encoding . writeVarUint8Array (
88
- encoder ,
89
- awarenessProtocol . encodeAwarenessUpdate ( this . awareness , changedClients )
90
- ) ;
91
- const buff = encoding . toUint8Array ( encoder ) ;
92
- this . conns . forEach ( ( _ , c ) => {
93
- send ( this , c , buff ) ;
94
- } ) ;
95
- } ;
96
- this . awareness . on ( 'update' , awarenessChangeHandler ) ;
97
- this . on ( 'update' , updateHandler ) ;
98
- }
99
- }
100
-
101
30
/**
102
31
* Gets a Y.Doc by name, whether in memory or on disk
103
32
*
@@ -165,13 +94,13 @@ const closeConn = (doc: IWSSharedDoc, conn: any) => {
165
94
conn . close ( ) ;
166
95
} ;
167
96
168
- const send = ( doc : IWSSharedDoc , conn : any , m : Uint8Array ) => {
97
+ export const send = ( doc : IWSSharedDoc , conn : any , m : Uint8Array ) => {
169
98
if ( conn . readyState !== wsReadyStateConnecting && conn . readyState !== wsReadyStateOpen ) {
170
99
closeConn ( doc , conn ) ;
171
100
}
172
101
try {
173
102
conn . send ( m , ( err : any ) => {
174
- if ( err != null ) {
103
+ if ( err !== null ) {
175
104
closeConn ( doc , conn ) ;
176
105
}
177
106
} ) ;
@@ -180,8 +109,6 @@ const send = (doc: IWSSharedDoc, conn: any, m: Uint8Array) => {
180
109
}
181
110
} ;
182
111
183
- const pingTimeout = 30000 ;
184
-
185
112
export const setupWSConnection = (
186
113
conn : any ,
187
114
req : any ,
0 commit comments