UNPKG

1.22 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.withInit = exports.onInit = void 0;
4const logger = require("../logger");
5let initCallback = null;
6let didInit = false;
7/**
8 * Registers a callback that should be run when in a production environment
9 * before executing any functions code.
10 * Calling this function more than once leads to undefined behavior.
11 * @param callback initialization callback to be run before any function executes.
12 */
13function onInit(callback) {
14 if (initCallback) {
15 logger.warn("Setting onInit callback more than once. Only the most recent callback will be called");
16 }
17 initCallback = callback;
18 didInit = false;
19}
20exports.onInit = onInit;
21/** @internal */
22function withInit(func) {
23 return async (...args) => {
24 if (!didInit) {
25 if (initCallback) {
26 await initCallback();
27 }
28 didInit = true;
29 }
30 // Note: This cast is actually inaccurate because it may be a promise, but
31 // it doesn't actually matter because the async function will promisify
32 // non-promises and forward promises.
33 return func(...args);
34 };
35}
36exports.withInit = withInit;