import test from 'ava' ; import { createMagnetarInstance } from '../helpers/createMagnetarInstance' ; import { pokedex , waitMs } from '@magnetarjs/test-utils' ; import { isPromise } from 'is-what' ; test ( 'stream (collection)' , async ( t ) => { const { pokedexModule } = createMagnetarInstance ( ) ; t . deepEqual ( pokedexModule . data . get ( '1' ) , pokedex ( 1 ) ) ; t . is ( pokedexModule . data . size , 1 ) ; pokedexModule . stream ( ) . catch ( ( e ) => t . fail ( e . message ) ) ; await waitMs ( 600 ) ; pokedexModule . closeStream ( ) ; t . deepEqual ( pokedexModule . data . get ( '1' ) , pokedex ( 1 ) ) ; t . deepEqual ( pokedexModule . data . get ( '2' ) , pokedex ( 2 ) ) ; t . deepEqual ( pokedexModule . data . get ( '3' ) , pokedex ( 3 ) ) ; t . is ( pokedexModule . data . size , 3 ) ; await waitMs ( 1000 ) ; t . is ( pokedexModule . data . size , 3 ) ; } ) ; test ( 'stream (doc)' , async ( t ) => { const { trainerModule } = createMagnetarInstance ( ) ; t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 } ) ; trainerModule . stream ( ) . catch ( ( e ) => t . fail ( e . message ) ) ; await waitMs ( 600 ) ; trainerModule . closeStream ( ) ; t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 , dream : 'job' } ) ; await waitMs ( 1000 ) ; t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 , dream : 'job' } ) ; } ) ; type P = { promise ? : Promise < any > ; resolve : ( p ? : any ) => any ; } ; test ( 'stream (doc) but updating multiple times in between stream' , async ( t ) => { const { trainerModule } = createMagnetarInstance ( ) ; t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 } ) ; const p1 = { resolve : ( ) => { } } ; p1 . promise = new Promise ( ( resolve ) => { p1 . resolve = resolve ; } ) ; const p2 = { resolve : ( ) => { } } ; p2 . promise = new Promise ( ( resolve ) => { p2 . resolve = resolve ; } ) ; const p3 = { resolve : ( ) => { } } ; p3 . promise = new Promise ( ( resolve ) => { p3 . resolve = resolve ; } ) ; const p4 = { resolve : ( ) => { } } ; p4 . promise = new Promise ( ( resolve ) => { p4 . resolve = resolve ; } ) ; trainerModule . stream ( [ p1 . promise , p2 . promise , p3 . promise , p4 . promise ] ) . catch ( ( e ) => t . fail ( e . message ) ) ; await Promise . all ( [ p1 . resolve ( ) , await p1 . promise ] ) ; await Promise . all ( [ p2 . resolve ( ) , await p2 . promise ] ) ; t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 } ) ; await trainerModule . merge ( { dream : 'job' } ) ; t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 , dream : 'job' } ) ; await trainerModule . merge ( { colour : 'blue' } ) ; t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 , dream : 'job' , colour : 'blue' } ) ; await Promise . all ( [ p3 . resolve ( ) , await p3 . promise ] ) ; t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 , dream : 'job' , colour : 'blue' } ) ; await Promise . all ( [ p4 . resolve ( ) , await p4 . promise ] ) ; t . deepEqual ( trainerModule . data , { name : 'Luca' , age : 10 , dream : 'job' , colour : 'blue' } ) ; trainerModule . closeStream ( ) ; } ) ; test ( 'stream (collection) where-filter stream doesnt affect base collection' , async ( t ) => { const { pokedexModule } = createMagnetarInstance ( ) ; t . deepEqual ( pokedexModule . streaming ( ) , null ) ; const collection = pokedexModule . where ( 'type' , 'array-contains' , 'Fire' ) . where ( 'base.Speed' , '>=' , 100 ) ; collection . stream ( ) ; await waitMs ( 1 ) ; t . deepEqual ( isPromise ( collection . streaming ( ) ) , true ) ; t . deepEqual ( pokedexModule . streaming ( ) , null ) ; collection . closeStream ( ) ; t . deepEqual ( collection . streaming ( ) , null ) ; } ) ;