soma-core 1.3.2 → 1.3.3
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 +42 -0
- package/app/SomaPageClient.tsx +26 -2
- package/app/[...path]/page.tsx +7 -6
- package/app/api/[...path]/route.ts +3 -3
- package/app/api/soma-registry/[...path]/route.ts +67 -0
- package/app/components/SomaHostPage.tsx +3 -8
- package/app/components/SomaPageClientBoundary.tsx +13 -0
- package/app/layout.tsx +6 -1
- package/app/page.tsx +3 -3
- package/app/soma/manifest/route.ts +1 -0
- package/app/soma/page-object/route.ts +1 -1
- package/core/build/compiler/full-manifest-builder.ts +6 -1
- package/core/build/compiler/rollup-plugin-soma.ts +6 -0
- package/core/build/compiler/soma-manifest-webpack-plugin.js +4 -0
- package/core/build/compiler/soma-precompile.js +3507 -2387
- package/core/build/compiler/soma-precompile.ts +31 -2
- package/core/build/compiler/soma-rspack-yaml-loader.js +3 -0
- package/core/build/compiler/soma-webpack-loader.js +7 -1
- package/core/build/compiler/vite-plugin-soma.ts +11 -1
- package/core/build/component/package-loader.ts +135 -0
- package/core/build/eject/apply-eject.ts +133 -0
- package/core/build/eject/plan-eject.ts +139 -0
- package/core/client/adaptive/flush-scheduler.ts +96 -0
- package/core/client/adaptive/hysteresis.ts +56 -0
- package/core/client/adaptive/index.ts +23 -0
- package/core/client/adaptive/observer-pool.ts +90 -0
- package/core/client/base/SomaPageRootBase.ts +50 -0
- package/core/client/compiler/adaptive-compile.ts +136 -0
- package/core/client/compiler/manifest-compiler.ts +155 -27
- package/core/client/component/ai-catalog.ts +262 -0
- package/core/client/component/component-definition.types.ts +65 -0
- package/core/client/component/component-expander.ts +119 -0
- package/core/client/component/component-manager.ts +933 -0
- package/core/client/component/component-pack.ts +271 -0
- package/core/client/component/component-registry.ts +71 -0
- package/core/client/component/pack-styles.ts +94 -0
- package/core/client/component/scoped-adapter.ts +85 -0
- package/core/client/component/verify-pack.ts +247 -0
- package/core/client/container/MiniProgramContainerAdapter.ts +15 -1
- package/core/client/container/ReactNativeContainerAdapter.ts +15 -1
- package/core/client/container/SomaContainerAdapter.ts +186 -180
- package/core/client/data/data-object-resolver.ts +417 -0
- package/core/client/diagnostics/SomaDiagnostics.ts +187 -186
- package/core/client/diagnostics/boot-timings.ts +152 -0
- package/core/client/engine/asset-resolver.ts +12 -2
- package/core/client/engine/dom-renderer.ts +235 -16
- package/core/client/engine/emit-event.ts +21 -0
- package/core/client/engine/event-engine.ts +34 -4
- package/core/client/engine/on-change-engine.ts +1 -1
- package/core/client/engine/property-watcher.ts +1 -1
- package/core/client/engine/reactive-proxy.ts +3 -3
- package/core/client/engine/render-engine.ts +39 -4
- package/core/client/engine/runtime-builder.ts +23 -0
- package/core/client/engine/soma-bootstrap.ts +359 -4
- package/core/client/engine/style-binder.ts +47 -1
- package/core/client/host-bridge/empty-host/data/README.md +17 -0
- package/core/client/observability/error-reporter.ts +173 -0
- package/core/client/parser/adaptive-parser.ts +264 -0
- package/core/client/parser/data-config-parser.ts +396 -0
- package/core/client/parser/pipeline-parser.ts +15 -3
- package/core/client/parser/style-config-parser.ts +65 -2
- package/core/client/parser/ui-config-parser.ts +52 -3
- package/core/client/pipes/pipe-registry.ts +249 -0
- package/core/client/quality/SomaQualityRegistry.ts +3 -0
- package/core/client/registry/declaration-renderer.ts +159 -0
- package/core/client/registry/index.ts +28 -0
- package/core/client/registry/page-lifecycle.ts +171 -0
- package/core/client/registry/remote-page.ts +139 -0
- package/core/client/runtime/SomaRuntimeServices.ts +24 -0
- package/core/client/security/attribute-presence.ts +70 -0
- package/core/client/security/html-sanitizer.ts +113 -0
- package/core/client/security/url-guard.ts +0 -0
- package/core/client/theme/theme-engine.ts +936 -0
- package/core/client/typography/typography-base.ts +34 -0
- package/core/client/validation/contract-validator.ts +48 -5
- package/core/index.ts +190 -2
- package/core/server/api/IService.ts +18 -6
- package/core/server/api/load-host-service.ts +3 -3
- package/core/server/host/content-security-policy.ts +190 -0
- package/core/server/host/csp-nonce-metadata.ts +75 -0
- package/core/server/host/resolve-host-request-hook.ts +5 -0
- package/core/server/host/server-page-assets.ts +10 -0
- package/core/server/index.ts +30 -1
- package/core/server/infra/cache/ServerFileCache.ts +1 -1
- package/core/server/mock/node/cli.mjs +2 -2
- package/core/server/registry/api-registry-store.ts +518 -0
- package/core/server/registry/conditional-object-store.ts +29 -0
- package/core/server/registry/declaration-precompiler.ts +129 -0
- package/core/server/registry/in-memory-conditional-object-store.ts +43 -0
- package/core/server/registry/index.ts +142 -0
- package/core/server/registry/page-registry-repository.ts +373 -0
- package/core/server/registry/page-registry-store.ts +903 -0
- package/core/server/registry/registry-error.ts +17 -0
- package/core/server/registry/registry-file-lock.ts +173 -0
- package/core/server/registry/registry-response.ts +71 -0
- package/core/server/registry/registry-subjects.ts +194 -0
- package/core/server/registry/registry-write-pipeline.ts +240 -0
- package/core/server/registry/request-body-limit.ts +99 -0
- package/core/server/registry/services/apis/Get.ts +25 -0
- package/core/server/registry/services/apis/List.ts +41 -0
- package/core/server/registry/services/apis/Register.ts +39 -0
- package/core/server/registry/services/apis/Unregister.ts +24 -0
- package/core/server/registry/services/index.ts +40 -0
- package/core/server/registry/services/pages/Get.ts +86 -0
- package/core/server/registry/services/pages/List.ts +53 -0
- package/core/server/registry/services/pages/Register.ts +49 -0
- package/core/server/registry/services/pages/Rollback.ts +36 -0
- package/core/server/registry/services/pages/Unregister.ts +23 -0
- package/core/server/registry/services/service-utils.ts +21 -0
- package/core/server/registry/validation/compile-gate.ts +71 -0
- package/core/server/registry/validation/component-existence.ts +55 -0
- package/core/server/registry/validation/credential-ban.ts +86 -0
- package/core/server/registry/validation/cross-check.ts +77 -0
- package/core/server/registry/validation/declaration-safety-scan.ts +102 -0
- package/core/shared/types/adaptive.types.ts +109 -0
- package/core/shared/types/component-contract.types.ts +110 -0
- package/core/shared/types/data-config.types.ts +105 -0
- package/core/shared/types/domain.types.ts +6 -0
- package/core/shared/types/engine.types.ts +41 -35
- package/core/shared/types/manifest.types.ts +55 -32
- package/core/shared/types/scope-manifest.types.ts +96 -0
- package/core/shared/types/style-config.types.ts +9 -0
- package/core/shared/types/ui-config.types.ts +118 -98
- package/dist/cli/soma.js +20 -5
- package/dist/core/index.cjs +5341 -894
- package/dist/types/build/compiler/full-manifest-builder.d.ts +4 -0
- package/dist/types/build/compiler/rollup-plugin-soma.d.ts +1 -0
- package/dist/types/build/compiler/soma-precompile.d.ts +9 -0
- package/dist/types/build/compiler/vite-plugin-soma.d.ts +3 -0
- package/dist/types/build/component/package-loader.d.ts +9 -0
- package/dist/types/build/eject/apply-eject.d.ts +22 -0
- package/dist/types/build/eject/plan-eject.d.ts +32 -0
- package/dist/types/client/adaptive/flush-scheduler.d.ts +13 -0
- package/dist/types/client/adaptive/hysteresis.d.ts +16 -0
- package/dist/types/client/adaptive/index.d.ts +5 -0
- package/dist/types/client/adaptive/observer-pool.d.ts +15 -0
- package/dist/types/client/base/SomaPageRootBase.d.ts +33 -0
- package/dist/types/client/compiler/adaptive-compile.d.ts +26 -0
- package/dist/types/client/compiler/manifest-compiler.d.ts +13 -2
- package/dist/types/client/component/ai-catalog.d.ts +73 -0
- package/dist/types/client/component/component-definition.types.d.ts +54 -0
- package/dist/types/client/component/component-expander.d.ts +25 -0
- package/dist/types/client/component/component-manager.d.ts +160 -0
- package/dist/types/client/component/component-pack.d.ts +90 -0
- package/dist/types/client/component/component-registry.d.ts +12 -0
- package/dist/types/client/component/pack-styles.d.ts +22 -0
- package/dist/types/client/component/scoped-adapter.d.ts +18 -0
- package/dist/types/client/component/verify-pack.d.ts +14 -0
- package/dist/types/client/data/data-object-resolver.d.ts +71 -0
- package/dist/types/client/diagnostics/SomaDiagnostics.d.ts +2 -1
- package/dist/types/client/diagnostics/boot-timings.d.ts +30 -0
- package/dist/types/client/engine/asset-resolver.d.ts +7 -1
- package/dist/types/client/engine/dom-renderer.d.ts +49 -1
- package/dist/types/client/engine/emit-event.d.ts +6 -0
- package/dist/types/client/engine/event-engine.d.ts +6 -0
- package/dist/types/client/engine/render-engine.d.ts +25 -1
- package/dist/types/client/engine/runtime-builder.d.ts +7 -0
- package/dist/types/client/engine/soma-bootstrap.d.ts +116 -0
- package/dist/types/client/observability/error-reporter.d.ts +51 -0
- package/dist/types/client/parser/adaptive-parser.d.ts +28 -0
- package/dist/types/client/parser/data-config-parser.d.ts +34 -0
- package/dist/types/client/parser/pipeline-parser.d.ts +2 -0
- package/dist/types/client/parser/style-config-parser.d.ts +5 -3
- package/dist/types/client/parser/ui-config-parser.d.ts +22 -1
- package/dist/types/client/pipes/pipe-registry.d.ts +38 -0
- package/dist/types/client/registry/declaration-renderer.d.ts +51 -0
- package/dist/types/client/registry/index.d.ts +10 -0
- package/dist/types/client/registry/page-lifecycle.d.ts +61 -0
- package/dist/types/client/registry/remote-page.d.ts +52 -0
- package/dist/types/client/runtime/SomaRuntimeServices.d.ts +16 -0
- package/dist/types/client/security/attribute-presence.d.ts +7 -0
- package/dist/types/client/security/html-sanitizer.d.ts +37 -0
- package/dist/types/client/security/url-guard.d.ts +11 -0
- package/dist/types/client/theme/theme-engine.d.ts +49 -0
- package/dist/types/client/typography/typography-base.d.ts +15 -0
- package/dist/types/client/validation/contract-validator.d.ts +2 -0
- package/dist/types/index.d.ts +36 -2
- package/dist/types/server/api/IService.d.ts +18 -6
- package/dist/types/server/host/content-security-policy.d.ts +53 -0
- package/dist/types/server/host/csp-nonce-metadata.d.ts +32 -0
- package/dist/types/server/host/resolve-host-request-hook.d.ts +5 -0
- package/dist/types/server/host/server-page-assets.d.ts +5 -0
- package/dist/types/server/index.d.ts +4 -1
- package/dist/types/server/registry/api-registry-store.d.ts +141 -0
- package/dist/types/server/registry/conditional-object-store.d.ts +27 -0
- package/dist/types/server/registry/declaration-precompiler.d.ts +30 -0
- package/dist/types/server/registry/in-memory-conditional-object-store.d.ts +8 -0
- package/dist/types/server/registry/index.d.ts +29 -0
- package/dist/types/server/registry/page-registry-repository.d.ts +58 -0
- package/dist/types/server/registry/page-registry-store.d.ts +259 -0
- package/dist/types/server/registry/registry-error.d.ts +12 -0
- package/dist/types/server/registry/registry-file-lock.d.ts +20 -0
- package/dist/types/server/registry/registry-response.d.ts +18 -0
- package/dist/types/server/registry/registry-subjects.d.ts +59 -0
- package/dist/types/server/registry/registry-write-pipeline.d.ts +48 -0
- package/dist/types/server/registry/request-body-limit.d.ts +31 -0
- package/dist/types/server/registry/services/apis/Get.d.ts +10 -0
- package/dist/types/server/registry/services/apis/List.d.ts +10 -0
- package/dist/types/server/registry/services/apis/Register.d.ts +13 -0
- package/dist/types/server/registry/services/apis/Unregister.d.ts +12 -0
- package/dist/types/server/registry/services/index.d.ts +3 -0
- package/dist/types/server/registry/services/pages/Get.d.ts +10 -0
- package/dist/types/server/registry/services/pages/List.d.ts +13 -0
- package/dist/types/server/registry/services/pages/Register.d.ts +11 -0
- package/dist/types/server/registry/services/pages/Rollback.d.ts +9 -0
- package/dist/types/server/registry/services/pages/Unregister.d.ts +9 -0
- package/dist/types/server/registry/services/service-utils.d.ts +10 -0
- package/dist/types/server/registry/validation/compile-gate.d.ts +10 -0
- package/dist/types/server/registry/validation/component-existence.d.ts +14 -0
- package/dist/types/server/registry/validation/credential-ban.d.ts +15 -0
- package/dist/types/server/registry/validation/cross-check.d.ts +16 -0
- package/dist/types/server/registry/validation/declaration-safety-scan.d.ts +15 -0
- package/dist/types/shared/types/adaptive.types.d.ts +88 -0
- package/dist/types/shared/types/component-contract.types.d.ts +68 -0
- package/dist/types/shared/types/data-config.types.d.ts +81 -0
- package/dist/types/shared/types/domain.types.d.ts +6 -0
- package/dist/types/shared/types/engine.types.d.ts +6 -0
- package/dist/types/shared/types/manifest.types.d.ts +23 -0
- package/dist/types/shared/types/scope-manifest.types.d.ts +82 -0
- package/dist/types/shared/types/style-config.types.d.ts +8 -0
- package/dist/types/shared/types/ui-config.types.d.ts +20 -0
- package/global.d.ts +4 -0
- package/next.config.js +76 -1
- package/package.json +32 -11
- package/proxy.ts +62 -0
- package/core/client/engine/theme-variables.ts +0 -37
- package/dist/tsconfig.types.tsbuildinfo +0 -1
- package/dist/types/client/engine/theme-variables.d.ts +0 -3
- package/middleware.ts +0 -33
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
// 格式化管道(组件体系 U4):绑定值的展示格式化,让 Domain 回归存原始值。
|
|
2
|
+
//
|
|
3
|
+
// 语法(soma-core/doc/soma 组件dsl扩展.md 第 3 章):
|
|
4
|
+
// item_price: item.price | money
|
|
5
|
+
// tip: page._locale.cart_total_count | tpl(count: page.totalCount)
|
|
6
|
+
// created: order.createdAt | date(YYYY-MM-DD HH:mm)
|
|
7
|
+
// 规则:纯函数、注册制白名单(未注册名编译报错)、可链式、仅用于展示位置。
|
|
8
|
+
|
|
9
|
+
import type { SomaPipeArg, SomaPipeCall } from '../../shared/types/scope-manifest.types';
|
|
10
|
+
|
|
11
|
+
export interface SomaPipeContext {
|
|
12
|
+
/** 读取绑定路径当前值(页面作用域=根实例;行作用域=行数据语义) */
|
|
13
|
+
read(path: string): unknown;
|
|
14
|
+
/** 当前 locale 表(货币符号、免费文案等本地化取值) */
|
|
15
|
+
locale(): Record<string, unknown> | undefined;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** 管道函数:纯函数,入值 + 已求值实参 → 展示字符串 */
|
|
19
|
+
export type SomaPipeFn = (
|
|
20
|
+
value: unknown,
|
|
21
|
+
args: Record<string, unknown>,
|
|
22
|
+
ctx: SomaPipeContext,
|
|
23
|
+
) => string;
|
|
24
|
+
|
|
25
|
+
export class SomaPipeError extends Error {
|
|
26
|
+
constructor(message: string) {
|
|
27
|
+
super(`[soma-core][管道] ${message}`);
|
|
28
|
+
this.name = 'SomaPipeError';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ParsedPipedValue {
|
|
33
|
+
/** 管道前的绑定路径(原样,未经 resolver) */
|
|
34
|
+
basePath: string;
|
|
35
|
+
pipes: SomaPipeCall[];
|
|
36
|
+
/** 实参中的绑定路径(原样),供 watchPaths 注册 */
|
|
37
|
+
argPaths: string[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 值是否携带管道(展示位置专用判断) */
|
|
41
|
+
export function hasPipes(rawValue: string): boolean {
|
|
42
|
+
return rawValue.includes(' | ');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const parseCache = new Map<string, ParsedPipedValue>();
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 解析 `path | pipe1 | pipe2(k: v, 字面量)` 形式的值。
|
|
49
|
+
* 实参判定:首段是已注册 var 的视为绑定路径,其余为字面量(数字/布尔自动转型)。
|
|
50
|
+
* 结果按原始串缓存(编译路径与行内解释路径共享)。
|
|
51
|
+
*/
|
|
52
|
+
export function parsePipedValue(
|
|
53
|
+
rawValue: string,
|
|
54
|
+
hasVar: (seg: string) => boolean,
|
|
55
|
+
): ParsedPipedValue {
|
|
56
|
+
const cacheKey = rawValue;
|
|
57
|
+
const cached = parseCache.get(cacheKey);
|
|
58
|
+
if (cached) {
|
|
59
|
+
return cached;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const segments = rawValue.split(/\s+\|\s+/);
|
|
63
|
+
const basePath = segments[0].trim();
|
|
64
|
+
const pipes: SomaPipeCall[] = [];
|
|
65
|
+
const argPaths: string[] = [];
|
|
66
|
+
|
|
67
|
+
for (const segment of segments.slice(1)) {
|
|
68
|
+
const match = segment.trim().match(/^([a-z][a-zA-Z0-9_]*)(?:\((.*)\))?$/);
|
|
69
|
+
if (!match) {
|
|
70
|
+
throw new SomaPipeError(`管道段格式错误: "${segment}"(应为 name 或 name(args))`);
|
|
71
|
+
}
|
|
72
|
+
const [, name, rawArgs] = match;
|
|
73
|
+
const args: Record<string, SomaPipeArg> = {};
|
|
74
|
+
|
|
75
|
+
if (rawArgs !== undefined && rawArgs.trim() !== '') {
|
|
76
|
+
const parts = rawArgs.split(',').map((part) => part.trim()).filter(Boolean);
|
|
77
|
+
parts.forEach((part, index) => {
|
|
78
|
+
const colonIndex = part.indexOf(':');
|
|
79
|
+
const key = colonIndex > 0 ? part.slice(0, colonIndex).trim() : String(index);
|
|
80
|
+
const valueText = colonIndex > 0 ? part.slice(colonIndex + 1).trim() : part;
|
|
81
|
+
const arg = classifyArg(valueText, hasVar);
|
|
82
|
+
args[key] = arg;
|
|
83
|
+
if (arg.kind === 'binding') {
|
|
84
|
+
argPaths.push(String(arg.value));
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
pipes.push({ name, args: Object.keys(args).length ? args : undefined });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const parsed: ParsedPipedValue = { basePath, pipes, argPaths };
|
|
93
|
+
parseCache.set(cacheKey, parsed);
|
|
94
|
+
return parsed;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function classifyArg(valueText: string, hasVar: (seg: string) => boolean): SomaPipeArg {
|
|
98
|
+
const quoted = valueText.match(/^"(.*)"$|^'(.*)'$/);
|
|
99
|
+
if (quoted) {
|
|
100
|
+
return { kind: 'literal', value: quoted[1] ?? quoted[2] ?? '' };
|
|
101
|
+
}
|
|
102
|
+
if (/^-?\d+(\.\d+)?$/.test(valueText)) {
|
|
103
|
+
return { kind: 'literal', value: Number(valueText) };
|
|
104
|
+
}
|
|
105
|
+
if (valueText === 'true' || valueText === 'false') {
|
|
106
|
+
return { kind: 'literal', value: valueText === 'true' };
|
|
107
|
+
}
|
|
108
|
+
if (hasVar(valueText.split('.')[0])) {
|
|
109
|
+
return { kind: 'binding', value: valueText };
|
|
110
|
+
}
|
|
111
|
+
return { kind: 'literal', value: valueText };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ─── 注册表 ──────────────────────────────────────────────────
|
|
115
|
+
|
|
116
|
+
export class SomaPipeRegistry {
|
|
117
|
+
private readonly fns = new Map<string, SomaPipeFn>();
|
|
118
|
+
|
|
119
|
+
constructor(custom?: Record<string, SomaPipeFn>) {
|
|
120
|
+
for (const [name, fn] of Object.entries(BUILTIN_PIPES)) {
|
|
121
|
+
this.fns.set(name, fn);
|
|
122
|
+
}
|
|
123
|
+
for (const [name, fn] of Object.entries(custom ?? {})) {
|
|
124
|
+
if (!/^[a-z][a-zA-Z0-9_]*$/.test(name)) {
|
|
125
|
+
throw new SomaPipeError(`管道名 "${name}" 不合法(小写字母开头)`);
|
|
126
|
+
}
|
|
127
|
+
this.fns.set(name, fn);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
has(name: string): boolean {
|
|
132
|
+
return this.fns.has(name);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** 编译期白名单校验:未注册名直接抛错(fail fast) */
|
|
136
|
+
assertKnown(pipes: SomaPipeCall[], where: string): void {
|
|
137
|
+
for (const pipe of pipes) {
|
|
138
|
+
if (!this.fns.has(pipe.name)) {
|
|
139
|
+
throw new SomaPipeError(
|
|
140
|
+
`未注册的管道 "${pipe.name}"(位置: ${where};已注册: ${[...this.fns.keys()].join(', ')})`,
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** 链式应用(从左到右);单个管道异常时降级为原值字符串并告警,不中断渲染 */
|
|
147
|
+
apply(value: unknown, pipes: SomaPipeCall[], ctx: SomaPipeContext): string {
|
|
148
|
+
let current: unknown = value;
|
|
149
|
+
for (const pipe of pipes) {
|
|
150
|
+
const fn = this.fns.get(pipe.name);
|
|
151
|
+
if (!fn) {
|
|
152
|
+
console.warn(`[soma-core][管道] 未注册的管道 "${pipe.name}",跳过`);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
const args: Record<string, unknown> = {};
|
|
156
|
+
for (const [key, arg] of Object.entries(pipe.args ?? {})) {
|
|
157
|
+
args[key] = arg.kind === 'binding' ? ctx.read(String(arg.value)) : arg.value;
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
current = fn(current, args, ctx);
|
|
161
|
+
} catch (error) {
|
|
162
|
+
console.warn(`[soma-core][管道] 管道 "${pipe.name}" 执行失败:`, error);
|
|
163
|
+
return String(value ?? '');
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return String(current ?? '');
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ─── 内置管道 ────────────────────────────────────────────────
|
|
171
|
+
|
|
172
|
+
function localeText(ctx: SomaPipeContext, key: string, fallback: string): string {
|
|
173
|
+
const locale = ctx.locale();
|
|
174
|
+
const value = locale?.[key];
|
|
175
|
+
return typeof value === 'string' ? value : fallback;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const BUILTIN_PIPES: Record<string, SomaPipeFn> = {
|
|
179
|
+
/**
|
|
180
|
+
* 金额:money | money(neg) | money(free)
|
|
181
|
+
* neg:非零值加负号(折扣行);free:零值显示免费文案(locale.money_free)。
|
|
182
|
+
*/
|
|
183
|
+
money(value, args, ctx) {
|
|
184
|
+
const num = Number(value);
|
|
185
|
+
if (!Number.isFinite(num)) {
|
|
186
|
+
return String(value ?? '');
|
|
187
|
+
}
|
|
188
|
+
const mode = args['0'];
|
|
189
|
+
if (mode === 'free' && num === 0) {
|
|
190
|
+
return localeText(ctx, 'money_free', '免费');
|
|
191
|
+
}
|
|
192
|
+
const symbol = localeText(ctx, 'money_symbol', '¥');
|
|
193
|
+
const amount = Math.abs(num).toLocaleString('zh-CN', {
|
|
194
|
+
minimumFractionDigits: 2,
|
|
195
|
+
maximumFractionDigits: 2,
|
|
196
|
+
});
|
|
197
|
+
const negative = mode === 'neg' ? num !== 0 : num < 0;
|
|
198
|
+
return `${negative ? '-' : ''}${symbol}${amount}`;
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
/** 模板插值:value 为模板串(通常 _locale.key),{name} 用实参替换 */
|
|
202
|
+
tpl(value, args) {
|
|
203
|
+
let text = String(value ?? '');
|
|
204
|
+
for (const [key, arg] of Object.entries(args)) {
|
|
205
|
+
text = text.split(`{${key}}`).join(String(arg ?? ''));
|
|
206
|
+
}
|
|
207
|
+
return text;
|
|
208
|
+
},
|
|
209
|
+
|
|
210
|
+
/** 日期格式化:date(YYYY-MM-DD HH:mm:ss),接受 Date/ISO 串/时间戳 */
|
|
211
|
+
date(value, args) {
|
|
212
|
+
const input = typeof value === 'number' || value instanceof Date
|
|
213
|
+
? new Date(value)
|
|
214
|
+
: new Date(String(value ?? ''));
|
|
215
|
+
if (Number.isNaN(input.getTime())) {
|
|
216
|
+
return String(value ?? '');
|
|
217
|
+
}
|
|
218
|
+
const format = String(args['0'] ?? 'YYYY-MM-DD');
|
|
219
|
+
const pad = (n: number) => String(n).padStart(2, '0');
|
|
220
|
+
return format
|
|
221
|
+
.replace('YYYY', String(input.getFullYear()))
|
|
222
|
+
.replace('MM', pad(input.getMonth() + 1))
|
|
223
|
+
.replace('DD', pad(input.getDate()))
|
|
224
|
+
.replace('HH', pad(input.getHours()))
|
|
225
|
+
.replace('mm', pad(input.getMinutes()))
|
|
226
|
+
.replace('ss', pad(input.getSeconds()));
|
|
227
|
+
},
|
|
228
|
+
|
|
229
|
+
/** 百分比:percent(小数位),输入为比率(0.125 → 12.5%) */
|
|
230
|
+
percent(value, args) {
|
|
231
|
+
const num = Number(value);
|
|
232
|
+
if (!Number.isFinite(num)) {
|
|
233
|
+
return String(value ?? '');
|
|
234
|
+
}
|
|
235
|
+
const digits = Number(args['0'] ?? 0);
|
|
236
|
+
return `${(num * 100).toFixed(digits)}%`;
|
|
237
|
+
},
|
|
238
|
+
|
|
239
|
+
/** 单复数文案:plural(单数key, 复数key),按数值取 locale 文案并替换 {count} */
|
|
240
|
+
plural(value, args, ctx) {
|
|
241
|
+
const num = Number(value);
|
|
242
|
+
const key = String(num === 1 ? args['0'] ?? '' : args['1'] ?? args['0'] ?? '');
|
|
243
|
+
const template = localeText(ctx, key, key);
|
|
244
|
+
return template.split('{count}').join(String(value ?? ''));
|
|
245
|
+
},
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
/** 默认注册表(仅内置管道),未传自定义管道时全局共享 */
|
|
249
|
+
export const defaultPipeRegistry = new SomaPipeRegistry();
|
|
@@ -38,7 +38,10 @@ export class SomaQualityRegistry {
|
|
|
38
38
|
{ id: 'cache', name: 'Parse cache', carrier: 'SomaAssetResolver', description: 'LRU cache for YAML parse results' },
|
|
39
39
|
];
|
|
40
40
|
|
|
41
|
+
// 可注册扩展面(Host 注入)。主题故意不在此列:令牌派生是引擎内建,见 theme-engine.ts。
|
|
41
42
|
static readonly extensibilityDimensions: ExtensibilityDimension[] = [
|
|
43
|
+
{ id: 'components', name: 'Component registry', carrier: 'SomaComponentRegistry', description: 'Host/page register SomaComponentDefinition; not theme' },
|
|
44
|
+
{ id: 'pipes', name: 'Format pipes', carrier: 'SomaPipeRegistry', description: 'Host register pure format pipes; merge with builtins' },
|
|
42
45
|
{ id: 'adapter', name: 'Container adapter', carrier: 'SomaContainerAdapter', description: 'Isolate DOM ops for cross-platform hosts' },
|
|
43
46
|
{ id: 'middleware', name: 'Middleware chains', carrier: 'SomaMiddlewareChain', description: 'Onion-model hooks for render and event engines' },
|
|
44
47
|
{ id: 'hotswap', name: 'Hot swap', carrier: 'SomaBootstrap.hotSwap', description: 'Replace ui/event/style/DomainClass at runtime' },
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// 声明渲染通道:拿着内存里的四件套声明直接上屏(空 Domain + initProps 演示数据)。
|
|
2
|
+
//
|
|
3
|
+
// REQ-002 自 soma-ai 收编归位:「soma-ai 管生成,soma-core 管使用」——
|
|
4
|
+
// 把声明变成运行页面是引擎能力,与声明从哪来(AI 生成 / 应用构造 / 注册中心拉取)无关。
|
|
5
|
+
//
|
|
6
|
+
// - 传入声明即渲染,不经任何存储;应用攒在自己内存里的声明也能直接喂;
|
|
7
|
+
// - 预编译产物(注册中心 P-a)可选:版本闸在服务端,这里只做结构性校验;
|
|
8
|
+
// - destroy 清场:引擎 unmount 不清 DOM,本通道的销毁语义是挂载点还原为空。
|
|
9
|
+
|
|
10
|
+
import { SomaBootstrap } from '../engine/soma-bootstrap';
|
|
11
|
+
import type { SomaComponentDefinition } from '../component/component-definition.types';
|
|
12
|
+
import type { SomaAdaptiveVocabulary } from '../../shared/types/adaptive.types';
|
|
13
|
+
import type { SomaErrorEvent, SomaPageIdentity } from '../observability/error-reporter';
|
|
14
|
+
import { SomaPageRootBase } from '../base/SomaPageRootBase';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 注册中心落库的预编译产物(P-a)。渲染时若版本对得上就直接喂给引擎,省掉一次 YAML 解析;
|
|
18
|
+
* 对不上则整份忽略——**缓存不是真理**,拿旧编译器的产物渲染新语义比慢一点危险得多。
|
|
19
|
+
*/
|
|
20
|
+
export interface PrecompiledDeclarationAssets {
|
|
21
|
+
uiConfig?: unknown;
|
|
22
|
+
eventConfig?: unknown;
|
|
23
|
+
triggerConfig?: unknown;
|
|
24
|
+
timerConfig?: unknown;
|
|
25
|
+
styleConfig?: unknown;
|
|
26
|
+
dataConfig?: unknown;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface GeneratedPageDeclarations {
|
|
30
|
+
html: string;
|
|
31
|
+
uiYaml: string;
|
|
32
|
+
eventYaml?: string;
|
|
33
|
+
styleYaml?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 错误采集开关(默认关闭)。
|
|
38
|
+
* 声明态页面是"注册即生效"的——出错时唯一有价值的问题是"哪个版本引入的"。
|
|
39
|
+
* 页面名与版本号只有渲染通道知道,不在这里盖戳,后面任何一层都补不回来。
|
|
40
|
+
*/
|
|
41
|
+
export interface PreviewObservabilityOptions {
|
|
42
|
+
/** 页面身份缺省值;renderPublishedPage 会用真实的 name@version 覆盖 */
|
|
43
|
+
page?: SomaPageIdentity;
|
|
44
|
+
/** 上报通道;缺省走 soma-core 的 console 单行 JSON 通道 */
|
|
45
|
+
report?: (event: SomaErrorEvent) => void;
|
|
46
|
+
/** 同签名错误去重窗口(毫秒) */
|
|
47
|
+
dedupeWindowMs?: number;
|
|
48
|
+
/** 单页上报条数上限 */
|
|
49
|
+
maxEvents?: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface GeneratedPagePreviewOptions {
|
|
53
|
+
/** 给了就开启错误采集(`{ observability: {} }` 即为一行开启,用默认通道) */
|
|
54
|
+
observability?: PreviewObservabilityOptions;
|
|
55
|
+
/** 页面声明含 adaptive.ladder 时由 Host 注入的组件词汇。 */
|
|
56
|
+
adaptiveVocabulary?: SomaAdaptiveVocabulary;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface PreviewHandle {
|
|
60
|
+
/**
|
|
61
|
+
* 用新产物重新渲染(先卸载旧页面)。
|
|
62
|
+
* page 传入时覆盖本次渲染的页面身份——发布页拉到版本号后才知道该盖什么戳。
|
|
63
|
+
*/
|
|
64
|
+
render(
|
|
65
|
+
decl: GeneratedPageDeclarations,
|
|
66
|
+
page?: SomaPageIdentity,
|
|
67
|
+
compiled?: PrecompiledDeclarationAssets,
|
|
68
|
+
): Promise<void>;
|
|
69
|
+
destroy(): Promise<void>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 采集未指定身份时的兜底戳:至少标明"这是即时预览产物,不是注册版本" */
|
|
73
|
+
const ANONYMOUS_PREVIEW_PAGE: SomaPageIdentity = { name: 'ai-preview', source: 'preview' };
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 预览页 Domain:无业务代码,数据全部来自产物的 initProps。
|
|
77
|
+
* 惰性建类:本模块可能被服务端脚本间接拉进 Node ESM,顶层继承会在导入那一刻
|
|
78
|
+
* 要求基类绑定就位;挪到首次使用时再建,浏览器行为一字不变。
|
|
79
|
+
*/
|
|
80
|
+
let previewPageRootCtor: (new () => SomaPageRootBase) | null = null;
|
|
81
|
+
|
|
82
|
+
function getPreviewPageRoot(): new () => SomaPageRootBase {
|
|
83
|
+
previewPageRootCtor ??= class PreviewPageRoot extends SomaPageRootBase {};
|
|
84
|
+
return previewPageRootCtor;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function createGeneratedPagePreview(
|
|
88
|
+
target: HTMLElement | string,
|
|
89
|
+
components: Record<string, SomaComponentDefinition>,
|
|
90
|
+
options: GeneratedPagePreviewOptions = {},
|
|
91
|
+
): PreviewHandle {
|
|
92
|
+
// Domain 层禁碰 DOM(架构规则),挂载点查找由本客户端助手代劳
|
|
93
|
+
const container = typeof target === 'string'
|
|
94
|
+
? document.getElementById(target)
|
|
95
|
+
: target;
|
|
96
|
+
if (!container) {
|
|
97
|
+
throw new Error(`[soma-core][声明渲染] 挂载点不存在: ${String(target)}`);
|
|
98
|
+
}
|
|
99
|
+
const observability = options.observability;
|
|
100
|
+
const adaptiveVocabulary = options.adaptiveVocabulary;
|
|
101
|
+
let bootstrap: SomaBootstrap | null = null;
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
async render(
|
|
105
|
+
decl: GeneratedPageDeclarations,
|
|
106
|
+
page?: SomaPageIdentity,
|
|
107
|
+
compiled?: PrecompiledDeclarationAssets,
|
|
108
|
+
): Promise<void> {
|
|
109
|
+
if (bootstrap) {
|
|
110
|
+
await bootstrap.unmount();
|
|
111
|
+
bootstrap = null;
|
|
112
|
+
}
|
|
113
|
+
const next = new SomaBootstrap({
|
|
114
|
+
components,
|
|
115
|
+
adaptiveVocabulary,
|
|
116
|
+
// 未开启采集时连字段都不传,引擎侧整条路径旁路(默认关闭)
|
|
117
|
+
...(observability
|
|
118
|
+
? {
|
|
119
|
+
errorReporting: {
|
|
120
|
+
...observability,
|
|
121
|
+
page: page ?? observability.page ?? ANONYMOUS_PREVIEW_PAGE,
|
|
122
|
+
},
|
|
123
|
+
}
|
|
124
|
+
: {}),
|
|
125
|
+
});
|
|
126
|
+
// 版本闸在服务端:那里同时知道"产物是哪个编译器版本产的"与"当前跑的是哪个版本",
|
|
127
|
+
// 客户端拿不到后者,在这里判等于自欺。服务端只有认定可用才会把 compiled 发下来,
|
|
128
|
+
// 这里只做最后一道结构性校验(ui/event 两段齐备,asset-resolver 的硬要求)。
|
|
129
|
+
const usable = compiled !== undefined
|
|
130
|
+
&& compiled.uiConfig !== undefined
|
|
131
|
+
&& compiled.eventConfig !== undefined;
|
|
132
|
+
await next.mount(container, {
|
|
133
|
+
html: decl.html,
|
|
134
|
+
uiYaml: decl.uiYaml,
|
|
135
|
+
eventYaml: decl.eventYaml ?? '',
|
|
136
|
+
styleYaml: decl.styleYaml,
|
|
137
|
+
DomainClass: getPreviewPageRoot(),
|
|
138
|
+
...(usable
|
|
139
|
+
? {
|
|
140
|
+
uiConfig: compiled.uiConfig as never,
|
|
141
|
+
eventConfig: compiled.eventConfig as never,
|
|
142
|
+
triggerConfig: compiled.triggerConfig as never,
|
|
143
|
+
timerConfig: compiled.timerConfig as never,
|
|
144
|
+
...(compiled.styleConfig !== undefined ? { styleConfig: compiled.styleConfig as never } : {}),
|
|
145
|
+
}
|
|
146
|
+
: {}),
|
|
147
|
+
});
|
|
148
|
+
bootstrap = next;
|
|
149
|
+
},
|
|
150
|
+
async destroy(): Promise<void> {
|
|
151
|
+
if (bootstrap) {
|
|
152
|
+
await bootstrap.unmount();
|
|
153
|
+
bootstrap = null;
|
|
154
|
+
}
|
|
155
|
+
// 引擎 unmount 不清 DOM(由下一次 mount 覆盖),销毁语义是清场
|
|
156
|
+
container.innerHTML = '';
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 运行时注册中心(客户端桶,'soma-core/registry')。
|
|
3
|
+
*
|
|
4
|
+
* REQ-002:声明 → 运行页面是引擎能力。两条消费路径:
|
|
5
|
+
* - createGeneratedPagePreview:拿着内存里的声明直接渲染(生成即渲、应用自组织的临时页);
|
|
6
|
+
* - renderPublishedPage:按名从注册中心拉取声明再渲染(注册即生效的共享页)。
|
|
7
|
+
*/
|
|
8
|
+
export {
|
|
9
|
+
createGeneratedPagePreview,
|
|
10
|
+
type GeneratedPageDeclarations,
|
|
11
|
+
type GeneratedPagePreviewOptions,
|
|
12
|
+
type PrecompiledDeclarationAssets,
|
|
13
|
+
type PreviewHandle,
|
|
14
|
+
type PreviewObservabilityOptions,
|
|
15
|
+
} from './declaration-renderer';
|
|
16
|
+
export {
|
|
17
|
+
createSomaPageLifecycle,
|
|
18
|
+
SomaPageLifecycle,
|
|
19
|
+
type MountedPageInfo,
|
|
20
|
+
type RegisterDeclarationOptions,
|
|
21
|
+
type SomaPageLifecycleOptions,
|
|
22
|
+
} from './page-lifecycle';
|
|
23
|
+
export {
|
|
24
|
+
renderPublishedPage,
|
|
25
|
+
type PublishedPageHandle,
|
|
26
|
+
type PublishedPageRecord,
|
|
27
|
+
type RenderPublishedPageOptions,
|
|
28
|
+
} from './remote-page';
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// 页面生命周期管理器:注册 → 挂载 → 卸载 → 注销的实例表。
|
|
2
|
+
//
|
|
3
|
+
// 为什么要有它:平台已经能"把声明变成页面",但**谁在管这些页面的死活**先前没人管——
|
|
4
|
+
// 每个应用自己拿着句柄、自己记哪个槽位挂了什么、自己保证切走时卸干净。
|
|
5
|
+
// ai-demo `/my-space` 就是手写这套簿记的样子:能跑,但每个应用都要重写一遍,
|
|
6
|
+
// 而写漏一次 destroy 在 2GB 机器上就是内存不还。
|
|
7
|
+
//
|
|
8
|
+
// 边界(和注册中心分得很清):
|
|
9
|
+
// - 它**不碰服务端**:声明从哪来不关它的事(应用现攒的、按名拉的都行);
|
|
10
|
+
// - 它**不碰路由**:挂到哪个 DOM 挂载点由调用方给,URL 空间归开发态页面;
|
|
11
|
+
// - 它只做一件事:把"名字 → 声明 → 若干个挂载实例"这张表管住,并保证卸载真还内存。
|
|
12
|
+
//
|
|
13
|
+
// 一个名字可以同时挂多处(多实例):同一份声明渲染在两个槽位是合法用法,
|
|
14
|
+
// unmount 按挂载点收,unregister 连声明带全部实例一起清。
|
|
15
|
+
|
|
16
|
+
import type { SomaComponentDefinition } from '../component/component-definition.types';
|
|
17
|
+
import type { SomaAdaptiveVocabulary } from '../../shared/types/adaptive.types';
|
|
18
|
+
import type { SomaPageIdentity } from '../observability/error-reporter';
|
|
19
|
+
import {
|
|
20
|
+
createGeneratedPagePreview,
|
|
21
|
+
type GeneratedPageDeclarations,
|
|
22
|
+
type PrecompiledDeclarationAssets,
|
|
23
|
+
type PreviewHandle,
|
|
24
|
+
type PreviewObservabilityOptions,
|
|
25
|
+
} from './declaration-renderer';
|
|
26
|
+
|
|
27
|
+
export interface SomaPageLifecycleOptions {
|
|
28
|
+
/** 组件定义表(通常是 createSomaUiComponents() 加宿主自治组件) */
|
|
29
|
+
components: Record<string, SomaComponentDefinition>;
|
|
30
|
+
/** 页面声明含 adaptive.ladder 时由 Host 注入的组件词汇 */
|
|
31
|
+
adaptiveVocabulary?: SomaAdaptiveVocabulary;
|
|
32
|
+
/** 错误采集(默认关闭);页面身份会自动盖上注册名 */
|
|
33
|
+
observability?: PreviewObservabilityOptions;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface RegisterDeclarationOptions {
|
|
37
|
+
/** 预编译产物(注册中心取页时会带回来),有就省一次解析 */
|
|
38
|
+
compiled?: PrecompiledDeclarationAssets;
|
|
39
|
+
/** 页面身份戳(缺省用注册名);注册中心来的声明应带上真实版本号 */
|
|
40
|
+
page?: SomaPageIdentity;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface MountedPageInfo {
|
|
44
|
+
/** 注册名 */
|
|
45
|
+
name: string;
|
|
46
|
+
/** 挂载点 id */
|
|
47
|
+
mountId: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface RegisteredEntry {
|
|
51
|
+
declaration: GeneratedPageDeclarations;
|
|
52
|
+
compiled?: PrecompiledDeclarationAssets;
|
|
53
|
+
page?: SomaPageIdentity;
|
|
54
|
+
/** 挂载点 id → 渲染句柄 */
|
|
55
|
+
mounts: Map<string, PreviewHandle>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 实例表。**纯内存、纯客户端**:进程内活着,刷新即空——
|
|
60
|
+
* 需要跨刷新存活的声明属于注册中心那一侧的事(按名拉取)。
|
|
61
|
+
*/
|
|
62
|
+
export class SomaPageLifecycle {
|
|
63
|
+
private readonly entries = new Map<string, RegisteredEntry>();
|
|
64
|
+
|
|
65
|
+
constructor(private readonly options: SomaPageLifecycleOptions) {}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 登记一份声明(同名即替换)。替换时会先把它已有的挂载全部卸掉——
|
|
69
|
+
* 否则屏幕上留着旧内容而表里已是新声明,是最难查的那类不一致。
|
|
70
|
+
*/
|
|
71
|
+
async register(
|
|
72
|
+
name: string,
|
|
73
|
+
declaration: GeneratedPageDeclarations,
|
|
74
|
+
options: RegisterDeclarationOptions = {},
|
|
75
|
+
): Promise<void> {
|
|
76
|
+
await this.unregister(name);
|
|
77
|
+
this.entries.set(name, {
|
|
78
|
+
declaration,
|
|
79
|
+
...(options.compiled ? { compiled: options.compiled } : {}),
|
|
80
|
+
...(options.page ? { page: options.page } : {}),
|
|
81
|
+
mounts: new Map(),
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** 这个名字登记过吗 */
|
|
86
|
+
has(name: string): boolean {
|
|
87
|
+
return this.entries.has(name);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** 已登记的名字(按登记顺序) */
|
|
91
|
+
names(): string[] {
|
|
92
|
+
return [...this.entries.keys()];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** 当前挂着的实例清单(名字 + 挂载点) */
|
|
96
|
+
mounted(): MountedPageInfo[] {
|
|
97
|
+
const list: MountedPageInfo[] = [];
|
|
98
|
+
for (const [name, entry] of this.entries) {
|
|
99
|
+
for (const mountId of entry.mounts.keys()) list.push({ name, mountId });
|
|
100
|
+
}
|
|
101
|
+
return list;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 把已登记的声明挂到某个挂载点。
|
|
106
|
+
* 同一挂载点重复 mount 会先卸掉那里的旧实例(挂载点是唯一的,不叠加)。
|
|
107
|
+
*/
|
|
108
|
+
async mount(name: string, mountId: string): Promise<void> {
|
|
109
|
+
const entry = this.entries.get(name);
|
|
110
|
+
if (!entry) {
|
|
111
|
+
throw new Error(`[soma-core][生命周期] 页面未登记:${name}(先 register 再 mount)`);
|
|
112
|
+
}
|
|
113
|
+
await this.unmount(name, mountId);
|
|
114
|
+
const handle = createGeneratedPagePreview(mountId, this.options.components, {
|
|
115
|
+
...(this.options.adaptiveVocabulary ? { adaptiveVocabulary: this.options.adaptiveVocabulary } : {}),
|
|
116
|
+
...(this.options.observability ? { observability: this.options.observability } : {}),
|
|
117
|
+
});
|
|
118
|
+
try {
|
|
119
|
+
await handle.render(
|
|
120
|
+
entry.declaration,
|
|
121
|
+
entry.page ?? { name, source: 'registry' },
|
|
122
|
+
entry.compiled,
|
|
123
|
+
);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
// 渲染失败就别把半死的句柄留在表里:清场后原样抛给调用方兜底
|
|
126
|
+
await handle.destroy().catch(() => undefined);
|
|
127
|
+
throw error;
|
|
128
|
+
}
|
|
129
|
+
entry.mounts.set(mountId, handle);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 卸载。给 mountId 只卸那一处,不给则卸这个名字的全部实例。
|
|
134
|
+
* 卸载**必须真还内存**:句柄 destroy 会 unmount 引擎并清空挂载点 DOM,
|
|
135
|
+
* 这里再把句柄从表里删掉——表还拿着引用,GC 就收不走。
|
|
136
|
+
*/
|
|
137
|
+
async unmount(name: string, mountId?: string): Promise<void> {
|
|
138
|
+
const entry = this.entries.get(name);
|
|
139
|
+
if (!entry) return;
|
|
140
|
+
if (mountId === undefined) {
|
|
141
|
+
const handles = [...entry.mounts.values()];
|
|
142
|
+
entry.mounts.clear();
|
|
143
|
+
await Promise.all(handles.map((handle) => handle.destroy()));
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const handle = entry.mounts.get(mountId);
|
|
147
|
+
if (!handle) return;
|
|
148
|
+
entry.mounts.delete(mountId);
|
|
149
|
+
await handle.destroy();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** 注销:先卸全部实例,再把声明从表里丢掉 */
|
|
153
|
+
async unregister(name: string): Promise<void> {
|
|
154
|
+
if (!this.entries.has(name)) return;
|
|
155
|
+
await this.unmount(name);
|
|
156
|
+
this.entries.delete(name);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** 全清:页面离开时调一次,别把整张表连同声明一起漏在内存里 */
|
|
160
|
+
async destroyAll(): Promise<void> {
|
|
161
|
+
const names = this.names();
|
|
162
|
+
for (const name of names) {
|
|
163
|
+
await this.unregister(name);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** 建一个实例表(与 createSomaUiComponents() 一起用) */
|
|
169
|
+
export function createSomaPageLifecycle(options: SomaPageLifecycleOptions): SomaPageLifecycle {
|
|
170
|
+
return new SomaPageLifecycle(options);
|
|
171
|
+
}
|