soma-core 1.3.5 → 1.4.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.
- package/README.md +20 -7
- package/app/SomaPageClient.tsx +4 -4
- package/app/components/SomaHostPage.tsx +5 -5
- package/app/components/SomaHydrationShell.tsx +4 -7
- package/app/globals.css +126 -3
- package/core/build/{v2 → blocks}/block-compile.ts +47 -18
- package/core/build/{v2/soma-file-loader.ts → blocks/block-container.ts} +28 -37
- package/core/build/{v2 → blocks}/block-loader.ts +1 -1
- package/core/build/{v2 → blocks}/html-index.ts +1 -1
- package/core/build/{v2/migrate-v1.ts → blocks/migrate-flat.ts} +27 -27
- package/core/build/compiler/host-config-sync.cjs +2 -9
- package/core/build/compiler/page-declaration-compiler.ts +32 -4
- package/core/build/compiler/soma-manifest-webpack-plugin.js +1 -1
- package/core/build/compiler/soma-precompile.js +2057 -35
- package/core/build/compiler/soma-precompile.ts +2 -2
- package/core/build/component/component-graph.ts +208 -0
- package/core/build/eject/component-eject.ts +473 -0
- package/core/build/eject/component-upgrade.ts +359 -0
- package/core/build/layout/constraint-solver.ts +235 -0
- package/core/build/layout/css-emitter.ts +116 -0
- package/core/build/layout/page-layout.ts +85 -0
- package/core/build/pack/pack-init.ts +265 -0
- package/core/client/base/SomaPageRootBase.ts +61 -0
- package/core/client/component/ai-catalog.ts +83 -8
- package/core/client/component/component-defaults.ts +100 -0
- package/core/client/component/component-definition.types.ts +39 -0
- package/core/client/component/component-manager.ts +79 -4
- package/core/client/component/component-pack.ts +126 -2
- package/core/client/component/contract-compat.ts +32 -3
- package/core/client/component/{example-v2.ts → example-blocks.ts} +19 -19
- package/core/client/component/keyboard-matrix.ts +144 -0
- package/core/client/component/pack-styles.ts +59 -1
- package/core/client/component/style-features.ts +131 -0
- package/core/client/component/theme-component-tokens.ts +83 -0
- package/core/client/data/data-object-resolver.ts +407 -4
- package/core/client/data/reaction-engine.ts +176 -0
- package/core/client/data/window-engine.ts +477 -0
- package/core/client/engine/event-engine.ts +98 -0
- package/core/client/engine/soma-bootstrap.ts +286 -7
- package/core/client/engine/step-runner.ts +134 -0
- package/core/client/engine/style-binder.ts +1 -1
- package/core/client/infra/notify/DomNotifyService.ts +2 -1
- package/core/client/infra/notify/INotifyTheme.ts +1 -1
- package/core/client/parser/{v2 → blocks}/namespace.ts +1 -1
- package/core/client/parser/{v2 → blocks}/page-parser.ts +30 -16
- package/core/client/parser/{v2 → blocks}/use-parser.ts +24 -25
- package/core/client/parser/data-config-parser.ts +316 -8
- package/core/client/parser/duration.ts +16 -0
- package/core/client/parser/layout-parser.ts +157 -0
- package/core/client/parser/page-state-parser.ts +166 -0
- package/core/client/parser/pipeline-parser.ts +76 -1
- package/core/client/parser/reaction-parser.ts +459 -0
- package/core/client/parser/reaction-sugar.ts +47 -0
- package/core/client/parser/step-parser.ts +359 -0
- package/core/client/parser/style-config-parser.ts +7 -7
- package/core/client/parser/ui-config-parser.ts +59 -0
- package/core/client/parser/value-lang.ts +1 -1
- package/core/client/parser/window-parser.ts +294 -0
- package/core/client/registry/index.ts +8 -0
- package/core/client/theme/theme-engine.ts +144 -16
- package/core/client/typography/typography-base.ts +1 -1
- package/core/index.ts +188 -13
- package/core/server/host/csp-nonce-metadata.ts +1 -1
- package/core/server/host/published-page-assets.ts +3 -8
- package/core/server/host/soma-page-compile.ts +1 -1
- package/core/server/index.ts +2 -2
- package/core/shared/block-errors.ts +11 -0
- package/core/shared/calc/coerce.ts +29 -0
- package/core/shared/calc/errors.ts +43 -0
- package/core/shared/calc/evaluator.ts +101 -0
- package/core/shared/calc/index.ts +12 -0
- package/core/shared/calc/lexer.ts +194 -0
- package/core/shared/calc/parser.ts +204 -0
- package/core/shared/calc/typer.ts +184 -0
- package/core/shared/path-algebra.ts +169 -0
- package/core/shared/predicates/index.ts +527 -0
- package/core/shared/published-page-route.ts +61 -0
- package/core/shared/types/adaptive.types.ts +1 -1
- package/core/shared/types/component-contract.types.ts +128 -2
- package/core/shared/types/data-config.types.ts +149 -2
- package/core/shared/types/domain.types.ts +13 -0
- package/core/shared/types/event.types.ts +22 -2
- package/core/shared/types/layout-constraint.types.ts +73 -0
- package/core/shared/types/page-state.types.ts +60 -0
- package/core/shared/types/reaction.types.ts +144 -0
- package/core/shared/types/scope-manifest.types.ts +11 -15
- package/core/shared/types/step.types.ts +91 -0
- package/core/shared/types/style-config.types.ts +4 -4
- package/core/shared/types/ui-config.types.ts +18 -0
- package/core/shared/types/window.types.ts +177 -0
- package/dist/cli/soma.js +28 -3
- package/dist/core/index.cjs +4829 -405
- package/dist/types/build/{v2 → blocks}/block-compile.d.ts +7 -7
- package/dist/types/build/{v2/soma-file-loader.d.ts → blocks/block-container.d.ts} +6 -11
- package/dist/types/build/{v2/migrate-v1.d.ts → blocks/migrate-flat.d.ts} +6 -6
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +2 -0
- package/dist/types/build/component/component-graph.d.ts +85 -0
- package/dist/types/build/eject/component-eject.d.ts +121 -0
- package/dist/types/build/eject/component-upgrade.d.ts +43 -0
- package/dist/types/build/layout/constraint-solver.d.ts +36 -0
- package/dist/types/build/layout/css-emitter.d.ts +18 -0
- package/dist/types/build/layout/page-layout.d.ts +24 -0
- package/dist/types/build/pack/pack-init.d.ts +8 -0
- package/dist/types/client/base/SomaPageRootBase.d.ts +28 -0
- package/dist/types/client/component/ai-catalog.d.ts +24 -1
- package/dist/types/client/component/component-defaults.d.ts +15 -0
- package/dist/types/client/component/component-definition.types.d.ts +38 -0
- package/dist/types/client/component/component-manager.d.ts +36 -0
- package/dist/types/client/component/component-pack.d.ts +32 -2
- package/dist/types/client/component/contract-compat.d.ts +2 -2
- package/dist/types/client/component/example-blocks.d.ts +9 -0
- package/dist/types/client/component/keyboard-matrix.d.ts +24 -0
- package/dist/types/client/component/pack-styles.d.ts +21 -0
- package/dist/types/client/component/style-features.d.ts +26 -0
- package/dist/types/client/component/theme-component-tokens.d.ts +14 -0
- package/dist/types/client/data/data-object-resolver.d.ts +106 -1
- package/dist/types/client/data/reaction-engine.d.ts +21 -0
- package/dist/types/client/data/window-engine.d.ts +27 -0
- package/dist/types/client/engine/event-engine.d.ts +15 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +70 -0
- package/dist/types/client/engine/step-runner.d.ts +24 -0
- package/dist/types/client/infra/notify/DomNotifyService.d.ts +2 -1
- package/dist/types/client/infra/notify/INotifyTheme.d.ts +1 -1
- package/dist/types/client/parser/{v2 → blocks}/page-parser.d.ts +2 -2
- package/dist/types/client/parser/{v2 → blocks}/use-parser.d.ts +3 -3
- package/dist/types/client/parser/duration.d.ts +1 -0
- package/dist/types/client/parser/layout-parser.d.ts +9 -0
- package/dist/types/client/parser/page-state-parser.d.ts +32 -0
- package/dist/types/client/parser/reaction-parser.d.ts +16 -0
- package/dist/types/client/parser/reaction-sugar.d.ts +13 -0
- package/dist/types/client/parser/step-parser.d.ts +23 -0
- package/dist/types/client/parser/style-config-parser.d.ts +1 -1
- package/dist/types/client/parser/ui-config-parser.d.ts +5 -0
- package/dist/types/client/parser/window-parser.d.ts +25 -0
- package/dist/types/client/registry/index.d.ts +1 -0
- package/dist/types/client/theme/theme-engine.d.ts +37 -0
- package/dist/types/index.d.ts +40 -9
- package/dist/types/server/host/published-page-assets.d.ts +3 -2
- package/dist/types/server/index.d.ts +1 -1
- package/dist/types/shared/block-errors.d.ts +5 -0
- package/dist/types/shared/calc/coerce.d.ts +4 -0
- package/dist/types/shared/calc/errors.d.ts +14 -0
- package/dist/types/shared/calc/evaluator.d.ts +2 -0
- package/dist/types/shared/calc/index.d.ts +6 -0
- package/dist/types/shared/calc/lexer.d.ts +20 -0
- package/dist/types/shared/calc/parser.d.ts +49 -0
- package/dist/types/shared/calc/typer.d.ts +16 -0
- package/dist/types/shared/path-algebra.d.ts +56 -0
- package/dist/types/shared/predicates/index.d.ts +79 -0
- package/dist/types/shared/published-page-route.d.ts +21 -0
- package/dist/types/shared/types/component-contract.types.d.ts +112 -0
- package/dist/types/shared/types/data-config.types.d.ts +132 -2
- package/dist/types/shared/types/domain.types.d.ts +12 -0
- package/dist/types/shared/types/event.types.d.ts +8 -2
- package/dist/types/shared/types/layout-constraint.types.d.ts +52 -0
- package/dist/types/shared/types/page-state.types.d.ts +49 -0
- package/dist/types/shared/types/reaction.types.d.ts +115 -0
- package/dist/types/shared/types/scope-manifest.types.d.ts +11 -11
- package/dist/types/shared/types/step.types.d.ts +76 -0
- package/dist/types/shared/types/style-config.types.d.ts +4 -4
- package/dist/types/shared/types/ui-config.types.d.ts +18 -0
- package/dist/types/shared/types/window.types.d.ts +171 -0
- package/package.json +2 -6
- package/dist/types/client/component/example-v2.d.ts +0 -9
- package/postcss.config.js +0 -6
- package/tailwind.config.ts +0 -43
- /package/dist/types/build/{v2 → blocks}/block-loader.d.ts +0 -0
- /package/dist/types/build/{v2 → blocks}/html-index.d.ts +0 -0
- /package/dist/types/client/parser/{v2 → blocks}/namespace.d.ts +0 -0
|
@@ -21,8 +21,17 @@ import type {
|
|
|
21
21
|
SomaDataConfig,
|
|
22
22
|
SomaDataDicts,
|
|
23
23
|
SomaDataObjectConfig,
|
|
24
|
+
SomaDataFieldConfig,
|
|
25
|
+
SomaDataPort,
|
|
24
26
|
SomaDataSourceConfig,
|
|
27
|
+
SomaDataSubmitResult,
|
|
28
|
+
SomaDataWriteConfig,
|
|
25
29
|
} from '../../shared/types/data-config.types';
|
|
30
|
+
import type { SomaReactionGraph } from '../../shared/types/reaction.types';
|
|
31
|
+
import type { SomaTransaction, SomaWindowHandle } from '../../shared/types/window.types';
|
|
32
|
+
import { createWindow } from './window-engine';
|
|
33
|
+
import { collectCalcRefs, evaluateCalc, parseCalc, type CalcAst } from '../../shared/calc';
|
|
34
|
+
import type { SomaNamedPredicates } from '../../shared/predicates';
|
|
26
35
|
|
|
27
36
|
// ─── 错误类 ───────────────────────────────────────────────────────────────────
|
|
28
37
|
|
|
@@ -58,6 +67,10 @@ export function isSomaMockEnabled(): boolean {
|
|
|
58
67
|
/** 解析器需要的最小 http 能力(结构兼容 IHttpClient / SomaHttpService) */
|
|
59
68
|
export interface SomaDataHttpLike {
|
|
60
69
|
get<T = unknown>(url: string, config?: { params?: Record<string, string> }): Promise<T>;
|
|
70
|
+
/** 写通道(§8):不实现时 submit 抛「本 Host 的 http 不支持写」而不是静默失败 */
|
|
71
|
+
post?<T = unknown>(url: string, body?: unknown): Promise<T>;
|
|
72
|
+
put?<T = unknown>(url: string, body?: unknown): Promise<T>;
|
|
73
|
+
patch?<T = unknown>(url: string, body?: unknown): Promise<T>;
|
|
61
74
|
}
|
|
62
75
|
|
|
63
76
|
export interface SomaDataResolverOptions {
|
|
@@ -121,6 +134,38 @@ function referencesObject(value: unknown, objectName: string): boolean {
|
|
|
121
134
|
return false;
|
|
122
135
|
}
|
|
123
136
|
|
|
137
|
+
/**
|
|
138
|
+
* 从 http 抛出的错误里挖出后端约定的 { message, fieldErrors }。
|
|
139
|
+
*
|
|
140
|
+
* 各家 http 客户端把响应体放在不同位置(error.response.data / error.data / error.body),
|
|
141
|
+
* 这里逐个试而不是绑定某一家——写通道不该要求 Host 换 http 客户端。
|
|
142
|
+
*/
|
|
143
|
+
function extractErrorPayload(error: unknown): {
|
|
144
|
+
message?: string;
|
|
145
|
+
fieldErrors?: Record<string, string>;
|
|
146
|
+
} {
|
|
147
|
+
const candidates: unknown[] = [error];
|
|
148
|
+
if (isPlainObject(error)) {
|
|
149
|
+
candidates.push(error.response, error.data, error.body);
|
|
150
|
+
if (isPlainObject(error.response)) candidates.push(error.response.data);
|
|
151
|
+
}
|
|
152
|
+
for (const candidate of candidates) {
|
|
153
|
+
if (!isPlainObject(candidate)) continue;
|
|
154
|
+
const rawFieldErrors = candidate.fieldErrors ?? candidate.field_errors ?? candidate.errors;
|
|
155
|
+
const message = typeof candidate.message === 'string' ? candidate.message : undefined;
|
|
156
|
+
if (isPlainObject(rawFieldErrors)) {
|
|
157
|
+
const fieldErrors: Record<string, string> = {};
|
|
158
|
+
for (const [key, value] of Object.entries(rawFieldErrors)) {
|
|
159
|
+
if (typeof value === 'string') fieldErrors[key] = value;
|
|
160
|
+
else if (Array.isArray(value) && typeof value[0] === 'string') fieldErrors[key] = value[0];
|
|
161
|
+
}
|
|
162
|
+
if (Object.keys(fieldErrors).length > 0) return { message, fieldErrors };
|
|
163
|
+
}
|
|
164
|
+
if (message) return { message };
|
|
165
|
+
}
|
|
166
|
+
return {};
|
|
167
|
+
}
|
|
168
|
+
|
|
124
169
|
// ─── 解析器 ──────────────────────────────────────────────────────────────────
|
|
125
170
|
|
|
126
171
|
export class SomaDataObjectResolver {
|
|
@@ -188,6 +233,261 @@ export class SomaDataObjectResolver {
|
|
|
188
233
|
return { objects: this.objects, dicts };
|
|
189
234
|
}
|
|
190
235
|
|
|
236
|
+
// ─── 写通道(§8,REQ-009 M-5)────────────────────────────────────────────
|
|
237
|
+
|
|
238
|
+
/** 已解析对象的只读视图(data 端口的 getObject) */
|
|
239
|
+
getObject(name: string): unknown {
|
|
240
|
+
return this.objects[name];
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** 字段字典(data 端口的 getDict);无字典时返回空表而不是 undefined */
|
|
244
|
+
getDict(name: string): Record<string, SomaDataFieldConfig> {
|
|
245
|
+
return this.config.objects[name]?.fields ?? {};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* 反应图与具名谓词表(REQ-010 S1b)。
|
|
250
|
+
* 图在**解析期**就建好了(拓扑序、反向索引、回溯表都在里面),这里只是把它交出去;
|
|
251
|
+
* 没声明 `reactions:` 的对象返回 null,组件据此走「无联动」的老路。
|
|
252
|
+
*/
|
|
253
|
+
/**
|
|
254
|
+
* 数据窗口(REQ-010 S2):声了 `window:` 段的对象才有,按对象名单例。
|
|
255
|
+
*
|
|
256
|
+
* 惰性创建而不是 `resolve()` 后统一建:一个声明了窗口的对象未必这一屏就在用它
|
|
257
|
+
* (同一份 `_data.yaml` 服务多个页面是常态),提前建会平白多发一轮取数。
|
|
258
|
+
* 对象 `reload` 时整个销毁——窗口缓存的是"按某组意图取回的行",
|
|
259
|
+
* 而 reload 的意思正是"那些行已经不作数了"。
|
|
260
|
+
*/
|
|
261
|
+
window(name: string): SomaWindowHandle | null {
|
|
262
|
+
const decl = this.config.objects[name];
|
|
263
|
+
if (!decl?.window) return null;
|
|
264
|
+
|
|
265
|
+
const existing = this.windows.get(name);
|
|
266
|
+
if (existing) return existing;
|
|
267
|
+
|
|
268
|
+
const handle = createWindow(decl.window, {
|
|
269
|
+
http: this.http,
|
|
270
|
+
api: decl.from[0]?.api ?? '',
|
|
271
|
+
idField: this.primaryKeyOf(decl),
|
|
272
|
+
onChange: this.onWindowChange,
|
|
273
|
+
commit: decl.write
|
|
274
|
+
? (tx) => this.commit(name, tx)
|
|
275
|
+
: undefined,
|
|
276
|
+
});
|
|
277
|
+
this.windows.set(name, handle);
|
|
278
|
+
return handle;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* 行主键:写通道的 `version` 位不是主键,所以不能拿它顶。
|
|
283
|
+
* 按约定取 `id`;字典里没有 `id` 时取声明序第一个字段——比报错好:
|
|
284
|
+
* 主键猜错的后果是选择态与编辑按错的键归组,而报错的后果是整张表打不开。
|
|
285
|
+
* 猜了就要说出来。
|
|
286
|
+
*/
|
|
287
|
+
private primaryKeyOf(decl: SomaDataObjectConfig): string {
|
|
288
|
+
const fields = decl.fields ?? {};
|
|
289
|
+
if ('id' in fields) return 'id';
|
|
290
|
+
const first = Object.keys(fields)[0];
|
|
291
|
+
if (first === undefined) return 'id';
|
|
292
|
+
console.warn(
|
|
293
|
+
`[soma-core][数据] 对象 "${decl.name}" 的字典里没有 id 字段,`
|
|
294
|
+
+ `窗口按 "${first}" 认行。若它不唯一,行选与单元格编辑会串行。`,
|
|
295
|
+
);
|
|
296
|
+
return first;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** 事务提交(REQ-010 F-36):窗口把整批改动交给写通道,编排复用 submit 那一套 */
|
|
300
|
+
async commit(name: string, tx: SomaTransaction): Promise<SomaDataSubmitResult> {
|
|
301
|
+
const rows = [...(tx.update ?? []), ...(tx.add ?? [])];
|
|
302
|
+
if (rows.length === 0 && (tx.remove ?? []).length === 0) {
|
|
303
|
+
return { ok: false, message: '空事务:没有要提交的改动' };
|
|
304
|
+
}
|
|
305
|
+
// 逐行走既有的 submit 编排(白名单构体、map 改名、派生列闸门、错误逆映射)。
|
|
306
|
+
// 一次网络往返打包整批是后端 BFF 的形状,这里不替它决定——
|
|
307
|
+
// 先让语义正确,批量合并是后端接口就绪之后的事。
|
|
308
|
+
for (const row of rows) {
|
|
309
|
+
const result = await this.submit(name, row as Record<string, unknown>);
|
|
310
|
+
if (!result.ok) return result;
|
|
311
|
+
}
|
|
312
|
+
return { ok: true };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
getReactions(name: string): { graph: SomaReactionGraph; named: SomaNamedPredicates } | null {
|
|
316
|
+
const object = this.config.objects[name];
|
|
317
|
+
if (!object?.reactions) return null;
|
|
318
|
+
return { graph: object.reactions, named: object.predicates ?? {} };
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* 提交一个数据对象(§8.6 全链)。
|
|
323
|
+
*
|
|
324
|
+
* 编排全部在这里,组件只做「收值 → 调它 → 渲染返回的 fieldErrors」:
|
|
325
|
+
* ① 按 permission 白名单剔除只读字段(version 乐观锁位是唯一显式例外);
|
|
326
|
+
* ② map 平面改名;
|
|
327
|
+
* ③ 选端点:显式 mode 优先,否则按对象当前是否已有主键值自动判定;
|
|
328
|
+
* ④ 调接口;
|
|
329
|
+
* ⑤ 失败时把后端的字段级错误**逆映射**回表单键(否则表单认不出那是自己的哪个字段);
|
|
330
|
+
* ⑥ 成功后按 reload 重取。
|
|
331
|
+
*
|
|
332
|
+
* 前端按 permission 过滤是 UX 与声明自洽,**不是安全边界**——
|
|
333
|
+
* 后端仍须按自身权限模型校验(数据对象层原则 2)。
|
|
334
|
+
*/
|
|
335
|
+
async submit(
|
|
336
|
+
name: string,
|
|
337
|
+
model: Record<string, unknown>,
|
|
338
|
+
mode?: 'create' | 'update',
|
|
339
|
+
): Promise<SomaDataSubmitResult> {
|
|
340
|
+
const decl = this.config.objects[name];
|
|
341
|
+
if (!decl) {
|
|
342
|
+
return { ok: false, message: `本页没有名为 "${name}" 的数据对象` };
|
|
343
|
+
}
|
|
344
|
+
const write = decl.write;
|
|
345
|
+
if (!write) {
|
|
346
|
+
return { ok: false, message: `数据对象 "${name}" 没有声明 write 段(它是只读对象)` };
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const resolvedMode = mode ?? this.inferWriteMode(name, write);
|
|
350
|
+
const endpoint = resolvedMode === 'update' ? write.update ?? write.create : write.create ?? write.update;
|
|
351
|
+
if (!endpoint) {
|
|
352
|
+
return { ok: false, message: `数据对象 "${name}" 没有可用于 ${resolvedMode} 的写端点` };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// DW-4:派生字段不进请求体。
|
|
356
|
+
//
|
|
357
|
+
// 它本可以像其它只读字段一样被静默丢弃,但那是错的:只读字段被丢弃时,
|
|
358
|
+
// 作者本来就没打算写它;而派生字段出现在提交体里,说明作者以为自己能写它。
|
|
359
|
+
// 静默丢弃的表现是「我改了利润并保存,没报错,但值没变」——他会去查后端。
|
|
360
|
+
const derived = Object.keys(model).filter((key) => (decl.fields ?? {})[key]?.calc !== undefined);
|
|
361
|
+
if (derived.length > 0) {
|
|
362
|
+
return {
|
|
363
|
+
ok: false,
|
|
364
|
+
message: `派生字段不能提交:${derived.join(' / ')}`
|
|
365
|
+
+ `——它们由 calc 算出(改上游字段即可),写它们没有落点`,
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const body = this.buildRequestBody(decl, write, model);
|
|
370
|
+
const send = this.methodOf(endpoint.method);
|
|
371
|
+
if (!send) {
|
|
372
|
+
return {
|
|
373
|
+
ok: false,
|
|
374
|
+
message: `本 Host 的 http 服务没有实现 ${endpoint.method}——写通道需要它`,
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
try {
|
|
379
|
+
const data = await send(endpoint.api, body);
|
|
380
|
+
await this.reloadAfterWrite(name, write);
|
|
381
|
+
return { ok: true, data };
|
|
382
|
+
} catch (error) {
|
|
383
|
+
return this.mapSubmitError(error, write);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** 手动重取(data 端口的 reload);不给名字时重取全部 */
|
|
388
|
+
async reload(names?: string[]): Promise<void> {
|
|
389
|
+
// 窗口缓存的是"按某组意图取回的行",而 reload 的意思正是"那些行已经不作数了"。
|
|
390
|
+
// 留着它们的话,重取之后列表纹丝不动——最像"没生效"的一种生效。
|
|
391
|
+
for (const name of names && names.length > 0 ? names : [...this.windows.keys()]) {
|
|
392
|
+
this.windows.get(name)?.destroy();
|
|
393
|
+
this.windows.delete(name);
|
|
394
|
+
}
|
|
395
|
+
if (!names || names.length === 0) {
|
|
396
|
+
await this.resolve();
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
for (const name of names) {
|
|
400
|
+
const decl = this.config.objects[name];
|
|
401
|
+
if (!decl) continue;
|
|
402
|
+
this.objects[name] = await this.resolveObject(decl);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* 自动判定 create / update:对象已有值(读通道取回过一条记录)即为更新。
|
|
408
|
+
* 显式 write_mode 永远优先——自动判定只是缺省,不是规则。
|
|
409
|
+
*/
|
|
410
|
+
private inferWriteMode(name: string, write: SomaDataWriteConfig): 'create' | 'update' {
|
|
411
|
+
if (!write.update) return 'create';
|
|
412
|
+
if (!write.create) return 'update';
|
|
413
|
+
const current = this.objects[name];
|
|
414
|
+
const hasRecord = isPlainObject(current) && Object.keys(current).length > 0;
|
|
415
|
+
return hasRecord ? 'update' : 'create';
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** 构造请求体:剔除只读字段 → map 改名 → 补乐观锁位 */
|
|
419
|
+
private buildRequestBody(
|
|
420
|
+
decl: SomaDataObjectConfig,
|
|
421
|
+
write: SomaDataWriteConfig,
|
|
422
|
+
model: Record<string, unknown>,
|
|
423
|
+
): Record<string, unknown> {
|
|
424
|
+
const fields = decl.fields ?? {};
|
|
425
|
+
const body: Record<string, unknown> = {};
|
|
426
|
+
|
|
427
|
+
// 白名单:声了 only 就只送这几个。乐观锁位不受它管——它是协议位不是业务字段,
|
|
428
|
+
// 漏送的后果是乐观锁静默失效(并发覆盖,且没有任何一方会知道)。
|
|
429
|
+
const only = write.only === undefined ? null : new Set(write.only);
|
|
430
|
+
|
|
431
|
+
for (const [key, value] of Object.entries(model)) {
|
|
432
|
+
const field = fields[key];
|
|
433
|
+
if (only !== null && !only.has(key) && key !== write.version) continue;
|
|
434
|
+
// 字典里没有的键:按 w 处理(与 resolveFieldComponent「只对显式 r 降级」一致)
|
|
435
|
+
if (field?.permission === 'r' && key !== write.version) continue;
|
|
436
|
+
body[write.map?.[key] ?? key] = value;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// 乐观锁位取自**读态**而不是表单:它是只读字段,表单里根本没有它
|
|
440
|
+
if (write.version && !(write.version in model)) {
|
|
441
|
+
const current = this.objects[decl.name];
|
|
442
|
+
const versionValue = isPlainObject(current) ? current[write.version] : undefined;
|
|
443
|
+
if (versionValue !== undefined) {
|
|
444
|
+
body[write.map?.[write.version] ?? write.version] = versionValue;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
return body;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
private methodOf(method: string): ((url: string, body?: unknown) => Promise<unknown>) | null {
|
|
451
|
+
const key = method.toLowerCase() as 'post' | 'put' | 'patch';
|
|
452
|
+
const fn = this.http[key];
|
|
453
|
+
return typeof fn === 'function' ? fn.bind(this.http) : null;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
private async reloadAfterWrite(name: string, write: SomaDataWriteConfig): Promise<void> {
|
|
457
|
+
if (!write.reload) return;
|
|
458
|
+
if (write.reload === 'all') {
|
|
459
|
+
await this.resolve();
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
const targets = write.reload === 'self' ? [name] : write.reload;
|
|
463
|
+
await this.reload(targets);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* 后端错误 → 表单可渲染的结果(§8.3)。
|
|
468
|
+
*
|
|
469
|
+
* 逆映射是关键的一步:后端按请求体键报错(`order_no`),
|
|
470
|
+
* 而表单只认自己的字段键(`orderNo`)。不逆映射的话错误会挂不上任何一个字段,
|
|
471
|
+
* 于是「提交失败」有文案却没有红框,用户不知道该改哪里。
|
|
472
|
+
*/
|
|
473
|
+
private mapSubmitError(error: unknown, write: SomaDataWriteConfig): SomaDataSubmitResult {
|
|
474
|
+
const payload = extractErrorPayload(error);
|
|
475
|
+
const message = payload.message
|
|
476
|
+
?? (error instanceof Error ? error.message : '提交失败');
|
|
477
|
+
|
|
478
|
+
if (!payload.fieldErrors) return { ok: false, message };
|
|
479
|
+
|
|
480
|
+
const reverse = new Map<string, string>();
|
|
481
|
+
for (const [formKey, bodyKey] of Object.entries(write.map ?? {})) {
|
|
482
|
+
reverse.set(bodyKey, formKey);
|
|
483
|
+
}
|
|
484
|
+
const fieldErrors: Record<string, string> = {};
|
|
485
|
+
for (const [bodyKey, text] of Object.entries(payload.fieldErrors)) {
|
|
486
|
+
fieldErrors[reverse.get(bodyKey) ?? bodyKey] = text;
|
|
487
|
+
}
|
|
488
|
+
return { ok: false, message, fieldErrors };
|
|
489
|
+
}
|
|
490
|
+
|
|
191
491
|
// ─── DAG 拓扑 ──────────────────────────────────────────────────────────────
|
|
192
492
|
|
|
193
493
|
/** Kahn 分层拓扑排序:返回逐层对象名(每层内部无依赖,可并发);残留节点即环 */
|
|
@@ -238,15 +538,20 @@ export class SomaDataObjectResolver {
|
|
|
238
538
|
// ─── 单对象解析 ────────────────────────────────────────────────────────────
|
|
239
539
|
|
|
240
540
|
private async resolveObject(decl: SomaDataObjectConfig): Promise<unknown> {
|
|
541
|
+
// 声了 window: 的对象,**窗口就是它的读通道**。这里再取一遍等于每个窗口页面
|
|
542
|
+
// 都先做一次没人看的全量取数——列表页恰恰是全量最贵的地方。
|
|
543
|
+
// 值给空对象而不是 undefined:`page.data.<对象>` 仍要能被绑定读到(读出空而不是炸)。
|
|
544
|
+
if (decl.window) return {};
|
|
545
|
+
|
|
241
546
|
// mock 开关全局打开且声明了样本:直接用样本(透传 transform 派生)
|
|
242
547
|
if (this.mockEnabled && decl.mock !== undefined) {
|
|
243
548
|
console.warn(`[soma-core][数据] 对象 "${decl.name}" 使用 mock 样本(SOMA_MOCK_ENABLED)`);
|
|
244
|
-
return this.
|
|
549
|
+
return this.finalize(cloneSample(decl.mock), decl);
|
|
245
550
|
}
|
|
246
551
|
|
|
247
552
|
// 无接口预览对象(仅 mock):样本即数据
|
|
248
553
|
if (decl.from.length === 0) {
|
|
249
|
-
return this.
|
|
554
|
+
return this.finalize(cloneSample(decl.mock) ?? {}, decl);
|
|
250
555
|
}
|
|
251
556
|
|
|
252
557
|
// 来源分波:不自引用的来源无内部依赖 → 首波并发 Promise.all;
|
|
@@ -294,12 +599,12 @@ export class SomaDataObjectResolver {
|
|
|
294
599
|
// 接口失败且声明了 mock:整对象回退样本并警示(不吞、不阻塞其余对象)
|
|
295
600
|
if (decl.mock !== undefined) {
|
|
296
601
|
console.warn(`[soma-core][数据] 对象 "${decl.name}" 来源 "${api}" 取数失败,回退 mock 样本:`, error);
|
|
297
|
-
return this.
|
|
602
|
+
return this.finalize(cloneSample(decl.mock), decl);
|
|
298
603
|
}
|
|
299
604
|
console.error(`[soma-core][数据] 对象 "${decl.name}" 来源 "${api}" 取数失败(保留已取得的部分数据):`, error);
|
|
300
605
|
}
|
|
301
606
|
|
|
302
|
-
return this.
|
|
607
|
+
return this.finalize(value, decl);
|
|
303
608
|
}
|
|
304
609
|
|
|
305
610
|
private async fetchSource(
|
|
@@ -390,6 +695,30 @@ export class SomaDataObjectResolver {
|
|
|
390
695
|
// ─── transform 派生字段 ────────────────────────────────────────────────────
|
|
391
696
|
|
|
392
697
|
/** 按 "字段路径 | 管道链" 补派生字段(复用 pipe-registry 链式执行) */
|
|
698
|
+
/** 派生列 AST 缓存:对象名 → 字段名 → AST */
|
|
699
|
+
private readonly derivedAsts = new Map<string, Map<string, CalcAst>>();
|
|
700
|
+
|
|
701
|
+
/** 数据窗口:对象名 → 窗口实例(惰性建,reload 时销毁) */
|
|
702
|
+
private readonly windows = new Map<string, SomaWindowHandle>();
|
|
703
|
+
|
|
704
|
+
/** 窗口内容变化时通知宿主重渲(由 bootstrap 注入) */
|
|
705
|
+
private onWindowChange: (() => void) | undefined;
|
|
706
|
+
|
|
707
|
+
/** 由 bootstrap 接上:窗口自己的取数完成后要能推动页面渲染 */
|
|
708
|
+
setWindowChangeHandler(handler: () => void): void {
|
|
709
|
+
this.onWindowChange = handler;
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* 取数收尾:先算派生列,再跑展示管道。
|
|
714
|
+
*
|
|
715
|
+
* 次序不能反:`calc` 产出的是带类型的值(能排序、能求和),`transform` 产出的是
|
|
716
|
+
* 给人看的串。先格式化再算式的话,`[金额] - [折扣]` 拿到的是 "¥1,000.00" 这样的串。
|
|
717
|
+
*/
|
|
718
|
+
private finalize(value: unknown, decl: SomaDataObjectConfig): unknown {
|
|
719
|
+
return this.applyTransform(this.applyDerived(value, decl), decl);
|
|
720
|
+
}
|
|
721
|
+
|
|
393
722
|
private applyTransform(value: unknown, decl: SomaDataObjectConfig): unknown {
|
|
394
723
|
if (!decl.transform) return value;
|
|
395
724
|
if (!isPlainObject(value)) {
|
|
@@ -414,4 +743,78 @@ export class SomaDataObjectResolver {
|
|
|
414
743
|
}
|
|
415
744
|
return value;
|
|
416
745
|
}
|
|
746
|
+
|
|
747
|
+
/**
|
|
748
|
+
* 派生列求值(REQ-010 F-10):把 `fields.<x>.calc` 算出来挂到值上。
|
|
749
|
+
*
|
|
750
|
+
* 与 `transform:` 的分工:`transform` 是**展示管道**(金额加千分位、日期换格式),
|
|
751
|
+
* 产出的是字符串;`calc` 是**算式**,产出的是带类型的值,能参与排序、聚合与比较。
|
|
752
|
+
* 混成一个的话,「利润」既是一个能求和的数、又是一个格式化过的串,取决于谁先跑。
|
|
753
|
+
*
|
|
754
|
+
* 只作用于普通对象与对象数组两种形状——其余形状(标量、嵌套聚合)原样返回:
|
|
755
|
+
* 猜一个结构去写值,写错的地方不会报错,只会多出一个没人认识的键。
|
|
756
|
+
*/
|
|
757
|
+
private applyDerived(value: unknown, decl: SomaDataObjectConfig): unknown {
|
|
758
|
+
const fields = decl.fields ?? {};
|
|
759
|
+
const derived = Object.keys(fields).filter((key) => fields[key].calc !== undefined);
|
|
760
|
+
if (derived.length === 0) return value;
|
|
761
|
+
|
|
762
|
+
// AST 按对象缓存:每次重取都重新解析一遍表达式是纯浪费,而声明在页面生命周期内不变
|
|
763
|
+
let asts = this.derivedAsts.get(decl.name);
|
|
764
|
+
if (!asts) {
|
|
765
|
+
asts = new Map();
|
|
766
|
+
for (const key of derived) asts.set(key, parseCalc(fields[key].calc!));
|
|
767
|
+
this.derivedAsts.set(decl.name, asts);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
// 派生列可以引用另一个派生列,所以要按依赖序算。解析期已经检过环,
|
|
771
|
+
// 这里按「引用的派生列先算」做一次简单的深度优先即可。
|
|
772
|
+
const order: string[] = [];
|
|
773
|
+
const seen = new Set<string>();
|
|
774
|
+
const visit = (key: string): void => {
|
|
775
|
+
if (seen.has(key)) return;
|
|
776
|
+
seen.add(key);
|
|
777
|
+
for (const ref of collectCalcRefs(asts!.get(key)!)) {
|
|
778
|
+
if (asts!.has(ref)) visit(ref);
|
|
779
|
+
}
|
|
780
|
+
order.push(key);
|
|
781
|
+
};
|
|
782
|
+
for (const key of derived) visit(key);
|
|
783
|
+
|
|
784
|
+
const fill = (row: Record<string, unknown>): void => {
|
|
785
|
+
for (const key of order) row[key] = evaluateCalc(asts!.get(key)!, row);
|
|
786
|
+
};
|
|
787
|
+
|
|
788
|
+
if (Array.isArray(value)) {
|
|
789
|
+
for (const row of value) if (isPlainObject(row)) fill(row);
|
|
790
|
+
return value;
|
|
791
|
+
}
|
|
792
|
+
if (isPlainObject(value)) {
|
|
793
|
+
fill(value);
|
|
794
|
+
// 对象里挂着的行数组(`{ list: [...] }` 这种取数形状)同样要算:
|
|
795
|
+
// 派生列在列表上才是常态,只处理根对象等于对最常见的形状没生效。
|
|
796
|
+
for (const nested of Object.values(value)) {
|
|
797
|
+
if (Array.isArray(nested)) for (const row of nested) if (isPlainObject(row)) fill(row);
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
return value;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* 把解析器包成注入给自治组件的 data 端口(§8.4)。
|
|
807
|
+
*
|
|
808
|
+
* 为什么是包一层而不是直接注入解析器:解析器还有 resolve / 内部拓扑等大量能力,
|
|
809
|
+
* 组件不该看得见。端口是刻意收窄的四个方法——读态、字典、提交、重取。
|
|
810
|
+
*/
|
|
811
|
+
export function createSomaDataPort(resolver: SomaDataObjectResolver): SomaDataPort {
|
|
812
|
+
return {
|
|
813
|
+
getObject: (name) => resolver.getObject(name),
|
|
814
|
+
getDict: (name) => resolver.getDict(name),
|
|
815
|
+
getReactions: (name) => resolver.getReactions(name),
|
|
816
|
+
window: (name) => resolver.window(name),
|
|
817
|
+
submit: (name, model, mode) => resolver.submit(name, model, mode),
|
|
818
|
+
reload: (names) => resolver.reload(names),
|
|
819
|
+
};
|
|
417
820
|
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// 反应图求值(REQ-010 S1b)——一次输入 → 一批字段状态。
|
|
2
|
+
//
|
|
3
|
+
// 引擎只做三件事:找出受影响的反应、按拓扑序求值、把 patch 合并成结果。
|
|
4
|
+
// 它不认识组件、不碰 DOM、不发请求——纯函数,入参不改,出参是新对象。
|
|
5
|
+
//
|
|
6
|
+
// ── 为什么可以「只跑受影响子图」而不是全量重算 ──────────────────────
|
|
7
|
+
// rc-field-form 的做法是给每个字段挂订阅、按 namePath 过滤、脏标记递归传播——
|
|
8
|
+
// 那是因为它的依赖只有运行时才知道。SOMA 的图在解析期就建好了,所以
|
|
9
|
+
// 「改了 amount 会牵动谁」是一次 `byField['amount']` 查表,不需要遍历全表。
|
|
10
|
+
//
|
|
11
|
+
// ── 传播为什么必然终止 ──────────────────────────────────────────────
|
|
12
|
+
// `value` patch 会改字段值,改完的字段要继续往下游传播。图无环(解析期已保证),
|
|
13
|
+
// 所以沿拓扑序单向走一趟就够:一条反应最多被求值一次,步数上界是反应条数。
|
|
14
|
+
// 这是「图」相对「回调链」的直接红利——回调链要靠脏标记与最大轮次去防死循环。
|
|
15
|
+
|
|
16
|
+
import type {
|
|
17
|
+
SomaFieldStatePatch,
|
|
18
|
+
SomaFormStatePatch,
|
|
19
|
+
SomaReactionGraph,
|
|
20
|
+
SomaReactionPhase,
|
|
21
|
+
SomaReactionResult,
|
|
22
|
+
} from '../../shared/types/reaction.types';
|
|
23
|
+
import { SOMA_REACTION_FORM_KEY } from '../../shared/types/reaction.types';
|
|
24
|
+
import { evaluatePredicate, type SomaFieldPredicate, type SomaNamedPredicates } from '../../shared/predicates';
|
|
25
|
+
import { evaluateCalc, type CalcAst } from '../../shared/calc';
|
|
26
|
+
|
|
27
|
+
export interface ReactionRunInput {
|
|
28
|
+
graph: SomaReactionGraph;
|
|
29
|
+
/** 当前值(不会被修改) */
|
|
30
|
+
values: Record<string, unknown>;
|
|
31
|
+
/** 本次变更的字段;空数组表示「全量重算」(挂载时用) */
|
|
32
|
+
changed?: readonly string[];
|
|
33
|
+
phase?: SomaReactionPhase;
|
|
34
|
+
named?: SomaNamedPredicates;
|
|
35
|
+
/** `on: field_validate_*` 的判定面:当前校验失败的字段 */
|
|
36
|
+
failedFields?: readonly string[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function runReactions(input: ReactionRunInput): SomaReactionResult {
|
|
40
|
+
const { graph, named = {} } = input;
|
|
41
|
+
const phase = input.phase ?? 'field_value_change';
|
|
42
|
+
const values: Record<string, unknown> = { ...input.values };
|
|
43
|
+
|
|
44
|
+
const patches: Record<string, SomaFieldStatePatch> = {};
|
|
45
|
+
const form: SomaFormStatePatch = {};
|
|
46
|
+
const detached = new Set<string>();
|
|
47
|
+
let steps = 0;
|
|
48
|
+
|
|
49
|
+
// 受影响集合:不给 changed 就是全量(挂载/提交等时机本就该全跑一遍)
|
|
50
|
+
const affected = new Set<number>();
|
|
51
|
+
if (input.changed === undefined) {
|
|
52
|
+
graph.reactions.forEach((reaction) => affected.add(reaction.index));
|
|
53
|
+
} else {
|
|
54
|
+
for (const field of input.changed) {
|
|
55
|
+
for (const index of graph.byField[field] ?? []) affected.add(index);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 按拓扑序单向走一趟。`value` patch 改了字段值时,把该字段的下游反应加入集合——
|
|
60
|
+
// 因为是拓扑序且图无环,加入的下游一定还在后面,不会漏也不会回头。
|
|
61
|
+
for (const reaction of graph.reactions) {
|
|
62
|
+
if (!affected.has(reaction.index)) continue;
|
|
63
|
+
if (reaction.phase !== phase) continue;
|
|
64
|
+
steps += 1;
|
|
65
|
+
|
|
66
|
+
const holds = evaluateCondition(reaction.config.when, values, named, reaction, input.failedFields);
|
|
67
|
+
const branch = holds ? reaction.config.fulfill : reaction.config.otherwise;
|
|
68
|
+
if (!branch) continue;
|
|
69
|
+
|
|
70
|
+
for (const [target, patchRaw] of Object.entries(branch)) {
|
|
71
|
+
if (target === SOMA_REACTION_FORM_KEY) {
|
|
72
|
+
// `fulfill: { submit: { disabled: true } }` —— patchRaw 就是 `{ disabled }` 本身,
|
|
73
|
+
// 不是再包一层的 `{ submit: {...} }`。多取一层的话它恒为 undefined,
|
|
74
|
+
// 表单级 patch 会静默丢失(表现是「锁提交」这条反应写了没反应)。
|
|
75
|
+
Object.assign(form, {
|
|
76
|
+
submit: { ...form.submit, ...(patchRaw as SomaFormStatePatch['submit']) },
|
|
77
|
+
});
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const patch = patchRaw as SomaFieldStatePatch;
|
|
81
|
+
|
|
82
|
+
// 合并:同一字段被多条反应写时,后写的赢(拓扑序内即声明序)
|
|
83
|
+
const merged: SomaFieldStatePatch = { ...patches[target], ...patch };
|
|
84
|
+
// `visible` 是 `display` 的糖,在这里就归一,下游只认 display
|
|
85
|
+
if (merged.visible !== undefined) {
|
|
86
|
+
merged.display = merged.visible ? 'visible' : 'hidden';
|
|
87
|
+
delete merged.visible;
|
|
88
|
+
}
|
|
89
|
+
patches[target] = merged;
|
|
90
|
+
|
|
91
|
+
if (merged.display === 'none') detached.add(target);
|
|
92
|
+
else detached.delete(target);
|
|
93
|
+
|
|
94
|
+
const nextValue = applyValuePatch(patch, target, values, reaction, holds);
|
|
95
|
+
if (nextValue !== NO_CHANGE) {
|
|
96
|
+
values[target] = nextValue;
|
|
97
|
+
// 下游继续传播(拓扑序保证它们还没跑过)
|
|
98
|
+
for (const index of graph.byField[target] ?? []) affected.add(index);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// `display: none` 的字段连数据一起摘除——这正是它与 hidden 的分别,
|
|
104
|
+
// 也是写通道剔除请求体的依据。
|
|
105
|
+
for (const field of detached) delete values[field];
|
|
106
|
+
|
|
107
|
+
return { patches, form, values, detached: [...detached], steps };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** 哨兵:区分「patch 没写 value」与「patch 把值写成了 undefined」 */
|
|
111
|
+
const NO_CHANGE = Symbol('no-change');
|
|
112
|
+
|
|
113
|
+
function applyValuePatch(
|
|
114
|
+
patch: SomaFieldStatePatch,
|
|
115
|
+
target: string,
|
|
116
|
+
values: Record<string, unknown>,
|
|
117
|
+
reaction: SomaReactionGraph['reactions'][number],
|
|
118
|
+
holds: boolean,
|
|
119
|
+
): unknown {
|
|
120
|
+
if (patch.value === undefined) return NO_CHANGE;
|
|
121
|
+
if (patch.value === 'clear') return null;
|
|
122
|
+
|
|
123
|
+
if (typeof patch.value === 'object' && 'from' in patch.value) {
|
|
124
|
+
return values[patch.value.from] ?? null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (typeof patch.value === 'object' && 'calc' in patch.value) {
|
|
128
|
+
// AST 在解析期就备好了:每帧重新解析同一串表达式是纯浪费
|
|
129
|
+
const key = `${holds ? 'fulfill' : 'otherwise'}:${target}`;
|
|
130
|
+
const ast = reaction.calcAsts[key] as CalcAst | undefined;
|
|
131
|
+
if (!ast) return NO_CHANGE;
|
|
132
|
+
return evaluateCalc(ast, values);
|
|
133
|
+
}
|
|
134
|
+
return NO_CHANGE;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function evaluateCondition(
|
|
138
|
+
when: SomaFieldPredicate | undefined,
|
|
139
|
+
values: Record<string, unknown>,
|
|
140
|
+
named: SomaNamedPredicates,
|
|
141
|
+
reaction: SomaReactionGraph['reactions'][number],
|
|
142
|
+
failedFields: readonly string[] | undefined,
|
|
143
|
+
): boolean {
|
|
144
|
+
// 校验类时机:条件是「target 这个字段校验失败了没有」,不是谓词
|
|
145
|
+
if (reaction.phase.startsWith('field_validate')) {
|
|
146
|
+
const target = String(reaction.config.target ?? '');
|
|
147
|
+
const failed = (failedFields ?? []).includes(target);
|
|
148
|
+
const wantFailed = reaction.phase === 'field_validate_failed';
|
|
149
|
+
const wantSuccess = reaction.phase === 'field_validate_success';
|
|
150
|
+
if (wantFailed) return failed;
|
|
151
|
+
if (wantSuccess) return !failed;
|
|
152
|
+
return true; // start / end 只是时机,本身不带判定
|
|
153
|
+
}
|
|
154
|
+
// 没有 when(只有 depends)= 依赖变了就执行 fulfill
|
|
155
|
+
if (when === undefined) return true;
|
|
156
|
+
return evaluatePredicate(when, values, named);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 打印一条字段的回溯链——诊断面。
|
|
161
|
+
*
|
|
162
|
+
* amis 的 `${x}` 只能沿运行时数据链向上找,给不出这个;这是「可静态回溯」
|
|
163
|
+
* 在人能看见的那一端的落点。
|
|
164
|
+
*/
|
|
165
|
+
export function explainField(graph: SomaReactionGraph, field: string): string {
|
|
166
|
+
const info = graph.provenance[field];
|
|
167
|
+
if (!info) return `字段 "${field}" 不在字典里`;
|
|
168
|
+
const lines = [`字段 ${field}`];
|
|
169
|
+
if (info.sourceApi) lines.push(` 数据来源:${info.sourceApi}`);
|
|
170
|
+
if (info.writtenBy.length === 0) lines.push(' 没有反应写它(它的值只来自接口或用户输入)');
|
|
171
|
+
else {
|
|
172
|
+
lines.push(` 被反应写:${info.writtenBy.map((i) => `#${i + 1}`).join(' / ')}(声明序)`);
|
|
173
|
+
lines.push(` 上游字段:${info.readsFrom.length > 0 ? info.readsFrom.join(' / ') : '无'}`);
|
|
174
|
+
}
|
|
175
|
+
return lines.join('\n');
|
|
176
|
+
}
|