soma-core 1.5.1 → 2.0.0
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/app/SomaPageClient.tsx +28 -9
- package/app/soma/manifest/route.ts +3 -0
- package/bin/migrate.cjs +71 -1
- package/bin/soma-lsp.cjs +8 -1
- package/core/build/blocks/block-compile.ts +23 -5
- package/core/build/compiler/full-manifest-builder.ts +15 -0
- package/core/build/compiler/page-assets.ts +17 -1
- package/core/build/compiler/page-declaration-compiler.ts +67 -11
- package/core/build/compiler/precompiled-manifest-loader.ts +7 -0
- package/core/build/compiler/soma-manifest-webpack-plugin.js +7 -0
- package/core/build/compiler/soma-precompile.js +212 -21
- package/core/build/component/package-loader.ts +35 -4
- package/core/build/component-tree/artifact-codec.ts +192 -0
- package/core/build/component-tree/binding-bridge.ts +272 -0
- package/core/build/component-tree/collection-binding-lowerer.ts +65 -0
- package/core/build/component-tree/definition-preparation.ts +320 -0
- package/core/build/component-tree/migration-files.ts +214 -0
- package/core/build/component-tree/migration.ts +170 -0
- package/core/build/component-tree/parser.ts +362 -0
- package/core/build/component-tree/registry-snapshot.ts +317 -0
- package/core/build/component-tree/source-preflight.ts +132 -0
- package/core/build/component-tree/structure-topology.ts +63 -0
- package/core/build/component-tree/validator.ts +318 -0
- package/core/build/eject/component-eject.ts +1 -0
- package/core/build/lsp/component-tree-service.ts +79 -0
- package/core/build/lsp/component-tree.cjs +42 -0
- package/core/build/lsp/cross-file-diagnostics.cjs +15 -3
- package/core/build/lsp/extra-completion.cjs +7 -0
- package/core/client/adaptive/observer-pool.ts +122 -8
- package/core/client/compiler/manifest-compiler.ts +48 -5
- package/core/client/component/ai-catalog.ts +41 -3
- package/core/client/component/catalog-examples.ts +60 -0
- package/core/client/component/component-defaults.ts +1 -1
- package/core/client/component/component-definition.types.ts +9 -0
- package/core/client/component/component-expander.ts +22 -6
- package/core/client/component/component-manager.ts +881 -45
- package/core/client/component/component-pack.ts +1 -1
- package/core/client/component/component-registry.ts +11 -2
- package/core/client/component/scoped-adapter.ts +29 -2
- package/core/client/component/structure-adapter.ts +207 -0
- package/core/client/component/verify-pack.ts +21 -1
- package/core/client/container/SomaContainerAdapter.ts +103 -2
- package/core/client/data/data-object-resolver.ts +45 -1
- package/core/client/engine/asset-resolver.ts +1 -1
- package/core/client/engine/dom-renderer.ts +712 -26
- package/core/client/engine/event-engine.ts +328 -21
- package/core/client/engine/property-watcher.ts +164 -18
- package/core/client/engine/reactive-proxy.ts +194 -23
- package/core/client/engine/render-engine.ts +120 -11
- package/core/client/engine/row-range.ts +62 -0
- package/core/client/engine/row-scope.ts +77 -0
- package/core/client/engine/runtime-builder.ts +2 -0
- package/core/client/engine/scope-resolver.ts +3 -3
- package/core/client/engine/soma-bootstrap.ts +176 -17
- package/core/client/parser/adaptive-parser.ts +1 -1
- package/core/client/parser/annotation-extractor.ts +1 -1
- package/core/client/parser/blocks/page-parser.ts +53 -3
- package/core/client/parser/blocks/use-parser.ts +21 -6
- package/core/client/parser/data-config-parser.ts +1 -1
- package/core/client/parser/pipeline-parser.ts +252 -15
- package/core/client/parser/style-config-parser.ts +1 -1
- package/core/client/parser/timer-parser.ts +1 -1
- package/core/client/parser/trigger-parser.ts +1 -1
- package/core/client/parser/ui-config-parser.ts +39 -7
- package/core/client/pipes/pipe-registry.ts +45 -4
- package/core/client/registry/declaration-renderer.ts +32 -24
- package/core/client/registry/remote-page.ts +1 -1
- package/core/client/runtime/SomaSnapshotStore.ts +1 -1
- package/core/client/scheduler/SomaScheduler.ts +1 -1
- package/core/client/security/attribute-presence.ts +3 -0
- package/core/client/theme/harmony.ts +135 -13
- package/core/client/theme/scene-tokens.ts +17 -2
- package/core/client/theme/theme-axes.ts +43 -6
- package/core/client/theme/theme-engine.ts +921 -126
- package/core/client/validation/contract-validator.ts +2 -2
- package/core/index.ts +3 -0
- package/core/server/host/page-markup-config.ts +42 -0
- package/core/server/host/published-page-artifact-cache.ts +36 -4
- package/core/server/host/published-page-assets.ts +13 -7
- package/core/server/host/resolve-host-tree-context.ts +43 -0
- package/core/server/host/server-page-assets.ts +23 -4
- package/core/server/host/soma-page-compile.ts +10 -0
- package/core/server/mock/node/cli.mjs +3 -3
- package/core/server/page-declaration.ts +7 -0
- package/core/server/registry/declaration-precompiler.ts +35 -3
- package/core/server/registry/page-registry-repository.ts +31 -3
- package/core/server/registry/page-registry-store.ts +64 -7
- package/core/server/registry/publish-asset-pin.ts +6 -1
- package/core/server/registry/services/pages/Get.ts +11 -6
- package/core/server/registry/validation/compile-gate.ts +10 -1
- package/core/shared/component-tree-artifact.ts +237 -0
- package/core/shared/component-tree-errors.ts +52 -0
- package/core/shared/path-algebra.ts +1 -1
- package/core/shared/types/component-contract.types.ts +51 -0
- package/core/shared/types/component-tree-artifact.types.ts +121 -0
- package/core/shared/types/component-tree.types.ts +170 -0
- package/core/shared/types/engine.types.ts +5 -0
- package/core/shared/types/event.types.ts +8 -4
- package/core/shared/types/manifest.types.ts +4 -0
- package/core/shared/types/ui-config.types.ts +8 -0
- package/dist/cli/soma.js +1 -1
- package/dist/core/index.cjs +14508 -11358
- package/dist/lsp/component-tree.cjs +20997 -0
- package/dist/types/build/blocks/block-compile.d.ts +10 -1
- package/dist/types/build/compiler/full-manifest-builder.d.ts +9 -0
- package/dist/types/build/compiler/page-assets.d.ts +6 -1
- package/dist/types/build/compiler/page-declaration-compiler.d.ts +11 -1
- package/dist/types/build/component/package-loader.d.ts +4 -1
- package/dist/types/build/component-tree/artifact-codec.d.ts +50 -0
- package/dist/types/build/component-tree/binding-bridge.d.ts +19 -0
- package/dist/types/build/component-tree/collection-binding-lowerer.d.ts +18 -0
- package/dist/types/build/component-tree/definition-preparation.d.ts +11 -0
- package/dist/types/build/component-tree/migration-files.d.ts +35 -0
- package/dist/types/build/component-tree/migration.d.ts +28 -0
- package/dist/types/build/component-tree/parser.d.ts +13 -0
- package/dist/types/build/component-tree/registry-snapshot.d.ts +15 -0
- package/dist/types/build/component-tree/source-preflight.d.ts +22 -0
- package/dist/types/build/component-tree/structure-topology.d.ts +7 -0
- package/dist/types/build/component-tree/validator.d.ts +9 -0
- package/dist/types/build/lsp/component-tree-service.d.ts +34 -0
- package/dist/types/client/adaptive/observer-pool.d.ts +9 -2
- package/dist/types/client/compiler/manifest-compiler.d.ts +11 -2
- package/dist/types/client/component/ai-catalog.d.ts +18 -1
- package/dist/types/client/component/catalog-examples.d.ts +18 -0
- package/dist/types/client/component/component-definition.types.d.ts +9 -0
- package/dist/types/client/component/component-expander.d.ts +2 -1
- package/dist/types/client/component/component-manager.d.ts +97 -0
- package/dist/types/client/component/scoped-adapter.d.ts +1 -0
- package/dist/types/client/component/structure-adapter.d.ts +17 -0
- package/dist/types/client/container/SomaContainerAdapter.d.ts +8 -0
- package/dist/types/client/data/data-object-resolver.d.ts +19 -0
- package/dist/types/client/engine/dom-renderer.d.ts +125 -2
- package/dist/types/client/engine/event-engine.d.ts +62 -3
- package/dist/types/client/engine/property-watcher.d.ts +41 -1
- package/dist/types/client/engine/reactive-proxy.d.ts +42 -2
- package/dist/types/client/engine/render-engine.d.ts +55 -3
- package/dist/types/client/engine/row-range.d.ts +13 -0
- package/dist/types/client/engine/row-scope.d.ts +20 -0
- package/dist/types/client/engine/runtime-builder.d.ts +1 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +4 -1
- package/dist/types/client/parser/blocks/use-parser.d.ts +18 -0
- package/dist/types/client/parser/pipeline-parser.d.ts +12 -4
- package/dist/types/client/parser/ui-config-parser.d.ts +2 -0
- package/dist/types/client/pipes/pipe-registry.d.ts +39 -1
- package/dist/types/client/registry/declaration-renderer.d.ts +5 -1
- package/dist/types/client/registry/remote-page.d.ts +1 -1
- package/dist/types/client/theme/harmony.d.ts +44 -2
- package/dist/types/client/theme/scene-tokens.d.ts +11 -1
- package/dist/types/client/theme/theme-axes.d.ts +36 -6
- package/dist/types/index.d.ts +3 -1
- package/dist/types/server/host/page-markup-config.d.ts +6 -0
- package/dist/types/server/host/published-page-artifact-cache.d.ts +9 -1
- package/dist/types/server/host/published-page-assets.d.ts +5 -3
- package/dist/types/server/host/resolve-host-tree-context.d.ts +8 -0
- package/dist/types/server/host/server-page-assets.d.ts +6 -0
- package/dist/types/server/host/soma-page-compile.d.ts +10 -1
- package/dist/types/server/page-declaration.d.ts +6 -0
- package/dist/types/server/registry/declaration-precompiler.d.ts +8 -3
- package/dist/types/server/registry/page-registry-repository.d.ts +5 -0
- package/dist/types/server/registry/page-registry-store.d.ts +11 -1
- package/dist/types/server/registry/publish-asset-pin.d.ts +4 -0
- package/dist/types/server/registry/validation/compile-gate.d.ts +4 -1
- package/dist/types/shared/component-tree-artifact.d.ts +5 -0
- package/dist/types/shared/component-tree-errors.d.ts +22 -0
- package/dist/types/shared/types/component-contract.types.d.ts +42 -0
- package/dist/types/shared/types/component-tree-artifact.types.d.ts +104 -0
- package/dist/types/shared/types/component-tree.types.d.ts +149 -0
- package/dist/types/shared/types/engine.types.d.ts +5 -0
- package/dist/types/shared/types/event.types.d.ts +8 -4
- package/dist/types/shared/types/manifest.types.d.ts +4 -0
- package/dist/types/shared/types/ui-config.types.d.ts +8 -0
- package/package.json +5 -4
- package/dist/preview-export.cjs +0 -3401
|
@@ -3,18 +3,22 @@
|
|
|
3
3
|
// 一个组件实例 = 一个复用现有引擎的"迷你页面":
|
|
4
4
|
// 实例作用域根(props + ctrl)→ ScopeResolver → ManifestCompiler
|
|
5
5
|
// → RenderEngine + EventEngine(经 Scoped 适配器隔离锚点)。
|
|
6
|
-
//
|
|
6
|
+
// 页面与实例之间有三条通道:
|
|
7
7
|
// props 下行(页面路径 → 实例作用域,prop 源变更触发实例整树重渲);
|
|
8
|
-
// emit 上行(契约 emit → 页面 Pipeline
|
|
8
|
+
// emit 上行(契约 emit → 页面 Pipeline,四动词与修饰符原样可用);
|
|
9
|
+
// model 回写(契约 mode: 'model' 的 prop 在实例内被改写 → 写回绑定它的数据路径,
|
|
10
|
+
// 走页面响应式代理,于是 props 下行的 watch 照常把同一个值推回实例,见 installModelProps)。
|
|
9
11
|
//
|
|
10
|
-
// 设计规范:soma-core/doc/soma
|
|
12
|
+
// 设计规范:soma-core/doc/soma 运行时引擎详设.md、soma-ui/doc/soma 组件体系与契约.md。
|
|
11
13
|
|
|
12
14
|
import { SomaManifestCompiler } from '../compiler/manifest-compiler';
|
|
13
15
|
import { isKnownDomEvent } from '../parser/pipeline-parser';
|
|
14
16
|
import { SomaRenderEngine } from '../engine/render-engine';
|
|
15
17
|
import { SomaEventEngine } from '../engine/event-engine';
|
|
16
18
|
import { SomaScopeResolver } from '../engine/scope-resolver';
|
|
19
|
+
import { SomaRowScope } from '../engine/row-scope';
|
|
17
20
|
import { SomaEmitEvent } from '../engine/emit-event';
|
|
21
|
+
import { SomaCompositionGate } from '../engine/dom-renderer';
|
|
18
22
|
import type { SomaScheduler } from '../scheduler/SomaScheduler';
|
|
19
23
|
import type { SomaDiagnostics } from '../diagnostics/SomaDiagnostics';
|
|
20
24
|
import type { SomaRuntimeManifest } from '../../shared/types/manifest.types';
|
|
@@ -25,6 +29,7 @@ import type {
|
|
|
25
29
|
ShowValue,
|
|
26
30
|
UiConfig,
|
|
27
31
|
} from '../../shared/types/ui-config.types';
|
|
32
|
+
import type { SomaContractProp } from '../../shared/types/component-contract.types';
|
|
28
33
|
import type { SomaComponentDefinition } from './component-definition.types';
|
|
29
34
|
import { SOMA_COMPONENT_CTRL_KEY } from './component-definition.types';
|
|
30
35
|
import { SomaComponentRegistry } from './component-registry';
|
|
@@ -39,6 +44,17 @@ import { SomaScopedContainerAdapter, SOMA_SCOPE_ATTR } from './scoped-adapter';
|
|
|
39
44
|
import type { SomaComponentDefaults } from './component-defaults';
|
|
40
45
|
import { ensureStyleFeatures } from './style-features';
|
|
41
46
|
import type { PackStyleOptions } from './pack-styles';
|
|
47
|
+
import { COMPONENT_TREE_CAPABILITIES, type TreeArtifact, type StructurePlan } from '../../shared/types/component-tree-artifact.types';
|
|
48
|
+
import { SomaComponentTreeError } from '../../shared/component-tree-errors';
|
|
49
|
+
import { assertDefinitionPreparation } from '../../shared/component-tree-artifact';
|
|
50
|
+
import { assertPreparedStructureProps, withPreparedStructureProps } from './structure-adapter';
|
|
51
|
+
import {
|
|
52
|
+
defaultPipeRegistry,
|
|
53
|
+
hasPipes,
|
|
54
|
+
parsePipedValue,
|
|
55
|
+
type ParsedPipedValue,
|
|
56
|
+
type SomaPipeRegistry,
|
|
57
|
+
} from '../pipes/pipe-registry';
|
|
42
58
|
|
|
43
59
|
const MAX_EXPANSION_ROUNDS = 8;
|
|
44
60
|
|
|
@@ -54,6 +70,8 @@ export interface SomaComponentPageRuntime {
|
|
|
54
70
|
}
|
|
55
71
|
|
|
56
72
|
export interface SomaComponentManagerOptions {
|
|
73
|
+
/** The complete artifact has already passed the bootstrap integrity gate. */
|
|
74
|
+
treeArtifact?: TreeArtifact;
|
|
57
75
|
registry: SomaComponentRegistry;
|
|
58
76
|
scheduler?: SomaScheduler;
|
|
59
77
|
diagnostics?: SomaDiagnostics;
|
|
@@ -69,10 +87,29 @@ export interface SomaComponentManagerOptions {
|
|
|
69
87
|
styleOptions?: PackStyleOptions;
|
|
70
88
|
}
|
|
71
89
|
|
|
72
|
-
/** 作用域读取器:绑定路径求值 + var
|
|
90
|
+
/** 作用域读取器:绑定路径求值 + var 判定 + model 回写(页面 / 行 / 父实例三种实现) */
|
|
73
91
|
interface ScopeReader {
|
|
74
92
|
read(path: string): unknown;
|
|
75
93
|
hasVar(seg: string): boolean;
|
|
94
|
+
/** 管道上下文的 locale 表:与同一作用域里模板绑定的管道读的是同一份(作用域根上的 `_locale`) */
|
|
95
|
+
locale(): Record<string, unknown> | undefined;
|
|
96
|
+
/**
|
|
97
|
+
* 把值写到绑定路径(只给 mode: 'model' 的 prop 用)。
|
|
98
|
+
* 写进去返回 null;写不了返回原因(进告警文案),此时什么都不写。
|
|
99
|
+
*/
|
|
100
|
+
write(path: string, value: unknown): string | null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* props 管道求值的出处:告警点名到实例锚点与组件名。
|
|
105
|
+
* `warned` 由管理器持有、跨实例共享:行内实例逐行求值、父实例每次重渲都会重算子组件 props,
|
|
106
|
+
* 同一锚点同一 prop 同一声明只告警一次,不然一张表刷一屏。
|
|
107
|
+
*/
|
|
108
|
+
interface PropPipeSite {
|
|
109
|
+
instanceId: string;
|
|
110
|
+
componentName: string;
|
|
111
|
+
pipes: SomaPipeRegistry;
|
|
112
|
+
warned: Set<string>;
|
|
76
113
|
}
|
|
77
114
|
|
|
78
115
|
interface StaticInstanceDecl {
|
|
@@ -82,6 +119,19 @@ interface StaticInstanceDecl {
|
|
|
82
119
|
propsDecl: Record<string, unknown>;
|
|
83
120
|
}
|
|
84
121
|
|
|
122
|
+
/**
|
|
123
|
+
* 行内实例对页面作用域路径的订阅(见 watchRowPageProps)。
|
|
124
|
+
* `propsDecl` / `rowData` 每次行渲染都刷新:键控列表复用行时实例不重建、行数据却换了,
|
|
125
|
+
* 页面路径变更时按最新的这一份重解析,行字段 prop 才不会被打回旧行值。
|
|
126
|
+
*/
|
|
127
|
+
interface RowPageWatch {
|
|
128
|
+
/** 订阅的页面路径(已解析、排序后拼接):行渲染时声明里的页面路径没变就沿用原订阅 */
|
|
129
|
+
paths: string;
|
|
130
|
+
propsDecl: Record<string, unknown>;
|
|
131
|
+
rowData: unknown;
|
|
132
|
+
dispose: () => void;
|
|
133
|
+
}
|
|
134
|
+
|
|
85
135
|
export class SomaComponentManager {
|
|
86
136
|
private readonly registry: SomaComponentRegistry;
|
|
87
137
|
private readonly scheduler?: SomaScheduler;
|
|
@@ -90,15 +140,28 @@ export class SomaComponentManager {
|
|
|
90
140
|
private readonly componentDefaults?: SomaComponentDefaults;
|
|
91
141
|
private readonly styleOptions?: PackStyleOptions;
|
|
92
142
|
private readonly expander = new SomaComponentExpander();
|
|
143
|
+
private readonly treeArtifact?: TreeArtifact;
|
|
144
|
+
private readonly preparedDefinitions = new Map<string, SomaComponentDefinition>();
|
|
93
145
|
|
|
94
146
|
private staticDecls: StaticInstanceDecl[] = [];
|
|
95
147
|
private propsIndex = new Map<string, Record<string, unknown>>();
|
|
96
148
|
private readonly instances = new Set<SomaComponentInstance>();
|
|
97
149
|
private readonly hostInstances = new WeakMap<HTMLElement, SomaComponentInstance>();
|
|
98
150
|
private readonly watchDisposers: Array<() => void> = [];
|
|
151
|
+
/** 行内实例的页面路径订阅,随实例释放(releaseHost)与管理器销毁一起撤掉 */
|
|
152
|
+
private readonly rowPageWatches = new Map<SomaComponentInstance, RowPageWatch>();
|
|
153
|
+
/** props 管道告警的去重键(见 PropPipeSite) */
|
|
154
|
+
private readonly warnedPropPipes = new Set<string>();
|
|
155
|
+
/**
|
|
156
|
+
* 正在构造实例的宿主(见 constructInstance)。控制器工厂里改写 model prop 会同步回写到父实例、
|
|
157
|
+
* 触发父整树重渲,重渲又为这个宿主调 mountChild——此刻 hostInstances 里还没有它,
|
|
158
|
+
* 不拦就会给同一个宿主再建一个实例(旧的那个带着监听器泄漏)。
|
|
159
|
+
*/
|
|
160
|
+
private readonly constructingHosts = new WeakSet<HTMLElement>();
|
|
99
161
|
private page: SomaComponentPageRuntime | null = null;
|
|
100
162
|
|
|
101
163
|
constructor(options: SomaComponentManagerOptions) {
|
|
164
|
+
this.treeArtifact = options.treeArtifact;
|
|
102
165
|
this.registry = options.registry;
|
|
103
166
|
this.scheduler = options.scheduler;
|
|
104
167
|
this.diagnostics = options.diagnostics;
|
|
@@ -138,7 +201,7 @@ export class SomaComponentManager {
|
|
|
138
201
|
throw new SomaComponentExpandError(`组件 "${name}" 的宿主元素缺少 data-soma-id`);
|
|
139
202
|
}
|
|
140
203
|
const def = this.registry.get(name);
|
|
141
|
-
this.
|
|
204
|
+
this.expandHost(host, def, this.propsIndex.get(instanceId) ?? {});
|
|
142
205
|
// 组合组件的子组件也必须在页面契约校验前展开。否则子组件模板
|
|
143
206
|
// 里的 data-soma-host 仍封在 template.content 中,页面会误报
|
|
144
207
|
// "缺少 data-soma-host 边界";实例挂载阶段再展开已经太晚。
|
|
@@ -200,7 +263,12 @@ export class SomaComponentManager {
|
|
|
200
263
|
dispose();
|
|
201
264
|
}
|
|
202
265
|
this.watchDisposers.length = 0;
|
|
203
|
-
for (const
|
|
266
|
+
for (const watch of this.rowPageWatches.values()) {
|
|
267
|
+
watch.dispose();
|
|
268
|
+
}
|
|
269
|
+
this.rowPageWatches.clear();
|
|
270
|
+
const ordered = [...this.instances].sort((a, b) => a.getHost().contains(b.getHost()) ? 1 : b.getHost().contains(a.getHost()) ? -1 : 0);
|
|
271
|
+
for (const instance of ordered) {
|
|
204
272
|
instance.destroy();
|
|
205
273
|
}
|
|
206
274
|
this.instances.clear();
|
|
@@ -226,12 +294,12 @@ export class SomaComponentManager {
|
|
|
226
294
|
return;
|
|
227
295
|
}
|
|
228
296
|
// 被摘除的行根元素自己就可能是组件宿主(行模板根 = 组件)
|
|
229
|
-
this.releaseHost(root);
|
|
230
297
|
for (const host of Array.from(
|
|
231
298
|
root.querySelectorAll<HTMLElement>(`[${SOMA_COMPONENT_ATTR}]`),
|
|
232
|
-
)) {
|
|
299
|
+
).reverse()) {
|
|
233
300
|
this.releaseHost(host);
|
|
234
301
|
}
|
|
302
|
+
this.releaseHost(root);
|
|
235
303
|
}
|
|
236
304
|
|
|
237
305
|
private releaseHost(host: HTMLElement): void {
|
|
@@ -241,6 +309,8 @@ export class SomaComponentManager {
|
|
|
241
309
|
}
|
|
242
310
|
this.hostInstances.delete(host);
|
|
243
311
|
this.instances.delete(instance);
|
|
312
|
+
this.rowPageWatches.get(instance)?.dispose();
|
|
313
|
+
this.rowPageWatches.delete(instance);
|
|
244
314
|
instance.destroy();
|
|
245
315
|
}
|
|
246
316
|
|
|
@@ -260,10 +330,15 @@ export class SomaComponentManager {
|
|
|
260
330
|
return;
|
|
261
331
|
}
|
|
262
332
|
|
|
333
|
+
if (this.constructingHosts.has(element)) {
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
263
336
|
const reader = this.createRowReader(this.page, rowData);
|
|
264
337
|
const existing = this.hostInstances.get(element);
|
|
265
338
|
if (existing) {
|
|
266
|
-
|
|
339
|
+
// 先刷新订阅里的声明与行数据,再下发:setProps 引发的 model 回写触发页面路径通知时,重解析读到的已是这一行的新数据
|
|
340
|
+
this.watchRowPageProps(element, existing, propsDecl, rowData, this.page);
|
|
341
|
+
existing.setProps(withPreparedStructureProps(element, resolveProps(propsDecl, reader, existing.def, this.defaultsFor(existing.def), this.pipeSite(element.getAttribute('data-soma-id') ?? name, existing.def)), propsDecl));
|
|
267
342
|
return;
|
|
268
343
|
}
|
|
269
344
|
|
|
@@ -272,8 +347,64 @@ export class SomaComponentManager {
|
|
|
272
347
|
throw new SomaComponentExpandError(`组件 "${name}" 的宿主元素缺少 data-soma-id`);
|
|
273
348
|
}
|
|
274
349
|
const def = this.registry.get(name);
|
|
275
|
-
this.
|
|
276
|
-
this.mountInstance(element, instanceId, def, propsDecl, reader);
|
|
350
|
+
this.expandHost(element, def, propsDecl);
|
|
351
|
+
const instance = this.mountInstance(element, instanceId, def, propsDecl, reader);
|
|
352
|
+
this.watchRowPageProps(element, instance, propsDecl, rowData, this.page);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* 行内实例订阅 props 声明里的页面作用域路径(首段是页面 bind 的 var,如 `page.kw`、管道实参 `page.unit`):
|
|
357
|
+
* 路径变更 → 按这一行最新的声明与行数据重解析 → 只更新这个实例,列表不重渲。
|
|
358
|
+
*
|
|
359
|
+
* 列表绑定只 watch 自己的数据源(`page.groups`),行模板里引用的页面路径不在任何 watch 里,
|
|
360
|
+
* 不订阅的话行内实例只在整个列表重渲时才拿到页面路径的新值。
|
|
361
|
+
* 行变量(`g.kw`)与对象作用域变量读的是行数据,行数据不经响应式代理,仍由列表重渲驱动,不订阅。
|
|
362
|
+
*
|
|
363
|
+
* 订阅随实例走:releaseHost 与 destroy 撤掉;行复用时声明里的页面路径没变就沿用原订阅,只换声明与行数据。
|
|
364
|
+
* 与静态实例(registerPropWatches)同一套路径收集(collectBindingPaths)与重解析(resolveProps 带 Host 默认)。
|
|
365
|
+
*/
|
|
366
|
+
private watchRowPageProps(
|
|
367
|
+
host: HTMLElement,
|
|
368
|
+
instance: SomaComponentInstance,
|
|
369
|
+
propsDecl: Record<string, unknown>,
|
|
370
|
+
rowData: unknown,
|
|
371
|
+
page: SomaComponentPageRuntime,
|
|
372
|
+
): void {
|
|
373
|
+
const collected = new Set<string>();
|
|
374
|
+
collectBindingPaths(propsDecl, page.resolver, collected);
|
|
375
|
+
const resolved = new Set<string>();
|
|
376
|
+
for (const path of collected) {
|
|
377
|
+
const first = path.split('.')[0];
|
|
378
|
+
// 页面 bind 的 var 映射到根(路径为空);显式键行作用域的别名同名时以行变量为准
|
|
379
|
+
if (page.resolver.getVarPath(first) !== '' || (rowData instanceof SomaRowScope && rowData.has(first))) {
|
|
380
|
+
continue;
|
|
381
|
+
}
|
|
382
|
+
resolved.add(page.resolver.resolvePath(path));
|
|
383
|
+
}
|
|
384
|
+
const paths = [...resolved].sort().join('\n');
|
|
385
|
+
const current = this.rowPageWatches.get(instance);
|
|
386
|
+
if (current && current.paths === paths) {
|
|
387
|
+
current.propsDecl = propsDecl;
|
|
388
|
+
current.rowData = rowData;
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
current?.dispose();
|
|
392
|
+
this.rowPageWatches.delete(instance);
|
|
393
|
+
if (resolved.size === 0) {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
const watch: RowPageWatch = { paths, propsDecl, rowData, dispose: () => {} };
|
|
397
|
+
const instanceId = host.getAttribute('data-soma-id') ?? instance.def.contract.name;
|
|
398
|
+
const disposers = [...resolved].map((path) => page.renderEngine.watch(path, () => {
|
|
399
|
+
// 已撤掉或换掉的订阅不再下发:属性观察者撤订阅是换一张新表,正在遍历的那一轮通知仍会调到这里
|
|
400
|
+
if (this.rowPageWatches.get(instance) !== watch) return;
|
|
401
|
+
const rowReader = this.createRowReader(page, watch.rowData);
|
|
402
|
+
instance.setProps(withPreparedStructureProps(host, resolveProps(watch.propsDecl, rowReader, instance.def, this.defaultsFor(instance.def), this.pipeSite(instanceId, instance.def)), watch.propsDecl));
|
|
403
|
+
}));
|
|
404
|
+
watch.dispose = () => {
|
|
405
|
+
for (const dispose of disposers) dispose();
|
|
406
|
+
};
|
|
407
|
+
this.rowPageWatches.set(instance, watch);
|
|
277
408
|
}
|
|
278
409
|
|
|
279
410
|
// ─── 实例装配 ──────────────────────────────────────────────
|
|
@@ -285,28 +416,39 @@ export class SomaComponentManager {
|
|
|
285
416
|
propsDecl: Record<string, unknown>,
|
|
286
417
|
reader: ScopeReader,
|
|
287
418
|
): SomaComponentInstance {
|
|
288
|
-
const instance = new SomaComponentInstance({
|
|
419
|
+
const instance = this.constructInstance(host, () => new SomaComponentInstance({
|
|
289
420
|
host,
|
|
290
421
|
instanceId,
|
|
291
422
|
def,
|
|
292
|
-
initialProps: resolveProps(propsDecl, reader, def, this.defaultsFor(def)),
|
|
423
|
+
initialProps: withPreparedStructureProps(host, resolveProps(propsDecl, reader, def, this.defaultsFor(def), this.pipeSite(instanceId, def)), propsDecl),
|
|
293
424
|
scheduler: this.scheduler,
|
|
294
425
|
diagnostics: this.diagnostics,
|
|
295
426
|
pipeRegistry: this.pipeRegistry,
|
|
296
427
|
styleOptions: this.styleOptions,
|
|
297
428
|
services: this.filterServices(def),
|
|
298
429
|
dispatchEmit: (emitName, payload) => this.dispatchEmit(instanceId, host, emitName, payload),
|
|
430
|
+
writeModel: createModelWriter(instanceId, def, propsDecl, reader),
|
|
299
431
|
registry: this.registry,
|
|
300
432
|
expandNested: (root) => this.expandNestedTemplates(root),
|
|
301
433
|
mountChild: (parent, childHost, childPropsDecl, rowData) =>
|
|
302
434
|
this.applyChildComponent(parent, childHost, childPropsDecl, rowData),
|
|
303
435
|
releaseSubtree: (removedRoot) => this.releaseSubtree(removedRoot),
|
|
304
|
-
});
|
|
436
|
+
}));
|
|
305
437
|
this.instances.add(instance);
|
|
306
438
|
this.hostInstances.set(host, instance);
|
|
307
439
|
return instance;
|
|
308
440
|
}
|
|
309
441
|
|
|
442
|
+
/** 构造期间把宿主登记进 constructingHosts(重入原因见该字段注释),构造抛错也要摘掉 */
|
|
443
|
+
private constructInstance(host: HTMLElement, build: () => SomaComponentInstance): SomaComponentInstance {
|
|
444
|
+
this.constructingHosts.add(host);
|
|
445
|
+
try {
|
|
446
|
+
return build();
|
|
447
|
+
} finally {
|
|
448
|
+
this.constructingHosts.delete(host);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
310
452
|
/**
|
|
311
453
|
* 父组件模板里的真实子组件:props 在父实例作用域求值,emit 回到父实例事件清单。
|
|
312
454
|
* 这是通用组合能力;父子双方仍各自拥有独立 controller / render / event 生命周期。
|
|
@@ -320,21 +462,22 @@ export class SomaComponentManager {
|
|
|
320
462
|
const name = element.getAttribute(SOMA_COMPONENT_ATTR);
|
|
321
463
|
const childId = element.getAttribute(SOMA_CHILD_COMPONENT_ID_ATTR);
|
|
322
464
|
if (!name || !childId) return;
|
|
465
|
+
if (this.constructingHosts.has(element)) return;
|
|
323
466
|
|
|
324
467
|
const reader = this.createChildReader(parent, rowData);
|
|
325
468
|
const existing = this.hostInstances.get(element);
|
|
326
469
|
if (existing) {
|
|
327
|
-
existing.setProps(resolveProps(propsDecl, reader, existing.def, this.defaultsFor(existing.def)));
|
|
470
|
+
existing.setProps(withPreparedStructureProps(element, resolveProps(propsDecl, reader, existing.def, this.defaultsFor(existing.def), this.pipeSite(childId, existing.def)), propsDecl));
|
|
328
471
|
return;
|
|
329
472
|
}
|
|
330
473
|
|
|
331
474
|
const def = this.registry.get(name);
|
|
332
|
-
this.
|
|
333
|
-
const child = new SomaComponentInstance({
|
|
475
|
+
this.expandHost(element, def, propsDecl);
|
|
476
|
+
const child = this.constructInstance(element, () => new SomaComponentInstance({
|
|
334
477
|
host: element,
|
|
335
478
|
instanceId: childId,
|
|
336
479
|
def,
|
|
337
|
-
initialProps: resolveProps(propsDecl, reader, def, this.defaultsFor(def)),
|
|
480
|
+
initialProps: withPreparedStructureProps(element, resolveProps(propsDecl, reader, def, this.defaultsFor(def), this.pipeSite(childId, def)), propsDecl),
|
|
338
481
|
scheduler: this.scheduler,
|
|
339
482
|
diagnostics: this.diagnostics,
|
|
340
483
|
pipeRegistry: this.pipeRegistry,
|
|
@@ -346,7 +489,8 @@ export class SomaComponentManager {
|
|
|
346
489
|
this.applyChildComponent(owner, childHost, childProps, nestedRow),
|
|
347
490
|
releaseSubtree: (removedRoot) => this.releaseSubtree(removedRoot),
|
|
348
491
|
dispatchEmit: (emitName, payload) => parent.dispatchChildEmit(childId, element, emitName, payload),
|
|
349
|
-
|
|
492
|
+
writeModel: createModelWriter(childId, def, propsDecl, reader),
|
|
493
|
+
}));
|
|
350
494
|
this.instances.add(child);
|
|
351
495
|
this.hostInstances.set(element, child);
|
|
352
496
|
}
|
|
@@ -384,15 +528,73 @@ export class SomaComponentManager {
|
|
|
384
528
|
);
|
|
385
529
|
}
|
|
386
530
|
for (const host of pending) {
|
|
387
|
-
|
|
531
|
+
const owner = this.preparedDefinitions.get(host.getAttribute(SOMA_SCOPE_ATTR) ?? '');
|
|
532
|
+
const childId = host.getAttribute(SOMA_CHILD_COMPONENT_ID_ATTR)!;
|
|
533
|
+
const props = owner?.uiConfig ? collectPagePropsDecls(owner.uiConfig.show).get(childId) : undefined;
|
|
534
|
+
this.expandHost(host, this.registry.get(host.getAttribute(SOMA_COMPONENT_ATTR)!), props ?? {});
|
|
388
535
|
}
|
|
389
536
|
}
|
|
390
537
|
}
|
|
391
538
|
|
|
539
|
+
private expandHost(host: HTMLElement, def: SomaComponentDefinition, props: Record<string, unknown>): void {
|
|
540
|
+
this.assertDefinitionPrepared(def);
|
|
541
|
+
let plan: StructurePlan | undefined;
|
|
542
|
+
const symbol = host.getAttribute('data-soma-id');
|
|
543
|
+
if (this.treeArtifact && symbol) {
|
|
544
|
+
const plans = this.treeArtifact.structurePlan.filter(item => item.instanceSymbol === symbol);
|
|
545
|
+
if (plans.length !== 1) this.artifactError('组件宿主没有唯一结构计划');
|
|
546
|
+
plan = plans[0];
|
|
547
|
+
const ref = this.treeArtifact.componentRefs.find(item => item.name === def.contract.name);
|
|
548
|
+
if (!ref || !def.structureDigest || !def.contractDigest
|
|
549
|
+
|| ref.structureDigest !== def.structureDigest || ref.contractDigest !== def.contractDigest
|
|
550
|
+
|| plan.structureDigest !== def.structureDigest) this.artifactError('组件供货摘要与结构资产不一致');
|
|
551
|
+
}
|
|
552
|
+
const staticProp = def.contract.structure?.semantics?.staticProp;
|
|
553
|
+
if (staticProp && Object.prototype.hasOwnProperty.call(this.defaultsFor(def) ?? {}, staticProp)) {
|
|
554
|
+
this.artifactError('Host 默认值不得覆盖结构语义属性');
|
|
555
|
+
}
|
|
556
|
+
const scope = this.expander.expand(host, def, plan, props);
|
|
557
|
+
this.preparedDefinitions.set(scope, def);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
private assertDefinitionPrepared(def: SomaComponentDefinition): void {
|
|
561
|
+
const plans = def.definitionRepeatPlans;
|
|
562
|
+
const references = new Set<string>();
|
|
563
|
+
const hasCollectionSource = (show: ShowMapping | undefined): boolean => Object.values(show ?? {}).some(value => {
|
|
564
|
+
if (!value || typeof value !== 'object') return false;
|
|
565
|
+
if (isNested(value) && value.bind.repeatPlanId) references.add(value.bind.repeatPlanId);
|
|
566
|
+
return (isNested(value) && value.bind.collectionSlot !== undefined) || hasCollectionSource(value.show);
|
|
567
|
+
});
|
|
568
|
+
if (hasCollectionSource(def.uiConfig?.show)) this.artifactError('组件内部 collectionSlot 必须在构建时准备为计划');
|
|
569
|
+
if (references.size !== (plans?.length ?? 0) || plans?.some(plan => !references.has(plan.planId) || plan.definitionName !== def.contract.name)) {
|
|
570
|
+
this.artifactError('组件集合绑定与准备计划未完整一一对应');
|
|
571
|
+
}
|
|
572
|
+
if (!plans?.length && !def.definitionPreparation) return;
|
|
573
|
+
const prep = def.definitionPreparation;
|
|
574
|
+
if (prep) assertDefinitionPreparation(prep);
|
|
575
|
+
if (!prep || prep.schemaVersion !== 1 || !prep.definitionDigest
|
|
576
|
+
|| (plans?.length && ['definition-repeat:1', 'repeat-range:1', 'row-bindings:1'].some(capability => !prep.requiredCapabilities.some(value => value === capability)))
|
|
577
|
+
|| prep.requiredCapabilities.some(capability => !COMPONENT_TREE_CAPABILITIES.includes(capability))) {
|
|
578
|
+
this.artifactError('组件定义准备封套或运行时能力不兼容');
|
|
579
|
+
}
|
|
580
|
+
for (const [name, digest] of Object.entries(prep.dependencyDigests)) {
|
|
581
|
+
if (!this.registry.has(name) || !digest || this.registry.get(name).structureDigest !== digest) {
|
|
582
|
+
this.artifactError('组件定义准备依赖摘要失效');
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
private artifactError(fixHint: string): never {
|
|
588
|
+
throw new SomaComponentTreeError({ code: 'CT-ARTIFACT-CAPABILITY', phase: 'runtime', sourceId: 'runtime', nodePath: 'component', fixHint });
|
|
589
|
+
}
|
|
590
|
+
|
|
392
591
|
private createChildReader(parent: SomaComponentInstance, rowData: unknown): ScopeReader {
|
|
393
592
|
return {
|
|
394
593
|
read: (path: string) => parent.read(path, rowData),
|
|
395
594
|
hasVar: (seg: string) => parent.hasVar(seg),
|
|
595
|
+
// 父实例模板绑定的管道读的是父作用域根上的 _locale(dom-renderer 经父 resolver 读根),这里同源
|
|
596
|
+
locale: () => parent.getScopeRoot()._locale as Record<string, unknown> | undefined,
|
|
597
|
+
write: (path: string, value: unknown) => parent.writeBoundPath(path, value, rowData),
|
|
396
598
|
};
|
|
397
599
|
}
|
|
398
600
|
|
|
@@ -425,13 +627,17 @@ export class SomaComponentManager {
|
|
|
425
627
|
/**
|
|
426
628
|
* 把页面事件清单中"实例id.契约emit名"的条目标记为 emit 路由。
|
|
427
629
|
* 覆盖静态实例与行模板内的实例(模板内容递归扫描)。
|
|
630
|
+
* 标完对没走上 emit 路由的订阅告警(见 warnUnroutedSubscriptions)。
|
|
428
631
|
*/
|
|
429
632
|
private markEmitRoutes(page: SomaComponentPageRuntime): void {
|
|
430
633
|
const hosts = new Map<string, string>();
|
|
431
634
|
collectComponentHosts(page.container, hosts);
|
|
432
635
|
|
|
636
|
+
// 宿主写着组件名、但注册表里没有它的那些:先攒起来,循环结束统一点名(见 warnUnregisteredHosts)。
|
|
637
|
+
const unregistered = new Map<string, string[]>();
|
|
433
638
|
for (const [instanceId, name] of hosts) {
|
|
434
639
|
if (!this.registry.has(name)) {
|
|
640
|
+
unregistered.set(name, [...(unregistered.get(name) ?? []), instanceId]);
|
|
435
641
|
continue;
|
|
436
642
|
}
|
|
437
643
|
const contract = this.registry.get(name).contract;
|
|
@@ -443,15 +649,8 @@ export class SomaComponentManager {
|
|
|
443
649
|
}
|
|
444
650
|
}
|
|
445
651
|
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
if (!event.emitRoute && event.domEvent && !isKnownDomEvent(event.domEvent)) {
|
|
449
|
-
console.warn(
|
|
450
|
-
`[soma-core][组件] 事件订阅 "${event.somaId}.${event.domEvent}" ` +
|
|
451
|
-
`既不是 DOM 事件,也未匹配任何组件契约 emit,永远不会触发`,
|
|
452
|
-
);
|
|
453
|
-
}
|
|
454
|
-
}
|
|
652
|
+
warnUnregisteredHosts(unregistered, this.registry);
|
|
653
|
+
warnUnroutedSubscriptions(page.manifest, hosts, this.registry);
|
|
455
654
|
}
|
|
456
655
|
|
|
457
656
|
// ─── props 求值 ────────────────────────────────────────────
|
|
@@ -463,6 +662,18 @@ export class SomaComponentManager {
|
|
|
463
662
|
return page.resolver.readFromRoot(resolved);
|
|
464
663
|
},
|
|
465
664
|
hasVar: (seg: string) => page.resolver.hasVar(seg),
|
|
665
|
+
locale: () => page.resolver.readFromRoot('_locale') as Record<string, unknown> | undefined,
|
|
666
|
+
write: (path: string, value: unknown) => writePagePath(page.resolver, path, value),
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/** 该实例 props 管道求值的出处;没传注册表时与页面绑定一样用内置表(manifest-compiler / dom-renderer 同款缺省) */
|
|
671
|
+
private pipeSite(instanceId: string, def: SomaComponentDefinition): PropPipeSite {
|
|
672
|
+
return {
|
|
673
|
+
instanceId,
|
|
674
|
+
componentName: def.contract.name,
|
|
675
|
+
pipes: this.pipeRegistry ?? defaultPipeRegistry,
|
|
676
|
+
warned: this.warnedPropPipes,
|
|
466
677
|
};
|
|
467
678
|
}
|
|
468
679
|
|
|
@@ -473,6 +684,15 @@ export class SomaComponentManager {
|
|
|
473
684
|
}
|
|
474
685
|
|
|
475
686
|
private createRowReader(page: SomaComponentPageRuntime, rowData: unknown): ScopeReader {
|
|
687
|
+
if (rowData instanceof SomaRowScope) return {
|
|
688
|
+
read: path => rowData.read(path, value => page.resolver.readFromRoot(page.resolver.resolvePath(value))),
|
|
689
|
+
hasVar: seg => rowData.has(seg) || page.resolver.hasVar(seg),
|
|
690
|
+
locale: () => page.resolver.readFromRoot('_locale') as Record<string, unknown> | undefined,
|
|
691
|
+
write: (path, value) => {
|
|
692
|
+
const first = path.split('.')[0];
|
|
693
|
+
return rowData.has(first) ? rowVariableRefusal(first) : writePagePath(page.resolver, path, value);
|
|
694
|
+
},
|
|
695
|
+
};
|
|
476
696
|
return {
|
|
477
697
|
read: (path: string) => {
|
|
478
698
|
const segments = path.split('.');
|
|
@@ -487,6 +707,9 @@ export class SomaComponentManager {
|
|
|
487
707
|
return segments.reduce((obj: any, key) => obj?.[key], rowData);
|
|
488
708
|
},
|
|
489
709
|
hasVar: (seg: string) => page.resolver.hasVar(seg),
|
|
710
|
+
locale: () => page.resolver.readFromRoot('_locale') as Record<string, unknown> | undefined,
|
|
711
|
+
// 行变量段(getVarPath 非空)读的是行数据,写回时由 writePagePath 统一拒收
|
|
712
|
+
write: (path: string, value: unknown) => writePagePath(page.resolver, path, value),
|
|
490
713
|
};
|
|
491
714
|
}
|
|
492
715
|
|
|
@@ -502,8 +725,10 @@ export class SomaComponentManager {
|
|
|
502
725
|
|
|
503
726
|
for (const path of paths) {
|
|
504
727
|
const resolved = page.resolver.resolvePath(path);
|
|
728
|
+
// 重解析必须带上 Host 默认:否则任一 prop 源变更(含 model 回写本身)都会把
|
|
729
|
+
// 页面没声明、由 Host 默认给值的 prop 打回契约默认(searchable 默认 true 的导轨,一键入搜索框就消失)
|
|
505
730
|
const dispose = page.renderEngine.watch(resolved, () => {
|
|
506
|
-
instance.setProps(resolveProps(decl.propsDecl, reader, decl.def));
|
|
731
|
+
instance.setProps(withPreparedStructureProps(decl.host, resolveProps(decl.propsDecl, reader, decl.def, this.defaultsFor(decl.def), this.pipeSite(decl.instanceId, decl.def)), decl.propsDecl));
|
|
507
732
|
});
|
|
508
733
|
this.watchDisposers.push(dispose);
|
|
509
734
|
}
|
|
@@ -524,6 +749,8 @@ interface SomaComponentInstanceOptions {
|
|
|
524
749
|
styleOptions?: PackStyleOptions;
|
|
525
750
|
services?: Record<string, unknown>;
|
|
526
751
|
dispatchEmit: (emitName: string, payload: unknown) => void;
|
|
752
|
+
/** model 回写通道:mode: 'model' 的 prop 在实例内被改写时调用(见 installModelProps) */
|
|
753
|
+
writeModel?: (propName: string, value: unknown) => void;
|
|
527
754
|
registry: SomaComponentRegistry;
|
|
528
755
|
/** 编译前的结构展开(插槽投影必须先于编译完成,见 expandNestedTemplates) */
|
|
529
756
|
expandNested: (root: HTMLElement) => void;
|
|
@@ -562,12 +789,23 @@ export class SomaComponentInstance {
|
|
|
562
789
|
}> = [];
|
|
563
790
|
private readonly mountChild: SomaComponentInstanceOptions['mountChild'];
|
|
564
791
|
private readonly styleOptions?: PackStyleOptions;
|
|
792
|
+
private readonly host: HTMLElement;
|
|
793
|
+
/** mode: 'model' 的 prop 的实际取值(作用域根上的同名键是它的访问器,见 installModelProps) */
|
|
794
|
+
private readonly modelValues = new Map<string, unknown>();
|
|
795
|
+
/** 宿主内输入法组合期间被改写、还没回写的 model prop(见 installModelProps) */
|
|
796
|
+
private readonly heldModelProps = new Set<string>();
|
|
797
|
+
/** 宿主上组合闸门的监听与补写随实例销毁撤掉 */
|
|
798
|
+
private readonly modelAbort = new AbortController();
|
|
565
799
|
|
|
566
800
|
constructor(options: SomaComponentInstanceOptions) {
|
|
801
|
+
this.host = options.host;
|
|
802
|
+
assertPreparedStructureProps(options.host, options.initialProps);
|
|
567
803
|
this.def = options.def;
|
|
568
804
|
this.mountChild = options.mountChild;
|
|
569
805
|
this.styleOptions = options.styleOptions;
|
|
570
806
|
this.scopeRoot = { ...options.initialProps };
|
|
807
|
+
// 必须在控制器工厂之前:控制器拿到的 props 就是这个作用域根,它对 model prop 的直接赋值也要被接住
|
|
808
|
+
this.installModelProps(options.writeModel);
|
|
571
809
|
|
|
572
810
|
// 特性分片(M-3):必须在控制器与渲染之前注入,否则首帧是无样式的。
|
|
573
811
|
ensureStyleFeatures(options.def, this.scopeRoot, this.styleOptions);
|
|
@@ -578,7 +816,17 @@ export class SomaComponentInstance {
|
|
|
578
816
|
emit: (name, payload) => options.dispatchEmit(name, payload),
|
|
579
817
|
host: options.host,
|
|
580
818
|
services: options.services,
|
|
581
|
-
// 惰性求值:工厂执行时引擎尚未就绪,refresh
|
|
819
|
+
// 惰性求值:工厂执行时引擎尚未就绪,refresh 只在实例存活且引擎可用时生效。
|
|
820
|
+
//
|
|
821
|
+
// 它是**同步整树重渲**(rerender → renderEngine.render(),整张绑定表逐条过一遍),
|
|
822
|
+
// 而且必须留成同步的:调用方大量存在「refresh() 之后当场读 DOM / 移焦点」的写法
|
|
823
|
+
// (列表键盘导航、焦点归还、滚动定位),改成异步会让这些当场失焦。
|
|
824
|
+
//
|
|
825
|
+
// 代价因此落在调用方身上:**不要把它直接接到每帧都来的事件源上**(scroll / pointermove /
|
|
826
|
+
// 观察者回调)。滚动那条路的正确接法是先合帧、再判断这一帧的渲染输入到底变没变,
|
|
827
|
+
// 组件侧的共享实现在 soma-ui `shared/scroll-frame.ts`(rAF 合帧 + 窗口签名去重)。
|
|
828
|
+
// 引擎这一侧只做得到"算出来的值没变就不写 DOM"(见 render-engine 的 lastWritten),
|
|
829
|
+
// 它省的是 DOM 写入,省不掉"把每个 getter 重算一遍"——那一半只有调用方自己拦得住。
|
|
582
830
|
refresh: () => {
|
|
583
831
|
if (!this.destroyed && this.renderEngine) {
|
|
584
832
|
this.rerender();
|
|
@@ -617,6 +865,7 @@ export class SomaComponentInstance {
|
|
|
617
865
|
markChildEmitRoutes(options.host, this.manifest, options.registry, scopeId);
|
|
618
866
|
|
|
619
867
|
this.renderEngine = new SomaRenderEngine({
|
|
868
|
+
repeatPlans: options.def.definitionRepeatPlans,
|
|
620
869
|
container: options.host,
|
|
621
870
|
adapter,
|
|
622
871
|
manifest: this.manifest,
|
|
@@ -685,6 +934,94 @@ export class SomaComponentInstance {
|
|
|
685
934
|
}
|
|
686
935
|
}
|
|
687
936
|
|
|
937
|
+
/**
|
|
938
|
+
* 契约 mode: 'model' 的 prop:作用域根上的同名键换成访问器,接住实例内部的每一次改写。
|
|
939
|
+
*
|
|
940
|
+
* 为什么在作用域根上拦而不是在某个写入点上拦:实例内部改 prop 的路不止一条——
|
|
941
|
+
* 模板 `model: 'p.keyword'` 经渲染引擎 writeToProxy 直接赋值(作用域根是普通对象,没有代理,
|
|
942
|
+
* 写完没人知道),控制器也可以 `props.keyword = x`(契约 P3 允许 model prop 被组件改写)。
|
|
943
|
+
* 两条路也会串起来走:RailList 的搜索框绑的是 `model: 'ctrl.keyword'`,渲染引擎写控制器存取器,存取器再 `props.keyword = x`。
|
|
944
|
+
* 这些路最后都落在作用域根的这个键上,只有这里能一处接全。
|
|
945
|
+
*
|
|
946
|
+
* 不会回环、不会跳光标,靠的是三处去重:
|
|
947
|
+
* - 这里:与实例当前值相同的写入直接丢(文本控件 input / change / blur 会连发同一个值);
|
|
948
|
+
* - createModelWriter:与绑定路径上现有值相同就不写(写的可能是父实例作用域根这种普通对象,那里没有代理);
|
|
949
|
+
* - setProps 走 modelValues 直接落值、不经访问器:回写触发页面 watch → setProps 推回的正是
|
|
950
|
+
* 刚写的值,渲染引擎 writeModelValue 比对控件现值相同就不写 `.value`。
|
|
951
|
+
*
|
|
952
|
+
* 宿主内有输入法组合进行时不回写:组合期间的改写只落进实例(模板照常读到),组合结束后按实例当前值
|
|
953
|
+
* 回写一次。判断与页面 model 绑定是同一个 SomaCompositionGate(见 watchHostComposition)。
|
|
954
|
+
* 模板 `model:` 这条路在渲染引擎里已经按控件挡过一道;控制器的 input 处理器由实例事件引擎的 input 闸门挡过一道
|
|
955
|
+
* (组合期间不执行、组合结束补发一次,见 event-engine 的 attachInputComposition,补发时组合已结束,改写直接回写)。
|
|
956
|
+
* 这里接住的是其余在组合期间改写 prop 的路:非 input 声明的处理器、控制器在别的时机直接赋值。
|
|
957
|
+
*
|
|
958
|
+
* 其他 prop 保持普通数据属性,没有 model prop 的组件(绝大多数)作用域根一字不变,也不挂组合监听。
|
|
959
|
+
*/
|
|
960
|
+
private installModelProps(writeModel: SomaComponentInstanceOptions['writeModel']): void {
|
|
961
|
+
const modelProps = Object.entries(this.def.contract.props ?? {}).filter(([, prop]) => prop.mode === 'model');
|
|
962
|
+
if (modelProps.length === 0) return;
|
|
963
|
+
const composition = writeModel ? this.watchHostComposition(writeModel) : undefined;
|
|
964
|
+
for (const [propName] of modelProps) {
|
|
965
|
+
this.modelValues.set(propName, this.scopeRoot[propName]);
|
|
966
|
+
Object.defineProperty(this.scopeRoot, propName, {
|
|
967
|
+
enumerable: true,
|
|
968
|
+
configurable: true,
|
|
969
|
+
get: () => this.modelValues.get(propName),
|
|
970
|
+
set: (value: unknown) => {
|
|
971
|
+
if (Object.is(value, this.modelValues.get(propName))) return;
|
|
972
|
+
this.modelValues.set(propName, value);
|
|
973
|
+
if (this.destroyed) return;
|
|
974
|
+
if (composition?.holds()) {
|
|
975
|
+
this.heldModelProps.add(propName);
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
this.heldModelProps.delete(propName);
|
|
979
|
+
writeModel?.(propName, value);
|
|
980
|
+
},
|
|
981
|
+
});
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
/**
|
|
986
|
+
* 宿主上的输入法组合闸门:监听挂在捕获阶段,组合事件先经过宿主、闸门状态先更新,
|
|
987
|
+
* 模板控件与控制器的监听随后改写 prop 时按更新后的状态判断。
|
|
988
|
+
* 组合结束后的补写:组合期间被改写的 model prop 按实例当前值各回写一次(与绑定路径上的值相同时 createModelWriter 不写)。
|
|
989
|
+
*/
|
|
990
|
+
private watchHostComposition(writeModel: NonNullable<SomaComponentInstanceOptions['writeModel']>): SomaCompositionGate {
|
|
991
|
+
const signal = this.modelAbort.signal;
|
|
992
|
+
return new SomaCompositionGate(
|
|
993
|
+
(type, listener) => this.host.addEventListener(type, listener, { capture: true, signal }),
|
|
994
|
+
signal,
|
|
995
|
+
() => {
|
|
996
|
+
for (const propName of [...this.heldModelProps]) {
|
|
997
|
+
this.heldModelProps.delete(propName);
|
|
998
|
+
if (!this.destroyed) writeModel(propName, this.modelValues.get(propName));
|
|
999
|
+
}
|
|
1000
|
+
},
|
|
1001
|
+
);
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
/**
|
|
1005
|
+
* 子组件的 model 回写落到本实例作用域(createChildReader 的 write)。
|
|
1006
|
+
*
|
|
1007
|
+
* 可写的只有本实例自己持有的东西:`p.*`(作用域根;若它本身也是 model prop,
|
|
1008
|
+
* 访问器会接着往上一层回写)与 `ctrl.*`(控制器字段)。行变量段读的是行数据,拒收。
|
|
1009
|
+
* 写进去之后整树重渲——本实例的绑定与静态子组件的 props 都要看到新值。
|
|
1010
|
+
*/
|
|
1011
|
+
writeBoundPath(path: string, value: unknown, rowData?: unknown): string | null {
|
|
1012
|
+
if (this.destroyed) return null;
|
|
1013
|
+
const first = path.split('.')[0];
|
|
1014
|
+
const ownRoot = first === SOMA_COMPONENT_CTRL_KEY || this.resolver.getVarPath(first) === '';
|
|
1015
|
+
if (!ownRoot || (rowData instanceof SomaRowScope && rowData.has(first))) {
|
|
1016
|
+
return rowVariableRefusal(first);
|
|
1017
|
+
}
|
|
1018
|
+
const refused = assignBoundPath(this.scopeRoot, this.resolver.resolvePath(path).split('.'), value);
|
|
1019
|
+
if (refused === null) {
|
|
1020
|
+
this.rerender();
|
|
1021
|
+
}
|
|
1022
|
+
return refused;
|
|
1023
|
+
}
|
|
1024
|
+
|
|
688
1025
|
/**
|
|
689
1026
|
* 整树重渲 + 把新的 props 推给模板里静态嵌套的子组件。
|
|
690
1027
|
*
|
|
@@ -725,10 +1062,16 @@ export class SomaComponentInstance {
|
|
|
725
1062
|
if (this.destroyed) {
|
|
726
1063
|
return;
|
|
727
1064
|
}
|
|
1065
|
+
assertPreparedStructureProps(this.host, next);
|
|
728
1066
|
for (const [key, value] of Object.entries(next)) {
|
|
729
1067
|
if (key === SOMA_COMPONENT_CTRL_KEY) {
|
|
730
1068
|
continue;
|
|
731
1069
|
}
|
|
1070
|
+
// model prop 的下行值直接落进 modelValues:经访问器会被当成实例内改写再回写一遍
|
|
1071
|
+
if (this.modelValues.has(key)) {
|
|
1072
|
+
this.modelValues.set(key, value);
|
|
1073
|
+
continue;
|
|
1074
|
+
}
|
|
732
1075
|
this.scopeRoot[key] = value;
|
|
733
1076
|
}
|
|
734
1077
|
// 特性开关可能是这一帧才翻真的(页面把 batch_actions 从空数组换成两条)。
|
|
@@ -751,8 +1094,16 @@ export class SomaComponentInstance {
|
|
|
751
1094
|
return this.scopeRoot;
|
|
752
1095
|
}
|
|
753
1096
|
|
|
1097
|
+
/** Internal lifecycle ordering: children must release while their DOM is still attached. */
|
|
1098
|
+
getHost(): HTMLElement {
|
|
1099
|
+
return this.host;
|
|
1100
|
+
}
|
|
1101
|
+
|
|
754
1102
|
/** 父实例作用域读取;rowData 存在时与列表行内组件的求值语义一致。 */
|
|
755
1103
|
read(path: string, rowData?: unknown): unknown {
|
|
1104
|
+
if (rowData instanceof SomaRowScope) {
|
|
1105
|
+
return rowData.read(path, value => this.resolver.readFromRoot(this.resolver.resolvePath(value)));
|
|
1106
|
+
}
|
|
756
1107
|
const segments = path.split('.');
|
|
757
1108
|
const first = segments[0];
|
|
758
1109
|
if (rowData !== undefined && this.resolver.hasVar(first) && first !== 'p' && first !== 'ctrl') {
|
|
@@ -775,7 +1126,7 @@ export class SomaComponentInstance {
|
|
|
775
1126
|
// 于是父组件无法把派生值喂给模板内的子组件,"用组件搭组件"看着能跑却是坏的
|
|
776
1127
|
// ——293 个 soma-ui 组件里只有 1 个敢嵌套,多个组件的文件头把这归因于"引擎不支持"。
|
|
777
1128
|
//
|
|
778
|
-
// 唯一调用方是子组件 reader(createChildReader),影响面止于子组件 props
|
|
1129
|
+
// 唯一调用方是子组件 reader(createChildReader),影响面止于子组件 props 求值与 model 回写的路径判定。
|
|
779
1130
|
if (segment === SOMA_COMPONENT_CTRL_KEY) return true;
|
|
780
1131
|
return this.resolver.hasVar(segment);
|
|
781
1132
|
}
|
|
@@ -808,6 +1159,8 @@ export class SomaComponentInstance {
|
|
|
808
1159
|
this.renderEngine.setComponentHook(null);
|
|
809
1160
|
this.renderEngine.setComponentReleaseHook(null);
|
|
810
1161
|
this.renderEngine.destroy();
|
|
1162
|
+
this.modelAbort.abort();
|
|
1163
|
+
this.heldModelProps.clear();
|
|
811
1164
|
this.staticChildren.length = 0;
|
|
812
1165
|
}
|
|
813
1166
|
}
|
|
@@ -816,14 +1169,26 @@ export class SomaComponentInstance {
|
|
|
816
1169
|
|
|
817
1170
|
/**
|
|
818
1171
|
* prop 声明值求值规则(DSL 扩展规范第 2 章):
|
|
819
|
-
* -
|
|
1172
|
+
* - 字符串:`!值` → 其余部分求值后布尔取反;`路径 | 管道 [| 管道…]` 且首段是已注册 var → 管道绑定
|
|
1173
|
+
* (见 resolvePipedProp);首段是已注册 var → 绑定路径;否则字面量字符串
|
|
820
1174
|
* - number / boolean → 字面量
|
|
821
1175
|
* - 数组 / 普通对象 → 逐项递归
|
|
1176
|
+
*
|
|
1177
|
+
* `propName` 只用于告警点名,嵌套项带上下标 / 键(`columns[0].label`)。
|
|
822
1178
|
*/
|
|
823
|
-
function resolvePropValue(value: unknown, reader: ScopeReader): unknown {
|
|
1179
|
+
function resolvePropValue(value: unknown, reader: ScopeReader, site: PropPipeSite, propName: string): unknown {
|
|
824
1180
|
if (typeof value === 'string') {
|
|
825
|
-
|
|
826
|
-
|
|
1181
|
+
const negate = value.startsWith('!');
|
|
1182
|
+
const raw = negate ? value.slice(1) : value;
|
|
1183
|
+
if (isPipedBinding(raw, (seg) => reader.hasVar(seg))) {
|
|
1184
|
+
const piped = resolvePipedProp(raw, reader, site, propName, value);
|
|
1185
|
+
if (piped === PIPE_UNRESOLVED) {
|
|
1186
|
+
return undefined;
|
|
1187
|
+
}
|
|
1188
|
+
return negate ? !piped : piped;
|
|
1189
|
+
}
|
|
1190
|
+
if (negate) {
|
|
1191
|
+
return !reader.read(raw);
|
|
827
1192
|
}
|
|
828
1193
|
if (reader.hasVar(value.split('.')[0])) {
|
|
829
1194
|
return reader.read(value);
|
|
@@ -831,18 +1196,116 @@ function resolvePropValue(value: unknown, reader: ScopeReader): unknown {
|
|
|
831
1196
|
return value;
|
|
832
1197
|
}
|
|
833
1198
|
if (Array.isArray(value)) {
|
|
834
|
-
return value.map((item) => resolvePropValue(item, reader));
|
|
1199
|
+
return value.map((item, index) => resolvePropValue(item, reader, site, `${propName}[${index}]`));
|
|
835
1200
|
}
|
|
836
1201
|
if (value !== null && typeof value === 'object') {
|
|
837
1202
|
const result: Record<string, unknown> = {};
|
|
838
1203
|
for (const [key, item] of Object.entries(value)) {
|
|
839
|
-
result[key] = resolvePropValue(item, reader);
|
|
1204
|
+
result[key] = resolvePropValue(item, reader, site, `${propName}.${key}`);
|
|
840
1205
|
}
|
|
841
1206
|
return result;
|
|
842
1207
|
}
|
|
843
1208
|
return value;
|
|
844
1209
|
}
|
|
845
1210
|
|
|
1211
|
+
/**
|
|
1212
|
+
* 声明值是不是管道绑定:带 ` | ` 且管道前那段的首段是作用域变量。
|
|
1213
|
+
* 首段不是变量的整串是字面量("是 | 否" 这类文案),不进管道解析——解析器对它会报管道段格式错误。
|
|
1214
|
+
* 求值(resolvePropValue)与 watch 收集(collectBindingPaths)共用这一个判定,两边不会各认各的。
|
|
1215
|
+
*/
|
|
1216
|
+
function isPipedBinding(raw: string, hasVar: (seg: string) => boolean): boolean {
|
|
1217
|
+
return hasPipes(raw) && hasVar(raw.slice(0, raw.indexOf(' | ')).trim().split('.')[0]);
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
/** 管道前那段的形状:作用域变量段 + 零到多段字段 */
|
|
1221
|
+
const PIPE_BASE_PATH = /^[A-Za-z_$][\w$]*(\.[\w$]+)*$/;
|
|
1222
|
+
|
|
1223
|
+
/**
|
|
1224
|
+
* 解析管道绑定(与页面绑定同一个 parsePipedValue、同一份解析缓存)。管道段格式错误由解析器抛出;
|
|
1225
|
+
* 管道前那段不是数据路径(`page.price * page.count | money` 这类算术段)也在这里抛:运行时读不出算术段,
|
|
1226
|
+
* 不拦的话它读成 undefined 再过管道,prop 静默拿到空串。页面绑定对同一写法在挂载时由契约校验报"路径不存在",
|
|
1227
|
+
* props 声明不经那道校验。
|
|
1228
|
+
*/
|
|
1229
|
+
function parsePipedProp(raw: string, hasVar: (seg: string) => boolean): ParsedPipedValue {
|
|
1230
|
+
const parsed = parsePipedValue(raw, hasVar);
|
|
1231
|
+
if (!PIPE_BASE_PATH.test(parsed.basePath)) {
|
|
1232
|
+
throw new Error(`管道前那段 ${JSON.stringify(parsed.basePath)} 不是数据路径(运行时只认"路径 | 管道")`);
|
|
1233
|
+
}
|
|
1234
|
+
return parsed;
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
/** 管道绑定求值失败的标记(区别于管道正常算出的任何值) */
|
|
1238
|
+
const PIPE_UNRESOLVED = Symbol('pipe-unresolved');
|
|
1239
|
+
|
|
1240
|
+
/**
|
|
1241
|
+
* 管道绑定求值,与页面 text / @属性 绑定走同一套:parsePipedValue 解析(经 parsePipedProp)、注册表 assertKnown 白名单、
|
|
1242
|
+
* 从左到右链式执行。基础路径与管道里的绑定型实参都经当前作用域读取器读
|
|
1243
|
+
* (页面路径、行变量、父实例的 p.* / ctrl.* 都认),locale 取同一作用域根上的 `_locale`。
|
|
1244
|
+
* 算出来的与页面绑定一样是展示字符串。
|
|
1245
|
+
*
|
|
1246
|
+
* 页面绑定对未注册管道在编译期 fail fast;props 声明不经编译器,只能在这里发现。
|
|
1247
|
+
* 这里不抛:一个 prop 写错不该让整个组件挂载失败(行内实例会让整张表失败),
|
|
1248
|
+
* 管道段格式错误、管道前不是数据路径、管道未注册、管道函数执行时抛错 → 告警点名实例锚点与 prop,
|
|
1249
|
+
* 返回 PIPE_UNRESOLVED,该 prop 按 undefined 下发。
|
|
1250
|
+
* 执行走 registry.tryApply 而不是 apply:apply 自己接住管道抛错、告警 `[soma-core][管道] 管道 "x" 执行失败`
|
|
1251
|
+
* 并降级为原值字符串(页面绑定用的是这一套),调用方分不出失败,prop 会拿到未经格式化的原值,
|
|
1252
|
+
* 告警也不带锚点与 prop、每次重算都打一遍。tryApply 把失败交回来,由这里按上面的口径告警一次。
|
|
1253
|
+
*/
|
|
1254
|
+
function resolvePipedProp(
|
|
1255
|
+
raw: string,
|
|
1256
|
+
reader: ScopeReader,
|
|
1257
|
+
site: PropPipeSite,
|
|
1258
|
+
propName: string,
|
|
1259
|
+
// 告警里点名的声明原文。取反声明的 `!` 在 resolvePropValue 里已经切掉了(求值要的是 `!` 之后那段),
|
|
1260
|
+
// 只传 raw 会把 "!page.kw | explode" 报成 "page.kw | explode",照着告警回去搜 _ui.yaml 搜不到那一行。
|
|
1261
|
+
decl: string = raw,
|
|
1262
|
+
): unknown {
|
|
1263
|
+
let parsed: ParsedPipedValue;
|
|
1264
|
+
try {
|
|
1265
|
+
parsed = parsePipedProp(raw, (seg) => reader.hasVar(seg));
|
|
1266
|
+
site.pipes.assertKnown(parsed.pipes, '组件 props');
|
|
1267
|
+
} catch (error) {
|
|
1268
|
+
warnUnresolvedPropPipe(site, propName, decl, error);
|
|
1269
|
+
return PIPE_UNRESOLVED;
|
|
1270
|
+
}
|
|
1271
|
+
const result = site.pipes.tryApply(reader.read(parsed.basePath), parsed.pipes, {
|
|
1272
|
+
read: (path) => reader.read(path),
|
|
1273
|
+
locale: () => reader.locale(),
|
|
1274
|
+
});
|
|
1275
|
+
if (!result.ok) {
|
|
1276
|
+
warnUnresolvedPropPipe(site, propName, decl, result.error, result.pipeName);
|
|
1277
|
+
return PIPE_UNRESOLVED;
|
|
1278
|
+
}
|
|
1279
|
+
return result.value;
|
|
1280
|
+
}
|
|
1281
|
+
|
|
1282
|
+
/**
|
|
1283
|
+
* props 管道无法求值的告警:点名组件、实例锚点、prop 与声明原文;同一锚点同一 prop 同一声明只告警一次。
|
|
1284
|
+
* `failedPipe` 有值表示管道函数执行时抛错,`error` 是它抛出的原始异常,追加为第二个实参(控制台可展开堆栈);
|
|
1285
|
+
* 没有值表示解析或白名单阶段失败,异常消息已经说清原因,只打一段文本。
|
|
1286
|
+
*/
|
|
1287
|
+
function warnUnresolvedPropPipe(
|
|
1288
|
+
site: PropPipeSite,
|
|
1289
|
+
propName: string,
|
|
1290
|
+
raw: string,
|
|
1291
|
+
error: unknown,
|
|
1292
|
+
failedPipe?: string,
|
|
1293
|
+
): void {
|
|
1294
|
+
const key = `${site.instanceId}\u0000${propName}\u0000${raw}`;
|
|
1295
|
+
if (site.warned.has(key)) {
|
|
1296
|
+
return;
|
|
1297
|
+
}
|
|
1298
|
+
site.warned.add(key);
|
|
1299
|
+
// SomaPipeError 的消息自带 [soma-core][管道] 前缀,拼进本条告警前去掉,免得一行两个前缀
|
|
1300
|
+
const reason = error instanceof Error ? error.message.replace(/^\[soma-core\]\[管道\]\s*/, '') : String(error);
|
|
1301
|
+
const detail = failedPipe === undefined ? reason : `管道 "${failedPipe}" 执行失败(${reason})`;
|
|
1302
|
+
console.warn(
|
|
1303
|
+
`[soma-core][组件] 组件 "${site.componentName}" 实例 "${site.instanceId}" 的 prop "${propName}" `
|
|
1304
|
+
+ `绑定为 ${JSON.stringify(raw)},管道无法求值:${detail}。该 prop 按 undefined 下发,组件照常挂载`,
|
|
1305
|
+
...(failedPipe === undefined ? [] : [error]),
|
|
1306
|
+
);
|
|
1307
|
+
}
|
|
1308
|
+
|
|
846
1309
|
/**
|
|
847
1310
|
* 契约默认值下发前的隔离拷贝。
|
|
848
1311
|
*
|
|
@@ -886,7 +1349,8 @@ function resolveProps(
|
|
|
886
1349
|
propsDecl: Record<string, unknown>,
|
|
887
1350
|
reader: ScopeReader,
|
|
888
1351
|
def: SomaComponentDefinition,
|
|
889
|
-
hostDefaults
|
|
1352
|
+
hostDefaults: Record<string, unknown> | undefined,
|
|
1353
|
+
site: PropPipeSite,
|
|
890
1354
|
): Record<string, unknown> {
|
|
891
1355
|
const result: Record<string, unknown> = {};
|
|
892
1356
|
|
|
@@ -905,11 +1369,135 @@ function resolveProps(
|
|
|
905
1369
|
}
|
|
906
1370
|
|
|
907
1371
|
for (const [propName, declValue] of Object.entries(propsDecl)) {
|
|
908
|
-
|
|
1372
|
+
const field = def.contract.props?.[propName];
|
|
1373
|
+
// Closed enum tokens are literals even when named page/p/ctrl. A path
|
|
1374
|
+
// remains a path only when it is not one of the declared tokens.
|
|
1375
|
+
result[propName] = field?.type === 'enum' && field.values?.some(value => value === declValue)
|
|
1376
|
+
? declValue : resolvePropValue(declValue, reader, site, propName);
|
|
909
1377
|
}
|
|
910
1378
|
return result;
|
|
911
1379
|
}
|
|
912
1380
|
|
|
1381
|
+
// ─── model 回写 ──────────────────────────────────────────────
|
|
1382
|
+
|
|
1383
|
+
/** 可写绑定路径的形状:作用域变量段 + 至少一段字段,逐段是标识符或数组下标 */
|
|
1384
|
+
const WRITABLE_BINDING_PATH = /^[A-Za-z_$][\w$]*(\.[\w$]+)+$/;
|
|
1385
|
+
/** 与 row-scope readRowPath 同一份拒收名单:沿这几段赋值就是改原型 */
|
|
1386
|
+
const UNSAFE_PATH_SEGMENTS: ReadonlySet<string> = new Set(['__proto__', 'prototype', 'constructor']);
|
|
1387
|
+
|
|
1388
|
+
/**
|
|
1389
|
+
* 一个实例的 model 回写函数:实例内改写了 model prop → 按 props 声明找到绑定路径 → 经读取器写回。
|
|
1390
|
+
*
|
|
1391
|
+
* 声明值与 resolveProps 同一套判定,能求值成路径的才可能可写;字面量、`!` 取反、带管道或运算的
|
|
1392
|
+
* 表达式都没有"源"可写。写不了时**不回写**,值只留在实例内,同一实例同一 prop 只告警一次
|
|
1393
|
+
* (键入会逐字符触发,告警刷屏等于没有告警)。
|
|
1394
|
+
* 页面没声明这个 prop 时静默:那是非受控用法,组件自己持有值,本来就不该回写。
|
|
1395
|
+
*/
|
|
1396
|
+
function createModelWriter(
|
|
1397
|
+
instanceId: string,
|
|
1398
|
+
def: SomaComponentDefinition,
|
|
1399
|
+
propsDecl: Record<string, unknown>,
|
|
1400
|
+
reader: ScopeReader,
|
|
1401
|
+
): (propName: string, value: unknown) => void {
|
|
1402
|
+
const warned = new Set<string>();
|
|
1403
|
+
return (propName, value) => {
|
|
1404
|
+
if (!Object.prototype.hasOwnProperty.call(propsDecl, propName)) {
|
|
1405
|
+
return;
|
|
1406
|
+
}
|
|
1407
|
+
const declValue = propsDecl[propName];
|
|
1408
|
+
const binding = modelBindingPath(declValue, def.contract.props?.[propName], reader);
|
|
1409
|
+
let refused: string | null;
|
|
1410
|
+
if ('reason' in binding) {
|
|
1411
|
+
refused = binding.reason;
|
|
1412
|
+
} else if (Object.is(reader.read(binding.path), value)) {
|
|
1413
|
+
return;
|
|
1414
|
+
} else {
|
|
1415
|
+
refused = reader.write(binding.path, value);
|
|
1416
|
+
}
|
|
1417
|
+
if (refused === null || warned.has(propName)) {
|
|
1418
|
+
return;
|
|
1419
|
+
}
|
|
1420
|
+
warned.add(propName);
|
|
1421
|
+
console.warn(
|
|
1422
|
+
`[soma-core][组件] 组件 "${def.contract.name}" 实例 "${instanceId}" 的双向 prop "${propName}" `
|
|
1423
|
+
+ `绑定为 ${JSON.stringify(declValue)},${refused}。组件内的改动只留在实例里,不回写;`
|
|
1424
|
+
+ '要双向请绑定到可写的数据路径(页面里写 page.字段,组件模板里写 p.字段 或 ctrl.字段)',
|
|
1425
|
+
);
|
|
1426
|
+
};
|
|
1427
|
+
}
|
|
1428
|
+
|
|
1429
|
+
function modelBindingPath(
|
|
1430
|
+
declValue: unknown,
|
|
1431
|
+
field: SomaContractProp | undefined,
|
|
1432
|
+
reader: ScopeReader,
|
|
1433
|
+
): { path: string } | { reason: string } {
|
|
1434
|
+
if (typeof declValue !== 'string') {
|
|
1435
|
+
return { reason: '这是字面量,没有可写的源' };
|
|
1436
|
+
}
|
|
1437
|
+
if (field?.type === 'enum' && field.values?.some(value => value === declValue)) {
|
|
1438
|
+
return { reason: '这是枚举字面量,没有可写的源' };
|
|
1439
|
+
}
|
|
1440
|
+
if (declValue.startsWith('!')) {
|
|
1441
|
+
return { reason: '取反表达式没有可写的源' };
|
|
1442
|
+
}
|
|
1443
|
+
const segments = declValue.split('.');
|
|
1444
|
+
if (!reader.hasVar(segments[0])) {
|
|
1445
|
+
return { reason: '首段不是作用域变量,按字面量字符串下发,没有可写的源' };
|
|
1446
|
+
}
|
|
1447
|
+
if (!WRITABLE_BINDING_PATH.test(declValue) || segments.some(seg => UNSAFE_PATH_SEGMENTS.has(seg))) {
|
|
1448
|
+
return { reason: '不是纯数据路径(带管道、运算、空白或非法段,或只有作用域变量本身)' };
|
|
1449
|
+
}
|
|
1450
|
+
return { path: declValue };
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
/** 行 / 嵌套作用域变量段的拒收原因(页面读取器与父实例读取器共用) */
|
|
1454
|
+
function rowVariableRefusal(varName: string): string {
|
|
1455
|
+
return `首段 "${varName}" 是列表行或嵌套作用域变量,行数据不在响应式代理里(数组靠整体替换触发更新),写进去上层不会知道`;
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
/**
|
|
1459
|
+
* 页面作用域写回:只收首段指向页面根的路径(`page.xxx`),经页面响应式代理赋值。
|
|
1460
|
+
* 代理的 set 触发 schedulePatch(页面绑定重渲)与 propertyWatcher(registerPropWatches 把值推回实例),
|
|
1461
|
+
* 与页面自己的 `model:` 绑定走的是同一条路。
|
|
1462
|
+
*/
|
|
1463
|
+
function writePagePath(resolver: SomaScopeResolver, path: string, value: unknown): string | null {
|
|
1464
|
+
const first = path.split('.')[0];
|
|
1465
|
+
if (resolver.getVarPath(first) !== '') {
|
|
1466
|
+
return rowVariableRefusal(first);
|
|
1467
|
+
}
|
|
1468
|
+
return assignBoundPath(resolver.readFromRoot(''), resolver.resolvePath(path).split('.'), value);
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
/**
|
|
1472
|
+
* 沿已解析路径逐段下钻后赋值。中间段不是对象、路径穿过数组、目标字段不可写时拒收,一个字节都不写。
|
|
1473
|
+
* 穿过数组要拒:代理不包数组,数组元素上的赋值不会通知任何人。
|
|
1474
|
+
*/
|
|
1475
|
+
function assignBoundPath(root: unknown, segments: string[], value: unknown): string | null {
|
|
1476
|
+
let owner = root;
|
|
1477
|
+
for (const seg of segments.slice(0, -1)) {
|
|
1478
|
+
if (Array.isArray(owner)) {
|
|
1479
|
+
return `路径在 "${seg}" 之前穿过了数组,数组元素不在响应式代理里(数组靠整体替换触发更新)`;
|
|
1480
|
+
}
|
|
1481
|
+
owner = (owner as Record<string, unknown> | null | undefined)?.[seg];
|
|
1482
|
+
if (owner === null || typeof owner !== 'object') {
|
|
1483
|
+
return `中间段 "${seg}" 当前不是对象,无处可写`;
|
|
1484
|
+
}
|
|
1485
|
+
}
|
|
1486
|
+
if (Array.isArray(owner)) {
|
|
1487
|
+
return '目标是数组元素,数组元素不在响应式代理里(数组靠整体替换触发更新)';
|
|
1488
|
+
}
|
|
1489
|
+
const key = segments[segments.length - 1];
|
|
1490
|
+
try {
|
|
1491
|
+
// Reflect.set 对只读 getter / 冻结对象返回 false 而不是抛错,不依赖所在模块是否严格模式
|
|
1492
|
+
if (!Reflect.set(owner as object, key, value)) {
|
|
1493
|
+
return `字段 "${key}" 不可写(只读 getter 或冻结对象)`;
|
|
1494
|
+
}
|
|
1495
|
+
} catch (error) {
|
|
1496
|
+
return `字段 "${key}" 赋值抛错:${error instanceof Error ? error.message : String(error)}`;
|
|
1497
|
+
}
|
|
1498
|
+
return null;
|
|
1499
|
+
}
|
|
1500
|
+
|
|
913
1501
|
/** 收集页面作用域(含 extended.show 嵌套,不进入 bind 行作用域)的 props 声明 */
|
|
914
1502
|
function collectPagePropsDecls(
|
|
915
1503
|
show: ShowMapping,
|
|
@@ -919,13 +1507,13 @@ function collectPagePropsDecls(
|
|
|
919
1507
|
if (typeof value === 'string' || value === null || typeof value !== 'object') {
|
|
920
1508
|
continue;
|
|
921
1509
|
}
|
|
922
|
-
if (isNested(value)) {
|
|
923
|
-
continue; // 行作用域内的 props 由渲染钩子按行处理
|
|
924
|
-
}
|
|
925
1510
|
const extended = value as ExtendedShowValue;
|
|
926
1511
|
if (extended.props) {
|
|
927
1512
|
into.set(rawKey.split('@')[0], extended.props);
|
|
928
1513
|
}
|
|
1514
|
+
if (isNested(value)) {
|
|
1515
|
+
continue; // Own props belong to the consumer; child show belongs to its rows.
|
|
1516
|
+
}
|
|
929
1517
|
if (extended.show) {
|
|
930
1518
|
collectPagePropsDecls(extended.show, into);
|
|
931
1519
|
}
|
|
@@ -933,7 +1521,11 @@ function collectPagePropsDecls(
|
|
|
933
1521
|
return into;
|
|
934
1522
|
}
|
|
935
1523
|
|
|
936
|
-
/**
|
|
1524
|
+
/**
|
|
1525
|
+
* 递归收集 props 声明里的绑定路径(供 watch 注册)。
|
|
1526
|
+
* 管道绑定收的是管道前那段路径与绑定型实参(与页面绑定的 watchPaths 同口径)——整串不是路径,
|
|
1527
|
+
* watch 它永远不会触发,prop 就停在首帧值。parsePipedProp 拒收的声明求值恒为 undefined,不必 watch。
|
|
1528
|
+
*/
|
|
937
1529
|
function collectBindingPaths(
|
|
938
1530
|
value: unknown,
|
|
939
1531
|
resolver: SomaScopeResolver,
|
|
@@ -941,7 +1533,21 @@ function collectBindingPaths(
|
|
|
941
1533
|
): void {
|
|
942
1534
|
if (typeof value === 'string') {
|
|
943
1535
|
const raw = value.startsWith('!') ? value.slice(1) : value;
|
|
944
|
-
|
|
1536
|
+
const hasVar = (seg: string) => resolver.hasVar(seg);
|
|
1537
|
+
if (isPipedBinding(raw, hasVar)) {
|
|
1538
|
+
let parsed: ParsedPipedValue;
|
|
1539
|
+
try {
|
|
1540
|
+
parsed = parsePipedProp(raw, hasVar);
|
|
1541
|
+
} catch {
|
|
1542
|
+
return;
|
|
1543
|
+
}
|
|
1544
|
+
into.add(parsed.basePath);
|
|
1545
|
+
for (const argPath of parsed.argPaths) {
|
|
1546
|
+
into.add(argPath);
|
|
1547
|
+
}
|
|
1548
|
+
return;
|
|
1549
|
+
}
|
|
1550
|
+
if (hasVar(raw.split('.')[0])) {
|
|
945
1551
|
into.add(raw);
|
|
946
1552
|
}
|
|
947
1553
|
return;
|
|
@@ -973,6 +1579,236 @@ function collectComponentHosts(root: ParentNode, into: Map<string, string>): voi
|
|
|
973
1579
|
});
|
|
974
1580
|
}
|
|
975
1581
|
|
|
1582
|
+
const HOVER_ALTERNATIVE = '悬停样式用 Style transitions 的 hover;悬停才出现的提示或面板用 click、focusin / focusout,或 Tooltip 组件';
|
|
1583
|
+
const CONTROLLER_ALTERNATIVE = '封装成组件,由控制器把监听直接挂到元素上,再经契约 emit 上报';
|
|
1584
|
+
|
|
1585
|
+
interface UnlistedDomEventHint {
|
|
1586
|
+
bubbles: boolean;
|
|
1587
|
+
behavior: string;
|
|
1588
|
+
alternative: string;
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
function hintGroup(
|
|
1592
|
+
names: readonly string[],
|
|
1593
|
+
bubbles: boolean,
|
|
1594
|
+
behavior: string,
|
|
1595
|
+
alternative: string,
|
|
1596
|
+
): [string, UnlistedDomEventHint][] {
|
|
1597
|
+
return names.map((name) => [name, { bubbles, behavior, alternative }]);
|
|
1598
|
+
}
|
|
1599
|
+
|
|
1600
|
+
/**
|
|
1601
|
+
* 常见但不在支持的 DOM 事件里的事件名 → 原生委托下的表现与替代写法。
|
|
1602
|
+
*
|
|
1603
|
+
* 文案与 pipeline-parser 的 UNLISTED_DOM_EVENT_HINTS 逐条一致(那张表没有导出):
|
|
1604
|
+
* `${behavior}。替代写法:${alternative}` 拼起来就是解析期告警里的那段。拆成两段,是因为
|
|
1605
|
+
* 组件实例上只讲表现——替代写法是改订阅契约 emit,不是这里的写法。解析期那张表增删名字时同步改这里。
|
|
1606
|
+
*/
|
|
1607
|
+
const UNLISTED_DOM_EVENT_HINTS: ReadonlyMap<string, UnlistedDomEventHint> = new Map([
|
|
1608
|
+
...hintGroup(['mouseover', 'mouseout', 'pointerover', 'pointerout'], true,
|
|
1609
|
+
'会冒泡,但指针在锚点内的子元素之间移动时会反复触发', HOVER_ALTERNATIVE),
|
|
1610
|
+
...hintGroup(['pointerenter', 'pointerleave'], false,
|
|
1611
|
+
'不冒泡,内层锚点上的声明永远不会执行', HOVER_ALTERNATIVE),
|
|
1612
|
+
...hintGroup(['mousedown', 'mouseup', 'pointerdown', 'pointerup', 'touchstart', 'touchend'], true,
|
|
1613
|
+
'会冒泡,能触发', `改用 click;确实要区分按下与抬起时${CONTROLLER_ALTERNATIVE}`),
|
|
1614
|
+
...hintGroup(['mousemove', 'pointermove', 'touchmove', 'wheel'], true,
|
|
1615
|
+
'会冒泡,每次派发都执行一遍 Pipeline', CONTROLLER_ALTERNATIVE),
|
|
1616
|
+
...hintGroup(['contextmenu', 'auxclick', 'pointercancel', 'touchcancel', 'dragstart', 'dragend',
|
|
1617
|
+
'dragenter', 'dragleave', 'dragover', 'animationend', 'transitionend'], true,
|
|
1618
|
+
'会冒泡,能触发', CONTROLLER_ALTERNATIVE),
|
|
1619
|
+
...hintGroup(['scrollend'], false,
|
|
1620
|
+
'在元素上不冒泡,内层锚点上的声明永远不会执行', CONTROLLER_ALTERNATIVE),
|
|
1621
|
+
...hintGroup(['keypress'], true, '会冒泡,能触发,但已被浏览器弃用', '改用 keydown'),
|
|
1622
|
+
...hintGroup(['beforeinput', 'compositionstart', 'compositionupdate', 'compositionend'], true,
|
|
1623
|
+
'会冒泡,能触发', '改用 input'),
|
|
1624
|
+
]);
|
|
1625
|
+
|
|
1626
|
+
/**
|
|
1627
|
+
* 支持的 DOM 事件里不冒泡的几个(见 pipeline-parser VALID_DOM_EVENTS 的注释):
|
|
1628
|
+
* 事件引擎只在页面容器上挂委托监听,宿主或内层锚点上的这类声明永远收不到。
|
|
1629
|
+
*/
|
|
1630
|
+
const LISTED_NON_BUBBLING_DOM_EVENTS: ReadonlySet<string> = new Set(['mouseenter', 'mouseleave', 'scroll']);
|
|
1631
|
+
|
|
1632
|
+
const NATIVE_EVENT_ARG = ',处理器第二实参是原生 Event,不是 emit payload';
|
|
1633
|
+
|
|
1634
|
+
/**
|
|
1635
|
+
* 激活期告警:页面事件清单里没走上 emit 路由、也不是普通锚点上合法 DOM 订阅的条目。
|
|
1636
|
+
*
|
|
1637
|
+
* 没匹配上契约 emit 的订阅不走 emit 路由,事件引擎按事件名原样在页面容器上挂原生委托——
|
|
1638
|
+
* 同名原生事件冒泡上来照样执行,并不是"永远不会触发"。这里手上有宿主表和注册表,
|
|
1639
|
+
* 锚点类型是确定的,按锚点类型分开说:
|
|
1640
|
+
* - 组件实例、契约里没有这个名字(白名单 DOM 事件名也算):组件交互只能走契约 emit,
|
|
1641
|
+
* 控制器 emit 的意图到不了这条订阅;能让它执行的只有组件内部冒泡到宿主的原生事件。
|
|
1642
|
+
* - 普通锚点、事件名不在支持的 DOM 事件里:讲原生委托下的实际表现与替代写法。
|
|
1643
|
+
* 合法 emit、普通锚点上的白名单 DOM 事件不告警。同一订阅(按键修饰符的多条候选)只告警一次。
|
|
1644
|
+
*
|
|
1645
|
+
* 与解析期告警(pipeline-parser)的分工:解析期看不到注册表和页面 HTML,只对 UNLISTED_DOM_EVENT_HINTS
|
|
1646
|
+
* 里的名字打一条"分不清是不是组件实例"的条件告警。两边同时命中时这里仍给完整说明,末尾注明锚点类型
|
|
1647
|
+
* 以本条为准,而不是只补一句"已确认":预编译模式下解析发生在构建期,浏览器控制台里只有本条。
|
|
1648
|
+
*
|
|
1649
|
+
* `registry` 为 null 表示本页与 Host 一个组件都没注册(组件管理器根本没建,
|
|
1650
|
+
* 见 warnSubscriptionsWithoutComponents):那时每个锚点都是普通锚点,宿主上写着组件名的那些
|
|
1651
|
+
* 连实例都不会有,两种都由这里按锚点类型点名。
|
|
1652
|
+
*
|
|
1653
|
+
* 锚点一共四档,四档都要有话说——少一档就是一条静默的订阅:
|
|
1654
|
+
* ① 普通锚点(宿主上没有 data-soma-component) → plainAnchorDetail
|
|
1655
|
+
* ② 整页零注册 + 宿主写着组件名(registry === null) → unregisteredHostDetail
|
|
1656
|
+
* ③ 组件实例,但契约没有这个 emit → instanceAnchorDetail
|
|
1657
|
+
* ④ 注册了别的组件、唯独没有这一个 → unregisteredOnPageDetail(2026-09-20 补)
|
|
1658
|
+
* ④ 此前落在一个没有 else 的 if 链末尾,detail 停在 undefined,整条订阅一声不吭。
|
|
1659
|
+
*/
|
|
1660
|
+
function warnUnroutedSubscriptions(
|
|
1661
|
+
manifest: SomaRuntimeManifest,
|
|
1662
|
+
hosts: ReadonlyMap<string, string>,
|
|
1663
|
+
registry: SomaComponentRegistry | null,
|
|
1664
|
+
): void {
|
|
1665
|
+
const warned = new Set<string>();
|
|
1666
|
+
for (const event of manifest.events) {
|
|
1667
|
+
const name = event.domEvent;
|
|
1668
|
+
const selector = `${event.somaId}.${name}`;
|
|
1669
|
+
if (warned.has(selector)) {
|
|
1670
|
+
continue;
|
|
1671
|
+
}
|
|
1672
|
+
const componentName = hosts.get(event.somaId);
|
|
1673
|
+
let detail: string | undefined;
|
|
1674
|
+
if (componentName === undefined) {
|
|
1675
|
+
detail = isKnownDomEvent(name) ? undefined : plainAnchorDetail(event.somaId, name);
|
|
1676
|
+
} else if (registry === null) {
|
|
1677
|
+
detail = isKnownDomEvent(name) ? undefined : unregisteredHostDetail(event.somaId, componentName, name);
|
|
1678
|
+
} else if (registry.has(componentName)) {
|
|
1679
|
+
const emits = Object.keys(registry.get(componentName).contract.emits ?? {});
|
|
1680
|
+
detail = emits.includes(name) ? undefined : instanceAnchorDetail(event.somaId, componentName, name, emits);
|
|
1681
|
+
} else {
|
|
1682
|
+
// 第三档(2026-09-20 补):本页注册了别的组件,**唯独没有这一个**。
|
|
1683
|
+
// 此前这一支 detail 停在 undefined,这条订阅整条静默——而它与上面两档一样,
|
|
1684
|
+
// 都是"锚点不会成为组件实例",只是原因不同。锚点类型在这里同样是**确定的**
|
|
1685
|
+
// (注册表就在手上,查得到"没有"),所以文案不留余地。
|
|
1686
|
+
detail = isKnownDomEvent(name) ? undefined : unregisteredOnPageDetail(event.somaId, componentName, name);
|
|
1687
|
+
}
|
|
1688
|
+
if (detail === undefined) {
|
|
1689
|
+
continue;
|
|
1690
|
+
}
|
|
1691
|
+
warned.add(selector);
|
|
1692
|
+
const confirmed = UNLISTED_DOM_EVENT_HINTS.has(name)
|
|
1693
|
+
? '(解析期对这条订阅有一条分不清锚点类型的告警,锚点类型以本条为准)'
|
|
1694
|
+
: '';
|
|
1695
|
+
console.warn(`[soma-core][组件] 事件订阅 "${selector}":${detail}${confirmed}`);
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
/**
|
|
1700
|
+
* 没有注册任何组件的页面(Host 与页面级资产都是空的)的事件名告警。
|
|
1701
|
+
*
|
|
1702
|
+
* 这类页面走不到 markEmitRoutes:组件管理器根本不建(soma-bootstrap 的 setupComponentManager 在 defs 为空时提前返回),
|
|
1703
|
+
* 于是 `card.reach_limit` 这种笔误全程静默——解析期那条只认 UNLISTED_DOM_EVENT_HINTS 里的 33 个
|
|
1704
|
+
* DOM 专有名字,snake_case 的假 emit 名一个都不在其中,而运行时这一侧又整个缺席。
|
|
1705
|
+
*
|
|
1706
|
+
* 为一条告警去建整个组件管理器不值当(展开模板、建注册表、挂行钩子,全是这类页面用不上的),
|
|
1707
|
+
* 这里只做管理器那一步真正需要的两件事:扫一遍宿主表(querySelectorAll,含 template 内容),
|
|
1708
|
+
* 再复用同一个 warnUnroutedSubscriptions。注册表传 null——本页确实一个组件定义都没有,
|
|
1709
|
+
* 这不是"查不到"而是"确定没有",锚点类型因此是确定的,文案不必留余地。
|
|
1710
|
+
*/
|
|
1711
|
+
export function warnSubscriptionsWithoutComponents(
|
|
1712
|
+
manifest: SomaRuntimeManifest,
|
|
1713
|
+
container: HTMLElement,
|
|
1714
|
+
): void {
|
|
1715
|
+
const hosts = new Map<string, string>();
|
|
1716
|
+
collectComponentHosts(container, hosts);
|
|
1717
|
+
warnUnroutedSubscriptions(manifest, hosts, null);
|
|
1718
|
+
}
|
|
1719
|
+
|
|
1720
|
+
/**
|
|
1721
|
+
* 宿主写着组件名,但**本页注册表里没有这一个**(其余组件都注册了)——此前这一档一声不吭。
|
|
1722
|
+
*
|
|
1723
|
+
* 与"本页零组件注册"那一档(warnSubscriptionsWithoutComponents)的区别只在组件管理器建没建;
|
|
1724
|
+
* 与"顶层宿主名字拼错"那一档的区别在于**够不够得到 registry.get()**:
|
|
1725
|
+
* · 顶层宿主(不在 template 里、没有 data-soma-child-id)在 prepare() 就走
|
|
1726
|
+
* `this.registry.get(name)`,未注册当场抛 SomaComponentRegistryError,**这一档是响的**;
|
|
1727
|
+
* · 而**封在 `<template>` 里的宿主**(行模板、插槽模板)prepare() 根本扫不到——
|
|
1728
|
+
* `querySelectorAll` 不下钻 template content,collectComponentHosts 才下钻。
|
|
1729
|
+
* 它们的实例化入口是 applyRowComponent / applyChildComponent,而那两处只对
|
|
1730
|
+
* **有 props 声明**的锚点触发(applyRowComponent 的第一句就是"props 声明的锚点")。
|
|
1731
|
+
* 于是"行模板里写了个没注册的组件名、又没给它写 props"这条路上:
|
|
1732
|
+
* prepare 不抛、实例不建、模板不注入、emit 不会有、控制台一个字都没有——
|
|
1733
|
+
* 页面上就是一个空壳 div。这正是本条要堵的形态。
|
|
1734
|
+
*
|
|
1735
|
+
* 为什么点名在 markEmitRoutes 而不是只在 warnUnroutedSubscriptions 里补一支:
|
|
1736
|
+
* 后者是**逐条订阅**说话的,零订阅的页面它一句都不会说;而"这个组件根本不会出现"
|
|
1737
|
+
* 这件事与有没有人订阅它的 emit 无关。两处各说各的那一半,都不重复。
|
|
1738
|
+
*
|
|
1739
|
+
* 已注册清单只印前 12 个:全量注册时它有三百多个名字,整串打出来会把真正的那行淹掉。
|
|
1740
|
+
*/
|
|
1741
|
+
function warnUnregisteredHosts(
|
|
1742
|
+
byName: ReadonlyMap<string, string[]>,
|
|
1743
|
+
registry: SomaComponentRegistry,
|
|
1744
|
+
): void {
|
|
1745
|
+
if (byName.size === 0) {
|
|
1746
|
+
return;
|
|
1747
|
+
}
|
|
1748
|
+
const all = registry.names();
|
|
1749
|
+
const shown = all.slice(0, 12).join(', ');
|
|
1750
|
+
const rest = all.length > 12 ? `…另有 ${all.length - 12} 个` : '';
|
|
1751
|
+
for (const [componentName, instanceIds] of byName) {
|
|
1752
|
+
console.warn(
|
|
1753
|
+
`[soma-core][组件] 宿主写着组件 "${componentName}"(锚点:${instanceIds.join('、')}),`
|
|
1754
|
+
+ '但本页注册表里没有它:模板不会注入、实例不会建、契约 emit 不会有人发,'
|
|
1755
|
+
+ `页面上留下的是一个空壳。本页已注册 ${all.length} 个:${shown}${rest}。`
|
|
1756
|
+
+ '修法:把它加进 bootstrap 的 components(自定义组件)或对应的 soma-ui 包,'
|
|
1757
|
+
+ '名字拼错的改回契约名——两者都不改的话,这处 DOM 永远只是个空 div。',
|
|
1758
|
+
);
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
/** 本页零组件注册,宿主上却写着组件名:实例不会建,契约 emit 不会有人发 */
|
|
1763
|
+
function unregisteredHostDetail(instanceId: string, componentName: string, name: string): string {
|
|
1764
|
+
return `${instanceId} 的宿主写着组件 "${componentName}",但本页与 Host 都没有注册组件定义,实例不会建;`
|
|
1765
|
+
+ `没有实例就没有契约 emit,这条订阅只剩引擎按事件名原样挂的原生委托——${nativeBehaviorOnHost(name)}`;
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
/**
|
|
1769
|
+
* 本页注册了别的组件、唯独没有这一个:实例不会建,契约 emit 不会有人发。
|
|
1770
|
+
* 与 unregisteredHostDetail 只差一句"为什么没有"——那一档是整页零注册,这一档是漏了/拼错了一个。
|
|
1771
|
+
* "这个组件根本不会出现"那一半由 warnUnregisteredHosts 单独点名(零订阅的页面也说得上话)。
|
|
1772
|
+
*/
|
|
1773
|
+
function unregisteredOnPageDetail(instanceId: string, componentName: string, name: string): string {
|
|
1774
|
+
return `${instanceId} 的宿主写着组件 "${componentName}",但本页注册表里没有它(其余组件已注册),实例不会建;`
|
|
1775
|
+
+ `没有实例就没有契约 emit,这条订阅只剩引擎按事件名原样挂的原生委托——${nativeBehaviorOnHost(name)}`;
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
function instanceAnchorDetail(instanceId: string, componentName: string, name: string, emits: string[]): string {
|
|
1779
|
+
const available = emits.length > 0 ? `可用 emit:${emits.join(', ')}` : '这个组件的契约没有声明任何 emit';
|
|
1780
|
+
return `${instanceId} 是组件 ${componentName} 的实例,契约没有 emit "${name}";组件交互只能走契约 emit(${available})。`
|
|
1781
|
+
+ `这条订阅不走 emit 路由,组件 emit 的意图到不了它,只剩引擎按事件名原样挂的原生委托——${nativeBehaviorOnHost(name)}`;
|
|
1782
|
+
}
|
|
1783
|
+
|
|
1784
|
+
/** 组件实例宿主上按原生事件名委托的实际表现 */
|
|
1785
|
+
function nativeBehaviorOnHost(name: string): string {
|
|
1786
|
+
if (LISTED_NON_BUBBLING_DOM_EVENTS.has(name)) {
|
|
1787
|
+
return `${name} 不冒泡,宿主上的声明永远不会执行`;
|
|
1788
|
+
}
|
|
1789
|
+
if (isKnownDomEvent(name)) {
|
|
1790
|
+
// 事件引擎把 focus / blur 改挂 focusin / focusout 委托
|
|
1791
|
+
const listened = name === 'focus' ? 'focusin' : name === 'blur' ? 'focusout' : name;
|
|
1792
|
+
return `组件内部的原生 ${listened} 冒泡到宿主时照样执行它${NATIVE_EVENT_ARG}`;
|
|
1793
|
+
}
|
|
1794
|
+
const hint = UNLISTED_DOM_EVENT_HINTS.get(name);
|
|
1795
|
+
if (hint) {
|
|
1796
|
+
return `${name} ${hint.behavior}${hint.bubbles ? NATIVE_EVENT_ARG : ''}`;
|
|
1797
|
+
}
|
|
1798
|
+
return `浏览器派发同名原生事件并冒泡到宿主时才会执行${NATIVE_EVENT_ARG};浏览器没有这个事件时永远不会触发(多为 emit 名拼错)`;
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1801
|
+
function plainAnchorDetail(somaId: string, name: string): string {
|
|
1802
|
+
const head = `${somaId} 是普通锚点(不是组件实例),${name} 不在支持的 DOM 事件里,`
|
|
1803
|
+
+ '不走 emit 路由,引擎按原生事件名原样委托——';
|
|
1804
|
+
const hint = UNLISTED_DOM_EVENT_HINTS.get(name);
|
|
1805
|
+
if (hint) {
|
|
1806
|
+
return `${head}${name} ${hint.behavior}。替代写法:${hint.alternative}`;
|
|
1807
|
+
}
|
|
1808
|
+
return `${head}浏览器有同名原生事件时,会冒泡的照样执行、不冒泡的永远不执行;没有这个事件时永远不会触发(多为拼写错误)。`
|
|
1809
|
+
+ `替代写法:改用支持的 DOM 事件(如 click、input、change、keydown);白名单外的交互${CONTROLLER_ALTERNATIVE}`;
|
|
1810
|
+
}
|
|
1811
|
+
|
|
976
1812
|
/**
|
|
977
1813
|
* 「本作用域自己声明的子组件宿主」选择器。
|
|
978
1814
|
* 结构展开会把整棵嵌套树摊进同一子树,只靠 data-soma-child-id 已分不清是谁的孩子;
|