| 1 |
"use strict"; |
| 2 |
var wp; |
| 3 |
(wp ||= {}).hooks = (() => { |
| 4 |
var __defProp = Object.defineProperty; |
| 5 |
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; |
| 6 |
var __getOwnPropNames = Object.getOwnPropertyNames; |
| 7 |
var __hasOwnProp = Object.prototype.hasOwnProperty; |
| 8 |
var __export = (target, all) => { |
| 9 |
for (var name in all) |
| 10 |
__defProp(target, name, { get: all[name], enumerable: true }); |
| 11 |
}; |
| 12 |
var __copyProps = (to, from, except, desc) => { |
| 13 |
if (from && typeof from === "object" || typeof from === "function") { |
| 14 |
for (let key of __getOwnPropNames(from)) |
| 15 |
if (!__hasOwnProp.call(to, key) && key !== except) |
| 16 |
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); |
| 17 |
} |
| 18 |
return to; |
| 19 |
}; |
| 20 |
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); |
| 21 |
|
| 22 |
// packages/hooks/build-module/index.mjs |
| 23 |
var index_exports = {}; |
| 24 |
__export(index_exports, { |
| 25 |
actions: () => actions, |
| 26 |
addAction: () => addAction, |
| 27 |
addFilter: () => addFilter, |
| 28 |
applyFilters: () => applyFilters, |
| 29 |
applyFiltersAsync: () => applyFiltersAsync, |
| 30 |
createHooks: () => createHooks_default, |
| 31 |
currentAction: () => currentAction, |
| 32 |
currentFilter: () => currentFilter, |
| 33 |
defaultHooks: () => defaultHooks, |
| 34 |
didAction: () => didAction, |
| 35 |
didFilter: () => didFilter, |
| 36 |
doAction: () => doAction, |
| 37 |
doActionAsync: () => doActionAsync, |
| 38 |
doingAction: () => doingAction, |
| 39 |
doingFilter: () => doingFilter, |
| 40 |
filters: () => filters, |
| 41 |
hasAction: () => hasAction, |
| 42 |
hasFilter: () => hasFilter, |
| 43 |
removeAction: () => removeAction, |
| 44 |
removeAllActions: () => removeAllActions, |
| 45 |
removeAllFilters: () => removeAllFilters, |
| 46 |
removeFilter: () => removeFilter |
| 47 |
}); |
| 48 |
|
| 49 |
// packages/hooks/build-module/validateNamespace.mjs |
| 50 |
function validateNamespace(namespace) { |
| 51 |
if ("string" !== typeof namespace || "" === namespace) { |
| 52 |
console.error("The namespace must be a non-empty string."); |
| 53 |
return false; |
| 54 |
} |
| 55 |
if (!/^[a-zA-Z][a-zA-Z0-9_.\-\/]*$/.test(namespace)) { |
| 56 |
console.error( |
| 57 |
"The namespace can only contain numbers, letters, dashes, periods, underscores and slashes." |
| 58 |
); |
| 59 |
return false; |
| 60 |
} |
| 61 |
return true; |
| 62 |
} |
| 63 |
var validateNamespace_default = validateNamespace; |
| 64 |
|
| 65 |
// packages/hooks/build-module/validateHookName.mjs |
| 66 |
function validateHookName(hookName) { |
| 67 |
if ("string" !== typeof hookName || "" === hookName) { |
| 68 |
console.error("The hook name must be a non-empty string."); |
| 69 |
return false; |
| 70 |
} |
| 71 |
if (/^__/.test(hookName)) { |
| 72 |
console.error("The hook name cannot begin with `__`."); |
| 73 |
return false; |
| 74 |
} |
| 75 |
if (!/^[a-zA-Z][a-zA-Z0-9_.-]*$/.test(hookName)) { |
| 76 |
console.error( |
| 77 |
"The hook name can only contain numbers, letters, dashes, periods and underscores." |
| 78 |
); |
| 79 |
return false; |
| 80 |
} |
| 81 |
return true; |
| 82 |
} |
| 83 |
var validateHookName_default = validateHookName; |
| 84 |
|
| 85 |
// packages/hooks/build-module/createAddHook.mjs |
| 86 |
function createAddHook(hooks, storeKey) { |
| 87 |
return function addHook(hookName, namespace, callback, priority = 10) { |
| 88 |
const hooksStore = hooks[storeKey]; |
| 89 |
if (!validateHookName_default(hookName)) { |
| 90 |
return; |
| 91 |
} |
| 92 |
if (!validateNamespace_default(namespace)) { |
| 93 |
return; |
| 94 |
} |
| 95 |
if ("function" !== typeof callback) { |
| 96 |
console.error("The hook callback must be a function."); |
| 97 |
return; |
| 98 |
} |
| 99 |
if ("number" !== typeof priority) { |
| 100 |
console.error( |
| 101 |
"If specified, the hook priority must be a number." |
| 102 |
); |
| 103 |
return; |
| 104 |
} |
| 105 |
const handler = { callback, priority, namespace }; |
| 106 |
if (hooksStore[hookName]) { |
| 107 |
const handlers = hooksStore[hookName].handlers; |
| 108 |
let i; |
| 109 |
for (i = handlers.length; i > 0; i--) { |
| 110 |
if (priority >= handlers[i - 1].priority) { |
| 111 |
break; |
| 112 |
} |
| 113 |
} |
| 114 |
if (i === handlers.length) { |
| 115 |
handlers[i] = handler; |
| 116 |
} else { |
| 117 |
handlers.splice(i, 0, handler); |
| 118 |
} |
| 119 |
hooksStore.__current.forEach((hookInfo) => { |
| 120 |
if (hookInfo.name === hookName && hookInfo.currentIndex >= i) { |
| 121 |
hookInfo.currentIndex++; |
| 122 |
} |
| 123 |
}); |
| 124 |
} else { |
| 125 |
hooksStore[hookName] = { |
| 126 |
handlers: [handler], |
| 127 |
runs: 0 |
| 128 |
}; |
| 129 |
} |
| 130 |
if (hookName !== "hookAdded") { |
| 131 |
hooks.doAction( |
| 132 |
"hookAdded", |
| 133 |
hookName, |
| 134 |
namespace, |
| 135 |
callback, |
| 136 |
priority |
| 137 |
); |
| 138 |
} |
| 139 |
}; |
| 140 |
} |
| 141 |
var createAddHook_default = createAddHook; |
| 142 |
|
| 143 |
// packages/hooks/build-module/createRemoveHook.mjs |
| 144 |
function createRemoveHook(hooks, storeKey, removeAll = false) { |
| 145 |
return function removeHook(hookName, namespace) { |
| 146 |
const hooksStore = hooks[storeKey]; |
| 147 |
if (!validateHookName_default(hookName)) { |
| 148 |
return; |
| 149 |
} |
| 150 |
if (!removeAll && !validateNamespace_default(namespace)) { |
| 151 |
return; |
| 152 |
} |
| 153 |
if (!hooksStore[hookName]) { |
| 154 |
return 0; |
| 155 |
} |
| 156 |
let handlersRemoved = 0; |
| 157 |
if (removeAll) { |
| 158 |
handlersRemoved = hooksStore[hookName].handlers.length; |
| 159 |
hooksStore[hookName] = { |
| 160 |
runs: hooksStore[hookName].runs, |
| 161 |
handlers: [] |
| 162 |
}; |
| 163 |
} else { |
| 164 |
const handlers = hooksStore[hookName].handlers; |
| 165 |
for (let i = handlers.length - 1; i >= 0; i--) { |
| 166 |
if (handlers[i].namespace === namespace) { |
| 167 |
handlers.splice(i, 1); |
| 168 |
handlersRemoved++; |
| 169 |
hooksStore.__current.forEach((hookInfo) => { |
| 170 |
if (hookInfo.name === hookName && hookInfo.currentIndex >= i) { |
| 171 |
hookInfo.currentIndex--; |
| 172 |
} |
| 173 |
}); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
if (hookName !== "hookRemoved") { |
| 178 |
hooks.doAction("hookRemoved", hookName, namespace); |
| 179 |
} |
| 180 |
return handlersRemoved; |
| 181 |
}; |
| 182 |
} |
| 183 |
var createRemoveHook_default = createRemoveHook; |
| 184 |
|
| 185 |
// packages/hooks/build-module/createHasHook.mjs |
| 186 |
function createHasHook(hooks, storeKey) { |
| 187 |
return function hasHook(hookName, namespace) { |
| 188 |
const hooksStore = hooks[storeKey]; |
| 189 |
if ("undefined" !== typeof namespace) { |
| 190 |
return hookName in hooksStore && hooksStore[hookName].handlers.some( |
| 191 |
(hook) => hook.namespace === namespace |
| 192 |
); |
| 193 |
} |
| 194 |
return hookName in hooksStore; |
| 195 |
}; |
| 196 |
} |
| 197 |
var createHasHook_default = createHasHook; |
| 198 |
|
| 199 |
// packages/hooks/build-module/createRunHook.mjs |
| 200 |
function createRunHook(hooks, storeKey, returnFirstArg, async) { |
| 201 |
return function runHook(hookName, ...args) { |
| 202 |
const hooksStore = hooks[storeKey]; |
| 203 |
if (!hooksStore[hookName]) { |
| 204 |
hooksStore[hookName] = { |
| 205 |
handlers: [], |
| 206 |
runs: 0 |
| 207 |
}; |
| 208 |
} |
| 209 |
hooksStore[hookName].runs++; |
| 210 |
const handlers = hooksStore[hookName].handlers; |
| 211 |
if (true) { |
| 212 |
if ("hookAdded" !== hookName && hooksStore.all) { |
| 213 |
handlers.push(...hooksStore.all.handlers); |
| 214 |
} |
| 215 |
} |
| 216 |
if (!handlers || !handlers.length) { |
| 217 |
return returnFirstArg ? args[0] : void 0; |
| 218 |
} |
| 219 |
const hookInfo = { |
| 220 |
name: hookName, |
| 221 |
currentIndex: 0 |
| 222 |
}; |
| 223 |
async function asyncRunner() { |
| 224 |
try { |
| 225 |
hooksStore.__current.add(hookInfo); |
| 226 |
let result = returnFirstArg ? args[0] : void 0; |
| 227 |
while (hookInfo.currentIndex < handlers.length) { |
| 228 |
const handler = handlers[hookInfo.currentIndex]; |
| 229 |
result = await handler.callback.apply(null, args); |
| 230 |
if (returnFirstArg) { |
| 231 |
args[0] = result; |
| 232 |
} |
| 233 |
hookInfo.currentIndex++; |
| 234 |
} |
| 235 |
return returnFirstArg ? result : void 0; |
| 236 |
} finally { |
| 237 |
hooksStore.__current.delete(hookInfo); |
| 238 |
} |
| 239 |
} |
| 240 |
function syncRunner() { |
| 241 |
try { |
| 242 |
hooksStore.__current.add(hookInfo); |
| 243 |
let result = returnFirstArg ? args[0] : void 0; |
| 244 |
while (hookInfo.currentIndex < handlers.length) { |
| 245 |
const handler = handlers[hookInfo.currentIndex]; |
| 246 |
result = handler.callback.apply(null, args); |
| 247 |
if (returnFirstArg) { |
| 248 |
args[0] = result; |
| 249 |
} |
| 250 |
hookInfo.currentIndex++; |
| 251 |
} |
| 252 |
return returnFirstArg ? result : void 0; |
| 253 |
} finally { |
| 254 |
hooksStore.__current.delete(hookInfo); |
| 255 |
} |
| 256 |
} |
| 257 |
return (async ? asyncRunner : syncRunner)(); |
| 258 |
}; |
| 259 |
} |
| 260 |
var createRunHook_default = createRunHook; |
| 261 |
|
| 262 |
// packages/hooks/build-module/createCurrentHook.mjs |
| 263 |
function createCurrentHook(hooks, storeKey) { |
| 264 |
return function currentHook() { |
| 265 |
const hooksStore = hooks[storeKey]; |
| 266 |
const currentArray = Array.from(hooksStore.__current); |
| 267 |
return currentArray.at(-1)?.name ?? null; |
| 268 |
}; |
| 269 |
} |
| 270 |
var createCurrentHook_default = createCurrentHook; |
| 271 |
|
| 272 |
// packages/hooks/build-module/createDoingHook.mjs |
| 273 |
function createDoingHook(hooks, storeKey) { |
| 274 |
return function doingHook(hookName) { |
| 275 |
const hooksStore = hooks[storeKey]; |
| 276 |
if ("undefined" === typeof hookName) { |
| 277 |
return hooksStore.__current.size > 0; |
| 278 |
} |
| 279 |
return Array.from(hooksStore.__current).some( |
| 280 |
(hook) => hook.name === hookName |
| 281 |
); |
| 282 |
}; |
| 283 |
} |
| 284 |
var createDoingHook_default = createDoingHook; |
| 285 |
|
| 286 |
// packages/hooks/build-module/createDidHook.mjs |
| 287 |
function createDidHook(hooks, storeKey) { |
| 288 |
return function didHook(hookName) { |
| 289 |
const hooksStore = hooks[storeKey]; |
| 290 |
if (!validateHookName_default(hookName)) { |
| 291 |
return; |
| 292 |
} |
| 293 |
return hooksStore[hookName] && hooksStore[hookName].runs ? hooksStore[hookName].runs : 0; |
| 294 |
}; |
| 295 |
} |
| 296 |
var createDidHook_default = createDidHook; |
| 297 |
|
| 298 |
// packages/hooks/build-module/createHooks.mjs |
| 299 |
var _Hooks = class { |
| 300 |
actions; |
| 301 |
filters; |
| 302 |
addAction; |
| 303 |
addFilter; |
| 304 |
removeAction; |
| 305 |
removeFilter; |
| 306 |
hasAction; |
| 307 |
hasFilter; |
| 308 |
removeAllActions; |
| 309 |
removeAllFilters; |
| 310 |
doAction; |
| 311 |
doActionAsync; |
| 312 |
applyFilters; |
| 313 |
applyFiltersAsync; |
| 314 |
currentAction; |
| 315 |
currentFilter; |
| 316 |
doingAction; |
| 317 |
doingFilter; |
| 318 |
didAction; |
| 319 |
didFilter; |
| 320 |
constructor() { |
| 321 |
this.actions = /* @__PURE__ */ Object.create(null); |
| 322 |
this.actions.__current = /* @__PURE__ */ new Set(); |
| 323 |
this.filters = /* @__PURE__ */ Object.create(null); |
| 324 |
this.filters.__current = /* @__PURE__ */ new Set(); |
| 325 |
this.addAction = createAddHook_default(this, "actions"); |
| 326 |
this.addFilter = createAddHook_default(this, "filters"); |
| 327 |
this.removeAction = createRemoveHook_default(this, "actions"); |
| 328 |
this.removeFilter = createRemoveHook_default(this, "filters"); |
| 329 |
this.hasAction = createHasHook_default(this, "actions"); |
| 330 |
this.hasFilter = createHasHook_default(this, "filters"); |
| 331 |
this.removeAllActions = createRemoveHook_default(this, "actions", true); |
| 332 |
this.removeAllFilters = createRemoveHook_default(this, "filters", true); |
| 333 |
this.doAction = createRunHook_default(this, "actions", false, false); |
| 334 |
this.doActionAsync = createRunHook_default(this, "actions", false, true); |
| 335 |
this.applyFilters = createRunHook_default(this, "filters", true, false); |
| 336 |
this.applyFiltersAsync = createRunHook_default(this, "filters", true, true); |
| 337 |
this.currentAction = createCurrentHook_default(this, "actions"); |
| 338 |
this.currentFilter = createCurrentHook_default(this, "filters"); |
| 339 |
this.doingAction = createDoingHook_default(this, "actions"); |
| 340 |
this.doingFilter = createDoingHook_default(this, "filters"); |
| 341 |
this.didAction = createDidHook_default(this, "actions"); |
| 342 |
this.didFilter = createDidHook_default(this, "filters"); |
| 343 |
} |
| 344 |
}; |
| 345 |
function createHooks() { |
| 346 |
return new _Hooks(); |
| 347 |
} |
| 348 |
var createHooks_default = createHooks; |
| 349 |
|
| 350 |
// packages/hooks/build-module/index.mjs |
| 351 |
var defaultHooks = createHooks_default(); |
| 352 |
var { |
| 353 |
addAction, |
| 354 |
addFilter, |
| 355 |
removeAction, |
| 356 |
removeFilter, |
| 357 |
hasAction, |
| 358 |
hasFilter, |
| 359 |
removeAllActions, |
| 360 |
removeAllFilters, |
| 361 |
doAction, |
| 362 |
doActionAsync, |
| 363 |
applyFilters, |
| 364 |
applyFiltersAsync, |
| 365 |
currentAction, |
| 366 |
currentFilter, |
| 367 |
doingAction, |
| 368 |
doingFilter, |
| 369 |
didAction, |
| 370 |
didFilter, |
| 371 |
actions, |
| 372 |
filters |
| 373 |
} = defaultHooks; |
| 374 |
return __toCommonJS(index_exports); |
| 375 |
})(); |
| 376 |
//# sourceMappingURL=index.js.map |
| 377 |
|