UNPKG

firebase-functions

Version:
59 lines (57 loc) 1.98 kB
//#region src/lifecycle/index.ts /** * Use a global singleton to manage the list of declared lifecycle hooks. * * This ensures that lifecycle hooks are shared between CJS and ESM builds, * avoiding the "dual-package hazard" where the src/bin/firebase-functions.ts (CJS) sees * an empty list while the user's code (ESM) populates a different list. */ const majorVersion = typeof "0" !== "undefined" ? "0" : "0"; const GLOBAL_SYMBOL = Symbol.for(`firebase-functions:lifecycle:declaredHooks:v${majorVersion}`); const globalSymbols = globalThis; if (!globalSymbols[GLOBAL_SYMBOL]) { globalSymbols[GLOBAL_SYMBOL] = {}; } /** * Singleton dictionary of declared lifecycle hooks. * @internal */ const declaredLifecycleHooks = globalSymbols[GLOBAL_SYMBOL]; /** * Registers an action to be executed automatically post-deployment when resources in this codebase * are deployed for the first time. * * @param action The lifecycle action to execute. */ function afterFirstDeploy(action) { if (declaredLifecycleHooks.afterFirstDeploy) { throw new Error("Only one afterFirstDeploy lifecycle hook is allowed per codebase."); } declaredLifecycleHooks.afterFirstDeploy = action; } /** * Registers an action to be executed automatically post-deployment when resources in this codebase * are updated. * * @param action The lifecycle action to execute. */ function afterRedeploy(action) { if (declaredLifecycleHooks.afterRedeploy) { throw new Error("Only one afterRedeploy lifecycle hook is allowed per codebase."); } declaredLifecycleHooks.afterRedeploy = action; } /** * Helper to clear declared lifecycle hooks. * @internal */ function clearDeclaredLifecycleHooks() { for (const key of Object.keys(declaredLifecycleHooks)) { delete declaredLifecycleHooks[key]; } } //#endregion exports.afterFirstDeploy = afterFirstDeploy; exports.afterRedeploy = afterRedeploy; exports.clearDeclaredLifecycleHooks = clearDeclaredLifecycleHooks; exports.declaredLifecycleHooks = declaredLifecycleHooks;