export type Runnable = ( ) => void ; export interface RunLoop { run ( fn ) : void ; } export function createRunLoop ( name ? ) { let didEnter = false ; let runnables = [ ] ; function run ( fn ) { runnables . push ( fn ) ; if ( ! didEnter ) { didEnter = true ; try { while ( true ) { let runnable = runnables . shift ( ) ; if ( runnable ) { try { runnable ( ) ; } catch ( e ) { console . error ( `Caught error in run loop \`${ name }\`:` ) ; console . error ( e ) ; } } else { break ; } } } finally { didEnter = false ; } } } return { run } ; }