UNPKG

@rnaga/wp-node

Version:

👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**

83 lines (82 loc) • 2.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Hooks = void 0; const action_1 = require("./action"); const filter_1 = require("./filter"); const hooks_reflect_1 = require("./hooks-reflect"); /** * Represents a collection of hooks for filtering and actions. * * TFilters - The type of filters. * TActions - The type of actions. */ class Hooks { hooks; /** * The map of hooks for each environment. * * @remarks The structure of the map is as follows: * - The key is the environment. * - The value is a map of hooks. * - The key is the name of the hook which should be unique across all hooks. * - The value is the hook class where its methods are decorated with `@filter` or `@action`. * @example * ```ts * @hook("example") * class ExampleHook { * @filter("example_filter") * async exampleFilter(n: number) { * return n + 10; * } * } * ``` */ static hooksEnvMap = new Map(); /** * Retrieves the hook map for the specified environment. * * @param env - The environment for which to retrieve the hook map. * @returns The hook map for the specified environment. */ static get(env) { const hookMap = Hooks.hooksEnvMap.get(env) ?? new Map(); return hookMap; } /** * Sets the hook map for the specified environment. * * @param env - The environment for which to set the hook map. * @param hookMap - The hook map to set. */ static set(env, hookMap) { Hooks.hooksEnvMap.set(env, hookMap); } #initialized = false; action; filter; /** * Creates an instance of Hooks. * * @param hooks - The map of hooks. */ constructor(hooks) { this.hooks = hooks; this.filter = new filter_1.Filter(); this.action = new action_1.Action(); } /** * Initializes the hooks. * If the hooks have already been initialized, this method does nothing. */ init() { if (this.#initialized) { return; } // Load default actions for (const hook of this.hooks.values()) { hooks_reflect_1.HooksReflect.register([this.filter, this.action], new hook(), hook); } this.#initialized = true; } } exports.Hooks = Hooks;