UNPKG

@hoover-institution/hubspot-lib

Version:

A toolkit for deep integration with HubSpot's Marketing Events API with a plugin-based architecture.

77 lines (67 loc) 1.98 kB
const hookPoints = new Map(); // hookPoint → { bitmask → handler } const pluginIdToName = new Map(); // bitmask → pluginName (for reverse lookup) const pluginNameToId = new Map(); // pluginName → bitmask (for collision prevention) /** * Registers a plugin handler function for a specific hook point and bitmask. * * @param {string} hookPoint * @param {number} bitmask * @param {Function} fn * @param {{ override?: boolean, pluginName?: string }} [options] */ export function registerPlugin( hookPoint, bitmask, fn, { override = false, pluginName } = {} ) { const handlers = hookPoints.get(hookPoint) || {}; if (!override && handlers[bitmask]) { throw new Error( `Plugin already registered for ${hookPoint} and bitmask ${bitmask}` ); } handlers[bitmask] = fn; hookPoints.set(hookPoint, handlers); // Store plugin name for later reverse lookup if (pluginName) { pluginIdToName.set(bitmask, pluginName); pluginNameToId.set(pluginName, bitmask); } } export function getHandlers(hookPoint) { return hookPoints.get(hookPoint) || {}; } export function getPluginName(bitmask) { return pluginIdToName.get(bitmask) ?? `PLUGIN_${bitmask}`; } export function getPluginId(pluginName) { return pluginNameToId.get(pluginName); } /** * Returns a combined bitmask representing all registered plugins. * This includes any plugin registered with a bitmask via registerPlugin(). * * @returns {number} Bitmask combining all registered plugin IDs */ export function getAllPluginBitmask() { let combined = 0; for (const bitmask of pluginIdToName.keys()) { combined |= bitmask; } return combined; } /** * Returns a shallow copy of the current hook map: * pluginName → bitmask * * @returns {Record<string, number>} */ export function getHookMap() { const result = {}; for (const [pluginName, bitmask] of pluginNameToId.entries()) { result[pluginName] = bitmask; } // @ts-ignore return result; }