@@ -67,7 +67,10 @@ describe('Cursors', () => {
67
67
it < CursorsTestContext > ( 'emits a cursorsUpdate event' , ( { space, dispensing, batching, fakeMessageStub } ) => {
68
68
const fakeMessage = {
69
69
...fakeMessageStub ,
70
- data : [ { position : { x : 1 , y : 1 } } , { position : { x : 1 , y : 2 } , data : { color : 'red' } } ] ,
70
+ data : [
71
+ { cursor : { position : { x : 1 , y : 1 } } } ,
72
+ { cursor : { position : { x : 1 , y : 2 } , data : { color : 'red' } } } ,
73
+ ] ,
71
74
} ;
72
75
73
76
const spy = vitest . fn ( ) ;
@@ -151,16 +154,19 @@ describe('Cursors', () => {
151
154
152
155
it < CursorsTestContext > ( 'creates an outgoingBuffer for a new cursor movement' , ( { batching, channel } ) => {
153
156
batching . pushCursorPosition ( channel , { position : { x : 1 , y : 1 } , data : { } } ) ;
154
- expect ( batching . outgoingBuffers ) . toEqual ( [ { position : { x : 1 , y : 1 } , data : { } } ] ) ;
157
+ expect ( batching . outgoingBuffer ) . toEqual ( [ { cursor : { position : { x : 1 , y : 1 } , data : { } } , offset : 0 } ] ) ;
155
158
} ) ;
156
159
157
160
it < CursorsTestContext > ( 'adds cursor data to an existing buffer' , ( { batching, channel } ) => {
161
+ vi . useFakeTimers ( ) ;
158
162
batching . pushCursorPosition ( channel , { position : { x : 1 , y : 1 } , data : { } } ) ;
159
- expect ( batching . outgoingBuffers ) . toEqual ( [ { position : { x : 1 , y : 1 } , data : { } } ] ) ;
163
+ expect ( batching . outgoingBuffer ) . toEqual ( [ { cursor : { position : { x : 1 , y : 1 } , data : { } } , offset : 0 } ] ) ;
164
+
165
+ vi . advanceTimersByTime ( 10 ) ;
160
166
batching . pushCursorPosition ( channel , { position : { x : 2 , y : 2 } , data : { } } ) ;
161
- expect ( batching . outgoingBuffers ) . toEqual ( [
162
- { position : { x : 1 , y : 1 } , data : { } } ,
163
- { position : { x : 2 , y : 2 } , data : { } } ,
167
+ expect ( batching . outgoingBuffer ) . toEqual ( [
168
+ { cursor : { position : { x : 1 , y : 1 } , data : { } } , offset : 0 } ,
169
+ { cursor : { position : { x : 2 , y : 2 } , data : { } } , offset : 10 } ,
164
170
] ) ;
165
171
} ) ;
166
172
@@ -195,17 +201,19 @@ describe('Cursors', () => {
195
201
196
202
it < CursorsTestContext > ( 'should publish the cursor buffer' , async ( { batching, channel } ) => {
197
203
batching . hasMovement = true ;
198
- batching . outgoingBuffers = [ { position : { x : 1 , y : 1 } , data : { } } ] ;
204
+ batching . outgoingBuffer = [ { cursor : { position : { x : 1 , y : 1 } , data : { } } , offset : 0 } ] ;
199
205
const spy = vi . spyOn ( channel , 'publish' ) ;
200
206
await batching [ 'batchToChannel' ] ( channel , CURSOR_UPDATE ) ;
201
- expect ( spy ) . toHaveBeenCalledWith ( CURSOR_UPDATE , [ { position : { x : 1 , y : 1 } , data : { } } ] ) ;
207
+ expect ( spy ) . toHaveBeenCalledWith ( CURSOR_UPDATE , [
208
+ { cursor : { position : { x : 1 , y : 1 } , data : { } } , offset : 0 } ,
209
+ ] ) ;
202
210
} ) ;
203
211
204
212
it < CursorsTestContext > ( 'should clear the buffer' , async ( { batching, channel } ) => {
205
213
batching . hasMovement = true ;
206
- batching . outgoingBuffers = [ { position : { x : 1 , y : 1 } , data : { } } ] ;
214
+ batching . outgoingBuffer = [ { cursor : { position : { x : 1 , y : 1 } , data : { } } , offset : 0 } ] ;
207
215
await batching [ 'batchToChannel' ] ( channel , CURSOR_UPDATE ) ;
208
- expect ( batching . outgoingBuffers ) . toEqual ( [ ] ) ;
216
+ expect ( batching . outgoingBuffer ) . toEqual ( [ ] ) ;
209
217
} ) ;
210
218
211
219
it < CursorsTestContext > ( 'should set hasMovements to false' , async ( { batching, channel } ) => {
@@ -233,28 +241,12 @@ describe('Cursors', () => {
233
241
expect ( spy ) . not . toHaveBeenCalled ( ) ;
234
242
} ) ;
235
243
236
- it < CursorsTestContext > ( 'does not call emitFromBatch if the loop is already running' , async ( {
237
- dispensing,
238
- fakeMessageStub,
239
- } ) => {
240
- const spy = vi . spyOn ( dispensing , 'emitFromBatch' ) ;
241
-
242
- const fakeMessage = {
243
- ...fakeMessageStub ,
244
- data : [ { position : { x : 1 , y : 1 } } ] ,
245
- } ;
246
-
247
- dispensing [ 'handlerRunning' ] = true ;
248
- dispensing . processBatch ( fakeMessage ) ;
249
- expect ( spy ) . not . toHaveBeenCalled ( ) ;
250
- } ) ;
251
-
252
244
it < CursorsTestContext > ( 'call emitFromBatch if there are updates' , async ( { dispensing, fakeMessageStub } ) => {
253
245
const spy = vi . spyOn ( dispensing , 'emitFromBatch' ) ;
254
246
255
247
const fakeMessage = {
256
248
...fakeMessageStub ,
257
- data : [ { position : { x : 1 , y : 1 } } ] ,
249
+ data : [ { cursor : { position : { x : 1 , y : 1 } } } ] ,
258
250
} ;
259
251
260
252
dispensing . processBatch ( fakeMessage ) ;
@@ -268,70 +260,70 @@ describe('Cursors', () => {
268
260
const fakeMessage = {
269
261
...fakeMessageStub ,
270
262
data : [
271
- { position : { x : 1 , y : 1 } } ,
272
- { position : { x : 2 , y : 3 } , data : { color : 'blue' } } ,
273
- { position : { x : 5 , y : 4 } } ,
263
+ { cursor : { position : { x : 1 , y : 1 } } , offset : 10 } ,
264
+ { cursor : { position : { x : 2 , y : 3 } , data : { color : 'blue' } } , offset : 20 } ,
265
+ { cursor : { position : { x : 5 , y : 4 } } , offset : 30 } ,
274
266
] ,
275
267
} ;
268
+ vi . useFakeTimers ( ) ;
276
269
270
+ const spy = vi . spyOn ( dispensing , 'setEmitCursorUpdate' ) ;
277
271
dispensing . processBatch ( fakeMessage ) ;
278
- expect ( dispensing [ 'buffer' ] ) . toEqual ( {
279
- connectionId : [
280
- {
281
- position : { x : 1 , y : 1 } ,
282
- data : undefined ,
283
- clientId : 'clientId' ,
284
- connectionId : 'connectionId' ,
285
- } ,
286
- {
287
- position : { x : 2 , y : 3 } ,
288
- data : { color : 'blue' } ,
289
- clientId : 'clientId' ,
290
- connectionId : 'connectionId' ,
291
- } ,
292
- {
293
- position : { x : 5 , y : 4 } ,
294
- data : undefined ,
295
- clientId : 'clientId' ,
296
- connectionId : 'connectionId' ,
297
- } ,
298
- ] ,
272
+
273
+ vi . advanceTimersByTime ( 10 ) ;
274
+ expect ( spy ) . toHaveBeenCalledWith ( {
275
+ position : { x : 1 , y : 1 } ,
276
+ data : undefined ,
277
+ clientId : 'clientId' ,
278
+ connectionId : 'connectionId' ,
279
+ } ) ;
280
+
281
+ vi . advanceTimersByTime ( 10 ) ;
282
+ expect ( spy ) . toHaveBeenCalledWith ( {
283
+ position : { x : 2 , y : 3 } ,
284
+ data : { color : 'blue' } ,
285
+ clientId : 'clientId' ,
286
+ connectionId : 'connectionId' ,
299
287
} ) ;
288
+
289
+ vi . advanceTimersByTime ( 10 ) ;
290
+ expect ( spy ) . toHaveBeenCalledWith ( {
291
+ position : { x : 5 , y : 4 } ,
292
+ data : undefined ,
293
+ clientId : 'clientId' ,
294
+ connectionId : 'connectionId' ,
295
+ } ) ;
296
+ expect ( spy ) . toHaveBeenCalledTimes ( 3 ) ;
300
297
} ) ;
301
298
302
- it < CursorsTestContext > ( 'runs until the batch is empty' , async ( { dispensing, batching , fakeMessageStub } ) => {
299
+ it < CursorsTestContext > ( 'runs until the batch is empty' , async ( { dispensing, fakeMessageStub } ) => {
303
300
vi . useFakeTimers ( ) ;
304
301
305
302
const fakeMessage = {
306
303
...fakeMessageStub ,
307
304
data : [
308
- { position : { x : 1 , y : 1 } } ,
309
- { position : { x : 2 , y : 3 } , data : { color : 'blue' } } ,
310
- { position : { x : 5 , y : 4 } } ,
305
+ { cursor : { position : { x : 1 , y : 1 } } , offset : 10 } ,
306
+ { cursor : { position : { x : 2 , y : 3 } , data : { color : 'blue' } } , offset : 20 } ,
307
+ { cursor : { position : { x : 5 , y : 4 } } , offset : 30 } ,
311
308
] ,
312
309
} ;
313
310
314
- expect ( dispensing [ 'handlerRunning' ] ) . toBe ( false ) ;
311
+ const spy = vi . spyOn ( dispensing , 'setEmitCursorUpdate' ) ;
312
+
315
313
expect ( dispensing . bufferHaveData ( ) ) . toBe ( false ) ;
316
314
317
315
dispensing . processBatch ( fakeMessage ) ;
318
- expect ( dispensing [ 'buffer' ] [ 'connectionId' ] ) . toHaveLength ( 3 ) ;
319
- expect ( dispensing [ 'handlerRunning' ] ) . toBe ( true ) ;
320
- expect ( dispensing . bufferHaveData ( ) ) . toBe ( true ) ;
321
- vi . advanceTimersByTime ( batching . batchTime / 2 ) ;
322
-
323
- expect ( dispensing [ 'buffer' ] [ 'connectionId' ] ) . toHaveLength ( 2 ) ;
324
- expect ( dispensing [ 'handlerRunning' ] ) . toBe ( true ) ;
325
- expect ( dispensing . bufferHaveData ( ) ) . toBe ( true ) ;
326
-
327
- vi . advanceTimersByTime ( batching . batchTime / 2 ) ;
328
- expect ( dispensing [ 'buffer' ] [ 'connectionId' ] ) . toHaveLength ( 1 ) ;
329
- expect ( dispensing [ 'handlerRunning' ] ) . toBe ( true ) ;
330
- expect ( dispensing . bufferHaveData ( ) ) . toBe ( true ) ;
331
-
332
- vi . advanceTimersByTime ( batching . batchTime ) ;
333
- expect ( dispensing [ 'buffer' ] [ 'connectionId' ] ) . toHaveLength ( 0 ) ;
334
- expect ( dispensing [ 'handlerRunning' ] ) . toBe ( false ) ;
316
+ expect ( spy ) . toHaveBeenCalledTimes ( 0 ) ;
317
+
318
+ vi . advanceTimersByTime ( 10 ) ;
319
+ expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ;
320
+
321
+ vi . advanceTimersByTime ( 10 ) ;
322
+ expect ( spy ) . toHaveBeenCalledTimes ( 2 ) ;
323
+
324
+ vi . advanceTimersByTime ( 10 ) ;
325
+ expect ( spy ) . toHaveBeenCalledTimes ( 3 ) ;
326
+
335
327
expect ( dispensing . bufferHaveData ( ) ) . toBe ( false ) ;
336
328
337
329
vi . useRealTimers ( ) ;
@@ -498,6 +490,7 @@ describe('Cursors', () => {
498
490
x : 2 ,
499
491
y : 3 ,
500
492
} ,
493
+ offset : 0 ,
501
494
} ,
502
495
} ;
503
496
0 commit comments