@363045841yyt/klinechart-core 0.7.3 → 0.7.5

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 (247) hide show
  1. package/README.md +201 -201
  2. package/README.zh-CN.md +201 -201
  3. package/dist/controllers/index.d.ts +1 -0
  4. package/dist/controllers/index.d.ts.map +1 -1
  5. package/dist/controllers/index.js +1 -0
  6. package/dist/controllers/index.js.map +1 -1
  7. package/dist/engine/chart.d.ts +11 -19
  8. package/dist/engine/chart.d.ts.map +1 -1
  9. package/dist/engine/chart.js +92 -109
  10. package/dist/engine/chart.js.map +1 -1
  11. package/dist/engine/renderers/Indicator/indicatorData.d.ts +1 -0
  12. package/dist/engine/renderers/Indicator/indicatorData.d.ts.map +1 -1
  13. package/dist/engine/renderers/Indicator/indicatorData.js +1 -1
  14. package/dist/engine/renderers/Indicator/indicatorData.js.map +1 -1
  15. package/dist/engine/renderers/webgl/candleSurface.js +47 -47
  16. package/dist/engine/subPaneManager.d.ts +4 -0
  17. package/dist/engine/subPaneManager.d.ts.map +1 -1
  18. package/dist/engine/subPaneManager.js +13 -0
  19. package/dist/engine/subPaneManager.js.map +1 -1
  20. package/dist/version.d.ts +1 -1
  21. package/dist/version.d.ts.map +1 -1
  22. package/dist/version.js +1 -2
  23. package/dist/version.js.map +1 -1
  24. package/package.json +129 -122
  25. package/src/__tests__/signal.test.ts +124 -124
  26. package/src/config/chartSettings.ts +66 -66
  27. package/src/controllers/__tests__/drawing.test.ts +214 -214
  28. package/src/controllers/__tests__/indicatorSelector.test.ts +481 -481
  29. package/src/controllers/__tests__/toolbar.test.ts +225 -225
  30. package/src/controllers/createChartController.ts +665 -665
  31. package/src/controllers/createDrawingController.ts +96 -96
  32. package/src/controllers/createIndicatorSelectorController.ts +307 -307
  33. package/src/controllers/createToolbarController.ts +146 -146
  34. package/src/controllers/index.ts +20 -19
  35. package/src/controllers/types.ts +284 -284
  36. package/src/engine/__tests__/chart.dpr.test.ts +401 -401
  37. package/src/engine/__tests__/paneRenderer.resize.test.ts +92 -92
  38. package/src/engine/chart-store.ts +121 -121
  39. package/src/engine/chart.d.ts +617 -617
  40. package/src/engine/chart.ts +2803 -2815
  41. package/src/engine/controller/__tests__/interaction.dpr.test.ts +259 -259
  42. package/src/engine/controller/interaction.ts +722 -722
  43. package/src/engine/controller/markerInteraction.ts +130 -130
  44. package/src/engine/controller/pinchTracker.ts +82 -82
  45. package/src/engine/controller/tooltipPosition.ts +48 -48
  46. package/src/engine/draw/__tests__/pixelAlign.spec.ts +176 -176
  47. package/src/engine/draw/pixelAlign.ts +259 -259
  48. package/src/engine/drawing/index.ts +655 -655
  49. package/src/engine/drawing/interaction.ts +842 -842
  50. package/src/engine/drawing/plugin.ts +343 -343
  51. package/src/engine/indicators/__tests__/__fixtures__/golden/atr.json +38 -38
  52. package/src/engine/indicators/__tests__/__fixtures__/golden/dema.json +14 -14
  53. package/src/engine/indicators/__tests__/__fixtures__/golden/hma.json +14 -14
  54. package/src/engine/indicators/__tests__/__fixtures__/golden/index.ts +55 -55
  55. package/src/engine/indicators/__tests__/__fixtures__/golden/kama.json +14 -14
  56. package/src/engine/indicators/__tests__/__fixtures__/golden/tema.json +14 -14
  57. package/src/engine/indicators/__tests__/__fixtures__/golden/wma.json +40 -40
  58. package/src/engine/indicators/__tests__/__fixtures__/synthetic.ts +65 -65
  59. package/src/engine/indicators/__tests__/_propertyAssertions.ts +76 -76
  60. package/src/engine/indicators/__tests__/atr.test.ts +153 -153
  61. package/src/engine/indicators/__tests__/calculators.test.ts +614 -614
  62. package/src/engine/indicators/__tests__/cmf-mfi.test.ts +100 -100
  63. package/src/engine/indicators/__tests__/dema.test.ts +73 -73
  64. package/src/engine/indicators/__tests__/donchian.test.ts +70 -70
  65. package/src/engine/indicators/__tests__/hma.test.ts +73 -73
  66. package/src/engine/indicators/__tests__/ichimoku.test.ts +105 -105
  67. package/src/engine/indicators/__tests__/kama.test.ts +80 -80
  68. package/src/engine/indicators/__tests__/keltner.test.ts +65 -65
  69. package/src/engine/indicators/__tests__/pivot-fib.test.ts +110 -110
  70. package/src/engine/indicators/__tests__/roc.test.ts +68 -68
  71. package/src/engine/indicators/__tests__/sar.test.ts +86 -86
  72. package/src/engine/indicators/__tests__/scheduler.test.ts +831 -831
  73. package/src/engine/indicators/__tests__/soa.test.ts +533 -533
  74. package/src/engine/indicators/__tests__/structure.test.ts +110 -110
  75. package/src/engine/indicators/__tests__/supertrend.test.ts +65 -65
  76. package/src/engine/indicators/__tests__/tema.test.ts +68 -68
  77. package/src/engine/indicators/__tests__/trix.test.ts +70 -70
  78. package/src/engine/indicators/__tests__/volatility.test.ts +117 -117
  79. package/src/engine/indicators/__tests__/volume.test.ts +115 -115
  80. package/src/engine/indicators/__tests__/volumeProfile.test.ts +74 -74
  81. package/src/engine/indicators/__tests__/vwap.test.ts +69 -69
  82. package/src/engine/indicators/__tests__/wma.test.ts +112 -112
  83. package/src/engine/indicators/__tests__/zones.test.ts +95 -95
  84. package/src/engine/indicators/atrState.ts +27 -27
  85. package/src/engine/indicators/bollState.ts +51 -51
  86. package/src/engine/indicators/calculators.ts +2593 -2593
  87. package/src/engine/indicators/cciState.ts +25 -25
  88. package/src/engine/indicators/chaikinVolState.ts +32 -32
  89. package/src/engine/indicators/cmfState.ts +27 -27
  90. package/src/engine/indicators/demaState.ts +27 -27
  91. package/src/engine/indicators/donchianState.ts +43 -43
  92. package/src/engine/indicators/eneState.ts +43 -43
  93. package/src/engine/indicators/expmaState.ts +43 -43
  94. package/src/engine/indicators/fastkState.ts +25 -25
  95. package/src/engine/indicators/fibState.ts +41 -41
  96. package/src/engine/indicators/hmaState.ts +27 -27
  97. package/src/engine/indicators/hvState.ts +28 -28
  98. package/src/engine/indicators/ichimokuState.ts +70 -70
  99. package/src/engine/indicators/indicator.worker.ts +169 -169
  100. package/src/engine/indicators/indicatorDefinitionRegistry.ts +62 -62
  101. package/src/engine/indicators/indicatorMetadata.ts +110 -110
  102. package/src/engine/indicators/indicatorRegistry.ts +106 -106
  103. package/src/engine/indicators/indicatorRuntime.ts +1548 -1548
  104. package/src/engine/indicators/kamaState.ts +34 -34
  105. package/src/engine/indicators/keltnerState.ts +49 -49
  106. package/src/engine/indicators/kstState.ts +42 -42
  107. package/src/engine/indicators/maState.ts +36 -36
  108. package/src/engine/indicators/macdState.ts +76 -76
  109. package/src/engine/indicators/mfiState.ts +27 -27
  110. package/src/engine/indicators/momState.ts +25 -25
  111. package/src/engine/indicators/obvState.ts +25 -25
  112. package/src/engine/indicators/parkinsonState.ts +28 -28
  113. package/src/engine/indicators/pivotState.ts +51 -51
  114. package/src/engine/indicators/pvtState.ts +25 -25
  115. package/src/engine/indicators/rocState.ts +27 -27
  116. package/src/engine/indicators/rsiState.ts +65 -65
  117. package/src/engine/indicators/sarState.ts +41 -41
  118. package/src/engine/indicators/scheduler.ts +1205 -1205
  119. package/src/engine/indicators/soa.ts +352 -352
  120. package/src/engine/indicators/stateComposer.ts +1262 -1262
  121. package/src/engine/indicators/stochState.ts +26 -26
  122. package/src/engine/indicators/structureState.ts +69 -69
  123. package/src/engine/indicators/supertrendState.ts +37 -37
  124. package/src/engine/indicators/temaState.ts +27 -27
  125. package/src/engine/indicators/trixState.ts +35 -35
  126. package/src/engine/indicators/vmaState.ts +27 -27
  127. package/src/engine/indicators/volumeProfileState.ts +63 -63
  128. package/src/engine/indicators/vwapState.ts +29 -29
  129. package/src/engine/indicators/wmaState.ts +27 -27
  130. package/src/engine/indicators/wmsrState.ts +25 -25
  131. package/src/engine/indicators/workerProtocol.ts +613 -613
  132. package/src/engine/indicators/zonesState.ts +47 -47
  133. package/src/engine/layout/pane.ts +161 -161
  134. package/src/engine/marker/registry.ts +265 -265
  135. package/src/engine/paneRenderer.ts +169 -169
  136. package/src/engine/renderers/Indicator/atr.ts +237 -237
  137. package/src/engine/renderers/Indicator/boll.ts +317 -317
  138. package/src/engine/renderers/Indicator/cci.ts +275 -275
  139. package/src/engine/renderers/Indicator/chaikinVol.ts +138 -138
  140. package/src/engine/renderers/Indicator/cmf.ts +137 -137
  141. package/src/engine/renderers/Indicator/dema.ts +136 -136
  142. package/src/engine/renderers/Indicator/donchian.ts +137 -137
  143. package/src/engine/renderers/Indicator/ene.ts +271 -271
  144. package/src/engine/renderers/Indicator/expma.ts +197 -197
  145. package/src/engine/renderers/Indicator/fastk.ts +316 -316
  146. package/src/engine/renderers/Indicator/fib.ts +141 -141
  147. package/src/engine/renderers/Indicator/hma.ts +136 -136
  148. package/src/engine/renderers/Indicator/hv.ts +124 -124
  149. package/src/engine/renderers/Indicator/ichimoku.ts +181 -181
  150. package/src/engine/renderers/Indicator/index.ts +241 -241
  151. package/src/engine/renderers/Indicator/indicatorData.ts +650 -650
  152. package/src/engine/renderers/Indicator/kama.ts +136 -136
  153. package/src/engine/renderers/Indicator/keltner.ts +137 -137
  154. package/src/engine/renderers/Indicator/kst.ts +302 -302
  155. package/src/engine/renderers/Indicator/ma.ts +200 -200
  156. package/src/engine/renderers/Indicator/macd.ts +477 -477
  157. package/src/engine/renderers/Indicator/macdLegend.ts +141 -141
  158. package/src/engine/renderers/Indicator/mainIndicatorLegend.ts +272 -272
  159. package/src/engine/renderers/Indicator/mfi.ts +142 -142
  160. package/src/engine/renderers/Indicator/mom.ts +311 -311
  161. package/src/engine/renderers/Indicator/obv.ts +123 -123
  162. package/src/engine/renderers/Indicator/parkinson.ts +124 -124
  163. package/src/engine/renderers/Indicator/pivot.ts +131 -131
  164. package/src/engine/renderers/Indicator/pvt.ts +123 -123
  165. package/src/engine/renderers/Indicator/roc.ts +143 -143
  166. package/src/engine/renderers/Indicator/rsi.ts +390 -390
  167. package/src/engine/renderers/Indicator/sar.ts +113 -113
  168. package/src/engine/renderers/Indicator/scale/atr_scale.ts +19 -19
  169. package/src/engine/renderers/Indicator/scale/cci_scale.ts +19 -19
  170. package/src/engine/renderers/Indicator/scale/fastk_scale.ts +19 -19
  171. package/src/engine/renderers/Indicator/scale/indicator_scale.ts +204 -204
  172. package/src/engine/renderers/Indicator/scale/kst_scale.ts +19 -19
  173. package/src/engine/renderers/Indicator/scale/macd_scale.ts +22 -22
  174. package/src/engine/renderers/Indicator/scale/mom_scale.ts +19 -19
  175. package/src/engine/renderers/Indicator/scale/rsi_scale.ts +19 -19
  176. package/src/engine/renderers/Indicator/scale/stoch_scale.ts +19 -19
  177. package/src/engine/renderers/Indicator/scale/volume_scale.ts +26 -26
  178. package/src/engine/renderers/Indicator/scale/wmsr_scale.ts +19 -19
  179. package/src/engine/renderers/Indicator/stoch.ts +359 -359
  180. package/src/engine/renderers/Indicator/structure.ts +126 -126
  181. package/src/engine/renderers/Indicator/subPaneConfig.ts +265 -265
  182. package/src/engine/renderers/Indicator/supertrend.ts +115 -115
  183. package/src/engine/renderers/Indicator/tema.ts +136 -136
  184. package/src/engine/renderers/Indicator/trix.ts +158 -158
  185. package/src/engine/renderers/Indicator/vma.ts +124 -124
  186. package/src/engine/renderers/Indicator/volumeProfile.ts +125 -125
  187. package/src/engine/renderers/Indicator/vwap.ts +123 -123
  188. package/src/engine/renderers/Indicator/wma.ts +136 -136
  189. package/src/engine/renderers/Indicator/wmsr.ts +328 -328
  190. package/src/engine/renderers/Indicator/zones.ts +104 -104
  191. package/src/engine/renderers/__tests__/boll.renderer.test.ts +314 -314
  192. package/src/engine/renderers/__tests__/ene.renderer.test.ts +305 -305
  193. package/src/engine/renderers/__tests__/expma.renderer.test.ts +279 -279
  194. package/src/engine/renderers/__tests__/ma.renderer.test.ts +426 -426
  195. package/src/engine/renderers/__tests__/mainIndicatorLegend.renderer.test.ts +502 -502
  196. package/src/engine/renderers/__tests__/yAxis.renderer.test.ts +173 -173
  197. package/src/engine/renderers/candle.ts +459 -459
  198. package/src/engine/renderers/crosshair.ts +69 -69
  199. package/src/engine/renderers/customMarkers.ts +162 -162
  200. package/src/engine/renderers/extremaMarkers.ts +246 -246
  201. package/src/engine/renderers/gridLines.ts +90 -90
  202. package/src/engine/renderers/lastPrice.ts +97 -97
  203. package/src/engine/renderers/paneTitle.ts +136 -136
  204. package/src/engine/renderers/subVolume.ts +236 -236
  205. package/src/engine/renderers/timeAxis.ts +121 -121
  206. package/src/engine/renderers/webgl/candleSurface.ts +955 -955
  207. package/src/engine/renderers/webgl/sharedWebGLSurface.ts +146 -146
  208. package/src/engine/renderers/yAxis.ts +105 -105
  209. package/src/engine/scale/__tests__/logFormula.spec.ts +148 -148
  210. package/src/engine/scale/logFormula.ts +130 -130
  211. package/src/engine/scale/price.ts +39 -39
  212. package/src/engine/scale/priceScale.ts +264 -264
  213. package/src/engine/subPaneManager.ts +442 -427
  214. package/src/engine/theme/colors.ts +642 -642
  215. package/src/engine/theme/fonts.ts +20 -20
  216. package/src/engine/utils/klineConfig.ts +49 -49
  217. package/src/engine/utils/tickCount.ts +11 -11
  218. package/src/engine/utils/tickPosition.ts +214 -214
  219. package/src/engine/utils/zoom.ts +83 -83
  220. package/src/engine/viewport/viewport.ts +67 -67
  221. package/src/index.ts +3 -3
  222. package/src/plugin/ConfigManager.ts +93 -93
  223. package/src/plugin/EventBus.ts +77 -77
  224. package/src/plugin/HookSystem.ts +106 -106
  225. package/src/plugin/PluginHost.ts +243 -243
  226. package/src/plugin/PluginRegistry.ts +92 -92
  227. package/src/plugin/StateStore.ts +73 -73
  228. package/src/plugin/index.ts +19 -19
  229. package/src/plugin/rendererPluginManager.ts +368 -368
  230. package/src/plugin/stateKeys.ts +8 -8
  231. package/src/plugin/types.ts +526 -526
  232. package/src/reactivity/index.ts +2 -2
  233. package/src/reactivity/signal.ts +119 -119
  234. package/src/semantic/controller.ts +251 -251
  235. package/src/semantic/drawShape.ts +260 -260
  236. package/src/semantic/index.ts +28 -28
  237. package/src/semantic/schema.json +256 -256
  238. package/src/semantic/types.ts +251 -251
  239. package/src/semantic/validator.ts +349 -349
  240. package/src/types/kLine.ts +13 -13
  241. package/src/types/price.ts +56 -56
  242. package/src/types/volumePrice.ts +33 -33
  243. package/src/utils/dateFormat.ts +208 -208
  244. package/src/utils/kLineDraw/axis.ts +562 -562
  245. package/src/utils/priceToY.ts +34 -34
  246. package/src/utils/volumePrice.ts +202 -202
  247. package/src/version.ts +1 -1
@@ -1,243 +1,243 @@
1
- /**
2
- * 插件宿主 - 核心管理类
3
- */
4
- import type { Plugin, PluginConfig, PluginHost, PluginState, BaseIndicatorState, PluginLogger, HookCallOptions } from './types'
5
- import { PluginRegistry } from './PluginRegistry'
6
- import { EventBus } from './EventBus'
7
- import { HookSystem } from './HookSystem'
8
- import { ConfigManager } from './ConfigManager'
9
- import { StateStore } from './StateStore'
10
-
11
- export class PluginHostImpl implements PluginHost {
12
- private registry: PluginRegistry
13
- private eventBus: EventBus
14
- private hookSystem: HookSystem
15
- private configManager: ConfigManager
16
- private stateStore: StateStore
17
- private services = new Map<string, unknown>()
18
- private isDestroyed = false
19
- private logger: PluginLogger
20
-
21
- constructor(logger?: PluginLogger) {
22
- this.registry = new PluginRegistry()
23
- this.eventBus = new EventBus()
24
- this.hookSystem = new HookSystem()
25
- this.configManager = new ConfigManager()
26
- this.stateStore = new StateStore()
27
- this.logger = logger ?? console
28
- }
29
-
30
- // 实现 PluginHost 接口
31
- readonly events = {
32
- on: <T = unknown>(event: string, handler: (data: T) => void) => {
33
- this.eventBus.on(event, handler)
34
- },
35
- off: <T = unknown>(event: string, handler: (data: T) => void) => {
36
- this.eventBus.off(event, handler)
37
- },
38
- emit: <T = unknown>(event: string, data: T) => {
39
- this.eventBus.emit(event, data)
40
- },
41
- once: <T = unknown>(event: string, handler: (data: T) => void) => {
42
- this.eventBus.once(event, handler)
43
- },
44
- }
45
-
46
- readonly hooks = {
47
- tap: <T = unknown, R = unknown>(
48
- hookName: string,
49
- fn: (context: T) => R | Promise<R>,
50
- priority = 0
51
- ) => {
52
- this.hookSystem.tap(hookName, fn, priority)
53
- },
54
- untap: (hookName: string, fn: (context: unknown) => unknown) => {
55
- this.hookSystem.untap(hookName, fn)
56
- },
57
- call: async <T = unknown, R = unknown>(hookName: string, context: T, options?: HookCallOptions) => {
58
- return this.hookSystem.call<T, R>(hookName, context, options)
59
- },
60
- callSync: <T = unknown, R = unknown>(hookName: string, context: T, options?: HookCallOptions) => {
61
- return this.hookSystem.callSync<T, R>(hookName, context, options)
62
- },
63
- }
64
-
65
- getConfig<K = unknown>(pluginName: string, key: string, defaultValue?: K): K {
66
- return this.configManager.get<K>(pluginName, key, defaultValue)
67
- }
68
-
69
- setConfig(pluginName: string, key: string, value: unknown): void {
70
- this.configManager.set(pluginName, key, value)
71
- }
72
-
73
- getPlugin<T extends Plugin = Plugin>(name: string): T | undefined {
74
- return this.registry.getPlugin<T>(name)
75
- }
76
-
77
- log(level: 'info' | 'warn' | 'error', message: string, ...args: unknown[]): void {
78
- const timestamp = new Date().toISOString()
79
- const prefix = `[${timestamp}] [${level.toUpperCase()}]`
80
- this.logger[level](`${prefix} ${message}`, ...args)
81
- }
82
-
83
- // ============ 状态存储 API ============
84
-
85
- setSharedState<T extends BaseIndicatorState>(namespace: string, state: T, ownerId?: string): void {
86
- this.stateStore.setState(namespace, state, ownerId)
87
- }
88
-
89
- getSharedState<T extends BaseIndicatorState>(namespace: string): T | undefined {
90
- return this.stateStore.getState<T>(namespace)
91
- }
92
-
93
- clearSharedState(namespace: string): void {
94
- this.stateStore.clearState(namespace)
95
- }
96
-
97
- registerStateOwner(ownerId: string, namespaces: string[]): void {
98
- this.stateStore.registerStateOwner(ownerId, namespaces)
99
- }
100
-
101
- clearByOwner(ownerId: string): void {
102
- this.stateStore.clearByOwner(ownerId)
103
- }
104
-
105
- registerService(name: string, service: unknown): void {
106
- this.services.set(name, service)
107
- }
108
-
109
- getService<T = unknown>(name: string): T | undefined {
110
- return this.services.get(name) as T | undefined
111
- }
112
-
113
- /**
114
- * 安装插件
115
- */
116
- async use(plugin: Plugin, config?: PluginConfig): Promise<void> {
117
- this.ensureNotDestroyed()
118
-
119
- if (this.registry.has(plugin.name)) {
120
- throw new Error(`Plugin "${plugin.name}" is already installed`)
121
- }
122
-
123
- try {
124
- // 注册插件
125
- const descriptor = this.registry.register(plugin, config)
126
-
127
- // 注册默认配置
128
- if (config) {
129
- this.configManager.setAll(plugin.name, config)
130
- }
131
-
132
- // 触发安装前钩子
133
- await this.hooks.call('plugin:beforeInstall', { plugin, config }, { throwOnError: true })
134
-
135
- // 安装插件
136
- await plugin.install(this, descriptor.config)
137
-
138
- // 更新状态
139
- this.registry.updateState(plugin.name, 'installed' as PluginState)
140
-
141
- // 触发安装后钩子
142
- await this.hooks.call('plugin:afterInstall', { plugin, config }, { throwOnError: true })
143
-
144
- this.log('info', `Plugin "${plugin.name}" installed successfully`)
145
- } catch (error) {
146
- this.registry.updateState(
147
- plugin.name,
148
- 'error' as PluginState,
149
- error instanceof Error ? error : new Error(String(error))
150
- )
151
- this.log('error', `Failed to install plugin "${plugin.name}":`, error)
152
- throw error
153
- }
154
- }
155
-
156
- /**
157
- * 移除插件
158
- */
159
- async remove(name: string): Promise<void> {
160
- this.ensureNotDestroyed()
161
-
162
- const descriptor = this.registry.get(name)
163
- if (!descriptor) {
164
- throw new Error(`Plugin "${name}" is not installed`)
165
- }
166
-
167
- try {
168
- // 触发卸载前钩子
169
- await this.hooks.call('plugin:beforeUninstall', { name }, { throwOnError: true })
170
-
171
- // 调用插件的卸载方法
172
- if (descriptor.plugin.uninstall) {
173
- await descriptor.plugin.uninstall()
174
- }
175
-
176
- // 清理
177
- this.registry.unregister(name)
178
- this.configManager.clear(name)
179
-
180
- // 触发卸载后钩子
181
- await this.hooks.call('plugin:afterUninstall', { name }, { throwOnError: true })
182
-
183
- this.log('info', `Plugin "${name}" removed successfully`)
184
- } catch (error) {
185
- this.log('error', `Failed to remove plugin "${name}":`, error)
186
- throw error
187
- }
188
- }
189
-
190
- /**
191
- * 获取所有插件
192
- */
193
- getPlugins() {
194
- return this.registry.getAll()
195
- }
196
-
197
- /**
198
- * 获取插件状态
199
- */
200
- getState(name: string): PluginState | undefined {
201
- return this.registry.get(name)?.state
202
- }
203
-
204
- /**
205
- * 销毁宿主
206
- */
207
- async destroy(): Promise<void> {
208
- if (this.isDestroyed) return
209
-
210
- // 卸载所有插件
211
- for (const descriptor of this.registry.getAll()) {
212
- try {
213
- if (descriptor.plugin.uninstall) {
214
- await descriptor.plugin.uninstall()
215
- }
216
- } catch (error) {
217
- this.log('error', `Error uninstalling "${descriptor.plugin.name}":`, error)
218
- }
219
- }
220
-
221
- // 清理资源
222
- this.registry.clear()
223
- this.eventBus.clear()
224
- this.hookSystem.clear()
225
- this.configManager.clear()
226
- this.stateStore.clear()
227
- this.services.clear()
228
-
229
- this.isDestroyed = true
230
- this.log('info', 'PluginHost destroyed')
231
- }
232
-
233
- private ensureNotDestroyed(): void {
234
- if (this.isDestroyed) {
235
- throw new Error('PluginHost has been destroyed')
236
- }
237
- }
238
- }
239
-
240
- // 导出工厂函数
241
- export function createPluginHost(logger?: PluginLogger): PluginHostImpl {
242
- return new PluginHostImpl(logger)
243
- }
1
+ /**
2
+ * 插件宿主 - 核心管理类
3
+ */
4
+ import type { Plugin, PluginConfig, PluginHost, PluginState, BaseIndicatorState, PluginLogger, HookCallOptions } from './types'
5
+ import { PluginRegistry } from './PluginRegistry'
6
+ import { EventBus } from './EventBus'
7
+ import { HookSystem } from './HookSystem'
8
+ import { ConfigManager } from './ConfigManager'
9
+ import { StateStore } from './StateStore'
10
+
11
+ export class PluginHostImpl implements PluginHost {
12
+ private registry: PluginRegistry
13
+ private eventBus: EventBus
14
+ private hookSystem: HookSystem
15
+ private configManager: ConfigManager
16
+ private stateStore: StateStore
17
+ private services = new Map<string, unknown>()
18
+ private isDestroyed = false
19
+ private logger: PluginLogger
20
+
21
+ constructor(logger?: PluginLogger) {
22
+ this.registry = new PluginRegistry()
23
+ this.eventBus = new EventBus()
24
+ this.hookSystem = new HookSystem()
25
+ this.configManager = new ConfigManager()
26
+ this.stateStore = new StateStore()
27
+ this.logger = logger ?? console
28
+ }
29
+
30
+ // 实现 PluginHost 接口
31
+ readonly events = {
32
+ on: <T = unknown>(event: string, handler: (data: T) => void) => {
33
+ this.eventBus.on(event, handler)
34
+ },
35
+ off: <T = unknown>(event: string, handler: (data: T) => void) => {
36
+ this.eventBus.off(event, handler)
37
+ },
38
+ emit: <T = unknown>(event: string, data: T) => {
39
+ this.eventBus.emit(event, data)
40
+ },
41
+ once: <T = unknown>(event: string, handler: (data: T) => void) => {
42
+ this.eventBus.once(event, handler)
43
+ },
44
+ }
45
+
46
+ readonly hooks = {
47
+ tap: <T = unknown, R = unknown>(
48
+ hookName: string,
49
+ fn: (context: T) => R | Promise<R>,
50
+ priority = 0
51
+ ) => {
52
+ this.hookSystem.tap(hookName, fn, priority)
53
+ },
54
+ untap: (hookName: string, fn: (context: unknown) => unknown) => {
55
+ this.hookSystem.untap(hookName, fn)
56
+ },
57
+ call: async <T = unknown, R = unknown>(hookName: string, context: T, options?: HookCallOptions) => {
58
+ return this.hookSystem.call<T, R>(hookName, context, options)
59
+ },
60
+ callSync: <T = unknown, R = unknown>(hookName: string, context: T, options?: HookCallOptions) => {
61
+ return this.hookSystem.callSync<T, R>(hookName, context, options)
62
+ },
63
+ }
64
+
65
+ getConfig<K = unknown>(pluginName: string, key: string, defaultValue?: K): K {
66
+ return this.configManager.get<K>(pluginName, key, defaultValue)
67
+ }
68
+
69
+ setConfig(pluginName: string, key: string, value: unknown): void {
70
+ this.configManager.set(pluginName, key, value)
71
+ }
72
+
73
+ getPlugin<T extends Plugin = Plugin>(name: string): T | undefined {
74
+ return this.registry.getPlugin<T>(name)
75
+ }
76
+
77
+ log(level: 'info' | 'warn' | 'error', message: string, ...args: unknown[]): void {
78
+ const timestamp = new Date().toISOString()
79
+ const prefix = `[${timestamp}] [${level.toUpperCase()}]`
80
+ this.logger[level](`${prefix} ${message}`, ...args)
81
+ }
82
+
83
+ // ============ 状态存储 API ============
84
+
85
+ setSharedState<T extends BaseIndicatorState>(namespace: string, state: T, ownerId?: string): void {
86
+ this.stateStore.setState(namespace, state, ownerId)
87
+ }
88
+
89
+ getSharedState<T extends BaseIndicatorState>(namespace: string): T | undefined {
90
+ return this.stateStore.getState<T>(namespace)
91
+ }
92
+
93
+ clearSharedState(namespace: string): void {
94
+ this.stateStore.clearState(namespace)
95
+ }
96
+
97
+ registerStateOwner(ownerId: string, namespaces: string[]): void {
98
+ this.stateStore.registerStateOwner(ownerId, namespaces)
99
+ }
100
+
101
+ clearByOwner(ownerId: string): void {
102
+ this.stateStore.clearByOwner(ownerId)
103
+ }
104
+
105
+ registerService(name: string, service: unknown): void {
106
+ this.services.set(name, service)
107
+ }
108
+
109
+ getService<T = unknown>(name: string): T | undefined {
110
+ return this.services.get(name) as T | undefined
111
+ }
112
+
113
+ /**
114
+ * 安装插件
115
+ */
116
+ async use(plugin: Plugin, config?: PluginConfig): Promise<void> {
117
+ this.ensureNotDestroyed()
118
+
119
+ if (this.registry.has(plugin.name)) {
120
+ throw new Error(`Plugin "${plugin.name}" is already installed`)
121
+ }
122
+
123
+ try {
124
+ // 注册插件
125
+ const descriptor = this.registry.register(plugin, config)
126
+
127
+ // 注册默认配置
128
+ if (config) {
129
+ this.configManager.setAll(plugin.name, config)
130
+ }
131
+
132
+ // 触发安装前钩子
133
+ await this.hooks.call('plugin:beforeInstall', { plugin, config }, { throwOnError: true })
134
+
135
+ // 安装插件
136
+ await plugin.install(this, descriptor.config)
137
+
138
+ // 更新状态
139
+ this.registry.updateState(plugin.name, 'installed' as PluginState)
140
+
141
+ // 触发安装后钩子
142
+ await this.hooks.call('plugin:afterInstall', { plugin, config }, { throwOnError: true })
143
+
144
+ this.log('info', `Plugin "${plugin.name}" installed successfully`)
145
+ } catch (error) {
146
+ this.registry.updateState(
147
+ plugin.name,
148
+ 'error' as PluginState,
149
+ error instanceof Error ? error : new Error(String(error))
150
+ )
151
+ this.log('error', `Failed to install plugin "${plugin.name}":`, error)
152
+ throw error
153
+ }
154
+ }
155
+
156
+ /**
157
+ * 移除插件
158
+ */
159
+ async remove(name: string): Promise<void> {
160
+ this.ensureNotDestroyed()
161
+
162
+ const descriptor = this.registry.get(name)
163
+ if (!descriptor) {
164
+ throw new Error(`Plugin "${name}" is not installed`)
165
+ }
166
+
167
+ try {
168
+ // 触发卸载前钩子
169
+ await this.hooks.call('plugin:beforeUninstall', { name }, { throwOnError: true })
170
+
171
+ // 调用插件的卸载方法
172
+ if (descriptor.plugin.uninstall) {
173
+ await descriptor.plugin.uninstall()
174
+ }
175
+
176
+ // 清理
177
+ this.registry.unregister(name)
178
+ this.configManager.clear(name)
179
+
180
+ // 触发卸载后钩子
181
+ await this.hooks.call('plugin:afterUninstall', { name }, { throwOnError: true })
182
+
183
+ this.log('info', `Plugin "${name}" removed successfully`)
184
+ } catch (error) {
185
+ this.log('error', `Failed to remove plugin "${name}":`, error)
186
+ throw error
187
+ }
188
+ }
189
+
190
+ /**
191
+ * 获取所有插件
192
+ */
193
+ getPlugins() {
194
+ return this.registry.getAll()
195
+ }
196
+
197
+ /**
198
+ * 获取插件状态
199
+ */
200
+ getState(name: string): PluginState | undefined {
201
+ return this.registry.get(name)?.state
202
+ }
203
+
204
+ /**
205
+ * 销毁宿主
206
+ */
207
+ async destroy(): Promise<void> {
208
+ if (this.isDestroyed) return
209
+
210
+ // 卸载所有插件
211
+ for (const descriptor of this.registry.getAll()) {
212
+ try {
213
+ if (descriptor.plugin.uninstall) {
214
+ await descriptor.plugin.uninstall()
215
+ }
216
+ } catch (error) {
217
+ this.log('error', `Error uninstalling "${descriptor.plugin.name}":`, error)
218
+ }
219
+ }
220
+
221
+ // 清理资源
222
+ this.registry.clear()
223
+ this.eventBus.clear()
224
+ this.hookSystem.clear()
225
+ this.configManager.clear()
226
+ this.stateStore.clear()
227
+ this.services.clear()
228
+
229
+ this.isDestroyed = true
230
+ this.log('info', 'PluginHost destroyed')
231
+ }
232
+
233
+ private ensureNotDestroyed(): void {
234
+ if (this.isDestroyed) {
235
+ throw new Error('PluginHost has been destroyed')
236
+ }
237
+ }
238
+ }
239
+
240
+ // 导出工厂函数
241
+ export function createPluginHost(logger?: PluginLogger): PluginHostImpl {
242
+ return new PluginHostImpl(logger)
243
+ }
@@ -1,92 +1,92 @@
1
- /**
2
- * 插件注册表
3
- */
4
- import type { Plugin, PluginDescriptor, PluginState } from './types'
5
-
6
- export class PluginRegistry {
7
- private plugins: Map<string, PluginDescriptor> = new Map()
8
-
9
- /**
10
- * 注册插件
11
- */
12
- register(plugin: Plugin, config?: Record<string, unknown>): PluginDescriptor {
13
- if (this.plugins.has(plugin.name)) {
14
- throw new Error(`Plugin "${plugin.name}" is already registered`)
15
- }
16
-
17
- const descriptor: PluginDescriptor = {
18
- plugin,
19
- config: {
20
- enabled: true,
21
- priority: 0,
22
- ...config,
23
- },
24
- state: 'registered' as PluginState,
25
- }
26
-
27
- this.plugins.set(plugin.name, descriptor)
28
- return descriptor
29
- }
30
-
31
- /**
32
- * 注销插件
33
- */
34
- unregister(name: string): boolean {
35
- return this.plugins.delete(name)
36
- }
37
-
38
- /**
39
- * 获取插件描述符
40
- */
41
- get(name: string): PluginDescriptor | undefined {
42
- return this.plugins.get(name)
43
- }
44
-
45
- /**
46
- * 获取插件实例
47
- */
48
- getPlugin<T extends Plugin = Plugin>(name: string): T | undefined {
49
- return this.plugins.get(name)?.plugin as T | undefined
50
- }
51
-
52
- /**
53
- * 获取所有插件
54
- */
55
- getAll(): PluginDescriptor[] {
56
- return Array.from(this.plugins.values())
57
- }
58
-
59
- /**
60
- * 获取所有已启用的插件
61
- */
62
- getEnabled(): PluginDescriptor[] {
63
- return this.getAll().filter((d) => d.config?.enabled !== false)
64
- }
65
-
66
- /**
67
- * 更新插件状态
68
- */
69
- updateState(name: string, state: PluginState, error?: Error): void {
70
- const descriptor = this.plugins.get(name)
71
- if (descriptor) {
72
- descriptor.state = state
73
- if (error) {
74
- descriptor.error = error
75
- }
76
- }
77
- }
78
-
79
- /**
80
- * 检查插件是否存在
81
- */
82
- has(name: string): boolean {
83
- return this.plugins.has(name)
84
- }
85
-
86
- /**
87
- * 清除所有插件
88
- */
89
- clear(): void {
90
- this.plugins.clear()
91
- }
92
- }
1
+ /**
2
+ * 插件注册表
3
+ */
4
+ import type { Plugin, PluginDescriptor, PluginState } from './types'
5
+
6
+ export class PluginRegistry {
7
+ private plugins: Map<string, PluginDescriptor> = new Map()
8
+
9
+ /**
10
+ * 注册插件
11
+ */
12
+ register(plugin: Plugin, config?: Record<string, unknown>): PluginDescriptor {
13
+ if (this.plugins.has(plugin.name)) {
14
+ throw new Error(`Plugin "${plugin.name}" is already registered`)
15
+ }
16
+
17
+ const descriptor: PluginDescriptor = {
18
+ plugin,
19
+ config: {
20
+ enabled: true,
21
+ priority: 0,
22
+ ...config,
23
+ },
24
+ state: 'registered' as PluginState,
25
+ }
26
+
27
+ this.plugins.set(plugin.name, descriptor)
28
+ return descriptor
29
+ }
30
+
31
+ /**
32
+ * 注销插件
33
+ */
34
+ unregister(name: string): boolean {
35
+ return this.plugins.delete(name)
36
+ }
37
+
38
+ /**
39
+ * 获取插件描述符
40
+ */
41
+ get(name: string): PluginDescriptor | undefined {
42
+ return this.plugins.get(name)
43
+ }
44
+
45
+ /**
46
+ * 获取插件实例
47
+ */
48
+ getPlugin<T extends Plugin = Plugin>(name: string): T | undefined {
49
+ return this.plugins.get(name)?.plugin as T | undefined
50
+ }
51
+
52
+ /**
53
+ * 获取所有插件
54
+ */
55
+ getAll(): PluginDescriptor[] {
56
+ return Array.from(this.plugins.values())
57
+ }
58
+
59
+ /**
60
+ * 获取所有已启用的插件
61
+ */
62
+ getEnabled(): PluginDescriptor[] {
63
+ return this.getAll().filter((d) => d.config?.enabled !== false)
64
+ }
65
+
66
+ /**
67
+ * 更新插件状态
68
+ */
69
+ updateState(name: string, state: PluginState, error?: Error): void {
70
+ const descriptor = this.plugins.get(name)
71
+ if (descriptor) {
72
+ descriptor.state = state
73
+ if (error) {
74
+ descriptor.error = error
75
+ }
76
+ }
77
+ }
78
+
79
+ /**
80
+ * 检查插件是否存在
81
+ */
82
+ has(name: string): boolean {
83
+ return this.plugins.has(name)
84
+ }
85
+
86
+ /**
87
+ * 清除所有插件
88
+ */
89
+ clear(): void {
90
+ this.plugins.clear()
91
+ }
92
+ }