@platforma-sdk/ui-vue 1.41.9 → 1.41.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +11 -11
- package/.turbo/turbo-type-check.log +1 -1
- package/CHANGELOG.md +12 -0
- package/dist/components/PlAgDataTable/PlAgDataTableV2.vue.d.ts +2 -7
- package/dist/components/PlAgDataTable/PlAgDataTableV2.vue.d.ts.map +1 -1
- package/dist/components/PlAgDataTable/PlAgDataTableV2.vue2.js +159 -181
- package/dist/components/PlAgDataTable/PlAgDataTableV2.vue2.js.map +1 -1
- package/dist/components/PlAgDataTable/types.d.ts +0 -14
- package/dist/components/PlAgDataTable/types.d.ts.map +1 -1
- package/dist/components/PlAgDataTable/types.js +9 -9
- package/dist/components/PlAgDataTable/types.js.map +1 -1
- package/dist/internal/createAppV2.d.ts +1 -7
- package/dist/internal/createAppV2.d.ts.map +1 -1
- package/dist/internal/createAppV2.js +66 -64
- package/dist/internal/createAppV2.js.map +1 -1
- package/package.json +5 -5
- package/src/components/PlAgDataTable/PlAgDataTableV2.vue +1 -38
- package/src/components/PlAgDataTable/types.ts +2 -16
- package/src/internal/createAppV2.ts +52 -36
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createAppV2.js","sources":["../../src/internal/createAppV2.ts"],"sourcesContent":["import { deepClone, delay, uniqueId } from '@milaboratories/helpers';\nimport type { Mutable } from '@milaboratories/helpers';\nimport type { NavigationState, BlockOutputsBase, BlockState, PlatformaV2, ValueWithUTag, AuthorMarker } from '@platforma-sdk/model';\nimport { hasAbortError, unwrapResult } from '@platforma-sdk/model';\nimport type { Ref } from 'vue';\nimport { reactive, computed, ref } from 'vue';\nimport type { StateModelOptions, UnwrapOutputs, OutputValues, OutputErrors, AppSettings } from '../types';\nimport { createModel } from '../createModel';\nimport { createAppModel } from './createAppModel';\nimport { parseQuery } from '../urls';\nimport { MultiError, unwrapValueOrErrors } from '../utils';\nimport { applyPatch } from 'fast-json-patch';\nimport { UpdateSerializer } from './UpdateSerializer';\n\nexport const patchPoolingDelay = 100;\n\nexport const createNextAuthorMarker = (marker: AuthorMarker | undefined): AuthorMarker => ({\n authorId: marker?.authorId ?? uniqueId(),\n localVersion: (marker?.localVersion ?? 0) + 1,\n});\n\n/**\n * Creates an application instance with reactive state management, outputs, and methods for state updates and navigation.\n *\n * @template Args - The type of arguments used in the application.\n * @template Outputs - The type of block outputs extending `BlockOutputsBase`.\n * @template UiState - The type of the UI state.\n * @template Href - The type of navigation href, defaulting to a string starting with `/`.\n *\n * @param state - Initial state of the application, including args, outputs, UI state, and navigation state.\n * @param platforma - A platform interface for interacting with block states.\n * @param settings - Application settings, such as debug flags.\n *\n * @returns A reactive application object with methods, getters, and state.\n */\nexport function createAppV2<\n Args = unknown,\n Outputs extends BlockOutputsBase = BlockOutputsBase,\n UiState = unknown,\n Href extends `/${string}` = `/${string}`,\n>(\n state: ValueWithUTag<BlockState<Args, Outputs, UiState, Href>>,\n platforma: PlatformaV2<Args, Outputs, UiState, Href>,\n settings: AppSettings,\n) {\n type AppModel = {\n args: Args;\n ui: UiState;\n };\n\n const debug = (msg: string, ...rest: unknown[]) => {\n if (settings.debug) {\n console.log(`%c>>> %c${msg}`, 'color: orange; font-weight: bold', 'color: orange', settings.appId, ...rest);\n }\n };\n\n const error = (msg: string, ...rest: unknown[]) => {\n console.error(`%c>>> %c${msg}`, 'color: red; font-weight: bold', 'color: red', settings.appId, ...rest);\n };\n\n const data = {\n isExternalSnapshot: false,\n author: {\n authorId: uniqueId(),\n localVersion: 0,\n },\n };\n\n const nextAuthorMarker = () => {\n data.author = createNextAuthorMarker(data.author);\n debug('nextAuthorMarker', data.author);\n return data.author;\n };\n\n const closedRef = ref(false);\n\n const uTagRef = ref(state.uTag);\n\n const debounceSpan = settings.debounceSpan ?? 200;\n\n const setArgsQueue = new UpdateSerializer({ debounceSpan });\n const setUiStateQueue = new UpdateSerializer({ debounceSpan });\n const setArgsAndUiStateQueue = new UpdateSerializer({ debounceSpan });\n const setNavigationStateQueue = new UpdateSerializer({ debounceSpan });\n /**\n * Reactive snapshot of the application state, including args, outputs, UI state, and navigation state.\n */\n const snapshot = ref<{\n args: Args;\n outputs: Partial<Outputs>;\n ui: UiState;\n navigationState: NavigationState<Href>;\n }>(state.value) as Ref<{\n args: Args;\n outputs: Partial<Outputs>;\n ui: UiState;\n navigationState: NavigationState<Href>;\n }>;\n\n const setBlockArgs = async (args: Args) => {\n return platforma.setBlockArgs(args, nextAuthorMarker());\n };\n\n const setBlockUiState = async (ui: UiState) => {\n return platforma.setBlockUiState(ui, nextAuthorMarker());\n };\n\n const setBlockArgsAndUiState = async (args: Args, ui: UiState) => {\n return platforma.setBlockArgsAndUiState(args, ui, nextAuthorMarker());\n };\n\n const setNavigationState = async (state: NavigationState<Href>) => {\n return platforma.setNavigationState(state);\n };\n\n (async () => {\n window.addEventListener('beforeunload', () => {\n closedRef.value = true;\n platforma.dispose().then(unwrapResult).catch((err) => {\n error('error in dispose', err);\n });\n });\n\n while (!closedRef.value) {\n try {\n const patches = await platforma.getPatches(uTagRef.value).then(unwrapResult);\n\n debug('patches', JSON.stringify(patches, null, 2));\n debug('uTagRef.value', uTagRef.value);\n debug('patches.uTag', patches.uTag);\n debug('patches.author', patches.author);\n debug('data.author', data.author);\n\n uTagRef.value = patches.uTag;\n\n if (patches.value.length === 0) {\n await new Promise((resolve) => setTimeout(resolve, patchPoolingDelay));\n continue;\n }\n\n const isAuthorChanged = data.author?.authorId !== patches.author?.authorId;\n\n // Immutable behavior, apply external changes to the snapshot\n if (isAuthorChanged || data.isExternalSnapshot) {\n debug('got external changes, applying them to the snapshot', JSON.stringify(snapshot.value, null, 2));\n snapshot.value = applyPatch(snapshot.value, patches.value, false, false).newDocument;\n data.isExternalSnapshot = isAuthorChanged;\n } else {\n // Mutable behavior\n snapshot.value = applyPatch(snapshot.value, patches.value).newDocument;\n }\n\n await new Promise((resolve) => setTimeout(resolve, patchPoolingDelay));\n } catch (err) {\n if (hasAbortError(err)) {\n debug('patches loop aborted');\n closedRef.value = true;\n } else {\n error('error in patches loop', err);\n await new Promise((resolve) => setTimeout(resolve, 1000));\n }\n }\n }\n })();\n\n const outputs = computed<OutputValues<Outputs>>(() => {\n const entries = Object.entries(snapshot.value.outputs as Partial<Readonly<Outputs>>).map(([k, vOrErr]) => [k, vOrErr.ok && vOrErr.value !== undefined ? vOrErr.value : undefined]);\n return Object.fromEntries(entries);\n });\n\n const outputErrors = computed<OutputErrors<Outputs>>(() => {\n const entries = Object.entries(snapshot.value.outputs as Partial<Readonly<Outputs>>).map(([k, vOrErr]) => [k, vOrErr && !vOrErr.ok ? new MultiError(vOrErr.errors) : undefined]);\n return Object.fromEntries(entries);\n });\n\n const appModel = createAppModel(\n {\n get() {\n return { args: snapshot.value.args, ui: snapshot.value.ui } as AppModel;\n },\n autoSave: true,\n onSave(newData: AppModel) {\n debug('onSave', newData);\n setArgsAndUiStateQueue.run(() => setBlockArgsAndUiState(newData.args, newData.ui).then(unwrapResult));\n },\n },\n {\n outputs,\n outputErrors,\n },\n settings,\n );\n\n const cloneArgs = () => deepClone(appModel.model.args) as Args;\n const cloneUiState = () => deepClone(appModel.model.ui) as UiState;\n const cloneNavigationState = () => deepClone(snapshot.value.navigationState) as Mutable<NavigationState<Href>>;\n\n const methods = {\n cloneArgs,\n cloneUiState,\n cloneNavigationState,\n createArgsModel<T extends Args = Args>(options: StateModelOptions<Args, T> = {}) {\n return createModel<T, Args>({\n get() {\n if (options.transform) {\n return options.transform(snapshot.value.args as Args);\n }\n\n return snapshot.value.args as T;\n },\n validate: options.validate,\n autoSave: true,\n onSave(newArgs) {\n setArgsQueue.run(() => setBlockArgs(newArgs).then(unwrapResult));\n },\n });\n },\n /**\n * defaultUiState is temporarily here, remove it after implementing initialUiState\n */\n createUiModel<T extends UiState = UiState>(options: StateModelOptions<UiState, T> = {}, defaultUiState: () => UiState) {\n return createModel<T, UiState>({\n get() {\n if (options.transform) {\n return options.transform(snapshot.value.ui as UiState);\n }\n\n return (snapshot.value.ui ?? defaultUiState()) as T;\n },\n validate: options.validate,\n autoSave: true,\n onSave(newData) {\n setUiStateQueue.run(() => setBlockUiState(newData).then(unwrapResult));\n },\n });\n },\n /**\n * Retrieves the unwrapped values of outputs for the given keys.\n *\n * @template K - Keys of the outputs to unwrap.\n * @param keys - List of output names.\n * @throws Error if the outputs contain errors.\n * @returns An object with unwrapped output values.\n */\n unwrapOutputs<K extends keyof Outputs>(...keys: K[]): UnwrapOutputs<Outputs, K> {\n const outputs = snapshot.value.outputs as Partial<Readonly<Outputs>>;\n const entries = keys.map((key) => [key, unwrapValueOrErrors(outputs[key])]);\n return Object.fromEntries(entries);\n },\n /**\n * Updates the arguments state by applying a callback.\n *\n * @param cb - Callback to modify the current arguments.\n * @returns A promise resolving after the update is applied.\n */\n updateArgs(cb: (args: Args) => void): Promise<boolean> {\n const newArgs = cloneArgs();\n cb(newArgs);\n debug('updateArgs', newArgs);\n appModel.model.args = newArgs;\n return setArgsQueue.run(() => setBlockArgs(newArgs).then(unwrapResult));\n },\n /**\n * Updates the UI state by applying a callback.\n *\n * @param cb - Callback to modify the current UI state.\n * @returns A promise resolving after the update is applied.\n * @todo Make it mutable since there is already an initial one\n */\n updateUiState(cb: (args: UiState) => UiState): Promise<boolean> {\n const newUiState = cb(cloneUiState());\n debug('updateUiState', newUiState);\n appModel.model.ui = newUiState;\n return setUiStateQueue.run(() => setBlockUiState(newUiState).then(unwrapResult));\n },\n /**\n * Navigates to a specific href by updating the navigation state.\n *\n * @param href - The target href to navigate to.\n * @returns A promise resolving after the navigation state is updated.\n */\n navigateTo(href: Href) {\n const newState = cloneNavigationState();\n newState.href = href;\n return setNavigationStateQueue.run(() => setNavigationState(newState).then(unwrapResult));\n },\n async allSettled() {\n await delay(0);\n return setArgsAndUiStateQueue.allSettled();\n },\n };\n\n const getters = {\n closedRef,\n snapshot,\n queryParams: computed(() => parseQuery<Href>(snapshot.value.navigationState.href as Href)),\n href: computed(() => snapshot.value.navigationState.href),\n hasErrors: computed(() => Object.values(snapshot.value.outputs as Partial<Readonly<Outputs>>).some((v) => !v?.ok)),\n };\n\n return reactive(Object.assign(appModel, methods, getters));\n}\n\nexport type BaseAppV2<\n Args = unknown,\n Outputs extends BlockOutputsBase = BlockOutputsBase,\n UiState = unknown,\n Href extends `/${string}` = `/${string}`,\n> = ReturnType<typeof createAppV2<Args, Outputs, UiState, Href>>;\n"],"names":["patchPoolingDelay","createNextAuthorMarker","marker","uniqueId","createAppV2","state","platforma","settings","debug","msg","rest","error","data","nextAuthorMarker","closedRef","ref","uTagRef","debounceSpan","setArgsQueue","UpdateSerializer","setUiStateQueue","setArgsAndUiStateQueue","setNavigationStateQueue","snapshot","setBlockArgs","args","setBlockUiState","ui","setBlockArgsAndUiState","setNavigationState","unwrapResult","err","patches","resolve","isAuthorChanged","_a","_b","applyPatch","hasAbortError","outputs","computed","entries","k","vOrErr","outputErrors","MultiError","appModel","createAppModel","newData","cloneArgs","deepClone","cloneUiState","cloneNavigationState","methods","options","createModel","newArgs","defaultUiState","keys","key","unwrapValueOrErrors","cb","newUiState","href","newState","delay","getters","parseQuery","v","reactive"],"mappings":";;;;;;;;;;;AAcO,MAAMA,IAAoB,KAEpBC,IAAyB,CAACC,OAAoD;AAAA,EACzF,WAAUA,KAAA,gBAAAA,EAAQ,aAAYC,EAAS;AAAA,EACvC,gBAAeD,KAAA,gBAAAA,EAAQ,iBAAgB,KAAK;AAC9C;AAgBgB,SAAAE,GAMdC,GACAC,GACAC,GACA;AAMM,QAAAC,IAAQ,CAACC,MAAgBC,MAAoB;AACjD,IAAIH,EAAS,SACH,QAAA,IAAI,WAAWE,CAAG,IAAI,oCAAoC,iBAAiBF,EAAS,OAAO,GAAGG,CAAI;AAAA,EAE9G,GAEMC,IAAQ,CAACF,MAAgBC,MAAoB;AACzC,YAAA,MAAM,WAAWD,CAAG,IAAI,iCAAiC,cAAcF,EAAS,OAAO,GAAGG,CAAI;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,KAAKlC,CAAY,CAAC;AAAA,IAC1F;AAAA,IACA,MAAM,aAAa;AACjB,mBAAMmC,EAAM,CAAC,GACN5C,EAAuB,WAAW;AAAA,IAAA;AAAA,EAE7C,GAEM6C,IAAU;AAAA,IACd,WAAApD;AAAA,IACA,UAAAS;AAAA,IACA,aAAaiB,EAAS,MAAM2B,EAAiB5C,EAAS,MAAM,gBAAgB,IAAY,CAAC;AAAA,IACzF,MAAMiB,EAAS,MAAMjB,EAAS,MAAM,gBAAgB,IAAI;AAAA,IACxD,WAAWiB,EAAS,MAAM,OAAO,OAAOjB,EAAS,MAAM,OAAqC,EAAE,KAAK,CAAC6C,MAAM,EAACA,KAAA,QAAAA,EAAG,GAAE,CAAC;AAAA,EACnH;AAEA,SAAOC,EAAS,OAAO,OAAOvB,GAAUO,GAASa,CAAO,CAAC;AAC3D;"}
|
|
1
|
+
{"version":3,"file":"createAppV2.js","sources":["../../src/internal/createAppV2.ts"],"sourcesContent":["import { deepClone, delay, uniqueId } from '@milaboratories/helpers';\nimport type { Mutable } from '@milaboratories/helpers';\nimport type { NavigationState, BlockOutputsBase, BlockState, PlatformaV2, ValueWithUTag, AuthorMarker } from '@platforma-sdk/model';\nimport { hasAbortError, unwrapResult } from '@platforma-sdk/model';\nimport type { Ref } from 'vue';\nimport { reactive, computed, ref, watch } from 'vue';\nimport type { StateModelOptions, UnwrapOutputs, OutputValues, OutputErrors, AppSettings } from '../types';\nimport { createModel } from '../createModel';\nimport { parseQuery } from '../urls';\nimport { MultiError, unwrapValueOrErrors } from '../utils';\nimport { applyPatch } from 'fast-json-patch';\nimport { UpdateSerializer } from './UpdateSerializer';\n\nexport const patchPoolingDelay = 100;\n\nexport const createNextAuthorMarker = (marker: AuthorMarker | undefined): AuthorMarker => ({\n authorId: marker?.authorId ?? uniqueId(),\n localVersion: (marker?.localVersion ?? 0) + 1,\n});\n\n/**\n * Creates an application instance with reactive state management, outputs, and methods for state updates and navigation.\n *\n * @template Args - The type of arguments used in the application.\n * @template Outputs - The type of block outputs extending `BlockOutputsBase`.\n * @template UiState - The type of the UI state.\n * @template Href - The type of navigation href, defaulting to a string starting with `/`.\n *\n * @param state - Initial state of the application, including args, outputs, UI state, and navigation state.\n * @param platforma - A platform interface for interacting with block states.\n * @param settings - Application settings, such as debug flags.\n *\n * @returns A reactive application object with methods, getters, and state.\n */\nexport function createAppV2<\n Args = unknown,\n Outputs extends BlockOutputsBase = BlockOutputsBase,\n UiState = unknown,\n Href extends `/${string}` = `/${string}`,\n>(\n state: ValueWithUTag<BlockState<Args, Outputs, UiState, Href>>,\n platforma: PlatformaV2<Args, Outputs, UiState, Href>,\n settings: AppSettings,\n) {\n const debug = (msg: string, ...rest: unknown[]) => {\n if (settings.debug) {\n console.log(`%c>>> %c${msg}`, 'color: orange; font-weight: bold', 'color: orange', settings.appId, ...rest);\n }\n };\n\n const error = (msg: string, ...rest: unknown[]) => {\n console.error(`%c>>> %c${msg}`, 'color: red; font-weight: bold', 'color: red', settings.appId, ...rest);\n };\n\n const data = {\n isExternalSnapshot: false,\n author: {\n authorId: uniqueId(),\n localVersion: 0,\n },\n };\n\n const nextAuthorMarker = () => {\n data.author = createNextAuthorMarker(data.author);\n debug('nextAuthorMarker', data.author);\n return data.author;\n };\n\n const closedRef = ref(false);\n\n const uTagRef = ref(state.uTag);\n\n const debounceSpan = settings.debounceSpan ?? 200;\n\n const setArgsQueue = new UpdateSerializer({ debounceSpan });\n const setUiStateQueue = new UpdateSerializer({ debounceSpan });\n const setArgsAndUiStateQueue = new UpdateSerializer({ debounceSpan });\n const setNavigationStateQueue = new UpdateSerializer({ debounceSpan });\n /**\n * Reactive snapshot of the application state, including args, outputs, UI state, and navigation state.\n */\n const snapshot = ref<{\n args: Args;\n outputs: Partial<Outputs>;\n ui: UiState;\n navigationState: NavigationState<Href>;\n }>(state.value) as Ref<{\n args: Args;\n outputs: Partial<Outputs>;\n ui: UiState;\n navigationState: NavigationState<Href>;\n }>;\n\n const setBlockArgs = async (args: Args) => {\n return platforma.setBlockArgs(args, nextAuthorMarker());\n };\n\n const setBlockUiState = async (ui: UiState) => {\n return platforma.setBlockUiState(ui, nextAuthorMarker());\n };\n\n const setBlockArgsAndUiState = async (args: Args, ui: UiState) => {\n return platforma.setBlockArgsAndUiState(args, ui, nextAuthorMarker());\n };\n\n const setNavigationState = async (state: NavigationState<Href>) => {\n return platforma.setNavigationState(state);\n };\n\n const outputs = computed<OutputValues<Outputs>>(() => {\n const entries = Object.entries(snapshot.value.outputs as Partial<Readonly<Outputs>>).map(([k, vOrErr]) => [k, vOrErr.ok && vOrErr.value !== undefined ? vOrErr.value : undefined]);\n return Object.fromEntries(entries);\n });\n\n const outputErrors = computed<OutputErrors<Outputs>>(() => {\n const entries = Object.entries(snapshot.value.outputs as Partial<Readonly<Outputs>>).map(([k, vOrErr]) => [k, vOrErr && !vOrErr.ok ? new MultiError(vOrErr.errors) : undefined]);\n return Object.fromEntries(entries);\n });\n\n const appModel = reactive({\n error: '',\n model: {\n args: deepClone(snapshot.value.args) as Args,\n ui: deepClone(snapshot.value.ui) as UiState,\n outputs,\n outputErrors,\n },\n }) as {\n error: string;\n model: {\n args: Args;\n ui: UiState;\n outputs: OutputValues<Outputs>;\n outputErrors: OutputErrors<Outputs>;\n };\n };\n\n const appModelWatch = watch(\n () => appModel.model,\n (_newData) => {\n const newData = deepClone(_newData);\n debug('appModel.model', newData);\n setArgsAndUiStateQueue.run(() => setBlockArgsAndUiState(newData.args, newData.ui).then(unwrapResult));\n },\n { deep: true },\n );\n\n const updateAppModelSilently = (newData: {\n args: Args;\n ui: UiState;\n }) => {\n debug('updateAppModelSilently', newData);\n appModelWatch.pause();\n appModel.model.args = deepClone(newData.args) as Args;\n appModel.model.ui = deepClone(newData.ui) as UiState;\n appModelWatch.resume();\n };\n\n (async () => {\n window.addEventListener('beforeunload', () => {\n closedRef.value = true;\n platforma.dispose().then(unwrapResult).catch((err) => {\n error('error in dispose', err);\n });\n });\n\n while (!closedRef.value) {\n try {\n const patches = await platforma.getPatches(uTagRef.value).then(unwrapResult);\n\n debug('patches.length', patches.value.length);\n debug('uTagRef.value', uTagRef.value);\n debug('patches.uTag', patches.uTag);\n debug('patches.author', patches.author);\n debug('data.author', data.author);\n\n uTagRef.value = patches.uTag;\n\n if (patches.value.length === 0) {\n await new Promise((resolve) => setTimeout(resolve, patchPoolingDelay));\n continue;\n }\n\n const isAuthorChanged = data.author?.authorId !== patches.author?.authorId;\n\n // Immutable behavior, apply external changes to the snapshot\n if (isAuthorChanged || data.isExternalSnapshot) {\n debug('got external changes, applying them to the snapshot', JSON.stringify(snapshot.value, null, 2));\n snapshot.value = applyPatch(snapshot.value, patches.value, false, false).newDocument;\n updateAppModelSilently(snapshot.value);\n data.isExternalSnapshot = isAuthorChanged;\n } else {\n // Mutable behavior\n snapshot.value = applyPatch(snapshot.value, patches.value).newDocument;\n }\n\n await new Promise((resolve) => setTimeout(resolve, patchPoolingDelay));\n } catch (err) {\n if (hasAbortError(err)) {\n debug('patches loop aborted');\n closedRef.value = true;\n } else {\n error('error in patches loop', err);\n await new Promise((resolve) => setTimeout(resolve, 1000));\n }\n }\n }\n })();\n\n const cloneArgs = () => deepClone(appModel.model.args) as Args;\n const cloneUiState = () => deepClone(appModel.model.ui) as UiState;\n const cloneNavigationState = () => deepClone(snapshot.value.navigationState) as Mutable<NavigationState<Href>>;\n\n const methods = {\n cloneArgs,\n cloneUiState,\n cloneNavigationState,\n createArgsModel<T extends Args = Args>(options: StateModelOptions<Args, T> = {}) {\n return createModel<T, Args>({\n get() {\n if (options.transform) {\n return options.transform(snapshot.value.args as Args);\n }\n\n return snapshot.value.args as T;\n },\n validate: options.validate,\n autoSave: true,\n onSave(newArgs) {\n setArgsQueue.run(() => setBlockArgs(newArgs).then(unwrapResult));\n },\n });\n },\n /**\n * defaultUiState is temporarily here, remove it after implementing initialUiState\n */\n createUiModel<T extends UiState = UiState>(options: StateModelOptions<UiState, T> = {}, defaultUiState: () => UiState) {\n return createModel<T, UiState>({\n get() {\n if (options.transform) {\n return options.transform(snapshot.value.ui as UiState);\n }\n\n return (snapshot.value.ui ?? defaultUiState()) as T;\n },\n validate: options.validate,\n autoSave: true,\n onSave(newData) {\n setUiStateQueue.run(() => setBlockUiState(newData).then(unwrapResult));\n },\n });\n },\n /**\n * Retrieves the unwrapped values of outputs for the given keys.\n *\n * @template K - Keys of the outputs to unwrap.\n * @param keys - List of output names.\n * @throws Error if the outputs contain errors.\n * @returns An object with unwrapped output values.\n */\n unwrapOutputs<K extends keyof Outputs>(...keys: K[]): UnwrapOutputs<Outputs, K> {\n const outputs = snapshot.value.outputs as Partial<Readonly<Outputs>>;\n const entries = keys.map((key) => [key, unwrapValueOrErrors(outputs[key])]);\n return Object.fromEntries(entries);\n },\n /**\n * Updates the arguments state by applying a callback.\n *\n * @param cb - Callback to modify the current arguments.\n * @returns A promise resolving after the update is applied.\n */\n updateArgs(cb: (args: Args) => void): Promise<boolean> {\n const newArgs = cloneArgs();\n cb(newArgs);\n debug('updateArgs', newArgs);\n appModel.model.args = newArgs;\n return setArgsQueue.run(() => setBlockArgs(newArgs).then(unwrapResult));\n },\n /**\n * Updates the UI state by applying a callback.\n *\n * @param cb - Callback to modify the current UI state.\n * @returns A promise resolving after the update is applied.\n * @todo Make it mutable since there is already an initial one\n */\n updateUiState(cb: (args: UiState) => UiState): Promise<boolean> {\n const newUiState = cb(cloneUiState());\n debug('updateUiState', newUiState);\n appModel.model.ui = newUiState;\n return setUiStateQueue.run(() => setBlockUiState(newUiState).then(unwrapResult));\n },\n /**\n * Navigates to a specific href by updating the navigation state.\n *\n * @param href - The target href to navigate to.\n * @returns A promise resolving after the navigation state is updated.\n */\n navigateTo(href: Href) {\n const newState = cloneNavigationState();\n newState.href = href;\n return setNavigationStateQueue.run(() => setNavigationState(newState).then(unwrapResult));\n },\n async allSettled() {\n await delay(0);\n return setArgsAndUiStateQueue.allSettled();\n },\n };\n\n const getters = {\n closedRef,\n snapshot,\n queryParams: computed(() => parseQuery<Href>(snapshot.value.navigationState.href as Href)),\n href: computed(() => snapshot.value.navigationState.href),\n hasErrors: computed(() => Object.values(snapshot.value.outputs as Partial<Readonly<Outputs>>).some((v) => !v?.ok)),\n };\n\n return reactive(Object.assign(appModel, methods, getters));\n}\n\nexport type BaseAppV2<\n Args = unknown,\n Outputs extends BlockOutputsBase = BlockOutputsBase,\n UiState = unknown,\n Href extends `/${string}` = `/${string}`,\n> = ReturnType<typeof createAppV2<Args, Outputs, UiState, Href>>;\n"],"names":["patchPoolingDelay","createNextAuthorMarker","marker","uniqueId","createAppV2","state","platforma","settings","debug","msg","rest","error","data","nextAuthorMarker","closedRef","ref","uTagRef","debounceSpan","setArgsQueue","UpdateSerializer","setUiStateQueue","setArgsAndUiStateQueue","setNavigationStateQueue","snapshot","setBlockArgs","args","setBlockUiState","ui","setBlockArgsAndUiState","setNavigationState","outputs","computed","entries","k","vOrErr","outputErrors","MultiError","appModel","reactive","deepClone","appModelWatch","watch","_newData","newData","unwrapResult","updateAppModelSilently","err","patches","resolve","isAuthorChanged","_a","_b","applyPatch","hasAbortError","cloneArgs","cloneUiState","cloneNavigationState","methods","options","createModel","newArgs","defaultUiState","keys","key","unwrapValueOrErrors","cb","newUiState","href","newState","delay","getters","parseQuery","v"],"mappings":";;;;;;;;;;AAaO,MAAMA,IAAoB,KAEpBC,IAAyB,CAACC,OAAoD;AAAA,EACzF,WAAUA,KAAA,gBAAAA,EAAQ,aAAYC,EAAS;AAAA,EACvC,gBAAeD,KAAA,gBAAAA,EAAQ,iBAAgB,KAAK;AAC9C;AAgBgB,SAAAE,GAMdC,GACAC,GACAC,GACA;AACM,QAAAC,IAAQ,CAACC,MAAgBC,MAAoB;AACjD,IAAIH,EAAS,SACH,QAAA,IAAI,WAAWE,CAAG,IAAI,oCAAoC,iBAAiBF,EAAS,OAAO,GAAGG,CAAI;AAAA,EAE9G,GAEMC,IAAQ,CAACF,MAAgBC,MAAoB;AACzC,YAAA,MAAM,WAAWD,CAAG,IAAI,iCAAiC,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AA,IAQA,cAAcqB,GAAkD;AACxD,YAAAC,IAAaD,EAAGV,GAAc;AACpC,aAAA/C,EAAM,iBAAiB0D,CAAU,GACjC7B,EAAS,MAAM,KAAK6B,GACb9C,EAAgB,IAAI,MAAMM,EAAgBwC,CAAU,EAAE,KAAKtB,CAAY,CAAC;AAAA,IACjF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOA,WAAWuB,GAAY;AACrB,YAAMC,IAAWZ,EAAqB;AACtC,aAAAY,EAAS,OAAOD,GACT7C,EAAwB,IAAI,MAAMO,EAAmBuC,CAAQ,EAAE,KAAKxB,CAAY,CAAC;AAAA,IAC1F;AAAA,IACA,MAAM,aAAa;AACjB,mBAAMyB,EAAM,CAAC,GACNhD,EAAuB,WAAW;AAAA,IAAA;AAAA,EAE7C,GAEMiD,IAAU;AAAA,IACd,WAAAxD;AAAA,IACA,UAAAS;AAAA,IACA,aAAaQ,EAAS,MAAMwC,EAAiBhD,EAAS,MAAM,gBAAgB,IAAY,CAAC;AAAA,IACzF,MAAMQ,EAAS,MAAMR,EAAS,MAAM,gBAAgB,IAAI;AAAA,IACxD,WAAWQ,EAAS,MAAM,OAAO,OAAOR,EAAS,MAAM,OAAqC,EAAE,KAAK,CAACiD,MAAM,EAACA,KAAA,QAAAA,EAAG,GAAE,CAAC;AAAA,EACnH;AAEA,SAAOlC,EAAS,OAAO,OAAOD,GAAUoB,GAASa,CAAO,CAAC;AAC3D;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platforma-sdk/ui-vue",
|
|
3
|
-
"version": "1.41.
|
|
3
|
+
"version": "1.41.13",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/lib.js",
|
|
6
6
|
"styles": "dist/lib.js",
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"d3-format": "^3.1.0",
|
|
27
27
|
"zod": "~3.23.8",
|
|
28
28
|
"@milaboratories/biowasm-tools": "^1.1.0",
|
|
29
|
-
"@
|
|
30
|
-
"@
|
|
29
|
+
"@platforma-sdk/model": "~1.41.6",
|
|
30
|
+
"@milaboratories/uikit": "2.3.21"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"happy-dom": "^15.11.7",
|
|
@@ -45,8 +45,8 @@
|
|
|
45
45
|
"@faker-js/faker": "^9.2.0",
|
|
46
46
|
"@milaboratories/ts-configs": "1.0.5",
|
|
47
47
|
"@milaboratories/build-configs": "1.0.5",
|
|
48
|
-
"@milaboratories/
|
|
49
|
-
"@milaboratories/
|
|
48
|
+
"@milaboratories/helpers": "^1.6.19",
|
|
49
|
+
"@milaboratories/eslint-config": "^1.0.4"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"test": "vitest run --passWithNoTests",
|
|
@@ -55,7 +55,7 @@ import type {
|
|
|
55
55
|
PlTableRowIdJson,
|
|
56
56
|
} from './types';
|
|
57
57
|
import { watchCached } from '@milaboratories/uikit';
|
|
58
|
-
import {
|
|
58
|
+
import { type PTableHidden } from './sources/common';
|
|
59
59
|
|
|
60
60
|
const tableState = defineModel<PlDataTableStateV2>({
|
|
61
61
|
required: true,
|
|
@@ -388,43 +388,6 @@ defineExpose<PlAgDataTableV2Controller>({
|
|
|
388
388
|
}
|
|
389
389
|
return true;
|
|
390
390
|
},
|
|
391
|
-
getRowCount: async () => {
|
|
392
|
-
const gridApi = await dataRenderedTracker.promise;
|
|
393
|
-
if (gridApi.isDestroyed()) return;
|
|
394
|
-
|
|
395
|
-
return gridApi.getDisplayedRowCount();
|
|
396
|
-
},
|
|
397
|
-
getRow: async (index) => {
|
|
398
|
-
const gridApi = await dataRenderedTracker.promise;
|
|
399
|
-
if (gridApi.isDestroyed()) return;
|
|
400
|
-
|
|
401
|
-
const visibleColumns: {
|
|
402
|
-
spec: PTableColumnSpec;
|
|
403
|
-
field: `${number}`;
|
|
404
|
-
}[] = getDataColDefs(gridApi.getColumnDefs())
|
|
405
|
-
.filter((def) => !def.hide)
|
|
406
|
-
.map((def) => ({
|
|
407
|
-
spec: parseJson(def.colId! satisfies string as PlTableColumnIdJson).labeled,
|
|
408
|
-
field: def.field! as `${number}`,
|
|
409
|
-
}));
|
|
410
|
-
|
|
411
|
-
const row = Array.isArray(index)
|
|
412
|
-
? gridApi.getRowNode(canonicalizeJson<PlTableRowId>(index))
|
|
413
|
-
: gridApi.getDisplayedRowAtIndex(index);
|
|
414
|
-
if (!row) return;
|
|
415
|
-
|
|
416
|
-
const rowData = row.data;
|
|
417
|
-
if (!rowData) return;
|
|
418
|
-
|
|
419
|
-
const getTableValue = (value: PTableValue | PTableHidden): PTableValue => {
|
|
420
|
-
if (isPTableHidden(value)) throw new Error('hidden value was not filtered out');
|
|
421
|
-
return value;
|
|
422
|
-
};
|
|
423
|
-
|
|
424
|
-
const spec: PTableColumnSpec[] = visibleColumns.map((column) => column.spec);
|
|
425
|
-
const data: PTableValue[] = visibleColumns.map((column) => getTableValue(rowData[column.field]));
|
|
426
|
-
return { spec, data };
|
|
427
|
-
},
|
|
428
391
|
});
|
|
429
392
|
|
|
430
393
|
function getDataColDefs(
|
|
@@ -104,7 +104,7 @@ export function usePlDataTableSettingsV2<T>(options: OptionsAdvanced<T> | Option
|
|
|
104
104
|
}
|
|
105
105
|
: () => ({});
|
|
106
106
|
return computed(() => {
|
|
107
|
-
const modelValue = toValue(options.model);
|
|
107
|
+
const modelValue = deepClone(toValue(options.model));
|
|
108
108
|
let settingsBase: PlDataTableSettingsV2Base;
|
|
109
109
|
if ('sourceId' in options) {
|
|
110
110
|
const sourceIdValue = deepClone(toValue(options.sourceId));
|
|
@@ -128,7 +128,7 @@ export function usePlDataTableSettingsV2<T>(options: OptionsAdvanced<T> | Option
|
|
|
128
128
|
}
|
|
129
129
|
} else {
|
|
130
130
|
if (options.sheets) {
|
|
131
|
-
const sheetsValue = toValue(options.sheets);
|
|
131
|
+
const sheetsValue = deepClone(toValue(options.sheets));
|
|
132
132
|
settingsBase = sheetsValue
|
|
133
133
|
? {
|
|
134
134
|
sourceId: canonicalize('static')!,
|
|
@@ -191,20 +191,6 @@ export type PlAgDataTableV2Controller = {
|
|
|
191
191
|
axesSpec: AxisId[];
|
|
192
192
|
selectedKeys: PTableKey[];
|
|
193
193
|
}) => Promise<boolean>;
|
|
194
|
-
/**
|
|
195
|
-
* Get number of rows in the table.
|
|
196
|
-
* @returns number of rows
|
|
197
|
-
*/
|
|
198
|
-
getRowCount: () => Promise<undefined | number>;
|
|
199
|
-
/**
|
|
200
|
-
* Get row by index or axes key.
|
|
201
|
-
* @param index - row index or axes key
|
|
202
|
-
* @returns row spec and data, `undefined` if row was not loaded
|
|
203
|
-
*/
|
|
204
|
-
getRow: (index: number | PlTableRowId) => Promise<undefined | {
|
|
205
|
-
spec: PTableColumnSpec[];
|
|
206
|
-
data: PTableValue[];
|
|
207
|
-
}>;
|
|
208
194
|
};
|
|
209
195
|
|
|
210
196
|
export type PlTableRowId = PTableKey;
|
|
@@ -3,10 +3,9 @@ import type { Mutable } from '@milaboratories/helpers';
|
|
|
3
3
|
import type { NavigationState, BlockOutputsBase, BlockState, PlatformaV2, ValueWithUTag, AuthorMarker } from '@platforma-sdk/model';
|
|
4
4
|
import { hasAbortError, unwrapResult } from '@platforma-sdk/model';
|
|
5
5
|
import type { Ref } from 'vue';
|
|
6
|
-
import { reactive, computed, ref } from 'vue';
|
|
6
|
+
import { reactive, computed, ref, watch } from 'vue';
|
|
7
7
|
import type { StateModelOptions, UnwrapOutputs, OutputValues, OutputErrors, AppSettings } from '../types';
|
|
8
8
|
import { createModel } from '../createModel';
|
|
9
|
-
import { createAppModel } from './createAppModel';
|
|
10
9
|
import { parseQuery } from '../urls';
|
|
11
10
|
import { MultiError, unwrapValueOrErrors } from '../utils';
|
|
12
11
|
import { applyPatch } from 'fast-json-patch';
|
|
@@ -43,11 +42,6 @@ export function createAppV2<
|
|
|
43
42
|
platforma: PlatformaV2<Args, Outputs, UiState, Href>,
|
|
44
43
|
settings: AppSettings,
|
|
45
44
|
) {
|
|
46
|
-
type AppModel = {
|
|
47
|
-
args: Args;
|
|
48
|
-
ui: UiState;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
45
|
const debug = (msg: string, ...rest: unknown[]) => {
|
|
52
46
|
if (settings.debug) {
|
|
53
47
|
console.log(`%c>>> %c${msg}`, 'color: orange; font-weight: bold', 'color: orange', settings.appId, ...rest);
|
|
@@ -113,6 +107,55 @@ export function createAppV2<
|
|
|
113
107
|
return platforma.setNavigationState(state);
|
|
114
108
|
};
|
|
115
109
|
|
|
110
|
+
const outputs = computed<OutputValues<Outputs>>(() => {
|
|
111
|
+
const entries = Object.entries(snapshot.value.outputs as Partial<Readonly<Outputs>>).map(([k, vOrErr]) => [k, vOrErr.ok && vOrErr.value !== undefined ? vOrErr.value : undefined]);
|
|
112
|
+
return Object.fromEntries(entries);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const outputErrors = computed<OutputErrors<Outputs>>(() => {
|
|
116
|
+
const entries = Object.entries(snapshot.value.outputs as Partial<Readonly<Outputs>>).map(([k, vOrErr]) => [k, vOrErr && !vOrErr.ok ? new MultiError(vOrErr.errors) : undefined]);
|
|
117
|
+
return Object.fromEntries(entries);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const appModel = reactive({
|
|
121
|
+
error: '',
|
|
122
|
+
model: {
|
|
123
|
+
args: deepClone(snapshot.value.args) as Args,
|
|
124
|
+
ui: deepClone(snapshot.value.ui) as UiState,
|
|
125
|
+
outputs,
|
|
126
|
+
outputErrors,
|
|
127
|
+
},
|
|
128
|
+
}) as {
|
|
129
|
+
error: string;
|
|
130
|
+
model: {
|
|
131
|
+
args: Args;
|
|
132
|
+
ui: UiState;
|
|
133
|
+
outputs: OutputValues<Outputs>;
|
|
134
|
+
outputErrors: OutputErrors<Outputs>;
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const appModelWatch = watch(
|
|
139
|
+
() => appModel.model,
|
|
140
|
+
(_newData) => {
|
|
141
|
+
const newData = deepClone(_newData);
|
|
142
|
+
debug('appModel.model', newData);
|
|
143
|
+
setArgsAndUiStateQueue.run(() => setBlockArgsAndUiState(newData.args, newData.ui).then(unwrapResult));
|
|
144
|
+
},
|
|
145
|
+
{ deep: true },
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
const updateAppModelSilently = (newData: {
|
|
149
|
+
args: Args;
|
|
150
|
+
ui: UiState;
|
|
151
|
+
}) => {
|
|
152
|
+
debug('updateAppModelSilently', newData);
|
|
153
|
+
appModelWatch.pause();
|
|
154
|
+
appModel.model.args = deepClone(newData.args) as Args;
|
|
155
|
+
appModel.model.ui = deepClone(newData.ui) as UiState;
|
|
156
|
+
appModelWatch.resume();
|
|
157
|
+
};
|
|
158
|
+
|
|
116
159
|
(async () => {
|
|
117
160
|
window.addEventListener('beforeunload', () => {
|
|
118
161
|
closedRef.value = true;
|
|
@@ -125,7 +168,7 @@ export function createAppV2<
|
|
|
125
168
|
try {
|
|
126
169
|
const patches = await platforma.getPatches(uTagRef.value).then(unwrapResult);
|
|
127
170
|
|
|
128
|
-
debug('patches',
|
|
171
|
+
debug('patches.length', patches.value.length);
|
|
129
172
|
debug('uTagRef.value', uTagRef.value);
|
|
130
173
|
debug('patches.uTag', patches.uTag);
|
|
131
174
|
debug('patches.author', patches.author);
|
|
@@ -144,6 +187,7 @@ export function createAppV2<
|
|
|
144
187
|
if (isAuthorChanged || data.isExternalSnapshot) {
|
|
145
188
|
debug('got external changes, applying them to the snapshot', JSON.stringify(snapshot.value, null, 2));
|
|
146
189
|
snapshot.value = applyPatch(snapshot.value, patches.value, false, false).newDocument;
|
|
190
|
+
updateAppModelSilently(snapshot.value);
|
|
147
191
|
data.isExternalSnapshot = isAuthorChanged;
|
|
148
192
|
} else {
|
|
149
193
|
// Mutable behavior
|
|
@@ -163,34 +207,6 @@ export function createAppV2<
|
|
|
163
207
|
}
|
|
164
208
|
})();
|
|
165
209
|
|
|
166
|
-
const outputs = computed<OutputValues<Outputs>>(() => {
|
|
167
|
-
const entries = Object.entries(snapshot.value.outputs as Partial<Readonly<Outputs>>).map(([k, vOrErr]) => [k, vOrErr.ok && vOrErr.value !== undefined ? vOrErr.value : undefined]);
|
|
168
|
-
return Object.fromEntries(entries);
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
const outputErrors = computed<OutputErrors<Outputs>>(() => {
|
|
172
|
-
const entries = Object.entries(snapshot.value.outputs as Partial<Readonly<Outputs>>).map(([k, vOrErr]) => [k, vOrErr && !vOrErr.ok ? new MultiError(vOrErr.errors) : undefined]);
|
|
173
|
-
return Object.fromEntries(entries);
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
const appModel = createAppModel(
|
|
177
|
-
{
|
|
178
|
-
get() {
|
|
179
|
-
return { args: snapshot.value.args, ui: snapshot.value.ui } as AppModel;
|
|
180
|
-
},
|
|
181
|
-
autoSave: true,
|
|
182
|
-
onSave(newData: AppModel) {
|
|
183
|
-
debug('onSave', newData);
|
|
184
|
-
setArgsAndUiStateQueue.run(() => setBlockArgsAndUiState(newData.args, newData.ui).then(unwrapResult));
|
|
185
|
-
},
|
|
186
|
-
},
|
|
187
|
-
{
|
|
188
|
-
outputs,
|
|
189
|
-
outputErrors,
|
|
190
|
-
},
|
|
191
|
-
settings,
|
|
192
|
-
);
|
|
193
|
-
|
|
194
210
|
const cloneArgs = () => deepClone(appModel.model.args) as Args;
|
|
195
211
|
const cloneUiState = () => deepClone(appModel.model.ui) as UiState;
|
|
196
212
|
const cloneNavigationState = () => deepClone(snapshot.value.navigationState) as Mutable<NavigationState<Href>>;
|