@hoover-institution/hubspot-lib
Version:
A toolkit for deep integration with HubSpot's Marketing Events API with a plugin-based architecture.
26 lines (23 loc) • 957 B
JavaScript
import { defineHooks } from "./hooks.js";
import { registerPlugin } from "./pluginRegistry.js";
/**
* Casts a plugin into a hook system by assigning a unique bitmask and
* registering the handler under the specified lifecycle hook point.
*
* If the plugin was already defined, it reuses the existing bitmask.
*
* @param {string} name - Unique plugin name (e.g., "MONGO_SYNC")
* @param {string} hookPoint - The lifecycle event to hook into (e.g., "CREATE_EVENT")
* @param {(payload: any) => any} handler - The function to call when the event fires
* @param {{ override?: boolean }} [options] - Optional override flag if redefining handler
*
* @example
* castHook("MONGO_SYNC", EVENTS.CREATE_EVENT, (payload) => syncToMongo(payload));
*/
export function castHook(name, hookPoint, handler, options = {}) {
const bitmask = defineHooks([name])[name];
registerPlugin(hookPoint, bitmask, handler, {
...options,
pluginName: name,
});
}