gbflowengine 0.0.0-stage → 0.1.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 (93) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +270 -2
  3. package/dist/gbflowengine.css +1 -0
  4. package/dist/gbflowengine.js +19682 -0
  5. package/dist/gbflowengine.js.map +1 -0
  6. package/dist/gbflowengine.umd.cjs +5 -0
  7. package/dist/gbflowengine.umd.cjs.map +1 -0
  8. package/package.json +83 -4
  9. package/src/center/DefinitionInspect.vue +291 -0
  10. package/src/center/FlowCenter.vue +33 -0
  11. package/src/center/index.js +29 -0
  12. package/src/center/storage.js +748 -0
  13. package/src/core/flow/condition.js +160 -0
  14. package/src/core/flow/customNode.js +385 -0
  15. package/src/core/flow/defaults.js +124 -0
  16. package/src/core/flow/flowConstant.js +265 -0
  17. package/src/core/flow/flowNodeUtils.js +30 -0
  18. package/src/core/flow/index.js +47 -0
  19. package/src/core/flow/node.js +44 -0
  20. package/src/core/flow/nodeFactory.js +58 -0
  21. package/src/core/flow/prediction.js +325 -0
  22. package/src/core/flow/runtime/auto.js +83 -0
  23. package/src/core/flow/runtime/decide.js +329 -0
  24. package/src/core/flow/runtime/event.js +28 -0
  25. package/src/core/flow/runtime/execute.js +357 -0
  26. package/src/core/flow/runtime/helpers.js +52 -0
  27. package/src/core/flow/runtime/query.js +146 -0
  28. package/src/core/flow/runtime/reject.js +137 -0
  29. package/src/core/flow/runtime/token.js +269 -0
  30. package/src/core/flow/runtime.js +129 -0
  31. package/src/core/flow/schema.js +864 -0
  32. package/src/definition/FlowDefinitionDesigner.vue +338 -0
  33. package/src/definition/index.js +9 -0
  34. package/src/index.js +39 -0
  35. package/src/storage.js +200 -0
  36. package/src/testing/FlowTestPanel.vue +2502 -0
  37. package/src/testing/UserConfigDrawer.vue +376 -0
  38. package/src/testing/diagram/FlowProgressDiagram.vue +318 -0
  39. package/src/testing/diagramModel.js +517 -0
  40. package/src/testing/index.js +23 -0
  41. package/src/testing/mock.js +246 -0
  42. package/src/testing/oaExecutors.js +51 -0
  43. package/src/testing/oaSeed.js +467 -0
  44. package/src/testing/sample.js +412 -0
  45. package/src/testing/seed.js +122 -0
  46. package/src/utils/format.js +27 -0
  47. package/src/visual/FlowCanvas.vue +441 -0
  48. package/src/visual/NodeProgressDetail.vue +43 -0
  49. package/src/visual/NodeStatusBadge.vue +17 -0
  50. package/src/visual/NodeWrap.vue +47 -0
  51. package/src/visual/ScWorkflow.vue +50 -0
  52. package/src/visual/SelectDialog.vue +460 -0
  53. package/src/visual/conditionHelpers.js +117 -0
  54. package/src/visual/index.js +6 -0
  55. package/src/visual/nodes/AddNode.vue +223 -0
  56. package/src/visual/nodes/ApproverNode.vue +348 -0
  57. package/src/visual/nodes/BranchNode.vue +442 -0
  58. package/src/visual/nodes/CustomNode.vue +339 -0
  59. package/src/visual/nodes/EndNode.vue +22 -0
  60. package/src/visual/nodes/PassNode.vue +101 -0
  61. package/src/visual/nodes/PromoterNode.vue +135 -0
  62. package/src/visual/nodes/RejectNode.vue +101 -0
  63. package/src/visual/nodes/SendNode.vue +152 -0
  64. package/src/visual/styles.scss +879 -0
  65. package/src/visual/useScNodeStatus.js +18 -0
  66. package/src/workbench/OaWorkbench.vue +1341 -0
  67. package/src/workbench/components/ApprovalWorkspace.vue +2090 -0
  68. package/src/workbench/components/CategoryIcon.vue +74 -0
  69. package/src/workbench/components/NotificationIcon.vue +73 -0
  70. package/src/workbench/components/ProcessSteps.vue +557 -0
  71. package/src/workbench/components/ResubmitDialog.vue +227 -0
  72. package/src/workbench/components/SidebarNav.vue +138 -0
  73. package/src/workbench/components/StartFlowDialog.vue +1058 -0
  74. package/src/workbench/components/UserAvatar.vue +68 -0
  75. package/src/workbench/custom-tasks.js +130 -0
  76. package/src/workbench/model.js +1080 -0
  77. package/src/workbench/pages/DesignPage.vue +798 -0
  78. package/src/workbench/pages/HomePage.vue +1050 -0
  79. package/src/workbench/pages/MonitorPage.vue +1029 -0
  80. package/src/workbench/pages/NotificationsPage.vue +554 -0
  81. package/src/workbench/pages/RequestsPage.vue +644 -0
  82. package/src/workbench/pages/TaskCenterPage.vue +1155 -0
  83. package/src/workbench/pages/TemplatesPage.vue +680 -0
  84. package/src/workbench/persistence.js +89 -0
  85. package/src/workbench/shared.js +18 -0
  86. package/src/workbench/styles.scss +555 -0
  87. package/src/workbench/useWorkbench.js +955 -0
  88. package/types/center.d.ts +5 -0
  89. package/types/core.d.ts +479 -0
  90. package/types/definition.d.ts +4 -0
  91. package/types/index.d.ts +34 -0
  92. package/types/testing.d.ts +20 -0
  93. package/types/visual.d.ts +20 -0
@@ -0,0 +1,338 @@
1
+ <template>
2
+ <div class="gb-flow-definition">
3
+ <template v-if="isDefinitionMode">
4
+ <el-tabs v-model="activeTab" class="gb-flow-definition__tabs">
5
+ <el-tab-pane label="基本信息" name="basic">
6
+ <div class="gb-flow-definition__basic">
7
+ <el-form :model="definition.meta" label-width="90px" style="max-width: 640px">
8
+ <el-form-item label="流程名称">
9
+ <el-input v-model="definition.meta.name" :disabled="readonly" placeholder="请输入流程名称" clearable />
10
+ </el-form-item>
11
+ <el-form-item label="流程分组">
12
+ <el-input v-model="definition.meta.groupId" :disabled="readonly" placeholder="分组标识(可选)" clearable />
13
+ </el-form-item>
14
+ <el-form-item label="流程描述">
15
+ <el-input v-model="definition.meta.description" :disabled="readonly" type="textarea" :rows="3" placeholder="请输入流程描述" />
16
+ </el-form-item>
17
+ <el-form-item label="标签">
18
+ <el-input v-model="tagsText" :disabled="readonly" placeholder="多个标签使用英文逗号分隔" clearable />
19
+ </el-form-item>
20
+ </el-form>
21
+ </div>
22
+ </el-tab-pane>
23
+
24
+ <el-tab-pane label="表单设计" name="form">
25
+ <div class="gb-flow-definition__pane">
26
+ <FormViewer
27
+ v-if="readonly"
28
+ :form-config="definition.form.components"
29
+ :show-actions="false"
30
+ readonly
31
+ />
32
+ <FormDesigner
33
+ v-else
34
+ v-model="definition.form.components"
35
+ :options="definition.form.options"
36
+ :meta="definition.meta"
37
+ :file-name="`${fileName}-form`"
38
+ />
39
+ </div>
40
+ </el-tab-pane>
41
+
42
+ <el-tab-pane label="流程设计" name="flow">
43
+ <div class="gb-flow-definition__pane">
44
+ <FlowCanvas
45
+ ref="canvasRef"
46
+ v-model="definition.workflow"
47
+ :form-variables="canvasFormVariables"
48
+ :readonly="readonly"
49
+ :meta="definition.meta"
50
+ :file-name="fileName"
51
+ :show-toolbar="showToolbar"
52
+ :export-builder="buildDefinitionPayload"
53
+ @change="emit('change', clone(definition))"
54
+ />
55
+ </div>
56
+ </el-tab-pane>
57
+ </el-tabs>
58
+ </template>
59
+
60
+ <FlowCanvas
61
+ v-else
62
+ ref="canvasRef"
63
+ v-model="workflowData"
64
+ :form-variables="props.formVariables"
65
+ :readonly="readonly"
66
+ :meta="meta"
67
+ :file-name="fileName"
68
+ :show-toolbar="showToolbar"
69
+ />
70
+ </div>
71
+ </template>
72
+
73
+ <script setup>
74
+ import { computed, onMounted, provide, ref, watch } from 'vue'
75
+ import { ElMessage } from 'element-plus'
76
+ import FlowCanvas from '../visual/FlowCanvas.vue'
77
+ import { FormDesigner, FormViewer } from 'gbformdesign'
78
+ import {
79
+ createDefinition,
80
+ downloadJSON,
81
+ enrichFormVariables,
82
+ ensureEndNode,
83
+ importDefinition,
84
+ normalizeDefinition,
85
+ normalizeForm,
86
+ validateDefinition
87
+ } from '../core/flow/index.js'
88
+
89
+ defineOptions({ name: 'GbFlowDefinitionDesigner' })
90
+
91
+ const props = defineProps({
92
+ modelValue: { type: Object, default: null },
93
+ formVariables: { type: Array, default: () => [] },
94
+ readonly: { type: Boolean, default: false },
95
+ adapters: { type: Object, default: () => ({}) },
96
+ meta: { type: Object, default: () => ({}) },
97
+ fileName: { type: String, default: 'flow' },
98
+ defaultTab: { type: String, default: 'basic' },
99
+ showToolbar: { type: Boolean, default: true },
100
+ autoEnsureEnd: { type: Boolean, default: true }
101
+ })
102
+
103
+ const emit = defineEmits(['update:modelValue', 'import', 'export', 'validate', 'change'])
104
+
105
+ const clone = (value) => JSON.parse(JSON.stringify(value))
106
+
107
+ const isDefinitionMode = computed(() => {
108
+ const value = props.modelValue
109
+ if (!value || typeof value !== 'object') return false
110
+ if (value.workflow || value.form || value.schemaVersion || value.kind) return true
111
+ return false
112
+ })
113
+
114
+ provide('gbDesignerAdapters', props.adapters)
115
+
116
+ const activeTab = ref(props.defaultTab || 'basic')
117
+ const canvasRef = ref(null)
118
+
119
+ // ==================== definition mode ====================
120
+
121
+ function normalizeDefinitionInput(value) {
122
+ if (value && (value.workflow || value.form || value.schemaVersion)) {
123
+ const next = normalizeDefinition(value)
124
+ ensureEndNode(next.workflow)
125
+ if (!next.form.options?.name) {
126
+ next.form.options = { ...(next.form.options || {}), name: next.meta?.name || '' }
127
+ }
128
+ return next
129
+ }
130
+ return normalizeDefinition(
131
+ createDefinition({
132
+ workflow: null,
133
+ form: [],
134
+ meta: {
135
+ name: props.fileName,
136
+ ...props.meta
137
+ }
138
+ })
139
+ )
140
+ }
141
+
142
+ const definition = ref(normalizeDefinitionInput(props.modelValue))
143
+ const workflowData = ref(props.modelValue && !isDefinitionMode.value ? clone(props.modelValue) : null)
144
+
145
+ let syncing = false
146
+
147
+ watch(
148
+ () => props.modelValue,
149
+ (value) => {
150
+ if (syncing || !value) return
151
+ if (!isDefinitionMode.value) {
152
+ if (JSON.stringify(value) === JSON.stringify(workflowData.value)) return
153
+ syncing = true
154
+ workflowData.value = clone(value)
155
+ requestAnimationFrame(() => {
156
+ syncing = false
157
+ })
158
+ return
159
+ }
160
+ if (JSON.stringify(value) === JSON.stringify(definition.value)) return
161
+ syncing = true
162
+ definition.value = normalizeDefinitionInput(value)
163
+ requestAnimationFrame(() => {
164
+ syncing = false
165
+ })
166
+ },
167
+ { deep: true }
168
+ )
169
+
170
+ watch(
171
+ definition,
172
+ (value) => {
173
+ if (syncing) return
174
+ if (props.autoEnsureEnd) ensureEndNode(value.workflow)
175
+ emit('update:modelValue', clone(value))
176
+ },
177
+ { deep: true }
178
+ )
179
+
180
+ watch(
181
+ workflowData,
182
+ (value) => {
183
+ if (syncing || !value) return
184
+ emit('update:modelValue', clone(value))
185
+ },
186
+ { deep: true }
187
+ )
188
+
189
+ onMounted(() => {
190
+ if (isDefinitionMode.value) {
191
+ if (props.autoEnsureEnd) ensureEndNode(definition.value.workflow)
192
+ if (!definition.value.form.options) {
193
+ definition.value.form.options = normalizeForm(definition.value.form).options
194
+ }
195
+ }
196
+ })
197
+
198
+ const tagsText = computed({
199
+ get: () => (definition.value.meta.tags || []).join(','),
200
+ set: (value) => {
201
+ definition.value.meta.tags = String(value || '')
202
+ .split(/[,,]/)
203
+ .map((tag) => tag.trim())
204
+ .filter(Boolean)
205
+ }
206
+ })
207
+
208
+ const canvasFormVariables = computed(() => {
209
+ if (props.formVariables?.length) return props.formVariables
210
+ const components = definition.value.form?.components || []
211
+ return components.length ? enrichFormVariables(components) : []
212
+ })
213
+
214
+ const buildDefinitionPayload = () => {
215
+ if (!isDefinitionMode.value) return null
216
+ return clone(definition.value)
217
+ }
218
+
219
+ // ==================== shared API ====================
220
+
221
+ const validate = () => {
222
+ const result = isDefinitionMode.value
223
+ ? validateDefinition(definition.value)
224
+ : canvasRef.value?.validate()
225
+ emit('validate', result)
226
+ return result
227
+ }
228
+
229
+ const getWorkflow = () =>
230
+ isDefinitionMode.value ? clone(definition.value.workflow) : canvasRef.value?.getWorkflow()
231
+
232
+ const getDefinition = () =>
233
+ isDefinitionMode.value
234
+ ? clone(definition.value)
235
+ : canvasRef.value?.getDefinition()
236
+
237
+ const importJSON = (input) => {
238
+ const result = importDefinition(input)
239
+ if (isDefinitionMode.value) {
240
+ definition.value = normalizeDefinitionInput(result.document)
241
+ } else {
242
+ canvasRef.value?.importJSON(input)
243
+ }
244
+ emit('import', result.document)
245
+ return result
246
+ }
247
+
248
+ const exportJSON = (space = 2) => {
249
+ if (isDefinitionMode.value) {
250
+ const payload = clone(definition.value)
251
+ emit('export', payload)
252
+ return JSON.stringify(payload, null, space)
253
+ }
254
+ return canvasRef.value?.exportJSON(space)
255
+ }
256
+
257
+ const downloadWorkflow = () => {
258
+ if (isDefinitionMode.value) {
259
+ const payload = clone(definition.value)
260
+ downloadJSON(payload, `${definition.value.meta?.name || props.fileName}.gbflow.json`)
261
+ emit('export', payload)
262
+ ElMessage.success('模板 JSON 已导出')
263
+ return
264
+ }
265
+ canvasRef.value?.downloadWorkflow()
266
+ }
267
+
268
+ const saveAsPng = () => canvasRef.value?.saveAsPng()
269
+
270
+ defineExpose({
271
+ getWorkflow,
272
+ getDefinition,
273
+ importJSON,
274
+ importDefinition,
275
+ exportJSON,
276
+ downloadWorkflow,
277
+ validate,
278
+ saveAsPng,
279
+ zoomIn: () => canvasRef.value?.zoomIn(),
280
+ zoomOut: () => canvasRef.value?.zoomOut(),
281
+ resetZoom: () => canvasRef.value?.resetZoom(),
282
+ definition
283
+ })
284
+ </script>
285
+
286
+ <style lang="scss">
287
+ .gb-flow-definition {
288
+ display: flex;
289
+ flex-direction: column;
290
+ width: 100%;
291
+ height: 100%;
292
+ min-height: 420px;
293
+ background: #efefef;
294
+ box-sizing: border-box;
295
+ overflow: hidden;
296
+
297
+ &__tabs {
298
+ flex: 1;
299
+ min-height: 0;
300
+ display: flex;
301
+ flex-direction: column;
302
+ }
303
+
304
+ &__tabs > .el-tabs__header {
305
+ margin: 0;
306
+ padding: 0 16px;
307
+ background: #fff;
308
+ border-bottom: 1px solid var(--el-border-color-light);
309
+ flex-shrink: 0;
310
+ }
311
+
312
+ &__tabs > .el-tabs__content {
313
+ flex: 1;
314
+ min-height: 0;
315
+ }
316
+
317
+ &__tabs .el-tab-pane {
318
+ height: 100%;
319
+ }
320
+
321
+ &__basic {
322
+ height: 100%;
323
+ overflow: auto;
324
+ background: #fff;
325
+ padding: 24px 32px;
326
+ }
327
+
328
+ &__pane {
329
+ height: 100%;
330
+ overflow: hidden;
331
+ }
332
+
333
+ > .gb-flow-canvas {
334
+ flex: 1;
335
+ min-height: 0;
336
+ }
337
+ }
338
+ </style>
@@ -0,0 +1,9 @@
1
+ /**
2
+ * 流程定义编排层:把「基本信息 + 表单设计 + 流程设计」组装成一个可编辑的流程定义。
3
+ *
4
+ * 职责边界:
5
+ * - 表单编辑交给 gbformdesign(FormDesigner)
6
+ * - 流程编辑使用本包 visual 层(FlowCanvas)
7
+ * - 本层只负责定义结构(meta / form / workflow)的归一化、同步与导入导出
8
+ */
9
+ export { default as FlowDefinitionDesigner } from './FlowDefinitionDesigner.vue'
package/src/index.js ADDED
@@ -0,0 +1,39 @@
1
+ import FlowTestPanel from './testing/FlowTestPanel.vue'
2
+ import UserConfigDrawer from './testing/UserConfigDrawer.vue'
3
+ import FlowProgressDiagram from './testing/diagram/FlowProgressDiagram.vue'
4
+ import FlowDefinitionDesigner from './definition/FlowDefinitionDesigner.vue'
5
+ import FlowCenter from './center/FlowCenter.vue'
6
+ import { FlowCanvas, ScWorkflow, NodeStatusBadge, NodeProgressDetail } from './visual/index.js'
7
+
8
+ export {
9
+ FlowCenter,
10
+ FlowTestPanel,
11
+ UserConfigDrawer,
12
+ FlowProgressDiagram,
13
+ FlowDefinitionDesigner,
14
+ FlowCanvas,
15
+ ScWorkflow,
16
+ NodeStatusBadge,
17
+ NodeProgressDetail
18
+ }
19
+
20
+ export * from './core/flow/index.js'
21
+ export * from './visual/index.js'
22
+ export * from './testing/mock.js'
23
+ export * from './testing/sample.js'
24
+ export * from './testing/oaSeed.js'
25
+ export * from './testing/oaExecutors.js'
26
+ export { buildDiagramModel, pruneUnselectedBranches } from './testing/diagramModel.js'
27
+
28
+ export const version = '0.1.0'
29
+
30
+ const components = [FlowCenter, FlowTestPanel, FlowProgressDiagram, FlowDefinitionDesigner, FlowCanvas, ScWorkflow]
31
+
32
+ export default {
33
+ version,
34
+ install(app) {
35
+ components.forEach((component) => {
36
+ if (component.name) app.component(component.name, component)
37
+ })
38
+ }
39
+ }
package/src/storage.js ADDED
@@ -0,0 +1,200 @@
1
+ /**
2
+ * Small async storage facade used by the flow center and the workbench.
3
+ * Prefers IndexedDB, falls back to localStorage, then to memory (SSR/tests).
4
+ * Values are JSON strings so all backends stay interchangeable.
5
+ */
6
+
7
+ const DB_NAME = 'gbflowengine'
8
+ const STORE_NAME = 'kv'
9
+ const DB_VERSION = 1
10
+ const memory = new Map()
11
+
12
+ const IDB_TIMEOUT = 1500
13
+
14
+ let dbPromise = null
15
+ let idbUnavailable = false
16
+
17
+ function hasIndexedDB() {
18
+ return !idbUnavailable && typeof indexedDB !== 'undefined' && indexedDB !== null
19
+ }
20
+
21
+ function withTimeout(promise, label) {
22
+ return new Promise((resolve, reject) => {
23
+ const timer = setTimeout(() => reject(new Error(label)), IDB_TIMEOUT)
24
+ promise.then(
25
+ (value) => {
26
+ clearTimeout(timer)
27
+ resolve(value)
28
+ },
29
+ (error) => {
30
+ clearTimeout(timer)
31
+ reject(error)
32
+ }
33
+ )
34
+ })
35
+ }
36
+
37
+ function hasLocalStorage() {
38
+ try {
39
+ return typeof localStorage !== 'undefined' && localStorage !== null
40
+ } catch {
41
+ return false
42
+ }
43
+ }
44
+
45
+ function openDatabase() {
46
+ if (!hasIndexedDB()) return Promise.reject(new Error('IndexedDB unavailable'))
47
+ if (dbPromise) return dbPromise
48
+ dbPromise = new Promise((resolve, reject) => {
49
+ const request = indexedDB.open(DB_NAME, DB_VERSION)
50
+ request.onupgradeneeded = () => {
51
+ const db = request.result
52
+ if (!db.objectStoreNames.contains(STORE_NAME)) db.createObjectStore(STORE_NAME)
53
+ }
54
+ request.onsuccess = () => resolve(request.result)
55
+ request.onerror = () => reject(request.error || new Error('IndexedDB open failed'))
56
+ request.onblocked = () => reject(new Error('IndexedDB open blocked'))
57
+ }).catch((error) => {
58
+ dbPromise = null
59
+ throw error
60
+ })
61
+ return dbPromise
62
+ }
63
+
64
+ async function idbGet(key) {
65
+ const db = await openDatabase()
66
+ return new Promise((resolve, reject) => {
67
+ const transaction = db.transaction(STORE_NAME, 'readonly')
68
+ const request = transaction.objectStore(STORE_NAME).get(key)
69
+ request.onsuccess = () => resolve(request.result)
70
+ request.onerror = () => reject(request.error)
71
+ })
72
+ }
73
+
74
+ async function idbSet(key, value) {
75
+ const db = await openDatabase()
76
+ return new Promise((resolve, reject) => {
77
+ const transaction = db.transaction(STORE_NAME, 'readwrite')
78
+ transaction.objectStore(STORE_NAME).put(value, key)
79
+ transaction.oncomplete = () => resolve()
80
+ transaction.onerror = () => reject(transaction.error)
81
+ })
82
+ }
83
+
84
+ async function idbDelete(key) {
85
+ const db = await openDatabase()
86
+ return new Promise((resolve, reject) => {
87
+ const transaction = db.transaction(STORE_NAME, 'readwrite')
88
+ transaction.objectStore(STORE_NAME).delete(key)
89
+ transaction.oncomplete = () => resolve()
90
+ transaction.onerror = () => reject(transaction.error)
91
+ })
92
+ }
93
+
94
+ function localGet(key) {
95
+ if (!hasLocalStorage()) return null
96
+ try {
97
+ return localStorage.getItem(key)
98
+ } catch {
99
+ return null
100
+ }
101
+ }
102
+
103
+ function localSet(key, value) {
104
+ if (!hasLocalStorage()) return false
105
+ try {
106
+ localStorage.setItem(key, value)
107
+ return true
108
+ } catch {
109
+ return false
110
+ }
111
+ }
112
+
113
+ function localRemove(key) {
114
+ if (!hasLocalStorage()) return
115
+ try {
116
+ localStorage.removeItem(key)
117
+ } catch {
118
+ // ignore quota/security failures
119
+ }
120
+ }
121
+
122
+ async function readRaw(key) {
123
+ if (hasIndexedDB()) {
124
+ try {
125
+ const value = await withTimeout(idbGet(key), 'IndexedDB read timeout')
126
+ if (value !== undefined && value !== null) return value
127
+ } catch {
128
+ idbUnavailable = true
129
+ // fall through to localStorage / memory
130
+ }
131
+ }
132
+ const legacy = localGet(key)
133
+ if (legacy !== null) {
134
+ // One-time migration from the previous localStorage-only version.
135
+ if (hasIndexedDB()) {
136
+ try {
137
+ await withTimeout(idbSet(key, legacy), 'IndexedDB migration timeout')
138
+ localRemove(key)
139
+ } catch {
140
+ idbUnavailable = true
141
+ // keep the localStorage copy if IndexedDB rejects the write
142
+ }
143
+ }
144
+ return legacy
145
+ }
146
+ return memory.get(key) ?? null
147
+ }
148
+
149
+ async function writeRaw(key, value) {
150
+ memory.set(key, value)
151
+ if (hasIndexedDB()) {
152
+ try {
153
+ await withTimeout(idbSet(key, value), 'IndexedDB write timeout')
154
+ } catch {
155
+ idbUnavailable = true
156
+ // localStorage below remains the fallback
157
+ }
158
+ }
159
+ // Mirrored on purpose: keeps a synchronous fallback if IndexedDB later fails.
160
+ localSet(key, value)
161
+ }
162
+
163
+ async function removeRaw(key) {
164
+ memory.delete(key)
165
+ if (hasIndexedDB()) {
166
+ try {
167
+ await withTimeout(idbDelete(key), 'IndexedDB delete timeout')
168
+ } catch {
169
+ idbUnavailable = true
170
+ // ignore and still clear the other backends
171
+ }
172
+ }
173
+ localRemove(key)
174
+ }
175
+
176
+ export const flowStorage = {
177
+ async getJSON(key, fallback = null) {
178
+ const raw = await readRaw(key)
179
+ if (raw === null || raw === undefined || raw === '') return fallback
180
+ try {
181
+ return JSON.parse(raw)
182
+ } catch {
183
+ return fallback
184
+ }
185
+ },
186
+
187
+ async setJSON(key, value) {
188
+ await writeRaw(key, JSON.stringify(value))
189
+ },
190
+
191
+ async remove(key) {
192
+ await removeRaw(key)
193
+ }
194
+ }
195
+
196
+ /** Test/DI hook: drop the cached IndexedDB connection. */
197
+ export function resetFlowStorageConnection() {
198
+ dbPromise = null
199
+ idbUnavailable = false
200
+ }