import { type ActionInstanceMap, type ActionTypes, createAction, createAsyncAction, } from '@equinor/fusion-observable'; import type { AppConfig, AppManifest, AppModulesInstance, AppScriptModule, AppSettings, } from '../types'; /** * Factory function that creates all action creators used by the {@link App} * state machine. * * Actions are grouped by domain: * - **Manifest** – `setManifest`, `fetchManifest` (async) * - **Config** – `setConfig`, `fetchConfig` (async) * - **Settings** – `setSettings`, `fetchSettings` (async), `updateSettings` (async) * - **Script module** – `setModule`, `importApp` (async) * - **Instance** – `setInstance` * - **Lifecycle** – `initialize` (async) */ const createActions = () => ({ /** Manifest loading */ setManifest: createAction('set_manifest', (manifest: AppManifest, update?: boolean) => ({ payload: manifest, meta: { // TODO when updating created: Date.now(), update, }, })), fetchManifest: createAsyncAction( 'fetch_manifest', (key: string, tag?: string, update?: boolean) => ({ payload: { key, tag }, meta: { update } }), (manifest: AppManifest) => ({ payload: manifest }), (error: unknown) => ({ payload: error }), ), /** Config loading */ setConfig: createAction('set_config', (config: AppConfig) => ({ payload: config })), fetchConfig: createAsyncAction( 'fetch_config', (manifest: AppManifest) => ({ payload: manifest }), (config: AppConfig) => ({ payload: config }), (error: unknown) => ({ payload: error }), ), /** Settings loading */ setSettings: createAction('set_settings', (settings?: AppSettings) => ({ payload: settings, })), /** Fetching settings */ fetchSettings: createAsyncAction( 'fetch_settings', (appKey: string) => ({ payload: { appKey } }), (settings: AppSettings) => ({ payload: settings }), (error: unknown) => ({ payload: error }), ), /** Updating settings */ updateSettings: createAsyncAction( 'update_settings', (appKey: string, settings: AppSettings) => ({ payload: { appKey, settings }, }), (settings: AppSettings) => ({ payload: settings, }), (error: unknown) => ({ payload: error, }), ), updateSettingsAbort: createAction('update_settings::abort', (id: string) => ({ payload: id, })), /** App loading */ // eslint-disable-next-line @typescript-eslint/no-explicit-any setModule: createAction('set_module', (module: any) => ({ payload: module })), importApp: createAsyncAction( 'import_app', (entrypoint: string) => ({ payload: entrypoint }), (module: AppScriptModule) => ({ payload: module }), (error: unknown) => ({ payload: error }), ), // App Instance setInstance: createAction('set_instance', (instance: AppModulesInstance) => ({ payload: instance, })), initialize: createAsyncAction( 'initialize_app', () => ({ payload: null }), () => ({ payload: null }), (error: unknown) => ({ payload: error }), ), }); /** Singleton action creator map used by the app state machine. */ export const actions = createActions(); /** Record mapping action names to their creator functions. */ export type ActionBuilder = ReturnType; /** Map of action names to their instantiated action shapes. */ export type ActionMap = ActionInstanceMap; /** Union of all action types dispatched by the app state machine. */ export type Actions = ActionTypes;