soma-core 1.0.1 → 1.0.2

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 (227) hide show
  1. package/app/SomaPageClient.tsx +336 -0
  2. package/app/api/[...path]/route.ts +9 -85
  3. package/app/api/page/route.ts +18 -16
  4. package/app/api/soma-manifest/route.ts +39 -0
  5. package/app/components/SomaHydrationShell.tsx +23 -0
  6. package/app/layout.tsx +2 -2
  7. package/app/loading.tsx +5 -0
  8. package/app/page.tsx +19 -128
  9. package/bin/soma-lsp.cjs +195 -0
  10. package/bin/soma-sync.cjs +3 -0
  11. package/bin/soma.cjs +14 -2
  12. package/core/api/IService.ts +6 -10
  13. package/core/api/load-host-service.ts +72 -0
  14. package/core/api/service-path.ts +39 -0
  15. package/core/base/SomaEntity.ts +3 -10
  16. package/core/base/SomaLocaleOnlyPageRoot.ts +3 -4
  17. package/core/base/SomaPageRootBase.ts +20 -3
  18. package/core/base/decorators/Event.ts +23 -0
  19. package/core/base/decorators/Locale.ts +35 -0
  20. package/core/base/decorators/Page.ts +16 -0
  21. package/core/base/decorators/Show.ts +20 -0
  22. package/core/base/decorators/ShowList.ts +34 -0
  23. package/core/base/decorators/index.ts +5 -0
  24. package/core/base/index.ts +2 -0
  25. package/core/base/runAsync.ts +32 -0
  26. package/core/compiler/full-manifest-builder.ts +33 -7
  27. package/core/compiler/host-config-sync.cjs +38 -0
  28. package/core/compiler/manifest-compiler.ts +3 -2
  29. package/core/compiler/page-base-resolver.ts +23 -0
  30. package/core/compiler/precompiled-manifest-loader.ts +34 -0
  31. package/core/compiler/rollup-plugin-soma.ts +45 -0
  32. package/core/compiler/rspack-plugin-soma.js +33 -0
  33. package/core/compiler/soma-manifest-webpack-plugin.js +72 -0
  34. package/core/compiler/soma-precompile.js +3513 -0
  35. package/core/compiler/soma-precompile.ts +52 -6
  36. package/core/compiler/soma-rspack-yaml-loader.js +22 -0
  37. package/core/compiler/soma-webpack-loader.js +7 -1
  38. package/core/compiler/vite-plugin-soma.ts +11 -1
  39. package/core/container/IContainerAdapter.ts +36 -0
  40. package/core/container/MiniProgramContainerAdapter.ts +190 -0
  41. package/core/container/ReactNativeContainerAdapter.ts +189 -0
  42. package/core/container/SomaContainerAdapter.ts +17 -6
  43. package/core/diagnostics/SomaDiagnostics.ts +1 -1
  44. package/core/diagnostics/SomaTelemetry.ts +100 -0
  45. package/core/diagnostics/telemetry-context.ts +15 -0
  46. package/core/engine/asset-resolver.ts +39 -4
  47. package/core/engine/dom-renderer.ts +9 -11
  48. package/core/engine/event-engine.ts +110 -41
  49. package/core/engine/on-change-engine.ts +71 -0
  50. package/core/engine/property-watcher.ts +117 -0
  51. package/core/engine/reactive-proxy.ts +11 -5
  52. package/core/engine/render-engine.ts +97 -11
  53. package/core/engine/runtime-builder.ts +88 -11
  54. package/core/engine/scope-resolver.ts +65 -9
  55. package/core/engine/soma-bootstrap.ts +448 -244
  56. package/core/engine/style-binder.ts +423 -0
  57. package/core/engine/theme-variables.ts +37 -0
  58. package/core/engine/timer-engine.ts +92 -0
  59. package/core/engine/trigger-engine.ts +55 -0
  60. package/core/host/resolve-host-bootstrap.ts +28 -0
  61. package/core/host/soma-bootstrap-host.stub.ts +4 -0
  62. package/core/index.ts +33 -2
  63. package/core/infra/auth/TokenAuthService.ts +17 -3
  64. package/core/infra/http/IHttpClient.ts +11 -1
  65. package/core/infra/http/SomaHttpService.ts +89 -31
  66. package/core/infra/i18n/DefaultI18n.ts +6 -18
  67. package/core/infra/i18n/II18n.ts +2 -3
  68. package/core/infra/notify/DomNotifyService.ts +98 -0
  69. package/core/infra/notify/INotifyTheme.ts +22 -0
  70. package/core/infra/realtime/IRealtimeService.ts +23 -0
  71. package/core/infra/realtime/IRealtimeTransport.ts +9 -0
  72. package/core/infra/realtime/SomaRealtimeService.ts +95 -0
  73. package/core/infra/realtime/SseRealtimeTransport.ts +36 -0
  74. package/core/infra/realtime/WebSocketRealtimeTransport.ts +102 -0
  75. package/core/infra/router/router-path.ts +28 -0
  76. package/core/infra/storage/SessionStorageService.ts +59 -0
  77. package/core/lsp/cross-file-diagnostics.cjs +160 -0
  78. package/core/lsp/event-yaml-completion.cjs +106 -0
  79. package/core/lsp/extra-completion.cjs +64 -0
  80. package/core/lsp/lsp-navigation.cjs +191 -0
  81. package/core/lsp/ui-yaml-completion.cjs +170 -0
  82. package/core/manifest/manifest-contract.ts +12 -0
  83. package/core/middleware/createPermissionMiddleware.ts +37 -0
  84. package/core/parser/annotation-extractor.ts +113 -0
  85. package/core/parser/pipeline-parser.ts +344 -276
  86. package/core/parser/style-config-parser.ts +340 -0
  87. package/core/parser/timer-parser.ts +70 -0
  88. package/core/parser/trigger-parser.ts +90 -0
  89. package/core/parser/ui-config-parser.ts +73 -1
  90. package/core/quality/SomaQualityRegistry.ts +68 -0
  91. package/core/runtime/SomaRuntimeServices.ts +100 -0
  92. package/core/runtime/SomaSnapshotStore.ts +5 -3
  93. package/core/types/domain.types.ts +5 -2
  94. package/core/types/event.types.ts +82 -57
  95. package/core/types/manifest.types.ts +2 -0
  96. package/core/types/style-config.types.ts +76 -0
  97. package/core/types/timer.types.ts +23 -0
  98. package/core/types/trigger.types.ts +14 -0
  99. package/core/types/ui-config.types.ts +9 -0
  100. package/core/types/watcher.types.ts +29 -0
  101. package/dist/cli/soma.js +23 -23
  102. package/dist/core/index.cjs +8968 -0
  103. package/dist/diagnostics/telemetry.cjs +105 -0
  104. package/dist/preview-export.cjs +3401 -0
  105. package/dist/tsconfig.types.tsbuildinfo +1 -0
  106. package/dist/types/api/IService.d.ts +28 -0
  107. package/dist/types/api/load-host-service.d.ts +5 -0
  108. package/dist/types/api/service-path.d.ts +13 -0
  109. package/dist/types/base/SomaApi.d.ts +5 -0
  110. package/dist/types/base/SomaDict.d.ts +89 -0
  111. package/dist/types/base/SomaEntity.d.ts +15 -0
  112. package/dist/types/base/SomaHttpClient.d.ts +66 -0
  113. package/dist/types/base/SomaLocaleOnlyPageRoot.d.ts +3 -0
  114. package/dist/types/base/SomaPageRootBase.d.ts +49 -0
  115. package/dist/types/base/SomaValidator.d.ts +55 -0
  116. package/dist/types/base/decorators/Event.d.ts +10 -0
  117. package/dist/types/base/decorators/Locale.d.ts +9 -0
  118. package/dist/types/base/decorators/Page.d.ts +4 -0
  119. package/dist/types/base/decorators/Show.d.ts +7 -0
  120. package/dist/types/base/decorators/ShowList.d.ts +12 -0
  121. package/dist/types/base/decorators/index.d.ts +5 -0
  122. package/dist/types/base/index.d.ts +9 -0
  123. package/dist/types/base/runAsync.d.ts +12 -0
  124. package/dist/types/compiler/full-manifest-builder.d.ts +35 -0
  125. package/dist/types/compiler/manifest-compiler.d.ts +29 -0
  126. package/dist/types/compiler/page-base-resolver.d.ts +6 -0
  127. package/dist/types/compiler/precompiled-manifest-loader.d.ts +5 -0
  128. package/dist/types/compiler/rollup-plugin-soma.d.ts +14 -0
  129. package/dist/types/compiler/route-builder.d.ts +12 -0
  130. package/dist/types/compiler/soma-precompile.d.ts +40 -0
  131. package/dist/types/compiler/vite-plugin-soma.d.ts +34 -0
  132. package/dist/types/container/IContainerAdapter.d.ts +30 -0
  133. package/dist/types/container/MiniProgramContainerAdapter.d.ts +52 -0
  134. package/dist/types/container/ReactNativeContainerAdapter.d.ts +47 -0
  135. package/dist/types/container/SomaContainerAdapter.d.ts +30 -0
  136. package/dist/types/diagnostics/SomaDiagnostics.d.ts +64 -0
  137. package/dist/types/diagnostics/SomaTelemetry.d.ts +32 -0
  138. package/dist/types/diagnostics/telemetry-context.d.ts +4 -0
  139. package/dist/types/engine/asset-resolver.d.ts +17 -0
  140. package/dist/types/engine/dom-renderer.d.ts +35 -0
  141. package/dist/types/engine/event-engine.d.ts +64 -0
  142. package/dist/types/engine/lis.d.ts +10 -0
  143. package/dist/types/engine/on-change-engine.d.ts +16 -0
  144. package/dist/types/engine/property-watcher.d.ts +18 -0
  145. package/dist/types/engine/reactive-proxy.d.ts +58 -0
  146. package/dist/types/engine/render-engine.d.ts +73 -0
  147. package/dist/types/engine/runtime-builder.d.ts +58 -0
  148. package/dist/types/engine/scope-resolver.d.ts +90 -0
  149. package/dist/types/engine/soma-bootstrap.d.ts +112 -0
  150. package/dist/types/engine/style-binder.d.ts +81 -0
  151. package/dist/types/engine/theme-variables.d.ts +3 -0
  152. package/dist/types/engine/timer-engine.d.ts +19 -0
  153. package/dist/types/engine/trigger-engine.d.ts +16 -0
  154. package/dist/types/host/resolve-host-bootstrap.d.ts +11 -0
  155. package/dist/types/host/soma-bootstrap-host.stub.d.ts +3 -0
  156. package/dist/types/index.d.ts +35 -0
  157. package/dist/types/infra/auth/IAuthService.d.ts +12 -0
  158. package/dist/types/infra/auth/TokenAuthService.d.ts +26 -0
  159. package/dist/types/infra/cache/ICache.d.ts +12 -0
  160. package/dist/types/infra/cache/MemoryCache.d.ts +14 -0
  161. package/dist/types/infra/cache/ServerFileCache.d.ts +37 -0
  162. package/dist/types/infra/config/IConfig.d.ts +9 -0
  163. package/dist/types/infra/config/SomaConfig.d.ts +21 -0
  164. package/dist/types/infra/event-bus/IEventBus.d.ts +8 -0
  165. package/dist/types/infra/event-bus/SomaEventBus.d.ts +9 -0
  166. package/dist/types/infra/http/IHttpClient.d.ts +28 -0
  167. package/dist/types/infra/http/SomaHttpService.d.ts +27 -0
  168. package/dist/types/infra/i18n/DefaultI18n.d.ts +7 -0
  169. package/dist/types/infra/i18n/II18n.d.ts +6 -0
  170. package/dist/types/infra/logger/ConsoleLogger.d.ts +14 -0
  171. package/dist/types/infra/logger/ILogger.d.ts +17 -0
  172. package/dist/types/infra/notify/DefaultNotifyService.d.ts +16 -0
  173. package/dist/types/infra/notify/DomNotifyService.d.ts +16 -0
  174. package/dist/types/infra/notify/INotifyService.d.ts +12 -0
  175. package/dist/types/infra/notify/INotifyTheme.d.ts +12 -0
  176. package/dist/types/infra/permission/IPermission.d.ts +10 -0
  177. package/dist/types/infra/permission/SomaPermission.d.ts +19 -0
  178. package/dist/types/infra/realtime/IRealtimeService.d.ts +13 -0
  179. package/dist/types/infra/realtime/IRealtimeTransport.d.ts +8 -0
  180. package/dist/types/infra/realtime/SomaRealtimeService.d.ts +20 -0
  181. package/dist/types/infra/realtime/SseRealtimeTransport.d.ts +11 -0
  182. package/dist/types/infra/realtime/WebSocketRealtimeTransport.d.ts +30 -0
  183. package/dist/types/infra/router/IRouter.d.ts +16 -0
  184. package/dist/types/infra/router/SomaRouter.d.ts +18 -0
  185. package/dist/types/infra/router/router-path.d.ts +6 -0
  186. package/dist/types/infra/storage/IStorageService.d.ts +7 -0
  187. package/dist/types/infra/storage/MemoryStorageService.d.ts +15 -0
  188. package/dist/types/infra/storage/SessionStorageService.d.ts +14 -0
  189. package/dist/types/infra/timer/ITimer.d.ts +13 -0
  190. package/dist/types/infra/timer/SomaTimer.d.ts +21 -0
  191. package/dist/types/manifest/manifest-contract.d.ts +4 -0
  192. package/dist/types/middleware/SomaMiddlewareChain.d.ts +6 -0
  193. package/dist/types/middleware/createPermissionMiddleware.d.ts +14 -0
  194. package/dist/types/parser/annotation-extractor.d.ts +12 -0
  195. package/dist/types/parser/pipeline-parser.d.ts +62 -0
  196. package/dist/types/parser/style-config-parser.d.ts +20 -0
  197. package/dist/types/parser/timer-parser.d.ts +6 -0
  198. package/dist/types/parser/trigger-parser.d.ts +13 -0
  199. package/dist/types/parser/ui-config-parser.d.ts +34 -0
  200. package/dist/types/parser/yaml-loader.d.ts +21 -0
  201. package/dist/types/quality/SomaQualityRegistry.d.ts +31 -0
  202. package/dist/types/runtime/SomaRuntimeServices.d.ts +35 -0
  203. package/dist/types/runtime/SomaSnapshotStore.d.ts +13 -0
  204. package/dist/types/scheduler/SomaScheduler.d.ts +25 -0
  205. package/dist/types/types/domain.types.d.ts +82 -0
  206. package/dist/types/types/engine.types.d.ts +22 -0
  207. package/dist/types/types/event.types.d.ts +61 -0
  208. package/dist/types/types/keyboard.types.d.ts +4 -0
  209. package/dist/types/types/manifest.types.d.ts +29 -0
  210. package/dist/types/types/scheduler.types.d.ts +8 -0
  211. package/dist/types/types/style-config.types.d.ts +62 -0
  212. package/dist/types/types/timer.types.d.ts +19 -0
  213. package/dist/types/types/trigger.types.d.ts +12 -0
  214. package/dist/types/types/ui-config.types.d.ts +85 -0
  215. package/dist/types/types/watcher.types.d.ts +24 -0
  216. package/dist/types/validation/contract-validator.d.ts +40 -0
  217. package/editor/vscode/extension.js +39 -0
  218. package/editor/vscode/language-configuration.json +17 -0
  219. package/editor/vscode/package.json +98 -0
  220. package/editor/vscode/syntaxes/soma-event-yaml.tmLanguage.json +30 -0
  221. package/editor/vscode/syntaxes/soma-ui-yaml.tmLanguage.json +42 -0
  222. package/global.d.ts +19 -0
  223. package/next.config.js +9 -4
  224. package/package.json +53 -16
  225. package/tailwind.config.ts +26 -7
  226. package/tsconfig.json +6 -2
  227. package/core/router/RouteHandler.ts +0 -93
@@ -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
  }
@@ -1,6 +1,11 @@
1
1
  // core/engine/scope-resolver.ts
2
2
 
3
- import type { UiConfig, NestedShowMapping } from '../types/ui-config.types';
3
+ import type {
4
+ ExtendedShowValue,
5
+ NestedShowMapping,
6
+ ShowMapping,
7
+ UiConfig,
8
+ } from '../types/ui-config.types';
4
9
 
5
10
  /**
6
11
  * SOMA 作用域解析器
@@ -75,6 +80,36 @@ export class SomaScopeResolver {
75
80
  return this.readValue(this.rootInstance, path);
76
81
  }
77
82
 
83
+ /**
84
+ * 将值写入根实例上的路径(支持 var 别名)。
85
+ */
86
+ writeToRoot(path: string, value: unknown): void {
87
+ const resolved = this.resolvePath(path);
88
+ this.writeValue(this.rootInstance, resolved, value);
89
+ }
90
+
91
+ /**
92
+ * 将值写入对象上的属性路径,中间缺失的对象会自动创建。
93
+ */
94
+ writeValue(obj: object, path: string, value: unknown): void {
95
+ if (!path) {
96
+ return;
97
+ }
98
+
99
+ const segments = path.split('.');
100
+ let current: any = obj;
101
+
102
+ for (let i = 0; i < segments.length - 1; i++) {
103
+ const seg = segments[i];
104
+ if (current[seg] == null || typeof current[seg] !== 'object') {
105
+ current[seg] = {};
106
+ }
107
+ current = current[seg];
108
+ }
109
+
110
+ current[segments[segments.length - 1]] = value;
111
+ }
112
+
78
113
  // ─── 编译优化:预编译属性访问器 ────────────────────────
79
114
 
80
115
  /** 编译缓存:path → compiled getter */
@@ -220,19 +255,40 @@ export class SomaScopeResolver {
220
255
  }
221
256
  }
222
257
 
223
- // 递归处理嵌套的 NestedShowMapping
224
258
  if (config.show) {
225
- for (const value of Object.values(config.show)) {
226
- if (typeof value === 'object' && value !== null && 'bind' in value) {
227
- const childInstance = varName
228
- ? this.scopeChain.get(varName) || parentInstance
229
- : parentInstance;
230
- this.walkConfig(value as NestedShowMapping, childInstance, false);
231
- }
259
+ this.walkShowMappings(config.show, parentInstance, varName);
260
+ }
261
+ }
262
+
263
+ /** 递归扫描 show 树,注册 when/show 条件块内的嵌套 bind */
264
+ private walkShowMappings(
265
+ show: ShowMapping,
266
+ parentInstance: object,
267
+ parentVarName: string | undefined,
268
+ ): void {
269
+ for (const value of Object.values(show)) {
270
+ if (this.isNestedShowMapping(value)) {
271
+ const childInstance = parentVarName
272
+ ? this.scopeChain.get(parentVarName) || parentInstance
273
+ : parentInstance;
274
+ this.walkConfig(value, childInstance, false);
275
+ continue;
276
+ }
277
+
278
+ if (this.isExtendedShowValue(value) && value.show) {
279
+ this.walkShowMappings(value.show, parentInstance, parentVarName);
232
280
  }
233
281
  }
234
282
  }
235
283
 
284
+ private isNestedShowMapping(value: unknown): value is NestedShowMapping {
285
+ return typeof value === 'object' && value !== null && 'bind' in value && 'show' in value;
286
+ }
287
+
288
+ private isExtendedShowValue(value: unknown): value is ExtendedShowValue {
289
+ return typeof value === 'object' && value !== null;
290
+ }
291
+
236
292
  /**
237
293
  * 通过 obj 路径从 scopeChain 或 parentInstance 解析到对象。
238
294
  */