@@ -67,7 +67,10 @@ describe('Cursors', () => {
6767 it < CursorsTestContext > ( 'emits a cursorsUpdate event' , ( { space, dispensing, batching, fakeMessageStub } ) => {
6868 const fakeMessage = {
6969 ...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+ ] ,
7174 } ;
7275
7376 const spy = vitest . fn ( ) ;
@@ -151,16 +154,19 @@ describe('Cursors', () => {
151154
152155 it < CursorsTestContext > ( 'creates an outgoingBuffer for a new cursor movement' , ( { batching, channel } ) => {
153156 batching . pushCursorPosition ( channel , { position : { x : 1 , y : 1 } , data : { } } ) ;
154- expect ( batching . outgoingBuffers ) . toEqual ( [ { position : { x : 1 , y : 1 } , data : { } } ] ) ;
157+ expect ( batching . outgoingBuffers ) . toEqual ( [ { cursor : { position : { x : 1 , y : 1 } , data : { } } , offset : 0 } ] ) ;
155158 } ) ;
156159
157160 it < CursorsTestContext > ( 'adds cursor data to an existing buffer' , ( { batching, channel } ) => {
161+ vi . useFakeTimers ( ) ;
158162 batching . pushCursorPosition ( channel , { position : { x : 1 , y : 1 } , data : { } } ) ;
159- expect ( batching . outgoingBuffers ) . toEqual ( [ { position : { x : 1 , y : 1 } , data : { } } ] ) ;
163+ expect ( batching . outgoingBuffers ) . toEqual ( [ { cursor : { position : { x : 1 , y : 1 } , data : { } } , offset : 0 } ] ) ;
164+
165+ vi . advanceTimersByTime ( 10 ) ;
160166 batching . pushCursorPosition ( channel , { position : { x : 2 , y : 2 } , data : { } } ) ;
161167 expect ( batching . outgoingBuffers ) . toEqual ( [
162- { position : { x : 1 , y : 1 } , data : { } } ,
163- { position : { x : 2 , y : 2 } , data : { } } ,
168+ { cursor : { position : { x : 1 , y : 1 } , data : { } } , offset : 0 } ,
169+ { cursor : { position : { x : 2 , y : 2 } , data : { } } , offset : 10 } ,
164170 ] ) ;
165171 } ) ;
166172
@@ -195,15 +201,17 @@ describe('Cursors', () => {
195201
196202 it < CursorsTestContext > ( 'should publish the cursor buffer' , async ( { batching, channel } ) => {
197203 batching . hasMovement = true ;
198- batching . outgoingBuffers = [ { position : { x : 1 , y : 1 } , data : { } } ] ;
204+ batching . outgoingBuffers = [ { cursor : { position : { x : 1 , y : 1 } , data : { } } , offset : 0 } ] ;
199205 const spy = vi . spyOn ( channel , 'publish' ) ;
200206 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+ ] ) ;
202210 } ) ;
203211
204212 it < CursorsTestContext > ( 'should clear the buffer' , async ( { batching, channel } ) => {
205213 batching . hasMovement = true ;
206- batching . outgoingBuffers = [ { position : { x : 1 , y : 1 } , data : { } } ] ;
214+ batching . outgoingBuffers = [ { cursor : { position : { x : 1 , y : 1 } , data : { } } , offset : 0 } ] ;
207215 await batching [ 'batchToChannel' ] ( channel , CURSOR_UPDATE ) ;
208216 expect ( batching . outgoingBuffers ) . toEqual ( [ ] ) ;
209217 } ) ;
@@ -233,28 +241,12 @@ describe('Cursors', () => {
233241 expect ( spy ) . not . toHaveBeenCalled ( ) ;
234242 } ) ;
235243
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-
252244 it < CursorsTestContext > ( 'call emitFromBatch if there are updates' , async ( { dispensing, fakeMessageStub } ) => {
253245 const spy = vi . spyOn ( dispensing , 'emitFromBatch' ) ;
254246
255247 const fakeMessage = {
256248 ...fakeMessageStub ,
257- data : [ { position : { x : 1 , y : 1 } } ] ,
249+ data : [ { cursor : { position : { x : 1 , y : 1 } } } ] ,
258250 } ;
259251
260252 dispensing . processBatch ( fakeMessage ) ;
@@ -268,70 +260,70 @@ describe('Cursors', () => {
268260 const fakeMessage = {
269261 ...fakeMessageStub ,
270262 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 } ,
274266 ] ,
275267 } ;
268+ vi . useFakeTimers ( ) ;
276269
270+ const spy = vi . spyOn ( dispensing , 'setEmitCursorUpdate' ) ;
277271 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' ,
299287 } ) ;
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 ) ;
300297 } ) ;
301298
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 } ) => {
303300 vi . useFakeTimers ( ) ;
304301
305302 const fakeMessage = {
306303 ...fakeMessageStub ,
307304 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 } ,
311308 ] ,
312309 } ;
313310
314- expect ( dispensing [ 'handlerRunning' ] ) . toBe ( false ) ;
311+ const spy = vi . spyOn ( dispensing , 'setEmitCursorUpdate' ) ;
312+
315313 expect ( dispensing . bufferHaveData ( ) ) . toBe ( false ) ;
316314
317315 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+
335327 expect ( dispensing . bufferHaveData ( ) ) . toBe ( false ) ;
336328
337329 vi . useRealTimers ( ) ;
@@ -498,6 +490,7 @@ describe('Cursors', () => {
498490 x : 2 ,
499491 y : 3 ,
500492 } ,
493+ offset : 0 ,
501494 } ,
502495 } ;
503496
0 commit comments