{"version":3,"file":"createRemoveHook.mjs","names":[],"sources":["../src/createRemoveHook.ts"],"sourcesContent":["import validateNamespace from './validateNamespace';\nimport validateHookName from './validateHookName';\nimport type { Hooks, StoreKey } from './types';\n\n/**\n * Removes the specified callback from the hook with a given hookName\n * and namespace.\n */\nexport type RemoveHook = (\n\t/**\n\t * The name of the hook to modify.\n\t */\n\thookName: string,\n\t/**\n\t * The unique namespace identifying the callback in the form `vendor/plugin/function`.\n\t */\n\tnamespace: string\n) => number | undefined;\n\n/** Removes all callbacks from a hook, regardless of namespace. */\nexport type RemoveAllHooks = (\n\thookName: string,\n\tnamespace?: string\n) => number | undefined;\n\n/**\n * Returns a function which, when invoked, will remove a specified hook or all\n * hooks by the given name.\n *\n * @param hooks             Hooks instance.\n * @param storeKey\n * @param [removeAll=false] Whether to remove all callbacks for a hookName,\n *                          without regard to namespace. Used to create\n *                          `removeAll*` functions.\n *\n * @return Function that removes hooks.\n */\nfunction createRemoveHook(\n\thooks: Hooks,\n\tstoreKey: StoreKey,\n\tremoveAll: true\n): RemoveAllHooks;\nfunction createRemoveHook(\n\thooks: Hooks,\n\tstoreKey: StoreKey,\n\tremoveAll?: false\n): RemoveHook;\nfunction createRemoveHook(\n\thooks: Hooks,\n\tstoreKey: StoreKey,\n\tremoveAll: boolean = false\n): RemoveAllHooks {\n\treturn function removeHook( hookName, namespace ) {\n\t\tconst hooksStore = hooks[ storeKey ];\n\n\t\tif ( ! validateHookName( hookName ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( ! removeAll && ! validateNamespace( namespace ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\t// Bail if no hooks exist by this name.\n\t\tif ( ! hooksStore[ hookName ] ) {\n\t\t\treturn 0;\n\t\t}\n\n\t\tlet handlersRemoved = 0;\n\n\t\tif ( removeAll ) {\n\t\t\thandlersRemoved = hooksStore[ hookName ].handlers.length;\n\t\t\thooksStore[ hookName ] = {\n\t\t\t\truns: hooksStore[ hookName ].runs,\n\t\t\t\thandlers: [],\n\t\t\t};\n\t\t} else {\n\t\t\t// Try to find the specified callback to remove.\n\t\t\tconst handlers = hooksStore[ hookName ].handlers;\n\t\t\tfor ( let i = handlers.length - 1; i >= 0; i-- ) {\n\t\t\t\tif ( handlers[ i ].namespace === namespace ) {\n\t\t\t\t\thandlers.splice( i, 1 );\n\t\t\t\t\thandlersRemoved++;\n\t\t\t\t\t// This callback may also be part of a hook that is\n\t\t\t\t\t// currently executing.  If the callback we're removing\n\t\t\t\t\t// comes after the current callback, there's no problem;\n\t\t\t\t\t// otherwise we need to decrease the execution index of any\n\t\t\t\t\t// other runs by 1 to account for the removed element.\n\t\t\t\t\thooksStore.__current.forEach( ( hookInfo ) => {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\thookInfo.name === hookName &&\n\t\t\t\t\t\t\thookInfo.currentIndex >= i\n\t\t\t\t\t\t) {\n\t\t\t\t\t\t\thookInfo.currentIndex--;\n\t\t\t\t\t\t}\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif ( hookName !== 'hookRemoved' ) {\n\t\t\thooks.doAction( 'hookRemoved', hookName, namespace );\n\t\t}\n\n\t\treturn handlersRemoved;\n\t};\n}\n\nexport default createRemoveHook;\n"],"mappings":";;;AA+CA,SAAS,iBACR,OACA,UACA,YAAqB,OACJ;CACjB,OAAO,SAAS,WAAY,UAAU,WAAY;EACjD,MAAM,aAAa,MAAO;EAE1B,IAAK,CAAE,iBAAkB,QAAS,GACjC;EAGD,IAAK,CAAE,aAAa,CAAE,kBAAmB,SAAU,GAClD;EAID,IAAK,CAAE,WAAY,WAClB,OAAO;EAGR,IAAI,kBAAkB;EAEtB,IAAK,WAAY;GAChB,kBAAkB,WAAY,SAAU,CAAC,SAAS;GAClD,WAAY,YAAa;IACxB,MAAM,WAAY,SAAU,CAAC;IAC7B,UAAU,CAAC;GACZ;EACD,OAAO;GAEN,MAAM,WAAW,WAAY,SAAU,CAAC;GACxC,KAAM,IAAI,IAAI,SAAS,SAAS,GAAG,KAAK,GAAG,KAC1C,IAAK,SAAU,EAAG,CAAC,cAAc,WAAY;IAC5C,SAAS,OAAQ,GAAG,CAAE;IACtB;IAMA,WAAW,UAAU,SAAW,aAAc;KAC7C,IACC,SAAS,SAAS,YAClB,SAAS,gBAAgB,GAEzB,SAAS;IAEX,CAAE;GACH;EAEF;EAEA,IAAK,aAAa,eACjB,MAAM,SAAU,eAAe,UAAU,SAAU;EAGpD,OAAO;CACR;AACD"}