@platforma-sdk/ui-vue 1.82.6 → 1.83.1

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.
Files changed (141) hide show
  1. package/.turbo/turbo-build.log +169 -23
  2. package/.turbo/turbo-formatter$colon$check.log +2 -2
  3. package/.turbo/turbo-linter$colon$check.log +2 -2
  4. package/.turbo/turbo-types$colon$check.log +2 -2
  5. package/CHANGELOG.md +62 -0
  6. package/dist/components/PlAgDataTable/PlAgDataTableV2.vue2.js +1 -1
  7. package/dist/components/PlAgDataTable/compositions/useFilterableColumns.d.ts +16 -16
  8. package/dist/components/PlAnnotations/components/AnnotationsSidebar.js.map +1 -1
  9. package/dist/components/PlAnnotations/components/AnnotationsSidebar.style.js.map +1 -1
  10. package/dist/components/PlAnnotations/components/AnnotationsSidebar.vue2.js +1 -1
  11. package/dist/components/PlAnnotations/components/AnnotationsSidebar.vue2.js.map +1 -1
  12. package/dist/components/PlAnnotations/components/FilterSidebar.js.map +1 -1
  13. package/dist/components/PlAnnotations/components/FilterSidebar.style.js.map +1 -1
  14. package/dist/components/PlAnnotations/components/FilterSidebar.vue2.js +1 -1
  15. package/dist/components/PlAnnotations/components/FilterSidebar.vue2.js.map +1 -1
  16. package/dist/components/PlAnnotations/components/PlAnnotations.vue2.js.map +1 -1
  17. package/dist/defineApp.d.ts +2 -32
  18. package/dist/defineApp.d.ts.map +1 -1
  19. package/dist/defineApp.js +20 -62
  20. package/dist/defineApp.js.map +1 -1
  21. package/dist/index.js +39 -39
  22. package/dist/internal/createAppV3.d.ts +1 -1
  23. package/dist/internal/createAppV3.js +16 -16
  24. package/dist/lib/util/helpers/dist/utils.js +2 -5
  25. package/dist/lib/util/helpers/dist/utils.js.map +1 -1
  26. package/dist/lib.js +1 -1
  27. package/package.json +8 -8
  28. package/src/components/PlAnnotations/components/AnnotationsSidebar.vue +2 -2
  29. package/src/components/PlAnnotations/components/FilterSidebar.vue +2 -2
  30. package/src/defineApp.ts +1 -183
  31. package/dist/components/BlockLayout.vue.d.ts +0 -3
  32. package/dist/components/BlockLayout.vue.d.ts.map +0 -1
  33. package/dist/components/BlockLoader.vue.d.ts +0 -6
  34. package/dist/components/BlockLoader.vue.d.ts.map +0 -1
  35. package/dist/components/LoaderPage.vue.d.ts +0 -3
  36. package/dist/components/LoaderPage.vue.d.ts.map +0 -1
  37. package/dist/components/NotFound.vue.d.ts +0 -3
  38. package/dist/components/NotFound.vue.d.ts.map +0 -1
  39. package/dist/components/PlAdvancedFilter/FilterEditor.vue.d.ts +0 -23
  40. package/dist/components/PlAdvancedFilter/FilterEditor.vue.d.ts.map +0 -1
  41. package/dist/components/PlAdvancedFilter/OperandButton.vue.d.ts +0 -9
  42. package/dist/components/PlAdvancedFilter/OperandButton.vue.d.ts.map +0 -1
  43. package/dist/components/PlAdvancedFilter/PlAdvancedFilter.vue.d.ts +0 -55
  44. package/dist/components/PlAdvancedFilter/PlAdvancedFilter.vue.d.ts.map +0 -1
  45. package/dist/components/PlAgCellFile/PlAgCellFile.vue.d.ts +0 -26
  46. package/dist/components/PlAgCellFile/PlAgCellFile.vue.d.ts.map +0 -1
  47. package/dist/components/PlAgCellProgress/PlAgCellProgress.vue.d.ts +0 -8
  48. package/dist/components/PlAgCellProgress/PlAgCellProgress.vue.d.ts.map +0 -1
  49. package/dist/components/PlAgCellStatusTag/PlAgCellStatusTag.vue.d.ts +0 -10
  50. package/dist/components/PlAgCellStatusTag/PlAgCellStatusTag.vue.d.ts.map +0 -1
  51. package/dist/components/PlAgChartHistogramCell/PlAgChartHistogramCell.vue.d.ts +0 -11
  52. package/dist/components/PlAgChartHistogramCell/PlAgChartHistogramCell.vue.d.ts.map +0 -1
  53. package/dist/components/PlAgChartStackedBarCell/PlAgChartStackedBarCell.vue.d.ts +0 -8
  54. package/dist/components/PlAgChartStackedBarCell/PlAgChartStackedBarCell.vue.d.ts.map +0 -1
  55. package/dist/components/PlAgColumnHeader/PlAgColumnHeader.vue.d.ts +0 -10
  56. package/dist/components/PlAgColumnHeader/PlAgColumnHeader.vue.d.ts.map +0 -1
  57. package/dist/components/PlAgCsvExporter/PlAgCsvExporter.vue.d.ts +0 -10
  58. package/dist/components/PlAgCsvExporter/PlAgCsvExporter.vue.d.ts.map +0 -1
  59. package/dist/components/PlAgDataTable/PlAgDataTableSheets.vue.d.ts +0 -31
  60. package/dist/components/PlAgDataTable/PlAgDataTableSheets.vue.d.ts.map +0 -1
  61. package/dist/components/PlAgDataTable/PlAgDataTableV2.vue.d.ts +0 -94
  62. package/dist/components/PlAgDataTable/PlAgDataTableV2.vue.d.ts.map +0 -1
  63. package/dist/components/PlAgDataTable/PlAgOverlayLoading.vue.d.ts +0 -10
  64. package/dist/components/PlAgDataTable/PlAgOverlayLoading.vue.d.ts.map +0 -1
  65. package/dist/components/PlAgDataTable/PlAgOverlayNoRows.vue.d.ts +0 -9
  66. package/dist/components/PlAgDataTable/PlAgOverlayNoRows.vue.d.ts.map +0 -1
  67. package/dist/components/PlAgDataTable/PlAgRowCount.vue.d.ts +0 -9
  68. package/dist/components/PlAgDataTable/PlAgRowCount.vue.d.ts.map +0 -1
  69. package/dist/components/PlAgGridColumnManager/PlAgGridColumnManager.vue.d.ts +0 -17
  70. package/dist/components/PlAgGridColumnManager/PlAgGridColumnManager.vue.d.ts.map +0 -1
  71. package/dist/components/PlAgRowNumCheckbox/PlAgRowNumCheckbox.vue.d.ts +0 -7
  72. package/dist/components/PlAgRowNumCheckbox/PlAgRowNumCheckbox.vue.d.ts.map +0 -1
  73. package/dist/components/PlAgRowNumHeader.vue.d.ts +0 -7
  74. package/dist/components/PlAgRowNumHeader.vue.d.ts.map +0 -1
  75. package/dist/components/PlAgTextAndButtonCell/PlAgTextAndButtonCell.vue.d.ts +0 -32
  76. package/dist/components/PlAgTextAndButtonCell/PlAgTextAndButtonCell.vue.d.ts.map +0 -1
  77. package/dist/components/PlAnnotations/components/AnnotationsSidebar.vue.d.ts +0 -14
  78. package/dist/components/PlAnnotations/components/AnnotationsSidebar.vue.d.ts.map +0 -1
  79. package/dist/components/PlAnnotations/components/FilterSidebar.vue.d.ts +0 -26
  80. package/dist/components/PlAnnotations/components/FilterSidebar.vue.d.ts.map +0 -1
  81. package/dist/components/PlAnnotations/components/PlAnnotations.vue.d.ts +0 -10
  82. package/dist/components/PlAnnotations/components/PlAnnotations.vue.d.ts.map +0 -1
  83. package/dist/components/PlAnnotations/components/PlAnnotationsModal.vue.d.ts +0 -8
  84. package/dist/components/PlAnnotations/components/PlAnnotationsModal.vue.d.ts.map +0 -1
  85. package/dist/components/PlAppErrorNotificationAlert/PlAppErrorNotificationAlert.vue.d.ts +0 -11
  86. package/dist/components/PlAppErrorNotificationAlert/PlAppErrorNotificationAlert.vue.d.ts.map +0 -1
  87. package/dist/components/PlBtnExportArchive/Item.vue.d.ts +0 -7
  88. package/dist/components/PlBtnExportArchive/Item.vue.d.ts.map +0 -1
  89. package/dist/components/PlBtnExportArchive/PlBtnExportArchive.vue.d.ts +0 -35
  90. package/dist/components/PlBtnExportArchive/PlBtnExportArchive.vue.d.ts.map +0 -1
  91. package/dist/components/PlBtnExportArchive/Summary.vue.d.ts +0 -11
  92. package/dist/components/PlBtnExportArchive/Summary.vue.d.ts.map +0 -1
  93. package/dist/components/PlDatasetSelector/PlDatasetSelector.vue.d.ts +0 -77
  94. package/dist/components/PlDatasetSelector/PlDatasetSelector.vue.d.ts.map +0 -1
  95. package/dist/components/PlTableFastSearch/PlTableFastSearch.vue.d.ts +0 -10
  96. package/dist/components/PlTableFastSearch/PlTableFastSearch.vue.d.ts.map +0 -1
  97. package/dist/components/PlTableFilters/PlTableFiltersV2.vue.d.ts +0 -14
  98. package/dist/components/PlTableFilters/PlTableFiltersV2.vue.d.ts.map +0 -1
  99. package/dist/internal/createAppModel.d.ts +0 -7
  100. package/dist/internal/createAppModel.d.ts.map +0 -1
  101. package/dist/internal/createAppModel.js +0 -57
  102. package/dist/internal/createAppModel.js.map +0 -1
  103. package/dist/internal/createAppV1.d.ts +0 -53
  104. package/dist/internal/createAppV1.d.ts.map +0 -1
  105. package/dist/internal/createAppV1.js +0 -129
  106. package/dist/internal/createAppV1.js.map +0 -1
  107. package/dist/internal/createAppV2.d.ts +0 -50
  108. package/dist/internal/createAppV2.d.ts.map +0 -1
  109. package/dist/internal/createAppV2.js +0 -137
  110. package/dist/internal/createAppV2.js.map +0 -1
  111. package/dist/internal/createAppV2.test.d.ts +0 -15
  112. package/dist/internal/createAppV2.test.d.ts.map +0 -1
  113. package/dist/internal/test-helpers/BlockMock.d.ts +0 -29
  114. package/dist/internal/test-helpers/BlockMock.d.ts.map +0 -1
  115. package/dist/internal/test-helpers/createMockApi.d.ts +0 -4
  116. package/dist/internal/test-helpers/createMockApi.d.ts.map +0 -1
  117. package/dist/internal/test-helpers/utils.d.ts +0 -4
  118. package/dist/internal/test-helpers/utils.d.ts.map +0 -1
  119. package/dist/internal/v1.static-test.d.ts +0 -7
  120. package/dist/internal/v1.static-test.d.ts.map +0 -1
  121. package/dist/internal/v2.static-test.d.ts +0 -7
  122. package/dist/internal/v2.static-test.d.ts.map +0 -1
  123. package/dist/plugins/Monetization/EndOfPeriod.vue.d.ts +0 -6
  124. package/dist/plugins/Monetization/EndOfPeriod.vue.d.ts.map +0 -1
  125. package/dist/plugins/Monetization/LimitCard.vue.d.ts +0 -11
  126. package/dist/plugins/Monetization/LimitCard.vue.d.ts.map +0 -1
  127. package/dist/plugins/Monetization/MonetizationSidebar.vue.d.ts +0 -3
  128. package/dist/plugins/Monetization/MonetizationSidebar.vue.d.ts.map +0 -1
  129. package/dist/plugins/Monetization/RunStatus.vue.d.ts +0 -23
  130. package/dist/plugins/Monetization/RunStatus.vue.d.ts.map +0 -1
  131. package/dist/plugins/Monetization/UserCabinetCard.vue.d.ts +0 -7
  132. package/dist/plugins/Monetization/UserCabinetCard.vue.d.ts.map +0 -1
  133. package/src/internal/createAppModel.ts +0 -112
  134. package/src/internal/createAppV1.ts +0 -324
  135. package/src/internal/createAppV2.test.ts +0 -207
  136. package/src/internal/createAppV2.ts +0 -392
  137. package/src/internal/test-helpers/BlockMock.ts +0 -161
  138. package/src/internal/test-helpers/createMockApi.ts +0 -104
  139. package/src/internal/test-helpers/utils.ts +0 -65
  140. package/src/internal/v1.static-test.ts +0 -107
  141. package/src/internal/v2.static-test.ts +0 -107
@@ -1,207 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach } from "vitest";
2
- import { createAppV2 } from "./createAppV2";
3
- import { type OutputWithStatus, unwrapResult } from "@platforma-sdk/model";
4
- import { BlockStateMock } from "./test-helpers/BlockMock";
5
- import { BlockMock } from "./test-helpers/BlockMock";
6
- import { delay } from "@milaboratories/helpers";
7
- import { createMockApi } from "./test-helpers/createMockApi";
8
- import { watch } from "vue";
9
- import { patchPoolingDelay } from "./createAppV2";
10
-
11
- type Args = {
12
- x: number;
13
- y: number;
14
- };
15
-
16
- type UiState = {
17
- label: string;
18
- delay?: number;
19
- };
20
-
21
- const defaultArgs = () => ({
22
- x: 0,
23
- y: 0,
24
- });
25
-
26
- type Outputs = {
27
- sum: OutputWithStatus<number>;
28
- };
29
-
30
- const defaultOutputs = (): Outputs => {
31
- return {
32
- sum: {
33
- ok: true,
34
- value: 0,
35
- stable: true,
36
- },
37
- };
38
- };
39
-
40
- const defaultState = (): BlockStateMock<Args, Outputs, UiState, `/${string}`> => {
41
- return new BlockStateMock(defaultArgs(), defaultOutputs(), { label: "" }, "/");
42
- };
43
-
44
- class BlockSum extends BlockMock<Args, Outputs, UiState, `/${string}`> {
45
- async process(): Promise<void> {
46
- const { args, author } = this.state;
47
- this.state.setState({
48
- author,
49
- outputs: { sum: { ok: true, value: args.x + args.y, stable: true } },
50
- });
51
- }
52
- }
53
-
54
- export const platforma = createMockApi<Args, Outputs, UiState>(new BlockSum(defaultState()));
55
-
56
- describe("createApp", { timeout: 20_000 }, () => {
57
- beforeEach(() => {
58
- // Mock window.addEventListener to prevent actual event listeners
59
- vi.stubGlobal("window", {
60
- addEventListener: vi.fn(),
61
- });
62
- });
63
-
64
- it("should create an app with reactive snapshot", async () => {
65
- const initialState = await platforma.loadBlockState().then(unwrapResult);
66
-
67
- const app = createAppV2(
68
- initialState,
69
- {
70
- ...platforma,
71
- blockModelInfo: {
72
- outputs: {
73
- sum: { withStatus: false },
74
- },
75
- pluginIds: [],
76
- featureFlags: {},
77
- pluginPublicOutputs: {},
78
- },
79
- },
80
- { debug: true, debounceSpan: 10 },
81
- );
82
-
83
- expect(app.model.args).toEqual({ x: 0, y: 0 });
84
- expect(app.model.ui).toEqual({ label: "" });
85
- expect(app.snapshot.navigationState.href).toBe("/");
86
-
87
- let watchCountShallow = 0;
88
-
89
- watch(
90
- () => app.model.args,
91
- () => {
92
- watchCountShallow++;
93
- },
94
- );
95
-
96
- app.model.args.x = 1;
97
- app.model.args.y = 2;
98
-
99
- const t1 = performance.now();
100
- await app.allSettled();
101
- await delay(100);
102
- const t2 = performance.now();
103
- console.log("allSettled", t2 - t1);
104
-
105
- expect(app.model.args).toEqual(app.snapshot.args);
106
- expect(app.model.args).toEqual({ x: 1, y: 2 });
107
- expect(app.model.outputs.sum).toEqual(3);
108
-
109
- app.model.args.x = 3;
110
- app.model.args.y = 3;
111
-
112
- const t3 = performance.now();
113
- await app.allSettled();
114
- await delay(patchPoolingDelay + 10);
115
- const t4 = performance.now();
116
- console.log("allSettled", t4 - t3);
117
-
118
- expect(watchCountShallow).toBe(0); // no changes
119
-
120
- expect(app.model.args).toEqual(app.snapshot.args);
121
- expect(app.model.args).toEqual({ x: 3, y: 3 });
122
- expect(app.model.outputs.sum).toEqual(6);
123
-
124
- app.closedRef = true;
125
- });
126
-
127
- it("states should be synchronized", async () => {
128
- const sharedBlock = new BlockSum(defaultState());
129
- const platforma1 = createMockApi<Args, Outputs, UiState>(sharedBlock);
130
- const platforma2 = createMockApi<Args, Outputs, UiState>(sharedBlock);
131
-
132
- const initialState1 = await platforma1.loadBlockState().then(unwrapResult);
133
- const initialState2 = await platforma2.loadBlockState().then(unwrapResult);
134
-
135
- const app1 = createAppV2(
136
- initialState1,
137
- {
138
- ...platforma1,
139
- blockModelInfo: {
140
- outputs: {
141
- sum: { withStatus: false },
142
- },
143
- pluginIds: [],
144
- featureFlags: {},
145
- pluginPublicOutputs: {},
146
- },
147
- },
148
- { appId: "app1", debug: true, debounceSpan: 10 },
149
- );
150
- const app2 = createAppV2(
151
- initialState2,
152
- {
153
- ...platforma2,
154
- blockModelInfo: {
155
- outputs: {
156
- sum: { withStatus: false },
157
- },
158
- pluginIds: [],
159
- featureFlags: {},
160
- pluginPublicOutputs: {},
161
- },
162
- },
163
- { appId: "app2", debug: true, debounceSpan: 10 },
164
- );
165
-
166
- app1.model.args.x = 1;
167
- app1.model.args.y = 2;
168
-
169
- await app1.allSettled();
170
- await app2.allSettled();
171
-
172
- expect(app1.model.args).toEqual(app2.model.args);
173
- expect(app1.model.args).toEqual({ x: 1, y: 2 });
174
- expect(app1.model.outputs.sum).toEqual(3);
175
- expect(app2.model.outputs.sum).toEqual(3);
176
-
177
- app2.model.args.x = 3;
178
- app2.model.args.y = 4;
179
-
180
- await Promise.all([app1.allSettled(), app2.allSettled()]);
181
- await delay(patchPoolingDelay + 10);
182
-
183
- expect(app1.model.args).toEqual(app2.model.args);
184
- expect(app1.model.args).toEqual({ x: 3, y: 4 });
185
- expect(app1.model.outputs.sum).toEqual(7);
186
- expect(app2.model.outputs.sum).toEqual(7);
187
-
188
- app1.model.args.x = 5;
189
- app1.model.args.y = 5;
190
- app1.model.ui.delay = 20;
191
- await delay(5);
192
- app2.model.args.x = 7;
193
- app2.model.args.y = 7;
194
-
195
- await Promise.all([app1.allSettled(), app2.allSettled()]);
196
- await delay(patchPoolingDelay + 10);
197
-
198
- expect(app1.model.args).toEqual(app1.snapshot.args);
199
- expect(app1.model.args).toEqual(app2.model.args);
200
- expect(app1.model.args).toEqual({ x: 5, y: 5 });
201
- expect(app1.model.outputs.sum).toEqual(10);
202
- expect(app2.model.outputs.sum).toEqual(10);
203
-
204
- app1.closedRef = true;
205
- app2.closedRef = true;
206
- });
207
- });
@@ -1,392 +0,0 @@
1
- import { deepClone, delay, uniqueId } from "@milaboratories/helpers";
2
- import type { Mutable } from "@milaboratories/helpers";
3
- import type {
4
- NavigationState,
5
- BlockOutputsBase,
6
- BlockState,
7
- PlatformaV2,
8
- ValueWithUTag,
9
- AuthorMarker,
10
- PlatformaExtended,
11
- } from "@platforma-sdk/model";
12
- import { hasAbortError, unwrapResult } from "@platforma-sdk/model";
13
- import type { Ref } from "vue";
14
- import { reactive, computed, ref } from "vue";
15
- import type {
16
- StateModelOptions,
17
- UnwrapOutputs,
18
- OutputValues,
19
- OutputErrors,
20
- AppSettings,
21
- } from "../types";
22
- import { createModel } from "../createModel";
23
- import { parseQuery } from "../urls";
24
- import { ensureOutputHasStableFlag, MultiError, unwrapOutput } from "../utils";
25
- import { applyPatch } from "fast-json-patch";
26
- import { UpdateSerializer } from "./UpdateSerializer";
27
- import { watchIgnorable } from "@vueuse/core";
28
-
29
- export const patchPoolingDelay = 150;
30
-
31
- export const createNextAuthorMarker = (marker: AuthorMarker | undefined): AuthorMarker => ({
32
- authorId: marker?.authorId ?? uniqueId(),
33
- localVersion: (marker?.localVersion ?? 0) + 1,
34
- });
35
-
36
- const stringifyForDebug = (v: unknown) => {
37
- try {
38
- return JSON.stringify(v, null, 2);
39
- } catch (err) {
40
- return err instanceof Error ? err.message : String(err);
41
- }
42
- };
43
-
44
- /**
45
- * Creates an application instance with reactive state management, outputs, and methods for state updates and navigation.
46
- *
47
- * @template Args - The type of arguments used in the application.
48
- * @template Outputs - The type of block outputs extending `BlockOutputsBase`.
49
- * @template UiState - The type of the UI state.
50
- * @template Href - The type of navigation href, defaulting to a string starting with `/`.
51
- *
52
- * @param state - Initial state of the application, including args, outputs, UI state, and navigation state.
53
- * @param platforma - A platform interface for interacting with block states.
54
- * @param settings - Application settings, such as debug flags.
55
- *
56
- * @returns A reactive application object with methods, getters, and state.
57
- */
58
- export function createAppV2<
59
- Args = unknown,
60
- Outputs extends BlockOutputsBase = BlockOutputsBase,
61
- UiState = unknown,
62
- Href extends `/${string}` = `/${string}`,
63
- >(
64
- state: ValueWithUTag<BlockState<Args, Outputs, UiState, Href>>,
65
- platforma: PlatformaExtended<PlatformaV2<Args, Outputs, UiState, Href>>,
66
- settings: AppSettings,
67
- ) {
68
- const debug = (msg: string, ...rest: unknown[]) => {
69
- if (settings.debug) {
70
- console.log(
71
- `%c>>> %c${msg}`,
72
- "color: orange; font-weight: bold",
73
- "color: orange",
74
- ...rest.map((r) => stringifyForDebug(r)),
75
- );
76
- }
77
- };
78
-
79
- const error = (msg: string, ...rest: unknown[]) => {
80
- console.error(
81
- `%c>>> %c${msg}`,
82
- "color: red; font-weight: bold",
83
- "color: red",
84
- ...rest.map((r) => stringifyForDebug(r)),
85
- );
86
- };
87
-
88
- const data = {
89
- isExternalSnapshot: false,
90
- author: {
91
- authorId: uniqueId(),
92
- localVersion: 0,
93
- },
94
- };
95
-
96
- const nextAuthorMarker = () => {
97
- data.author = createNextAuthorMarker(data.author);
98
- debug("nextAuthorMarker", data.author);
99
- return data.author;
100
- };
101
-
102
- const closedRef = ref(false);
103
-
104
- const uTagRef = ref(state.uTag);
105
-
106
- const debounceSpan = settings.debounceSpan ?? 200;
107
-
108
- const setArgsQueue = new UpdateSerializer({ debounceSpan });
109
- const setUiStateQueue = new UpdateSerializer({ debounceSpan });
110
- const setArgsAndUiStateQueue = new UpdateSerializer({ debounceSpan });
111
- const setNavigationStateQueue = new UpdateSerializer({ debounceSpan });
112
-
113
- /**
114
- * Reactive snapshot of the application state, including args, outputs, UI state, and navigation state.
115
- */
116
- const snapshot = ref<{
117
- args: Args;
118
- outputs: Partial<Outputs>;
119
- ui: UiState;
120
- navigationState: NavigationState<Href>;
121
- }>(state.value) as Ref<{
122
- args: Args;
123
- outputs: Partial<Outputs>;
124
- ui: UiState;
125
- navigationState: NavigationState<Href>;
126
- }>;
127
-
128
- const setBlockArgs = async (args: Args) => {
129
- return platforma.setBlockArgs(args, nextAuthorMarker());
130
- };
131
-
132
- const setBlockUiState = async (ui: UiState) => {
133
- return platforma.setBlockUiState(ui, nextAuthorMarker());
134
- };
135
-
136
- const setBlockArgsAndUiState = async (args: Args, ui: UiState) => {
137
- return platforma.setBlockArgsAndUiState(args, ui, nextAuthorMarker());
138
- };
139
-
140
- const setNavigationState = async (state: NavigationState<Href>) => {
141
- return platforma.setNavigationState(state);
142
- };
143
-
144
- const outputs = computed<OutputValues<Outputs>>(() => {
145
- const entries = Object.entries(snapshot.value.outputs as Partial<Readonly<Outputs>>).map(
146
- ([k, outputWithStatus]) =>
147
- platforma.blockModelInfo.outputs[k].withStatus
148
- ? [k, ensureOutputHasStableFlag(outputWithStatus)]
149
- : [
150
- k,
151
- outputWithStatus.ok && outputWithStatus.value !== undefined
152
- ? outputWithStatus.value
153
- : undefined,
154
- ],
155
- );
156
- return Object.fromEntries(entries);
157
- });
158
-
159
- const outputErrors = computed<OutputErrors<Outputs>>(() => {
160
- const entries = Object.entries(snapshot.value.outputs as Partial<Readonly<Outputs>>).map(
161
- ([k, vOrErr]) => [
162
- k,
163
- vOrErr && vOrErr.ok === false ? new MultiError(vOrErr.errors) : undefined,
164
- ],
165
- );
166
- return Object.fromEntries(entries);
167
- });
168
-
169
- const appModel = reactive({
170
- error: "",
171
- model: {
172
- args: deepClone(snapshot.value.args) as Args,
173
- ui: deepClone(snapshot.value.ui) as UiState,
174
- outputs,
175
- outputErrors,
176
- },
177
- }) as {
178
- error: string;
179
- model: {
180
- args: Args;
181
- ui: UiState;
182
- outputs: OutputValues<Outputs>;
183
- outputErrors: OutputErrors<Outputs>;
184
- };
185
- };
186
-
187
- const { ignoreUpdates } = watchIgnorable(
188
- () => appModel.model,
189
- (_newData) => {
190
- const newData = deepClone(_newData);
191
- debug("setArgsAndUiStateQueue appModel.model, args", newData.args, "ui", newData.ui);
192
- setArgsAndUiStateQueue.run(() =>
193
- setBlockArgsAndUiState(newData.args, newData.ui).then(unwrapResult),
194
- );
195
- },
196
- { deep: true },
197
- );
198
-
199
- const updateAppModel = (newData: { args: Args; ui: UiState }) => {
200
- debug("updateAppModel", newData);
201
- appModel.model.args = deepClone(newData.args) as Args;
202
- appModel.model.ui = deepClone(newData.ui) as UiState;
203
- };
204
-
205
- (async () => {
206
- window.addEventListener("beforeunload", () => {
207
- closedRef.value = true;
208
- platforma
209
- .dispose()
210
- .then(unwrapResult)
211
- .catch((err) => {
212
- error("error in dispose", err);
213
- });
214
- });
215
-
216
- while (!closedRef.value) {
217
- try {
218
- const patches = await platforma.getPatches(uTagRef.value).then(unwrapResult);
219
-
220
- debug("patches.length", patches.value.length);
221
- debug("uTagRef.value", uTagRef.value);
222
- debug("patches.uTag", patches.uTag);
223
- debug("patches.author", patches.author);
224
- debug("data.author", data.author);
225
-
226
- uTagRef.value = patches.uTag;
227
-
228
- if (patches.value.length === 0) {
229
- await new Promise((resolve) => setTimeout(resolve, patchPoolingDelay));
230
- continue;
231
- }
232
-
233
- const isAuthorChanged = data.author?.authorId !== patches.author?.authorId;
234
-
235
- // Immutable behavior, apply external changes to the snapshot
236
- if (isAuthorChanged || data.isExternalSnapshot) {
237
- debug("got external changes, applying them to the snapshot", patches.value);
238
- ignoreUpdates(() => {
239
- snapshot.value = applyPatch(snapshot.value, patches.value, false, false).newDocument;
240
- updateAppModel({ args: snapshot.value.args, ui: snapshot.value.ui });
241
- data.isExternalSnapshot = isAuthorChanged;
242
- });
243
- } else {
244
- // Mutable behavior
245
- debug("outputs changed", patches.value);
246
- ignoreUpdates(() => {
247
- snapshot.value = applyPatch(snapshot.value, patches.value).newDocument;
248
- });
249
- }
250
-
251
- await new Promise((resolve) => setTimeout(resolve, patchPoolingDelay));
252
- } catch (err) {
253
- if (hasAbortError(err)) {
254
- debug("patches loop aborted");
255
- closedRef.value = true;
256
- } else {
257
- error("error in patches loop", err);
258
- await new Promise((resolve) => setTimeout(resolve, 1000));
259
- }
260
- }
261
- }
262
- })();
263
-
264
- const cloneArgs = () => deepClone(appModel.model.args) as Args;
265
- const cloneUiState = () => deepClone(appModel.model.ui) as UiState;
266
- const cloneNavigationState = () =>
267
- deepClone(snapshot.value.navigationState) as Mutable<NavigationState<Href>>;
268
-
269
- const methods = {
270
- cloneArgs,
271
- cloneUiState,
272
- cloneNavigationState,
273
- createArgsModel<T extends Args = Args>(options: StateModelOptions<Args, T> = {}) {
274
- return createModel<T, Args>({
275
- get() {
276
- if (options.transform) {
277
- return options.transform(snapshot.value.args as Args);
278
- }
279
-
280
- return snapshot.value.args as T;
281
- },
282
- validate: options.validate,
283
- autoSave: true,
284
- onSave(newArgs) {
285
- setArgsQueue.run(() => setBlockArgs(newArgs).then(unwrapResult));
286
- },
287
- });
288
- },
289
- /**
290
- * defaultUiState is temporarily here, remove it after implementing initialUiState
291
- */
292
- createUiModel<T extends UiState = UiState>(
293
- options: StateModelOptions<UiState, T> = {},
294
- defaultUiState: () => UiState,
295
- ) {
296
- return createModel<T, UiState>({
297
- get() {
298
- if (options.transform) {
299
- return options.transform(snapshot.value.ui as UiState);
300
- }
301
-
302
- return (snapshot.value.ui ?? defaultUiState()) as T;
303
- },
304
- validate: options.validate,
305
- autoSave: true,
306
- onSave(newData) {
307
- setUiStateQueue.run(() => setBlockUiState(newData).then(unwrapResult));
308
- },
309
- });
310
- },
311
- /**
312
- * Retrieves the unwrapped values of outputs for the given keys.
313
- *
314
- * @template K - Keys of the outputs to unwrap.
315
- * @param keys - List of output names.
316
- * @throws Error if the outputs contain errors.
317
- * @returns An object with unwrapped output values.
318
- */
319
- unwrapOutputs<K extends keyof Outputs>(...keys: K[]): UnwrapOutputs<Outputs, K> {
320
- const outputs = snapshot.value.outputs as Partial<Readonly<Outputs>>;
321
- const entries = keys.map((key) => [key, unwrapOutput(outputs[key])]);
322
- return Object.fromEntries(entries);
323
- },
324
- /**
325
- * Updates the arguments state by applying a callback.
326
- *
327
- * @param cb - Callback to modify the current arguments.
328
- * @returns A promise resolving after the update is applied.
329
- */
330
- updateArgs(cb: (args: Args) => void): Promise<boolean> {
331
- const newArgs = cloneArgs();
332
- cb(newArgs);
333
- debug("updateArgs", newArgs);
334
- appModel.model.args = newArgs;
335
- return setArgsQueue.run(() => setBlockArgs(newArgs).then(unwrapResult));
336
- },
337
- /**
338
- * Updates the UI state by applying a callback.
339
- *
340
- * @param cb - Callback to modify the current UI state.
341
- * @returns A promise resolving after the update is applied.
342
- * @todo Make it mutable since there is already an initial one
343
- */
344
- updateUiState(cb: (args: UiState) => UiState): Promise<boolean> {
345
- const newUiState = cb(cloneUiState());
346
- debug("updateUiState", newUiState);
347
- appModel.model.ui = newUiState;
348
- return setUiStateQueue.run(() => setBlockUiState(newUiState).then(unwrapResult));
349
- },
350
- /**
351
- * Navigates to a specific href by updating the navigation state.
352
- *
353
- * @param href - The target href to navigate to.
354
- * @returns A promise resolving after the navigation state is updated.
355
- */
356
- navigateTo(href: Href) {
357
- const newState = cloneNavigationState();
358
- newState.href = href;
359
- return setNavigationStateQueue.run(() => setNavigationState(newState).then(unwrapResult));
360
- },
361
- async allSettled() {
362
- await delay(0);
363
- return setArgsAndUiStateQueue.allSettled();
364
- },
365
- };
366
-
367
- const getters = {
368
- closedRef,
369
- snapshot,
370
- queryParams: computed(() => parseQuery<Href>(snapshot.value.navigationState.href as Href)),
371
- href: computed(() => snapshot.value.navigationState.href),
372
- hasErrors: computed(() =>
373
- Object.values(snapshot.value.outputs as Partial<Readonly<Outputs>>).some((v) => !v?.ok),
374
- ),
375
- };
376
-
377
- const app = reactive(Object.assign(appModel, methods, getters));
378
-
379
- if (settings.debug) {
380
- // @ts-expect-error (to inspect in console in debug mode)
381
- globalThis.__block_app__ = app;
382
- }
383
-
384
- return app;
385
- }
386
-
387
- export type BaseAppV2<
388
- Args = unknown,
389
- Outputs extends BlockOutputsBase = BlockOutputsBase,
390
- UiState = unknown,
391
- Href extends `/${string}` = `/${string}`,
392
- > = ReturnType<typeof createAppV2<Args, Outputs, UiState, Href>>;