1
- import * as awarenessProtocol from 'y-protocols/awareness' ;
2
- import * as syncProtocol from 'y-protocols/sync' ;
3
-
4
1
import * as decoding from 'lib0/decoding' ;
5
2
import * as encoding from 'lib0/encoding' ;
6
3
import * as map from 'lib0/map' ;
4
+ import * as awarenessProtocol from 'y-protocols/awareness' ;
5
+ import * as syncProtocol from 'y-protocols/sync' ;
7
6
8
7
import type { IPersistence , IWSSharedDoc } from '@/types/interfaces' ;
9
8
@@ -38,9 +37,11 @@ export const getYDoc = (docname: string, gc = true) =>
38
37
map . setIfUndefined ( docs , docname , ( ) => {
39
38
const doc = new WSSharedDoc ( docname ) ;
40
39
doc . gc = gc ;
40
+
41
41
if ( persistence !== null ) {
42
42
persistence . bindState ( docname , doc ) ;
43
43
}
44
+
44
45
docs . set ( docname , doc ) ;
45
46
return doc ;
46
47
} ) ;
@@ -50,6 +51,7 @@ const messageListener = (conn: any, doc: IWSSharedDoc, message: Uint8Array) => {
50
51
const encoder = encoding . createEncoder ( ) ;
51
52
const decoder = decoding . createDecoder ( message ) ;
52
53
const messageType = decoding . readVarUint ( decoder ) ;
54
+
53
55
switch ( messageType ) {
54
56
case messageSync :
55
57
encoding . writeVarUint ( encoder , messageSync ) ;
@@ -61,7 +63,9 @@ const messageListener = (conn: any, doc: IWSSharedDoc, message: Uint8Array) => {
61
63
if ( encoding . length ( encoder ) > 1 ) {
62
64
send ( doc , conn , encoding . toUint8Array ( encoder ) ) ;
63
65
}
66
+
64
67
break ;
68
+
65
69
case messageAwareness : {
66
70
awarenessProtocol . applyAwarenessUpdate (
67
71
doc . awareness ,
@@ -80,9 +84,11 @@ const closeConn = (doc: IWSSharedDoc, conn: any) => {
80
84
if ( doc . conns . has ( conn ) ) {
81
85
const controlledIds = doc . conns . get ( conn ) ;
82
86
doc . conns . delete ( conn ) ;
87
+
83
88
if ( controlledIds ) {
84
89
awarenessProtocol . removeAwarenessStates ( doc . awareness , Array . from ( controlledIds ) , null ) ;
85
90
}
91
+
86
92
if ( doc . conns . size === 0 && persistence !== null ) {
87
93
// if persisted, we store state and destroy ydocument
88
94
persistence . writeState ( doc . name , doc ) . then ( ( ) => {
@@ -91,13 +97,15 @@ const closeConn = (doc: IWSSharedDoc, conn: any) => {
91
97
docs . delete ( doc . name ) ;
92
98
}
93
99
}
100
+
94
101
conn . close ( ) ;
95
102
} ;
96
103
97
104
export const send = ( doc : IWSSharedDoc , conn : any , m : Uint8Array ) => {
98
105
if ( conn . readyState !== wsReadyStateConnecting && conn . readyState !== wsReadyStateOpen ) {
99
106
closeConn ( doc , conn ) ;
100
107
}
108
+
101
109
try {
102
110
conn . send ( m , ( err : any ) => {
103
111
if ( err !== null ) {
@@ -128,9 +136,11 @@ export const setupWSConnection = (
128
136
if ( doc . conns . has ( conn ) ) {
129
137
closeConn ( doc , conn ) ;
130
138
}
139
+
131
140
clearInterval ( pingInterval ) ;
132
141
} else if ( doc . conns . has ( conn ) ) {
133
142
pongReceived = false ;
143
+
134
144
try {
135
145
conn . ping ( ) ;
136
146
} catch ( e ) {
@@ -146,6 +156,7 @@ export const setupWSConnection = (
146
156
conn . on ( 'pong' , ( ) => {
147
157
pongReceived = true ;
148
158
} ) ;
159
+
149
160
// put the following in a variables in a block so the interval handlers don't keep in in
150
161
// scope
151
162
{
@@ -155,6 +166,7 @@ export const setupWSConnection = (
155
166
syncProtocol . writeSyncStep1 ( encoder , doc ) ;
156
167
send ( doc , conn , encoding . toUint8Array ( encoder ) ) ;
157
168
const awarenessStates = doc . awareness . getStates ( ) ;
169
+
158
170
if ( awarenessStates . size > 0 ) {
159
171
const encoder = encoding . createEncoder ( ) ;
160
172
encoding . writeVarUint ( encoder , messageAwareness ) ;
0 commit comments