1
+ const g = require ( "./global.js" ) ;
2
+ g . codeshare . debug = window . location . hostname == "localhost" ;
3
+
1
4
const sharedb = require ( 'sharedb/lib/client' ) ;
2
5
const CodeMirror = require ( 'codemirror' ) ;
3
6
const shortid = require ( 'shortid' ) ;
@@ -35,8 +38,13 @@ socket.addEventListener('error', function () {
35
38
connStatusSpan . style . backgroundColor = 'red' ;
36
39
} ) ;
37
40
38
- const setDocStatus = ( msg ) => {
41
+ const setDocStatus = ( msg , ok = true ) => {
39
42
docStatusSpan . innerHTML = msg ;
43
+ if ( ok ) {
44
+ docStatusSpan . style . backgroundColor = undefined ;
45
+ } else {
46
+ docStatusSpan . style . backgroundColor = 'red' ;
47
+ }
40
48
} ;
41
49
42
50
setDocStatus ( "initializing..." ) ;
@@ -119,9 +127,11 @@ doc.subscribe(function (err) {
119
127
log . info ( "doc before op:" , ops , source ) ;
120
128
} ) ;
121
129
130
+ let applyingOp = false ;
122
131
doc . on ( "op" , ( ops , source ) => {
123
132
log . group ( "doc op:" , ops , source ) ;
124
133
try {
134
+ applyingOp = true ;
125
135
if ( source ) {
126
136
return ;
127
137
}
@@ -155,6 +165,7 @@ doc.subscribe(function (err) {
155
165
console . error ( thrown . message ) ;
156
166
throw thrown ;
157
167
} finally {
168
+ applyingOp = false ;
158
169
log . groupEnd ( ) ;
159
170
}
160
171
} ) ;
@@ -167,7 +178,7 @@ doc.subscribe(function (err) {
167
178
} else {
168
179
// log.info("codeMirror.getValue()", codeMirror.getValue())
169
180
if ( doc . type === null ) {
170
- setDocStatus ( "synchronization failed... please save your doc manually and refresh" ) ;
181
+ setDocStatus ( "synchronization failed... please save your doc manually and refresh" , false ) ;
171
182
// doc.create(codeMirror.getValue(), (err) => {
172
183
// if (err) {
173
184
// console.error("error while creating, resync in 5 sec", err);
@@ -185,44 +196,53 @@ doc.subscribe(function (err) {
185
196
} ) ;
186
197
} ;
187
198
199
+ const errHandler = ( err ) => {
200
+ if ( err ) {
201
+ console . error ( "error while submitting" , err ) ;
202
+ setDocStatus ( "synchronizing..." ) ;
203
+ if ( ! needSync ) {
204
+ log . info ( "already synchronizing..." ) ;
205
+ needSync = true ;
206
+ sync ( ) ;
207
+ }
208
+ }
209
+ } ;
210
+ let opQueue = [ ] ;
188
211
codeMirror . on ( "beforeChange" , ( codeMirror , change ) => {
189
212
log . group ( "on codeMirror beforeChange" ) ;
190
213
try {
214
+ let changeCp = change ;
191
215
if ( needSync ) {
192
216
log . info ( "need sychronize" ) ;
193
- while ( change ) {
194
- if ( change . origin !== "setValue" ) {
195
- change . cancel ( ) ;
217
+ while ( changeCp ) {
218
+ if ( changeCp . origin !== "setValue" ) {
219
+ changeCp . cancel ( ) ;
196
220
}
197
- change = change . next ;
221
+ changeCp = changeCp . next ;
198
222
}
199
223
} else {
200
- while ( change ) {
201
- log . info ( "change" , change ) ;
202
- if ( change . origin !== "+sharedb" ) {
203
- const indexFrom = codeMirror . indexFromPos ( change . from ) ;
204
- const indexTo = codeMirror . indexFromPos ( change . to ) ;
205
- doc . submitOp ( [
224
+ while ( changeCp ) {
225
+ log . info ( "change" , changeCp ) ;
226
+ if ( changeCp . origin !== "+sharedb" ) {
227
+ const indexFrom = codeMirror . indexFromPos ( changeCp . from ) ;
228
+ const indexTo = codeMirror . indexFromPos ( changeCp . to ) ;
229
+ opQueue . push ( [
206
230
indexFrom ,
207
231
{ d : indexTo - indexFrom } ,
208
- change . text . join ( "\n" )
209
- ] , ( err ) => {
210
- if ( err ) {
211
- console . error ( "error while submitting" , err ) ;
212
- setDocStatus ( "synchronizing..." ) ;
213
- if ( ! needSync ) {
214
- log . info ( "already synchronizing..." ) ;
215
- needSync = true ;
216
- sync ( ) ;
217
- }
218
- }
219
- } ) ;
232
+ changeCp . text . join ( "\n" )
233
+ ] ) ;
220
234
}
221
- change = change . next ;
235
+ changeCp = changeCp . next ;
222
236
}
223
237
}
224
238
} catch ( thrown ) {
225
239
console . error ( thrown . message ) ;
240
+ // cancel all changes if error
241
+ opQueue = [ ] ;
242
+ while ( change ) {
243
+ change . cancel ( ) ;
244
+ change = change . next ;
245
+ }
226
246
throw thrown ;
227
247
} finally {
228
248
log . groupEnd ( ) ;
@@ -242,8 +262,13 @@ doc.subscribe(function (err) {
242
262
}
243
263
change = change . next ;
244
264
}
265
+ } else {
266
+ for ( let i = 0 ; i < opQueue . length ; i ++ ) {
267
+ doc . submitOp ( opQueue [ i ] , errHandler ) ;
268
+ }
245
269
}
246
270
} finally {
271
+ opQueue = [ ] ;
247
272
log . groupEnd ( ) ;
248
273
}
249
274
} ) ;
@@ -260,6 +285,9 @@ doc.subscribe(function (err) {
260
285
codeMirror . on ( 'cursorActivity' , ( codeMirror ) => {
261
286
log . group ( 'on codeMirror cursorActivity' , codeMirror ) ;
262
287
try {
288
+ if ( applyingOp ) {
289
+ return ;
290
+ }
263
291
const cursorPos = codeMirror . getCursor ( ) ;
264
292
const index = codeMirror . indexFromPos ( cursorPos ) ;
265
293
localPresence . submit ( index , ( err ) => {
0 commit comments