wp/wp-includes/js/dist/hooks.js
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    41 __webpack_require__.d(__webpack_exports__, {
    41 __webpack_require__.d(__webpack_exports__, {
    42   actions: () => (/* binding */ actions),
    42   actions: () => (/* binding */ actions),
    43   addAction: () => (/* binding */ addAction),
    43   addAction: () => (/* binding */ addAction),
    44   addFilter: () => (/* binding */ addFilter),
    44   addFilter: () => (/* binding */ addFilter),
    45   applyFilters: () => (/* binding */ applyFilters),
    45   applyFilters: () => (/* binding */ applyFilters),
       
    46   applyFiltersAsync: () => (/* binding */ applyFiltersAsync),
    46   createHooks: () => (/* reexport */ build_module_createHooks),
    47   createHooks: () => (/* reexport */ build_module_createHooks),
    47   currentAction: () => (/* binding */ currentAction),
    48   currentAction: () => (/* binding */ currentAction),
    48   currentFilter: () => (/* binding */ currentFilter),
    49   currentFilter: () => (/* binding */ currentFilter),
    49   defaultHooks: () => (/* binding */ defaultHooks),
    50   defaultHooks: () => (/* binding */ defaultHooks),
    50   didAction: () => (/* binding */ didAction),
    51   didAction: () => (/* binding */ didAction),
    51   didFilter: () => (/* binding */ didFilter),
    52   didFilter: () => (/* binding */ didFilter),
    52   doAction: () => (/* binding */ doAction),
    53   doAction: () => (/* binding */ doAction),
       
    54   doActionAsync: () => (/* binding */ doActionAsync),
    53   doingAction: () => (/* binding */ doingAction),
    55   doingAction: () => (/* binding */ doingAction),
    54   doingFilter: () => (/* binding */ doingFilter),
    56   doingFilter: () => (/* binding */ doingFilter),
    55   filters: () => (/* binding */ filters),
    57   filters: () => (/* binding */ filters),
    56   hasAction: () => (/* binding */ hasAction),
    58   hasAction: () => (/* binding */ hasAction),
    57   hasFilter: () => (/* binding */ hasFilter),
    59   hasFilter: () => (/* binding */ hasFilter),
    59   removeAllActions: () => (/* binding */ removeAllActions),
    61   removeAllActions: () => (/* binding */ removeAllActions),
    60   removeAllFilters: () => (/* binding */ removeAllFilters),
    62   removeAllFilters: () => (/* binding */ removeAllFilters),
    61   removeFilter: () => (/* binding */ removeFilter)
    63   removeFilter: () => (/* binding */ removeFilter)
    62 });
    64 });
    63 
    65 
    64 ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/validateNamespace.js
    66 ;// ./node_modules/@wordpress/hooks/build-module/validateNamespace.js
    65 /**
    67 /**
    66  * Validate a namespace string.
    68  * Validate a namespace string.
    67  *
    69  *
    68  * @param {string} namespace The namespace to validate - should take the form
    70  * @param {string} namespace The namespace to validate - should take the form
    69  *                           `vendor/plugin/function`.
    71  *                           `vendor/plugin/function`.
    83   }
    85   }
    84   return true;
    86   return true;
    85 }
    87 }
    86 /* harmony default export */ const build_module_validateNamespace = (validateNamespace);
    88 /* harmony default export */ const build_module_validateNamespace = (validateNamespace);
    87 
    89 
    88 ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/validateHookName.js
    90 ;// ./node_modules/@wordpress/hooks/build-module/validateHookName.js
    89 /**
    91 /**
    90  * Validate a hookName string.
    92  * Validate a hookName string.
    91  *
    93  *
    92  * @param {string} hookName The hook name to validate. Should be a non empty string containing
    94  * @param {string} hookName The hook name to validate. Should be a non empty string containing
    93  *                          only numbers, letters, dashes, periods and underscores. Also,
    95  *                          only numbers, letters, dashes, periods and underscores. Also,
   113   }
   115   }
   114   return true;
   116   return true;
   115 }
   117 }
   116 /* harmony default export */ const build_module_validateHookName = (validateHookName);
   118 /* harmony default export */ const build_module_validateHookName = (validateHookName);
   117 
   119 
   118 ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createAddHook.js
   120 ;// ./node_modules/@wordpress/hooks/build-module/createAddHook.js
   119 /**
   121 /**
   120  * Internal dependencies
   122  * Internal dependencies
   121  */
   123  */
   122 
   124 
   123 
   125 
   207     }
   209     }
   208   };
   210   };
   209 }
   211 }
   210 /* harmony default export */ const build_module_createAddHook = (createAddHook);
   212 /* harmony default export */ const build_module_createAddHook = (createAddHook);
   211 
   213 
   212 ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createRemoveHook.js
   214 ;// ./node_modules/@wordpress/hooks/build-module/createRemoveHook.js
   213 /**
   215 /**
   214  * Internal dependencies
   216  * Internal dependencies
   215  */
   217  */
   216 
   218 
   217 
   219 
   287     return handlersRemoved;
   289     return handlersRemoved;
   288   };
   290   };
   289 }
   291 }
   290 /* harmony default export */ const build_module_createRemoveHook = (createRemoveHook);
   292 /* harmony default export */ const build_module_createRemoveHook = (createRemoveHook);
   291 
   293 
   292 ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createHasHook.js
   294 ;// ./node_modules/@wordpress/hooks/build-module/createHasHook.js
   293 /**
   295 /**
   294  * @callback HasHook
   296  * @callback HasHook
   295  *
   297  *
   296  * Returns whether any handlers are attached for the given hookName and optional namespace.
   298  * Returns whether any handlers are attached for the given hookName and optional namespace.
   297  *
   299  *
   322     return hookName in hooksStore;
   324     return hookName in hooksStore;
   323   };
   325   };
   324 }
   326 }
   325 /* harmony default export */ const build_module_createHasHook = (createHasHook);
   327 /* harmony default export */ const build_module_createHasHook = (createHasHook);
   326 
   328 
   327 ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createRunHook.js
   329 ;// ./node_modules/@wordpress/hooks/build-module/createRunHook.js
   328 /**
   330 /**
   329  * Returns a function which, when invoked, will execute all callbacks
   331  * Returns a function which, when invoked, will execute all callbacks
   330  * registered to a hook of the specified type, optionally returning the final
   332  * registered to a hook of the specified type, optionally returning the final
   331  * value of the call chain.
   333  * value of the call chain.
   332  *
   334  *
   333  * @param {import('.').Hooks}    hooks                  Hooks instance.
   335  * @param {import('.').Hooks}    hooks          Hooks instance.
   334  * @param {import('.').StoreKey} storeKey
   336  * @param {import('.').StoreKey} storeKey
   335  * @param {boolean}              [returnFirstArg=false] Whether each hook callback is expected to
   337  * @param {boolean}              returnFirstArg Whether each hook callback is expected to return its first argument.
   336  *                                                      return its first argument.
   338  * @param {boolean}              async          Whether the hook callback should be run asynchronously
   337  *
   339  *
   338  * @return {(hookName:string, ...args: unknown[]) => undefined|unknown} Function that runs hook callbacks.
   340  * @return {(hookName:string, ...args: unknown[]) => undefined|unknown} Function that runs hook callbacks.
   339  */
   341  */
   340 function createRunHook(hooks, storeKey, returnFirstArg = false) {
   342 function createRunHook(hooks, storeKey, returnFirstArg, async) {
   341   return function runHooks(hookName, ...args) {
   343   return function runHook(hookName, ...args) {
   342     const hooksStore = hooks[storeKey];
   344     const hooksStore = hooks[storeKey];
   343     if (!hooksStore[hookName]) {
   345     if (!hooksStore[hookName]) {
   344       hooksStore[hookName] = {
   346       hooksStore[hookName] = {
   345         handlers: [],
   347         handlers: [],
   346         runs: 0
   348         runs: 0
   356     }
   358     }
   357     const hookInfo = {
   359     const hookInfo = {
   358       name: hookName,
   360       name: hookName,
   359       currentIndex: 0
   361       currentIndex: 0
   360     };
   362     };
   361     hooksStore.__current.push(hookInfo);
   363     async function asyncRunner() {
   362     while (hookInfo.currentIndex < handlers.length) {
   364       try {
   363       const handler = handlers[hookInfo.currentIndex];
   365         hooksStore.__current.add(hookInfo);
   364       const result = handler.callback.apply(null, args);
   366         let result = returnFirstArg ? args[0] : undefined;
   365       if (returnFirstArg) {
   367         while (hookInfo.currentIndex < handlers.length) {
   366         args[0] = result;
   368           const handler = handlers[hookInfo.currentIndex];
       
   369           result = await handler.callback.apply(null, args);
       
   370           if (returnFirstArg) {
       
   371             args[0] = result;
       
   372           }
       
   373           hookInfo.currentIndex++;
       
   374         }
       
   375         return returnFirstArg ? result : undefined;
       
   376       } finally {
       
   377         hooksStore.__current.delete(hookInfo);
   367       }
   378       }
   368       hookInfo.currentIndex++;
   379     }
   369     }
   380     function syncRunner() {
   370     hooksStore.__current.pop();
   381       try {
   371     if (returnFirstArg) {
   382         hooksStore.__current.add(hookInfo);
   372       return args[0];
   383         let result = returnFirstArg ? args[0] : undefined;
   373     }
   384         while (hookInfo.currentIndex < handlers.length) {
   374     return undefined;
   385           const handler = handlers[hookInfo.currentIndex];
       
   386           result = handler.callback.apply(null, args);
       
   387           if (returnFirstArg) {
       
   388             args[0] = result;
       
   389           }
       
   390           hookInfo.currentIndex++;
       
   391         }
       
   392         return returnFirstArg ? result : undefined;
       
   393       } finally {
       
   394         hooksStore.__current.delete(hookInfo);
       
   395       }
       
   396     }
       
   397     return (async ? asyncRunner : syncRunner)();
   375   };
   398   };
   376 }
   399 }
   377 /* harmony default export */ const build_module_createRunHook = (createRunHook);
   400 /* harmony default export */ const build_module_createRunHook = (createRunHook);
   378 
   401 
   379 ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createCurrentHook.js
   402 ;// ./node_modules/@wordpress/hooks/build-module/createCurrentHook.js
   380 /**
   403 /**
   381  * Returns a function which, when invoked, will return the name of the
   404  * Returns a function which, when invoked, will return the name of the
   382  * currently running hook, or `null` if no hook of the given type is currently
   405  * currently running hook, or `null` if no hook of the given type is currently
   383  * running.
   406  * running.
   384  *
   407  *
   387  *
   410  *
   388  * @return {() => string | null} Function that returns the current hook name or null.
   411  * @return {() => string | null} Function that returns the current hook name or null.
   389  */
   412  */
   390 function createCurrentHook(hooks, storeKey) {
   413 function createCurrentHook(hooks, storeKey) {
   391   return function currentHook() {
   414   return function currentHook() {
   392     var _hooksStore$__current;
   415     var _currentArray$at$name;
   393     const hooksStore = hooks[storeKey];
   416     const hooksStore = hooks[storeKey];
   394     return (_hooksStore$__current = hooksStore.__current[hooksStore.__current.length - 1]?.name) !== null && _hooksStore$__current !== void 0 ? _hooksStore$__current : null;
   417     const currentArray = Array.from(hooksStore.__current);
       
   418     return (_currentArray$at$name = currentArray.at(-1)?.name) !== null && _currentArray$at$name !== void 0 ? _currentArray$at$name : null;
   395   };
   419   };
   396 }
   420 }
   397 /* harmony default export */ const build_module_createCurrentHook = (createCurrentHook);
   421 /* harmony default export */ const build_module_createCurrentHook = (createCurrentHook);
   398 
   422 
   399 ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createDoingHook.js
   423 ;// ./node_modules/@wordpress/hooks/build-module/createDoingHook.js
   400 /**
   424 /**
   401  * @callback DoingHook
   425  * @callback DoingHook
   402  * Returns whether a hook is currently being executed.
   426  * Returns whether a hook is currently being executed.
   403  *
   427  *
   404  * @param {string} [hookName] The name of the hook to check for.  If
   428  * @param {string} [hookName] The name of the hook to check for.  If
   421   return function doingHook(hookName) {
   445   return function doingHook(hookName) {
   422     const hooksStore = hooks[storeKey];
   446     const hooksStore = hooks[storeKey];
   423 
   447 
   424     // If the hookName was not passed, check for any current hook.
   448     // If the hookName was not passed, check for any current hook.
   425     if ('undefined' === typeof hookName) {
   449     if ('undefined' === typeof hookName) {
   426       return 'undefined' !== typeof hooksStore.__current[0];
   450       return hooksStore.__current.size > 0;
   427     }
   451     }
   428 
   452 
   429     // Return the __current hook.
   453     // Find if the `hookName` hook is in `__current`.
   430     return hooksStore.__current[0] ? hookName === hooksStore.__current[0].name : false;
   454     return Array.from(hooksStore.__current).some(hook => hook.name === hookName);
   431   };
   455   };
   432 }
   456 }
   433 /* harmony default export */ const build_module_createDoingHook = (createDoingHook);
   457 /* harmony default export */ const build_module_createDoingHook = (createDoingHook);
   434 
   458 
   435 ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createDidHook.js
   459 ;// ./node_modules/@wordpress/hooks/build-module/createDidHook.js
   436 /**
   460 /**
   437  * Internal dependencies
   461  * Internal dependencies
   438  */
   462  */
   439 
   463 
   440 
   464 
   466     return hooksStore[hookName] && hooksStore[hookName].runs ? hooksStore[hookName].runs : 0;
   490     return hooksStore[hookName] && hooksStore[hookName].runs ? hooksStore[hookName].runs : 0;
   467   };
   491   };
   468 }
   492 }
   469 /* harmony default export */ const build_module_createDidHook = (createDidHook);
   493 /* harmony default export */ const build_module_createDidHook = (createDidHook);
   470 
   494 
   471 ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/createHooks.js
   495 ;// ./node_modules/@wordpress/hooks/build-module/createHooks.js
   472 /**
   496 /**
   473  * Internal dependencies
   497  * Internal dependencies
   474  */
   498  */
   475 
   499 
   476 
   500 
   489  */
   513  */
   490 class _Hooks {
   514 class _Hooks {
   491   constructor() {
   515   constructor() {
   492     /** @type {import('.').Store} actions */
   516     /** @type {import('.').Store} actions */
   493     this.actions = Object.create(null);
   517     this.actions = Object.create(null);
   494     this.actions.__current = [];
   518     this.actions.__current = new Set();
   495 
   519 
   496     /** @type {import('.').Store} filters */
   520     /** @type {import('.').Store} filters */
   497     this.filters = Object.create(null);
   521     this.filters = Object.create(null);
   498     this.filters.__current = [];
   522     this.filters.__current = new Set();
   499     this.addAction = build_module_createAddHook(this, 'actions');
   523     this.addAction = build_module_createAddHook(this, 'actions');
   500     this.addFilter = build_module_createAddHook(this, 'filters');
   524     this.addFilter = build_module_createAddHook(this, 'filters');
   501     this.removeAction = build_module_createRemoveHook(this, 'actions');
   525     this.removeAction = build_module_createRemoveHook(this, 'actions');
   502     this.removeFilter = build_module_createRemoveHook(this, 'filters');
   526     this.removeFilter = build_module_createRemoveHook(this, 'filters');
   503     this.hasAction = build_module_createHasHook(this, 'actions');
   527     this.hasAction = build_module_createHasHook(this, 'actions');
   504     this.hasFilter = build_module_createHasHook(this, 'filters');
   528     this.hasFilter = build_module_createHasHook(this, 'filters');
   505     this.removeAllActions = build_module_createRemoveHook(this, 'actions', true);
   529     this.removeAllActions = build_module_createRemoveHook(this, 'actions', true);
   506     this.removeAllFilters = build_module_createRemoveHook(this, 'filters', true);
   530     this.removeAllFilters = build_module_createRemoveHook(this, 'filters', true);
   507     this.doAction = build_module_createRunHook(this, 'actions');
   531     this.doAction = build_module_createRunHook(this, 'actions', false, false);
   508     this.applyFilters = build_module_createRunHook(this, 'filters', true);
   532     this.doActionAsync = build_module_createRunHook(this, 'actions', false, true);
       
   533     this.applyFilters = build_module_createRunHook(this, 'filters', true, false);
       
   534     this.applyFiltersAsync = build_module_createRunHook(this, 'filters', true, true);
   509     this.currentAction = build_module_createCurrentHook(this, 'actions');
   535     this.currentAction = build_module_createCurrentHook(this, 'actions');
   510     this.currentFilter = build_module_createCurrentHook(this, 'filters');
   536     this.currentFilter = build_module_createCurrentHook(this, 'filters');
   511     this.doingAction = build_module_createDoingHook(this, 'actions');
   537     this.doingAction = build_module_createDoingHook(this, 'actions');
   512     this.doingFilter = build_module_createDoingHook(this, 'filters');
   538     this.doingFilter = build_module_createDoingHook(this, 'filters');
   513     this.didAction = build_module_createDidHook(this, 'actions');
   539     this.didAction = build_module_createDidHook(this, 'actions');
   525 function createHooks() {
   551 function createHooks() {
   526   return new _Hooks();
   552   return new _Hooks();
   527 }
   553 }
   528 /* harmony default export */ const build_module_createHooks = (createHooks);
   554 /* harmony default export */ const build_module_createHooks = (createHooks);
   529 
   555 
   530 ;// CONCATENATED MODULE: ./node_modules/@wordpress/hooks/build-module/index.js
   556 ;// ./node_modules/@wordpress/hooks/build-module/index.js
   531 /**
   557 /**
   532  * Internal dependencies
   558  * Internal dependencies
   533  */
   559  */
   534 
   560 
   535 
   561 
   553  * @property {string} name         Hook name
   579  * @property {string} name         Hook name
   554  * @property {number} currentIndex The index
   580  * @property {number} currentIndex The index
   555  */
   581  */
   556 
   582 
   557 /**
   583 /**
   558  * @typedef {Record<string, Hook> & {__current: Current[]}} Store
   584  * @typedef {Record<string, Hook> & {__current: Set<Current>}} Store
   559  */
   585  */
   560 
   586 
   561 /**
   587 /**
   562  * @typedef {'actions' | 'filters'} StoreKey
   588  * @typedef {'actions' | 'filters'} StoreKey
   563  */
   589  */
   575   hasAction,
   601   hasAction,
   576   hasFilter,
   602   hasFilter,
   577   removeAllActions,
   603   removeAllActions,
   578   removeAllFilters,
   604   removeAllFilters,
   579   doAction,
   605   doAction,
       
   606   doActionAsync,
   580   applyFilters,
   607   applyFilters,
       
   608   applyFiltersAsync,
   581   currentAction,
   609   currentAction,
   582   currentFilter,
   610   currentFilter,
   583   doingAction,
   611   doingAction,
   584   doingFilter,
   612   doingFilter,
   585   didAction,
   613   didAction,