import Promise from "../utils/promise" ; import timing from "../utils/timing" ; import Api from "../api" ; function defaultGetArgument ( ) { return undefined ; } function defaultHandleResult ( output , result ) { return output ; } param { string } name param { Function param { Function param { Output } output return { Promise < Output > } function callHook ( name , getArgument , handleResult , output ) { getArgument = getArgument || defaultGetArgument ; handleResult = handleResult || defaultHandleResult ; const logger = output . getLogger ( ) ; const plugins = output . getPlugins ( ) ; logger . debug . ln ( `calling hook "${ name }"` ) ; const context = Api . encodeGlobal ( output ) ; return timing . measure ( `call.hook.${ name }` , Promise ( getArgument ( output ) ) . then ( ( arg ) => { return Promise . reduce ( plugins , ( prev , plugin ) => { const hook = plugin . getHook ( name ) ; if ( ! hook ) { return prev ; } return hook . call ( context , prev ) ; } , arg ) ; } ) . then ( ( result ) => { output = Api . decodeGlobal ( output , context ) ; return handleResult ( output , result ) ; } ) ) ; } export default callHook ;