import { findInterceptor } from '../intercept/intercept' ; export type ErrorDetail = { error : Error ; chain : string [ ] ; info ? : object ; } ; export type ObserverFn = ( chain : string [ ] , detail ? : object ) => void ; export type ErrorObserverFn = ( detail : ErrorDetail ) => void ; export type UnsubsriberFn = ( ) => void ; const observers = [ ] ; const errorObservers = [ ] ; export function addSpyObserver ( fn ) { observers . push ( fn ) ; return ( ) => { observers . splice ( observers . indexOf ( fn ) , 1 ) ; } ; } export function addSpyErrorObserver ( fn ) { errorObservers . push ( fn ) ; return ( ) => { errorObservers . splice ( errorObservers . indexOf ( fn ) , 1 ) ; } ; } export function forceBroadcast ( chain , detail = { } ) { observers . forEach ( fn => { fn ( chain , detail ) ; } ) ; } export function broadcastError ( detail ) { errorObservers . forEach ( fn => { fn ( detail ) ; } ) ; } export function broadcast ( chain , detail = { } ) { const interceptor = findInterceptor ( chain ) ; if ( interceptor === null || interceptor ( forceBroadcast , chain , detail ) === false ) { forceBroadcast ( chain , detail ) ; } }