@forge/jira-bridge

1.3.0-next.01.3.0-next.1
out/custom-field/registerUIMHandlers.js
+out/custom-field/registerUIMHandlers.jsNew file
+70
Index: package/out/custom-field/registerUIMHandlers.js
===================================================================
--- package/out/custom-field/registerUIMHandlers.js
+++ package/out/custom-field/registerUIMHandlers.js
@@ -0,0 +1,70 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.isFcfError = void 0;
+exports.registerUIMHandlers = registerUIMHandlers;
+const errors_1 = require("../errors");
+const getCustomFieldInternalAPI_1 = require("./getCustomFieldInternalAPI");
+const errors_2 = require("../ui-modifications/types/errors");
+const fcfErrorCodes = new Set(errors_2.FCF_ERROR_CODES);
+const HANDLER_UNAVAILABLE_MESSAGE = 'The UIM handler is unregistered.';
+const STALE_CONTEXT_MESSAGE = 'The UIM handler result arrived after the handler was unregistered.';
+const isFcfErrorCode = (code) => typeof code === 'string' && fcfErrorCodes.has(code);
+const isFcfError = (error) => error instanceof Error && isFcfErrorCode(error.code);
+exports.isFcfError = isFcfError;
+const createFcfError = (code, message = code) => Object.assign(new Error(message), { code });
+async function registerUIMHandlers(handlers) {
+    if (!handlers ||
+        typeof handlers !== 'object' ||
+        Array.isArray(handlers) ||
+        (handlers.setValue !== undefined && typeof handlers.setValue !== 'function') ||
+        (handlers.setOptionsVisibility !== undefined && typeof handlers.setOptionsVisibility !== 'function')) {
+        throw new Error('registerUIMHandlers expects an object with optional callable setValue and setOptionsVisibility handlers');
+    }
+    const { actions } = await (0, getCustomFieldInternalAPI_1.getCustomFieldInternalAPI)();
+    const { onBridgeError } = actions;
+    if (typeof actions.registerUIMHandlers !== 'function') {
+        throw createFcfError('FCF_UIM_HANDLERS_UNSUPPORTED', 'This host does not support custom field UIM handlers');
+    }
+    let active = true;
+    const guard = (handler) => async (...args) => {
+        if (!active) {
+            throw createFcfError('FCF_HANDLER_UNAVAILABLE', HANDLER_UNAVAILABLE_MESSAGE);
+        }
+        let result;
+        try {
+            result = await handler.apply(handlers, args);
+        }
+        catch (error) {
+            if (!active) {
+                throw createFcfError('FCF_STALE_CONTEXT', STALE_CONTEXT_MESSAGE);
+            }
+            throw error;
+        }
+        if (!active) {
+            throw createFcfError('FCF_STALE_CONTEXT', STALE_CONTEXT_MESSAGE);
+        }
+        return result;
+    };
+    try {
+        const registration = await actions.registerUIMHandlers({
+            ...(handlers.setValue && { setValue: guard(handlers.setValue) }),
+            ...(handlers.setOptionsVisibility && { setOptionsVisibility: guard(handlers.setOptionsVisibility) })
+        });
+        if (typeof (registration === null || registration === void 0 ? void 0 : registration.unregister) !== 'function') {
+            throw new Error('registerUIMHandlers host returned an invalid registration');
+        }
+        let disposal;
+        return {
+            unregister: () => {
+                active = false;
+                disposal !== null && disposal !== void 0 ? disposal : (disposal = Promise.resolve().then(() => registration.unregister()));
+                return disposal;
+            }
+        };
+    }
+    catch (error) {
+        active = false;
+        onBridgeError === null || onBridgeError === void 0 ? void 0 : onBridgeError({ error: errors_1.REGISTER_UIM_HANDLERS_ERROR, cause: error });
+        throw error;
+    }
+}