soma-core 1.0.1 → 1.0.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.
Files changed (234) hide show
  1. package/app/SomaPageClient.tsx +364 -0
  2. package/app/api/[...path]/route.ts +9 -85
  3. package/app/api/page/route.ts +57 -35
  4. package/app/api/soma-manifest/route.ts +39 -0
  5. package/app/components/SomaHydrationShell.tsx +23 -0
  6. package/app/favicon.ico/route.ts +13 -0
  7. package/app/layout.tsx +2 -2
  8. package/app/loading.tsx +5 -0
  9. package/app/page.tsx +19 -128
  10. package/bin/soma-lsp.cjs +195 -0
  11. package/bin/soma-sync.cjs +3 -0
  12. package/bin/soma.cjs +13 -1
  13. package/core/api/IService.ts +6 -10
  14. package/core/api/load-host-service.ts +75 -0
  15. package/core/api/service-path.ts +39 -0
  16. package/core/base/SomaEntity.ts +3 -10
  17. package/core/base/SomaLocaleOnlyPageRoot.ts +3 -4
  18. package/core/base/SomaPageRootBase.ts +20 -3
  19. package/core/base/decorators/Event.ts +23 -0
  20. package/core/base/decorators/Locale.ts +35 -0
  21. package/core/base/decorators/Page.ts +16 -0
  22. package/core/base/decorators/Show.ts +20 -0
  23. package/core/base/decorators/ShowList.ts +34 -0
  24. package/core/base/decorators/index.ts +5 -0
  25. package/core/base/index.ts +2 -0
  26. package/core/base/runAsync.ts +32 -0
  27. package/core/compiler/full-manifest-builder.ts +33 -7
  28. package/core/compiler/host-config-sync.cjs +38 -0
  29. package/core/compiler/manifest-compiler.ts +3 -2
  30. package/core/compiler/page-base-resolver.ts +26 -0
  31. package/core/compiler/precompiled-manifest-loader.ts +34 -0
  32. package/core/compiler/rollup-plugin-soma.ts +45 -0
  33. package/core/compiler/rspack-plugin-soma.js +33 -0
  34. package/core/compiler/soma-manifest-webpack-plugin.js +188 -0
  35. package/core/compiler/soma-precompile.js +3513 -0
  36. package/core/compiler/soma-precompile.ts +52 -6
  37. package/core/compiler/soma-rspack-yaml-loader.js +22 -0
  38. package/core/compiler/soma-webpack-loader.js +7 -1
  39. package/core/compiler/vite-plugin-soma.ts +11 -1
  40. package/core/container/IContainerAdapter.ts +36 -0
  41. package/core/container/MiniProgramContainerAdapter.ts +190 -0
  42. package/core/container/ReactNativeContainerAdapter.ts +189 -0
  43. package/core/container/SomaContainerAdapter.ts +17 -6
  44. package/core/diagnostics/SomaDiagnostics.ts +1 -1
  45. package/core/diagnostics/SomaTelemetry.ts +100 -0
  46. package/core/diagnostics/telemetry-context.ts +15 -0
  47. package/core/engine/asset-resolver.ts +39 -4
  48. package/core/engine/dom-renderer.ts +169 -35
  49. package/core/engine/event-engine.ts +110 -41
  50. package/core/engine/on-change-engine.ts +71 -0
  51. package/core/engine/property-watcher.ts +117 -0
  52. package/core/engine/reactive-proxy.ts +11 -5
  53. package/core/engine/render-engine.ts +97 -11
  54. package/core/engine/runtime-builder.ts +88 -11
  55. package/core/engine/scope-resolver.ts +65 -9
  56. package/core/engine/soma-bootstrap.ts +448 -244
  57. package/core/engine/style-binder.ts +423 -0
  58. package/core/engine/theme-variables.ts +37 -0
  59. package/core/engine/timer-engine.ts +92 -0
  60. package/core/engine/trigger-engine.ts +55 -0
  61. package/core/host/app-config.stub.ts +2 -0
  62. package/core/host/locale.stub.ts +2 -0
  63. package/core/host/resolve-host-bootstrap.ts +28 -0
  64. package/core/host/soma-bootstrap-host.stub.ts +4 -0
  65. package/core/host/source-packages.ts +112 -0
  66. package/core/index.ts +33 -2
  67. package/core/infra/auth/TokenAuthService.ts +17 -3
  68. package/core/infra/http/IHttpClient.ts +11 -1
  69. package/core/infra/http/SomaHttpService.ts +89 -31
  70. package/core/infra/i18n/DefaultI18n.ts +6 -18
  71. package/core/infra/i18n/II18n.ts +2 -3
  72. package/core/infra/notify/DomNotifyService.ts +98 -0
  73. package/core/infra/notify/INotifyTheme.ts +22 -0
  74. package/core/infra/realtime/IRealtimeService.ts +23 -0
  75. package/core/infra/realtime/IRealtimeTransport.ts +9 -0
  76. package/core/infra/realtime/SomaRealtimeService.ts +95 -0
  77. package/core/infra/realtime/SseRealtimeTransport.ts +36 -0
  78. package/core/infra/realtime/WebSocketRealtimeTransport.ts +102 -0
  79. package/core/infra/router/router-path.ts +28 -0
  80. package/core/infra/storage/SessionStorageService.ts +59 -0
  81. package/core/lsp/cross-file-diagnostics.cjs +160 -0
  82. package/core/lsp/event-yaml-completion.cjs +106 -0
  83. package/core/lsp/extra-completion.cjs +64 -0
  84. package/core/lsp/lsp-navigation.cjs +191 -0
  85. package/core/lsp/ui-yaml-completion.cjs +170 -0
  86. package/core/manifest/manifest-contract.ts +12 -0
  87. package/core/middleware/createPermissionMiddleware.ts +37 -0
  88. package/core/parser/annotation-extractor.ts +113 -0
  89. package/core/parser/pipeline-parser.ts +344 -276
  90. package/core/parser/style-config-parser.ts +340 -0
  91. package/core/parser/timer-parser.ts +70 -0
  92. package/core/parser/trigger-parser.ts +90 -0
  93. package/core/parser/ui-config-parser.ts +73 -1
  94. package/core/quality/SomaQualityRegistry.ts +68 -0
  95. package/core/runtime/SomaRuntimeServices.ts +100 -0
  96. package/core/runtime/SomaSnapshotStore.ts +5 -3
  97. package/core/types/domain.types.ts +5 -2
  98. package/core/types/event.types.ts +82 -57
  99. package/core/types/manifest.types.ts +2 -0
  100. package/core/types/style-config.types.ts +76 -0
  101. package/core/types/timer.types.ts +23 -0
  102. package/core/types/trigger.types.ts +14 -0
  103. package/core/types/ui-config.types.ts +9 -0
  104. package/core/types/watcher.types.ts +29 -0
  105. package/dist/cli/soma.js +23 -23
  106. package/dist/core/index.cjs +9065 -0
  107. package/dist/diagnostics/telemetry.cjs +105 -0
  108. package/dist/preview-export.cjs +3401 -0
  109. package/dist/tsconfig.types.tsbuildinfo +1 -0
  110. package/dist/types/api/IService.d.ts +28 -0
  111. package/dist/types/api/load-host-service.d.ts +5 -0
  112. package/dist/types/api/service-path.d.ts +13 -0
  113. package/dist/types/base/SomaApi.d.ts +5 -0
  114. package/dist/types/base/SomaDict.d.ts +89 -0
  115. package/dist/types/base/SomaEntity.d.ts +15 -0
  116. package/dist/types/base/SomaHttpClient.d.ts +66 -0
  117. package/dist/types/base/SomaLocaleOnlyPageRoot.d.ts +3 -0
  118. package/dist/types/base/SomaPageRootBase.d.ts +49 -0
  119. package/dist/types/base/SomaValidator.d.ts +55 -0
  120. package/dist/types/base/decorators/Event.d.ts +10 -0
  121. package/dist/types/base/decorators/Locale.d.ts +9 -0
  122. package/dist/types/base/decorators/Page.d.ts +4 -0
  123. package/dist/types/base/decorators/Show.d.ts +7 -0
  124. package/dist/types/base/decorators/ShowList.d.ts +12 -0
  125. package/dist/types/base/decorators/index.d.ts +5 -0
  126. package/dist/types/base/index.d.ts +9 -0
  127. package/dist/types/base/runAsync.d.ts +12 -0
  128. package/dist/types/compiler/full-manifest-builder.d.ts +35 -0
  129. package/dist/types/compiler/manifest-compiler.d.ts +29 -0
  130. package/dist/types/compiler/page-base-resolver.d.ts +6 -0
  131. package/dist/types/compiler/precompiled-manifest-loader.d.ts +5 -0
  132. package/dist/types/compiler/rollup-plugin-soma.d.ts +14 -0
  133. package/dist/types/compiler/route-builder.d.ts +12 -0
  134. package/dist/types/compiler/soma-precompile.d.ts +40 -0
  135. package/dist/types/compiler/vite-plugin-soma.d.ts +34 -0
  136. package/dist/types/container/IContainerAdapter.d.ts +30 -0
  137. package/dist/types/container/MiniProgramContainerAdapter.d.ts +52 -0
  138. package/dist/types/container/ReactNativeContainerAdapter.d.ts +47 -0
  139. package/dist/types/container/SomaContainerAdapter.d.ts +30 -0
  140. package/dist/types/diagnostics/SomaDiagnostics.d.ts +64 -0
  141. package/dist/types/diagnostics/SomaTelemetry.d.ts +32 -0
  142. package/dist/types/diagnostics/telemetry-context.d.ts +4 -0
  143. package/dist/types/engine/asset-resolver.d.ts +17 -0
  144. package/dist/types/engine/dom-renderer.d.ts +41 -0
  145. package/dist/types/engine/event-engine.d.ts +64 -0
  146. package/dist/types/engine/lis.d.ts +10 -0
  147. package/dist/types/engine/on-change-engine.d.ts +16 -0
  148. package/dist/types/engine/property-watcher.d.ts +18 -0
  149. package/dist/types/engine/reactive-proxy.d.ts +58 -0
  150. package/dist/types/engine/render-engine.d.ts +73 -0
  151. package/dist/types/engine/runtime-builder.d.ts +58 -0
  152. package/dist/types/engine/scope-resolver.d.ts +90 -0
  153. package/dist/types/engine/soma-bootstrap.d.ts +112 -0
  154. package/dist/types/engine/style-binder.d.ts +81 -0
  155. package/dist/types/engine/theme-variables.d.ts +3 -0
  156. package/dist/types/engine/timer-engine.d.ts +19 -0
  157. package/dist/types/engine/trigger-engine.d.ts +16 -0
  158. package/dist/types/host/app-config.stub.d.ts +3 -0
  159. package/dist/types/host/locale.stub.d.ts +3 -0
  160. package/dist/types/host/resolve-host-bootstrap.d.ts +11 -0
  161. package/dist/types/host/soma-bootstrap-host.stub.d.ts +3 -0
  162. package/dist/types/host/source-packages.d.ts +8 -0
  163. package/dist/types/index.d.ts +35 -0
  164. package/dist/types/infra/auth/IAuthService.d.ts +12 -0
  165. package/dist/types/infra/auth/TokenAuthService.d.ts +26 -0
  166. package/dist/types/infra/cache/ICache.d.ts +12 -0
  167. package/dist/types/infra/cache/MemoryCache.d.ts +14 -0
  168. package/dist/types/infra/cache/ServerFileCache.d.ts +37 -0
  169. package/dist/types/infra/config/IConfig.d.ts +9 -0
  170. package/dist/types/infra/config/SomaConfig.d.ts +21 -0
  171. package/dist/types/infra/event-bus/IEventBus.d.ts +8 -0
  172. package/dist/types/infra/event-bus/SomaEventBus.d.ts +9 -0
  173. package/dist/types/infra/http/IHttpClient.d.ts +28 -0
  174. package/dist/types/infra/http/SomaHttpService.d.ts +27 -0
  175. package/dist/types/infra/i18n/DefaultI18n.d.ts +7 -0
  176. package/dist/types/infra/i18n/II18n.d.ts +6 -0
  177. package/dist/types/infra/logger/ConsoleLogger.d.ts +14 -0
  178. package/dist/types/infra/logger/ILogger.d.ts +17 -0
  179. package/dist/types/infra/notify/DefaultNotifyService.d.ts +16 -0
  180. package/dist/types/infra/notify/DomNotifyService.d.ts +16 -0
  181. package/dist/types/infra/notify/INotifyService.d.ts +12 -0
  182. package/dist/types/infra/notify/INotifyTheme.d.ts +12 -0
  183. package/dist/types/infra/permission/IPermission.d.ts +10 -0
  184. package/dist/types/infra/permission/SomaPermission.d.ts +19 -0
  185. package/dist/types/infra/realtime/IRealtimeService.d.ts +13 -0
  186. package/dist/types/infra/realtime/IRealtimeTransport.d.ts +8 -0
  187. package/dist/types/infra/realtime/SomaRealtimeService.d.ts +20 -0
  188. package/dist/types/infra/realtime/SseRealtimeTransport.d.ts +11 -0
  189. package/dist/types/infra/realtime/WebSocketRealtimeTransport.d.ts +30 -0
  190. package/dist/types/infra/router/IRouter.d.ts +16 -0
  191. package/dist/types/infra/router/SomaRouter.d.ts +18 -0
  192. package/dist/types/infra/router/router-path.d.ts +6 -0
  193. package/dist/types/infra/storage/IStorageService.d.ts +7 -0
  194. package/dist/types/infra/storage/MemoryStorageService.d.ts +15 -0
  195. package/dist/types/infra/storage/SessionStorageService.d.ts +14 -0
  196. package/dist/types/infra/timer/ITimer.d.ts +13 -0
  197. package/dist/types/infra/timer/SomaTimer.d.ts +21 -0
  198. package/dist/types/manifest/manifest-contract.d.ts +4 -0
  199. package/dist/types/middleware/SomaMiddlewareChain.d.ts +6 -0
  200. package/dist/types/middleware/createPermissionMiddleware.d.ts +14 -0
  201. package/dist/types/parser/annotation-extractor.d.ts +12 -0
  202. package/dist/types/parser/pipeline-parser.d.ts +62 -0
  203. package/dist/types/parser/style-config-parser.d.ts +20 -0
  204. package/dist/types/parser/timer-parser.d.ts +6 -0
  205. package/dist/types/parser/trigger-parser.d.ts +13 -0
  206. package/dist/types/parser/ui-config-parser.d.ts +34 -0
  207. package/dist/types/parser/yaml-loader.d.ts +21 -0
  208. package/dist/types/quality/SomaQualityRegistry.d.ts +31 -0
  209. package/dist/types/runtime/SomaRuntimeServices.d.ts +35 -0
  210. package/dist/types/runtime/SomaSnapshotStore.d.ts +13 -0
  211. package/dist/types/scheduler/SomaScheduler.d.ts +25 -0
  212. package/dist/types/types/domain.types.d.ts +82 -0
  213. package/dist/types/types/engine.types.d.ts +22 -0
  214. package/dist/types/types/event.types.d.ts +61 -0
  215. package/dist/types/types/keyboard.types.d.ts +4 -0
  216. package/dist/types/types/manifest.types.d.ts +29 -0
  217. package/dist/types/types/scheduler.types.d.ts +8 -0
  218. package/dist/types/types/style-config.types.d.ts +62 -0
  219. package/dist/types/types/timer.types.d.ts +19 -0
  220. package/dist/types/types/trigger.types.d.ts +12 -0
  221. package/dist/types/types/ui-config.types.d.ts +85 -0
  222. package/dist/types/types/watcher.types.d.ts +24 -0
  223. package/dist/types/validation/contract-validator.d.ts +40 -0
  224. package/editor/vscode/extension.js +39 -0
  225. package/editor/vscode/language-configuration.json +17 -0
  226. package/editor/vscode/package.json +98 -0
  227. package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +30 -0
  228. package/editor/vscode/syntaxes/soma-ui-yaml.tmLanguage.json +42 -0
  229. package/global.d.ts +22 -3
  230. package/next.config.js +73 -4
  231. package/package.json +58 -17
  232. package/tailwind.config.ts +36 -13
  233. package/tsconfig.json +6 -2
  234. package/core/router/RouteHandler.ts +0 -93
@@ -0,0 +1,71 @@
1
+ // core/engine/on-change-engine.ts
2
+ // 解析 _ui.yaml bind.onChange 并注册属性变更回调
3
+
4
+ import type { SomaScopeResolver } from './scope-resolver';
5
+ import type { SomaPropertyWatcher } from './property-watcher';
6
+ import type { OnChangeRule } from '../types/watcher.types';
7
+ import type { UiConfig } from '../types/ui-config.types';
8
+
9
+ export interface SomaOnChangeEngineOptions {
10
+ propertyWatcher: SomaPropertyWatcher;
11
+ resolver: SomaScopeResolver;
12
+ uiConfig: UiConfig;
13
+ proxiedInstance: object;
14
+ onRefresh?: () => void | Promise<void>;
15
+ }
16
+
17
+ export class SomaOnChangeEngine {
18
+ private readonly unwatchers: Array<() => void> = [];
19
+
20
+ init(options: SomaOnChangeEngineOptions): void {
21
+ const rules = options.uiConfig.bind.onChange;
22
+ if (!rules || rules.length === 0) {
23
+ return;
24
+ }
25
+
26
+ const varName = options.uiConfig.bind.var;
27
+
28
+ for (const rule of rules) {
29
+ this.registerRule(rule, varName, options);
30
+ }
31
+ }
32
+
33
+ destroy(): void {
34
+ for (const unwatch of this.unwatchers) {
35
+ unwatch();
36
+ }
37
+ this.unwatchers.length = 0;
38
+ }
39
+
40
+ private registerRule(
41
+ rule: OnChangeRule,
42
+ varName: string,
43
+ options: SomaOnChangeEngineOptions,
44
+ ): void {
45
+ const resolvedCondition = options.resolver.resolvePath(rule.condition);
46
+ const funcPath = rule.callback.includes('.')
47
+ ? rule.callback
48
+ : `${varName}.${rule.callback}`;
49
+
50
+ const unwatch = options.propertyWatcher.watch(
51
+ resolvedCondition,
52
+ async (newValue, oldValue) => {
53
+ try {
54
+ const fn = options.resolver.resolveFunction(funcPath);
55
+ const result = await fn(newValue, oldValue);
56
+ if (rule.refresh && result !== false) {
57
+ await options.onRefresh?.();
58
+ }
59
+ } catch (e) {
60
+ console.error(`[SomaOnChangeEngine] "${funcPath}" error:`, e);
61
+ }
62
+ },
63
+ {
64
+ prefix: false,
65
+ debounce: rule.debounce,
66
+ },
67
+ );
68
+
69
+ this.unwatchers.push(unwatch);
70
+ }
71
+ }
@@ -0,0 +1,117 @@
1
+ // core/engine/property-watcher.ts
2
+ // 属性变更观察者注册表 — RenderEngine.watch / onChange / triggers 共用
3
+
4
+ import type { WatchCallback, WatcherOptions } from '../types/watcher.types';
5
+
6
+ interface WatcherEntry {
7
+ id: number;
8
+ watchPath: string;
9
+ prefix: boolean;
10
+ glob: boolean;
11
+ callback: WatchCallback;
12
+ debounce?: number;
13
+ timer?: ReturnType<typeof setTimeout>;
14
+ }
15
+
16
+ let nextWatcherId = 1;
17
+
18
+ export class SomaPropertyWatcher {
19
+ private entries: WatcherEntry[] = [];
20
+ private root: object = {};
21
+
22
+ setRoot(root: object): void {
23
+ this.root = root;
24
+ }
25
+
26
+ /**
27
+ * 注册属性监听器。
28
+ * @returns 取消监听的函数
29
+ */
30
+ watch(
31
+ watchPath: string,
32
+ callback: WatchCallback,
33
+ options: WatcherOptions = {},
34
+ ): () => void {
35
+ const entry: WatcherEntry = {
36
+ id: nextWatcherId++,
37
+ watchPath,
38
+ prefix: options.prefix ?? !watchPath.includes('*'),
39
+ glob: options.glob ?? watchPath.includes('*'),
40
+ callback,
41
+ debounce: options.debounce,
42
+ };
43
+ this.entries.push(entry);
44
+
45
+ if (options.immediate) {
46
+ void this.invoke(entry, undefined, undefined, watchPath);
47
+ }
48
+
49
+ return () => {
50
+ if (entry.timer) clearTimeout(entry.timer);
51
+ this.entries = this.entries.filter((e) => e.id !== entry.id);
52
+ };
53
+ }
54
+
55
+ /** Proxy set trap 驱动:通知所有匹配的 watcher */
56
+ notify(changedPath: string, newValue: unknown, oldValue: unknown): void {
57
+ for (const entry of this.entries) {
58
+ if (!this.matches(entry.watchPath, changedPath, entry.prefix, entry.glob)) {
59
+ continue;
60
+ }
61
+
62
+ if (entry.debounce && entry.debounce > 0) {
63
+ if (entry.timer) clearTimeout(entry.timer);
64
+ entry.timer = setTimeout(() => {
65
+ entry.timer = undefined;
66
+ void this.invoke(entry, newValue, oldValue, changedPath);
67
+ }, entry.debounce);
68
+ } else {
69
+ void this.invoke(entry, newValue, oldValue, changedPath);
70
+ }
71
+ }
72
+ }
73
+
74
+ destroy(): void {
75
+ for (const entry of this.entries) {
76
+ if (entry.timer) clearTimeout(entry.timer);
77
+ }
78
+ this.entries = [];
79
+ }
80
+
81
+ private matches(watchPath: string, changedPath: string, prefix: boolean, glob: boolean): boolean {
82
+ if (glob || watchPath.includes('*')) {
83
+ return matchGlobPath(watchPath, changedPath);
84
+ }
85
+ if (!prefix) {
86
+ return watchPath === changedPath;
87
+ }
88
+ return changedPath === watchPath || changedPath.startsWith(`${watchPath}.`);
89
+ }
90
+
91
+ private async invoke(
92
+ entry: WatcherEntry,
93
+ newValue: unknown,
94
+ oldValue: unknown,
95
+ path: string,
96
+ ): Promise<void> {
97
+ try {
98
+ await entry.callback(newValue, oldValue, { path, root: this.root });
99
+ } catch (e) {
100
+ console.error(`[SomaPropertyWatcher] callback error at "${path}":`, e);
101
+ }
102
+ }
103
+ }
104
+
105
+ /** 通配符路径匹配:`*` 匹配单段(如 a.*.c 匹配 a.b.c) */
106
+ export function matchGlobPath(pattern: string, path: string): boolean {
107
+ const patternParts = pattern.split('.');
108
+ const pathParts = path.split('.');
109
+ if (patternParts.length !== pathParts.length) {
110
+ return false;
111
+ }
112
+ for (let i = 0; i < patternParts.length; i++) {
113
+ if (patternParts[i] === '*') continue;
114
+ if (patternParts[i] !== pathParts[i]) return false;
115
+ }
116
+ return true;
117
+ }
@@ -23,12 +23,12 @@ export class SomaReactiveProxy {
23
23
  private childProxyCache = new WeakMap<object, any>();
24
24
 
25
25
  /** 变更回调(可延迟设置) */
26
- private onChange: ((path: string, value: any) => void) | null = null;
26
+ private onChange: ((path: string, value: any, oldValue: any) => void) | null = null;
27
27
  private diagnostics: SomaDiagnostics | null = null;
28
28
  private readonly devMode: boolean;
29
29
 
30
30
  constructor(
31
- onChange?: (path: string, value: any) => void,
31
+ onChange?: (path: string, value: any, oldValue: any) => void,
32
32
  diagnostics?: SomaDiagnostics,
33
33
  devMode: boolean = true,
34
34
  ) {
@@ -38,7 +38,7 @@ export class SomaReactiveProxy {
38
38
  }
39
39
 
40
40
  /** 延迟设置变更回调 */
41
- setOnChange(fn: ((path: string, value: any) => void) | null): void {
41
+ setOnChange(fn: ((path: string, value: any, oldValue: any) => void) | null): void {
42
42
  this.onChange = fn;
43
43
  }
44
44
 
@@ -71,7 +71,7 @@ export class SomaReactiveProxy {
71
71
  // 一次性触发所有变更通知
72
72
  for (const [path, value] of paths) {
73
73
  try {
74
- if (this.onChange) this.onChange(path, value);
74
+ if (this.onChange) this.onChange(path, value, undefined);
75
75
  } catch (e) {
76
76
  console.error('[SomaReactiveProxy] batch onChange error:', e);
77
77
  }
@@ -122,6 +122,11 @@ export class SomaReactiveProxy {
122
122
 
123
123
  const value = Reflect.get(target, prop, target);
124
124
 
125
+ // 引擎注入的基础设施不代理(Storage/DOM API 经 Proxy 会 Illegal invocation)
126
+ if (prop === '_services' || prop === '_config') {
127
+ return value;
128
+ }
129
+
125
130
  // 快速路径:原始值和 null/undefined 直接返回,无需任何判断
126
131
  if (value === null || value === undefined) return value;
127
132
  const t = typeof value;
@@ -171,7 +176,7 @@ export class SomaReactiveProxy {
171
176
  // batch 模式:收集变更路径,延迟到 endBatch 统一触发
172
177
  self.batchedPaths.set(path, value);
173
178
  } else if (self.onChange) {
174
- self.onChange(path, value);
179
+ self.onChange(path, value, oldValue);
175
180
  }
176
181
  } catch (e) {
177
182
  console.error('[SomaReactiveProxy] onChange error:', e);
@@ -209,6 +214,7 @@ export class SomaReactiveProxy {
209
214
  private shouldSkip(obj: any): boolean {
210
215
  if (obj == null) return true;
211
216
  if (Array.isArray(obj)) return true; // 数组不代理,通过引用替换触发更新
217
+ if (typeof Storage !== 'undefined' && obj instanceof Storage) return true;
212
218
  return (
213
219
  obj instanceof Set ||
214
220
  obj instanceof Map ||
@@ -3,23 +3,26 @@ import type { BindingEntry } from '../types/engine.types';
3
3
  import type { SomaRuntimeManifest } from '../types/manifest.types';
4
4
  import { SomaScheduler } from '../scheduler/SomaScheduler';
5
5
  import { SomaMiddlewareChain } from '../middleware/SomaMiddlewareChain';
6
- import type { SomaContainerAdapter } from '../container/SomaContainerAdapter';
6
+ import type { IContainerAdapter } from '../container/IContainerAdapter';
7
7
  import { SomaScopeResolver } from './scope-resolver';
8
8
  import { SomaDomRenderer } from './dom-renderer';
9
+ import { SomaPropertyWatcher } from './property-watcher';
10
+ import type { WatchCallback, WatcherOptions } from '../types/watcher.types';
9
11
 
10
12
  export interface SomaRenderEngineOptions {
11
13
  container: HTMLElement;
12
- adapter: SomaContainerAdapter;
14
+ adapter: IContainerAdapter;
13
15
  manifest: SomaRuntimeManifest;
14
16
  proxiedInstance: any;
15
17
  resolver: SomaScopeResolver;
16
18
  scheduler?: SomaScheduler;
17
19
  diagnostics?: SomaDiagnostics;
18
20
  middleware?: SomaMiddlewareChain<SomaRenderMiddlewareContext>;
21
+ propertyWatcher?: SomaPropertyWatcher;
19
22
  }
20
23
 
21
24
  export interface SomaRenderMiddlewareContext {
22
- phase: 'full-render' | 'patch';
25
+ phase: 'full-render' | 'patch' | 'commit';
23
26
  path?: string;
24
27
  changedPaths: string[];
25
28
  bindings: BindingEntry[];
@@ -27,7 +30,7 @@ export interface SomaRenderMiddlewareContext {
27
30
 
28
31
  export class SomaRenderEngine {
29
32
  private readonly container: HTMLElement;
30
- private readonly adapter: SomaContainerAdapter;
33
+ private readonly adapter: IContainerAdapter;
31
34
  private readonly resolver: SomaScopeResolver;
32
35
  private readonly renderer: SomaDomRenderer;
33
36
  private readonly scheduler: SomaScheduler;
@@ -38,8 +41,11 @@ export class SomaRenderEngine {
38
41
  private scopeTracker = new WeakMap<HTMLElement, any>();
39
42
  private pendingPatches = new Map<string, any>();
40
43
  private flushScheduled = false;
44
+ private transactionDepth = 0;
41
45
  private destroyed = false;
42
46
  private proxiedInstance: any;
47
+ private readonly propertyWatcher: SomaPropertyWatcher;
48
+ private initProps: Record<string, unknown> = {};
43
49
 
44
50
  constructor(options: SomaRenderEngineOptions) {
45
51
  this.container = options.container;
@@ -49,6 +55,9 @@ export class SomaRenderEngine {
49
55
  this.diagnostics = options.diagnostics ?? null;
50
56
  this.middleware = options.middleware ?? null;
51
57
  this.proxiedInstance = options.proxiedInstance;
58
+ this.propertyWatcher = options.propertyWatcher ?? new SomaPropertyWatcher();
59
+ this.propertyWatcher.setRoot(options.proxiedInstance);
60
+ this.initProps = { ...(options.manifest.initProps ?? {}) };
52
61
 
53
62
  this.renderer = new SomaDomRenderer();
54
63
  this.renderer.init(this.adapter, this.resolver, this.scopeTracker);
@@ -70,6 +79,27 @@ export class SomaRenderEngine {
70
79
  return this.scopeTracker;
71
80
  }
72
81
 
82
+ /** 注册属性监听器(文档 §10.2 watch API) */
83
+ watch(
84
+ path: string,
85
+ callback: WatchCallback,
86
+ options?: WatcherOptions,
87
+ ): () => void {
88
+ return this.propertyWatcher.watch(path, callback, options);
89
+ }
90
+
91
+ getPropertyWatcher(): SomaPropertyWatcher {
92
+ return this.propertyWatcher;
93
+ }
94
+
95
+ setInitProps(props: Record<string, unknown>): void {
96
+ this.initProps = { ...props };
97
+ }
98
+
99
+ getInitProps(): Record<string, unknown> {
100
+ return { ...this.initProps };
101
+ }
102
+
73
103
  render(): void {
74
104
  void this.executeRender({
75
105
  phase: 'full-render',
@@ -79,7 +109,43 @@ export class SomaRenderEngine {
79
109
  }
80
110
 
81
111
  flush(): Promise<void> {
82
- return this.flushPatches();
112
+ return this.flushPatches('patch');
113
+ }
114
+
115
+ /** 事件事务开始:defer 数据 patch flush,仅收集 dirty path */
116
+ beginTransaction(): void {
117
+ this.transactionDepth++;
118
+ }
119
+
120
+ /** 事件事务结束:不应再额外 flush(commit 已在 handler 后完成) */
121
+ endTransaction(): void {
122
+ if (this.transactionDepth > 0) {
123
+ this.transactionDepth--;
124
+ }
125
+ }
126
+
127
+ /** 一次性提交事务内积累的 dirty path(Phase 2 commit 点) */
128
+ commit(): Promise<void> {
129
+ return this.flushPatches('commit');
130
+ }
131
+
132
+ /** force 全量 render 前丢弃 pending patch(避免 commit + render 双写) */
133
+ discardPendingPatches(): void {
134
+ this.pendingPatches.clear();
135
+ this.flushScheduled = false;
136
+ }
137
+
138
+ isInTransaction(): boolean {
139
+ return this.transactionDepth > 0;
140
+ }
141
+
142
+ /** 将 DOM 表单控件的当前值写回代理实例(点击提交前同步) */
143
+ syncModels(): void {
144
+ for (const entry of this.bindingTable) {
145
+ if (entry.type === 'model') {
146
+ this.writeToProxy(entry.dataPath, this.adapter.getValue(entry.element));
147
+ }
148
+ }
83
149
  }
84
150
 
85
151
  destroy(): void {
@@ -87,6 +153,7 @@ export class SomaRenderEngine {
87
153
  this.pendingPatches.clear();
88
154
  this.bindingTable = [];
89
155
  this.bindingIndex.clear();
156
+ this.propertyWatcher.destroy();
90
157
  this.renderer.destroy();
91
158
  }
92
159
 
@@ -96,16 +163,25 @@ export class SomaRenderEngine {
96
163
  }
97
164
 
98
165
  this.pendingPatches.set(path, value);
166
+
167
+ if (this.transactionDepth > 0) {
168
+ return;
169
+ }
170
+
171
+ this.ensureFlushScheduled(path);
172
+ }
173
+
174
+ private ensureFlushScheduled(label: string): void {
99
175
  if (this.flushScheduled) {
100
176
  return;
101
177
  }
102
178
 
103
179
  this.flushScheduled = true;
104
180
  this.scheduler.schedule(
105
- () => this.flushPatches(),
181
+ () => this.flushPatches('patch'),
106
182
  {
107
183
  priority: 'visible',
108
- label: `render:${path}`,
184
+ label: `render:${label}`,
109
185
  },
110
186
  ).catch(() => {
111
187
  // 静默忽略:unmount 时 cancelAll 会 reject 未执行的任务
@@ -176,7 +252,7 @@ export class SomaRenderEngine {
176
252
  return result;
177
253
  }
178
254
 
179
- private async flushPatches(): Promise<void> {
255
+ private async flushPatches(phase: 'patch' | 'commit' = 'patch'): Promise<void> {
180
256
  this.flushScheduled = false;
181
257
  if (this.destroyed || this.pendingPatches.size === 0) {
182
258
  return;
@@ -209,7 +285,7 @@ export class SomaRenderEngine {
209
285
  const bindings = [...affectedBindings.keys()];
210
286
  const patchValues = affectedBindings;
211
287
  await this.executeRender({
212
- phase: 'patch',
288
+ phase,
213
289
  changedPaths,
214
290
  bindings,
215
291
  }, patchValues);
@@ -221,8 +297,7 @@ export class SomaRenderEngine {
221
297
  ): Promise<void> {
222
298
  const startedAt = this.adapter.now();
223
299
  const terminal = async (current: SomaRenderMiddlewareContext): Promise<void> => {
224
- for (const entry of current.bindings) {
225
- // 优先使用 patch 值(精确匹配),否则用编译后的 getter 直接读取
300
+ const renderBinding = (entry: BindingEntry): void => {
226
301
  const patchValue = patchValues?.get(entry);
227
302
  const value = patchValue !== undefined
228
303
  ? patchValue
@@ -230,6 +305,17 @@ export class SomaRenderEngine {
230
305
  ? entry.compiledGetter(this.proxiedInstance)
231
306
  : this.resolver.readFromRoot(entry.dataPath);
232
307
  this.renderer.renderEntry(entry, value);
308
+ };
309
+
310
+ if (current.phase === 'patch' || current.phase === 'commit') {
311
+ // patch/commit 阶段:各 binding 互不依赖,并发调度(同一 microtask 内完成)
312
+ await Promise.all(
313
+ current.bindings.map((entry) => Promise.resolve().then(() => renderBinding(entry))),
314
+ );
315
+ } else {
316
+ for (const entry of current.bindings) {
317
+ renderBinding(entry);
318
+ }
233
319
  }
234
320
 
235
321
  this.diagnostics?.recordRender({
@@ -2,23 +2,36 @@
2
2
  // 职责:从解析后的资产 + DOM 容器组装完整的运行时引擎组
3
3
 
4
4
  import { SomaManifestCompiler } from '../compiler/manifest-compiler';
5
- import type { SomaContainerAdapter } from '../container/SomaContainerAdapter';
5
+ import type { IContainerAdapter } from '../container/IContainerAdapter';
6
6
  import type { SomaDiagnostics } from '../diagnostics/SomaDiagnostics';
7
7
  import type { SomaMiddlewareChain } from '../middleware/SomaMiddlewareChain';
8
8
  import type { SomaScheduler } from '../scheduler/SomaScheduler';
9
9
  import type { ParsedEvent } from '../types/event.types';
10
+ import type { PropertyTrigger } from '../types/trigger.types';
11
+ import type { ParsedTimer } from '../types/timer.types';
12
+ import type { ITimer } from '../infra/timer/ITimer';
13
+ import type { StyleConfig } from '../types/style-config.types';
10
14
  import type { UiConfig } from '../types/ui-config.types';
11
15
  import { SomaContractValidator } from '../validation/contract-validator';
12
16
  import { SomaEventEngine, type SomaEventMiddlewareContext } from './event-engine';
17
+ import { SomaOnChangeEngine } from './on-change-engine';
13
18
  import type { SomaRenderMiddlewareContext } from './render-engine';
14
19
  import { SomaRenderEngine } from './render-engine';
20
+ import { SomaPropertyWatcher } from './property-watcher';
15
21
  import { SomaScopeResolver } from './scope-resolver';
22
+ import { SomaStyleBinder } from './style-binder';
23
+ import { SomaTriggerEngine } from './trigger-engine';
24
+ import { SomaTimerEngine } from './timer-engine';
16
25
 
17
26
  export interface RuntimeBuildInput {
18
27
  container: HTMLElement;
19
- adapter: SomaContainerAdapter;
28
+ adapter: IContainerAdapter;
20
29
  uiConfig: UiConfig;
21
30
  eventConfig: ParsedEvent[];
31
+ triggers?: PropertyTrigger[];
32
+ timers?: ParsedTimer[];
33
+ timerService?: ITimer;
34
+ styleConfig?: StyleConfig;
22
35
  proxiedInstance: any;
23
36
  scheduler: SomaScheduler;
24
37
  diagnostics: SomaDiagnostics;
@@ -29,18 +42,16 @@ export interface RuntimeBuildInput {
29
42
  export interface SomaRuntime {
30
43
  renderEngine: SomaRenderEngine;
31
44
  eventEngine: SomaEventEngine;
45
+ styleBinder: SomaStyleBinder | null;
46
+ propertyWatcher: SomaPropertyWatcher;
47
+ onChangeEngine: SomaOnChangeEngine;
48
+ triggerEngine: SomaTriggerEngine;
49
+ timerEngine: SomaTimerEngine;
32
50
  resolver: SomaScopeResolver;
33
51
  }
34
52
 
35
53
  /**
36
54
  * 运行时构建器:将配置 + 实例 + DOM 组装为可运行的引擎组。
37
- *
38
- * 编排流程:
39
- * 1. SomaScopeResolver 构建变量映射
40
- * 2. SomaContractValidator 校验契约
41
- * 3. SomaManifestCompiler 编译 manifest
42
- * 4. SomaRenderEngine 创建 + 首次渲染
43
- * 5. SomaEventEngine 创建 + 事件绑定
44
55
  */
45
56
  export class SomaRuntimeBuilder {
46
57
  private readonly manifestCompiler = new SomaManifestCompiler();
@@ -66,6 +77,9 @@ export class SomaRuntimeBuilder {
66
77
  resolver,
67
78
  });
68
79
 
80
+ const propertyWatcher = new SomaPropertyWatcher();
81
+ propertyWatcher.setRoot(input.proxiedInstance);
82
+
69
83
  const renderEngine = new SomaRenderEngine({
70
84
  container: input.container,
71
85
  adapter: input.adapter,
@@ -75,6 +89,7 @@ export class SomaRuntimeBuilder {
75
89
  scheduler: input.scheduler,
76
90
  diagnostics: input.diagnostics,
77
91
  middleware: input.renderMiddleware,
92
+ propertyWatcher,
78
93
  });
79
94
 
80
95
  renderEngine.render();
@@ -87,7 +102,11 @@ export class SomaRuntimeBuilder {
87
102
  scopeTracker: renderEngine.getScopeTracker(),
88
103
  callbacks: {
89
104
  onRefresh: () => renderEngine.render(),
90
- onFlush: () => renderEngine.flush() ?? Promise.resolve(),
105
+ onCommit: () => renderEngine.commit(),
106
+ onBeginTransaction: () => renderEngine.beginTransaction(),
107
+ onEndTransaction: () => renderEngine.endTransaction(),
108
+ onDiscardPending: () => renderEngine.discardPendingPatches(),
109
+ onSyncModels: () => renderEngine.syncModels(),
91
110
  },
92
111
  diagnostics: input.diagnostics,
93
112
  scheduler: input.scheduler,
@@ -95,6 +114,64 @@ export class SomaRuntimeBuilder {
95
114
  });
96
115
  eventEngine.init();
97
116
 
98
- return { renderEngine, eventEngine, resolver };
117
+ let styleBinder: SomaStyleBinder | null = null;
118
+ if (input.styleConfig) {
119
+ styleBinder = new SomaStyleBinder({
120
+ container: input.container,
121
+ adapter: input.adapter,
122
+ styleConfig: input.styleConfig,
123
+ });
124
+ styleBinder.init();
125
+ }
126
+
127
+ const onChangeEngine = new SomaOnChangeEngine();
128
+ onChangeEngine.init({
129
+ propertyWatcher,
130
+ resolver,
131
+ uiConfig: input.uiConfig,
132
+ proxiedInstance: input.proxiedInstance,
133
+ onRefresh: () => renderEngine.render(),
134
+ });
135
+
136
+ const triggerEngine = new SomaTriggerEngine();
137
+ triggerEngine.init({
138
+ propertyWatcher,
139
+ resolver,
140
+ container: input.container,
141
+ adapter: input.adapter,
142
+ triggers: input.triggers ?? [],
143
+ });
144
+
145
+ const timerEngine = new SomaTimerEngine();
146
+
147
+ return {
148
+ renderEngine,
149
+ eventEngine,
150
+ styleBinder,
151
+ propertyWatcher,
152
+ onChangeEngine,
153
+ triggerEngine,
154
+ timerEngine,
155
+ resolver,
156
+ };
157
+ }
158
+
159
+ /** onMount 之后启动声明式定时器 */
160
+ startTimers(
161
+ runtime: Pick<SomaRuntime, 'eventEngine' | 'timerEngine'>,
162
+ options: {
163
+ container: HTMLElement;
164
+ adapter: IContainerAdapter;
165
+ timers: ParsedTimer[];
166
+ timerService: ITimer;
167
+ },
168
+ ): void {
169
+ runtime.timerEngine.start({
170
+ container: options.container,
171
+ adapter: options.adapter,
172
+ timer: options.timerService,
173
+ eventEngine: runtime.eventEngine,
174
+ timers: options.timers,
175
+ });
99
176
  }
100
177
  }