@wordpress/widget-dashboard
Version:
Stateless rendering engine for widget dashboards: the WidgetDashboard compound component and its grid-settings kit.
1 lines • 3.45 kB
Source Map (JSON)
{"version":3,"file":"use-run-actions.cjs","names":["useDashboardInternalContext","useObservableValue","useCallback"],"sources":["../../../src/components/widget-actions/use-run-actions.ts"],"sourcesContent":["import { useCallback } from '@wordpress/element';\nimport { useObservableValue } from '@wordpress/compose';\nimport type { WidgetCallbackAction } from '@wordpress/widget-primitives';\nimport { useDashboardInternalContext } from '../../context/dashboard-context';\nimport { setActionPending } from '../../utils/pending-actions-map';\n\nconst NONE: ReadonlySet< string > = new Set();\n\nfunction isThenable( value: unknown ): value is PromiseLike< unknown > {\n\treturn (\n\t\ttypeof ( value as PromiseLike< unknown > | null )?.then === 'function'\n\t);\n}\n\n/**\n * Logs a callback failure with the instance and the action it belongs to.\n * The rejection is passed through so a plain object stays inspectable.\n *\n * Hosts have no notices capability yet, so this is the only surface.\n *\n * @param {string} uuid The instance.\n * @param {string} actionId The action.\n * @param {unknown} error Whatever the callback threw or rejected with.\n */\nfunction logActionFailure(\n\tuuid: string,\n\tactionId: string,\n\terror: unknown\n): void {\n\t// eslint-disable-next-line no-console -- Deliberately log errors here.\n\tconsole.error( `Widget ${ uuid } action \"${ actionId }\" failed.`, error );\n}\n\n/**\n * Runs an instance's callback actions and tracks, by `id`, the ones whose\n * promise has not settled. The state lives with the instance, so it\n * survives the surface that ran the action unmounting.\n *\n * @param {string} uuid The instance.\n */\nexport function useRunActions( uuid: string ): {\n\trun: ( action: WidgetCallbackAction ) => Promise< void >;\n\tpendingIds: ReadonlySet< string >;\n} {\n\tconst { pendingActions } = useDashboardInternalContext();\n\tconst pendingIds = useObservableValue( pendingActions, uuid ) ?? NONE;\n\n\tconst run = useCallback(\n\t\tasync ( action: WidgetCallbackAction ) => {\n\t\t\tlet result: void | Promise< void >;\n\t\t\ttry {\n\t\t\t\tresult = action.callback();\n\t\t\t} catch ( error ) {\n\t\t\t\tlogActionFailure( uuid, action.id, error );\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif ( ! isThenable( result ) ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsetActionPending( pendingActions, uuid, action.id, true );\n\t\t\ttry {\n\t\t\t\tawait result;\n\t\t\t} catch ( error ) {\n\t\t\t\tlogActionFailure( uuid, action.id, error );\n\t\t\t} finally {\n\t\t\t\tsetActionPending( pendingActions, uuid, action.id, false );\n\t\t\t}\n\t\t},\n\t\t[ pendingActions, uuid ]\n\t);\n\n\treturn { run, pendingIds };\n}\n"],"mappings":";;;;;;;;;;AAMA,MAAM,uBAA8B,IAAI,IAAI;AAE5C,SAAS,WAAY,OAAkD;CACtE,OACC,OAAS,OAA0C,SAAS;AAE9D;;;;;;;;;;;AAYA,SAAS,iBACR,MACA,UACA,OACO;CAEP,QAAQ,MAAO,UAAW,KAAM,WAAY,SAAU,YAAY,KAAM;AACzE;;;;;;;;AASA,SAAgB,cAAe,MAG7B;CACD,MAAM,EAAE,oBAAA,GAAmBA,oCAAAA,4BAAAA,CAA4B;CACvD,MAAM,cAAA,GAAaC,mBAAAA,mBAAAA,CAAoB,gBAAgB,IAAK,KAAK;CA4BjE,OAAO;EAAE,MAAA,GA1BGC,mBAAAA,YAAAA,CACX,OAAQ,WAAkC;GACzC,IAAI;GACJ,IAAI;IACH,SAAS,OAAO,SAAS;GAC1B,SAAU,OAAQ;IACjB,iBAAkB,MAAM,OAAO,IAAI,KAAM;IACzC;GACD;GAEA,IAAK,CAAE,WAAY,MAAO,GACzB;GAGD,CAAA,GAAA,mCAAA,iBAAA,CAAkB,gBAAgB,MAAM,OAAO,IAAI,IAAK;GACxD,IAAI;IACH,MAAM;GACP,SAAU,OAAQ;IACjB,iBAAkB,MAAM,OAAO,IAAI,KAAM;GAC1C,UAAU;IACT,CAAA,GAAA,mCAAA,iBAAA,CAAkB,gBAAgB,MAAM,OAAO,IAAI,KAAM;GAC1D;EACD,GACA,CAAE,gBAAgB,IAAK,CAGb;EAAG;CAAW;AAC1B"}