UNPKG

@wordpress/widget-dashboard

Version:

Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.

62 lines (61 loc) • 2.28 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_element = require("@wordpress/element"); let _wordpress_compose = require("@wordpress/compose"); let ______context_dashboard_context_tsx = require("../../context/dashboard-context.cjs"); let ______utils_pending_actions_map_ts = require("../../utils/pending-actions-map.cjs"); // packages/widget-dashboard/src/components/widget-actions/use-run-actions.ts const NONE = /* @__PURE__ */ new Set(); function isThenable(value) { return typeof value?.then === "function"; } /** * Logs a callback failure with the instance and the action it belongs to. * The rejection is passed through so a plain object stays inspectable. * * Hosts have no notices capability yet, so this is the only surface. * * @param {string} uuid The instance. * @param {string} actionId The action. * @param {unknown} error Whatever the callback threw or rejected with. */ function logActionFailure(uuid, actionId, error) { console.error(`Widget ${uuid} action "${actionId}" failed.`, error); } /** * Runs an instance's callback actions and tracks, by `id`, the ones whose * promise has not settled. The state lives with the instance, so it * survives the surface that ran the action unmounting. * * @param {string} uuid The instance. */ function useRunActions(uuid) { const { pendingActions } = (0, ______context_dashboard_context_tsx.useDashboardInternalContext)(); const pendingIds = (0, _wordpress_compose.useObservableValue)(pendingActions, uuid) ?? NONE; return { run: (0, _wordpress_element.useCallback)(async (action) => { let result; try { result = action.callback(); } catch (error) { logActionFailure(uuid, action.id, error); return; } if (!isThenable(result)) return; (0, ______utils_pending_actions_map_ts.setActionPending)(pendingActions, uuid, action.id, true); try { await result; } catch (error) { logActionFailure(uuid, action.id, error); } finally { (0, ______utils_pending_actions_map_ts.setActionPending)(pendingActions, uuid, action.id, false); } }, [pendingActions, uuid]), pendingIds }; } exports.useRunActions = useRunActions; //# sourceMappingURL=use-run-actions.cjs.map