@@ -93,7 +93,7 @@ import { createMagnetarInstance } from '../helpers/createMagnetarInstance.js'
9393 // Test with onFirstData callback
9494 pokedexModule
9595 . stream ( {
96- onFirstData : ( { empty } : { empty : boolean } ) => {
96+ onFirstData : ( { empty } ) => {
9797 onFirstDataCalled = true
9898 onFirstDataCallCount ++
9999 onFirstDataEmptyValue = empty
@@ -133,7 +133,7 @@ import { createMagnetarInstance } from '../helpers/createMagnetarInstance.js'
133133 // Test with onFirstData callback on empty collection
134134 emptyCollection
135135 . stream ( {
136- onFirstData : ( { empty } : { empty : boolean } ) => {
136+ onFirstData : ( { empty } ) => {
137137 onFirstDataCalled = true
138138 } ,
139139 } )
@@ -164,7 +164,7 @@ import { createMagnetarInstance } from '../helpers/createMagnetarInstance.js'
164164
165165 nonExistingDoc
166166 . stream ( {
167- onFirstData : ( { empty } : { empty : boolean } ) => {
167+ onFirstData : ( { empty } ) => {
168168 onFirstDataCalled = true
169169 onFirstDataEmptyValue = empty
170170 } ,
@@ -194,7 +194,7 @@ import { createMagnetarInstance } from '../helpers/createMagnetarInstance.js'
194194
195195 existingDoc
196196 . stream ( {
197- onFirstData : ( { empty } : { empty : boolean } ) => {
197+ onFirstData : ( { empty } ) => {
198198 onFirstDataCalled = true
199199 onFirstDataEmptyValue = empty
200200 } ,
@@ -245,7 +245,7 @@ import { createMagnetarInstance } from '../helpers/createMagnetarInstance.js'
245245 assert . deepEqual ( onFirstDataCallCount , 2 )
246246
247247 // First call should have empty: false (non-empty collection)
248- assert . deepEqual ( onFirstDataPayloads [ 0 ] , { empty : false } )
248+ assert . deepEqual ( onFirstDataPayloads [ 0 ] , { empty : undefined } )
249249
250250 // Second call should have existingStream: true
251251 assert . deepEqual ( onFirstDataPayloads [ 1 ] , { empty : undefined , existingStream : true } )
@@ -254,3 +254,54 @@ import { createMagnetarInstance } from '../helpers/createMagnetarInstance.js'
254254 pokedexModule . closeStream ( )
255255 } )
256256}
257+
258+ {
259+ const testName = 'stream with onFirstData callback, close and stream again'
260+ test ( testName , async ( ) => {
261+ const { pokedexModule } = await createMagnetarInstance ( 'read' )
262+ let onFirstDataCallCount = 0
263+ const onFirstDataPayloads : { empty ?: boolean } [ ] = [ ]
264+
265+ // Start first stream
266+ pokedexModule
267+ . stream ( {
268+ onFirstData : ( payload ) => {
269+ console . log ( `1!` )
270+ onFirstDataCallCount ++
271+ onFirstDataPayloads . push ( payload )
272+ } ,
273+ } )
274+ . catch ( ( e : any ) => assert . fail ( e . message ) )
275+
276+ // Wait for first stream to be established
277+ await waitMs ( 1000 ) // this test needs about 1 sec to be able to pass, idk why
278+ pokedexModule . closeAllStreams ( )
279+ await waitMs ( 1 )
280+
281+ // Start second stream (should return existing stream)
282+ pokedexModule
283+ . stream ( {
284+ onFirstData : ( payload ) => {
285+ console . log ( `yes!` )
286+ onFirstDataCallCount ++
287+ onFirstDataPayloads . push ( payload )
288+ } ,
289+ } )
290+ . catch ( ( e : any ) => assert . fail ( e . message ) )
291+
292+ // Wait for second call to complete
293+ await waitMs ( 1000 ) // this test needs about 1 sec to be able to pass, idk why
294+
295+ // Should have been called twice
296+ assert . deepEqual ( onFirstDataCallCount , 2 )
297+
298+ // First call should have empty: false (non-empty collection)
299+ assert . deepEqual ( onFirstDataPayloads [ 0 ] , { empty : false } )
300+
301+ // Second call should have existingStream: true
302+ assert . deepEqual ( onFirstDataPayloads [ 1 ] , { empty : false } )
303+
304+ // Close the stream
305+ pokedexModule . closeStream ( )
306+ } )
307+ }
0 commit comments