import { SubscribeArgs , SubscribeHandler , SubscribePsuedoIterable , SubscriptionDefinition , } from '../types' ; /** Creates subscribe handler */ export const subscribe = ( topic ) => ( ... args ) => createHandler ( { definitions : [ { topic } ] } ) ; /** Add filter to subscribe handler */ export const withFilter = ( handler , filter ) => ( ... args ) => { const iterable = handler ( ... args ) ; if ( iterable . definitions . length !== 1 ) { throw Error ( "Cannot call 'withFilter' on invalid type" ) ; } return createHandler ( { definitions : [ { ... iterable . definitions [ 0 ] , filter : typeof filter === 'function' ? filter ( ... args ) : filter , } , ] , } ) ; } ; /** Merge multiple subscribe handlers */ export const concat = ( ... handlers ) => ( ... args ) => createHandler ( { definitions : handlers . map ( ( h ) => h ( ... args ) . definitions ) . flat ( ) , } ) ; const createHandler = ( arg ) => { const h = ( ( ) => { throw Error ( 'Subscription handler should not have been called' ) ; } ) as any ; h . definitions = arg . definitions ; return h ; } ;