export type Listener = { index : number ; listener : ( arg : { contractName : string ; data : any ; } ) => void ; on : boolean ; } ; export const listeners = [ ] ; export default function subscribe < T > ( listener ) { if ( typeof listener !== 'function' ) { throw Error ( 'Expected listener to be a function' ) ; } const index = listeners . length ; const newListener = { index , listener , on : true , } ; listeners . push ( newListener ) ; return function unsubscribe ( ) { for ( let i = 0 ; i < listeners . length ; i ++ ) { if ( listeners [ i ] . index === index ) { listeners [ i ] . on = false ; } } } ; }