{"version":3,"file":"hable.umd.js","sources":["../src/hable.js","../src/utils.js"],"sourcesContent":["import { serial, flatHooks } from './utils'\n\nexport default class Hookable {\n constructor (logger = console) {\n this._logger = logger\n this._hooks = {}\n this._deprecatedHooks = {}\n\n // Allow destructuring hook and callHook functions out of instance object\n this.hook = this.hook.bind(this)\n this.callHook = this.callHook.bind(this)\n }\n\n hook (name, fn) {\n if (!name || typeof fn !== 'function') {\n return\n }\n\n const originalName = name\n let deprecatedHook\n while (this._deprecatedHooks[name]) {\n deprecatedHook = this._deprecatedHooks[name]\n if (typeof deprecatedHook === 'string') {\n deprecatedHook = { to: deprecatedHook }\n }\n name = deprecatedHook.to\n }\n if (deprecatedHook) {\n if (!deprecatedHook.message) {\n this._logger.warn(\n `${originalName} hook has been deprecated` +\n (deprecatedHook.to ? `, please use ${deprecatedHook.to}` : '')\n )\n } else {\n this._logger.warn(deprecatedHook.message)\n }\n }\n\n this._hooks[name] = this._hooks[name] || []\n this._hooks[name].push(fn)\n }\n\n deprecateHook (old, name) {\n this._deprecatedHooks[old] = name\n }\n\n deprecateHooks (deprecatedHooks) {\n Object.assign(this._deprecatedHooks, deprecatedHooks)\n }\n\n addHooks (configHooks) {\n const hooks = flatHooks(configHooks)\n for (const key in hooks) {\n this.hook(key, hooks[key])\n }\n }\n\n async callHook (name, ...args) {\n if (!this._hooks[name]) {\n return\n }\n try {\n await serial(this._hooks[name], fn => fn(...args))\n } catch (err) {\n if (name !== 'error') {\n await this.callHook('error', err)\n }\n if (this._logger.fatal) {\n this._logger.fatal(err)\n } else {\n this._logger.error(err)\n }\n }\n }\n\n clearHook (name) {\n if (name) {\n delete this._hooks[name]\n }\n }\n\n clearHooks () {\n this._hooks = {}\n }\n}\n","export function flatHooks (configHooks, hooks = {}, parentName) {\n for (const key in configHooks) {\n const subHook = configHooks[key]\n const name = parentName ? `${parentName}:${key}` : key\n if (typeof subHook === 'object' && subHook !== null) {\n flatHooks(subHook, hooks, name)\n } else {\n hooks[name] = subHook\n }\n }\n return hooks\n}\n\nexport function serial (tasks, fn) {\n return tasks.reduce((promise, task) => promise.then(previous => fn(task, previous)), Promise.resolve(null))\n}\n"],"names":["constructor","logger","console","_logger","_hooks","_deprecatedHooks","hook","this","bind","callHook","name","fn","originalName","deprecatedHook","to","message","warn","push","deprecateHook","old","deprecateHooks","deprecatedHooks","Object","assign","addHooks","configHooks","hooks","flatHooks","parentName","key","subHook","args","tasks","reduce","promise","task","then","previous","Promise","resolve","err","fatal","error","clearHook","clearHooks"],"mappings":"wMAGEA,YAAaC,EAASC,cACfC,QAAUF,OACVG,OAAS,QACTC,iBAAmB,QAGnBC,KAAOC,KAAKD,KAAKE,KAAKD,WACtBE,SAAWF,KAAKE,SAASD,KAAKD,MAGrCD,KAAMI,EAAMC,OACLD,GAAsB,mBAAPC,eAIdC,EAAeF,MACjBG,OACGN,KAAKF,iBAAiBK,IAEG,iBAD9BG,EAAiBN,KAAKF,iBAAiBK,MAErCG,EAAiB,CAAEC,GAAID,IAEzBH,EAAOG,EAAeC,GAEpBD,IACGA,EAAeE,aAMbZ,QAAQa,KAAKH,EAAeE,cAL5BZ,QAAQa,QACRJ,8BACFC,EAAeC,mBAAqBD,EAAeC,KAAO,WAO5DV,OAAOM,GAAQH,KAAKH,OAAOM,IAAS,QACpCN,OAAOM,GAAMO,KAAKN,GAGzBO,cAAeC,EAAKT,QACbL,iBAAiBc,GAAOT,EAG/BU,eAAgBC,GACdC,OAAOC,OAAOhB,KAAKF,iBAAkBgB,GAGvCG,SAAUC,SACFC,ECnDH,SAASC,EAAWF,EAAaC,EAAQ,GAAIE,OAC7C,MAAMC,KAAOJ,EAAa,OACvBK,EAAUL,EAAYI,GACtBnB,EAAOkB,KAAgBA,KAAcC,IAAQA,EAC5B,iBAAZC,GAAoC,OAAZA,EACjCH,EAAUG,EAASJ,EAAOhB,GAE1BgB,EAAMhB,GAAQoB,SAGXJ,EDyCSC,CAAUF,OACnB,MAAMI,KAAOH,OACXpB,KAAKuB,EAAKH,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