@taicode/common-web 3.1.0 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (340) hide show
  1. package/output/components/alert-dialog.d.ts +20 -0
  2. package/output/components/alert-dialog.d.ts.map +1 -0
  3. package/output/components/alert-dialog.js +65 -0
  4. package/output/components/alert.d.ts +11 -0
  5. package/output/components/alert.d.ts.map +1 -0
  6. package/output/components/alert.js +38 -0
  7. package/output/components/avatar.d.ts +13 -0
  8. package/output/components/avatar.d.ts.map +1 -0
  9. package/output/components/avatar.js +40 -0
  10. package/output/components/badge.d.ts +10 -0
  11. package/output/components/badge.d.ts.map +1 -0
  12. package/output/components/badge.js +31 -0
  13. package/output/components/button.d.ts +12 -0
  14. package/output/components/button.d.ts.map +1 -0
  15. package/output/components/button.js +44 -0
  16. package/output/components/card.d.ts +8 -0
  17. package/output/components/card.d.ts.map +1 -0
  18. package/output/components/card.js +38 -0
  19. package/output/components/checkbox.d.ts +5 -0
  20. package/output/components/checkbox.d.ts.map +1 -0
  21. package/output/components/checkbox.js +21 -0
  22. package/output/components/command.d.ts +20 -0
  23. package/output/components/command.d.ts.map +1 -0
  24. package/output/components/command.js +55 -0
  25. package/output/components/dialog.d.ts +20 -0
  26. package/output/components/dialog.d.ts.map +1 -0
  27. package/output/components/dialog.js +58 -0
  28. package/output/components/direction.d.ts +9 -0
  29. package/output/components/direction.d.ts.map +1 -0
  30. package/output/components/direction.js +10 -0
  31. package/output/components/dropdown-menu.d.ts +29 -0
  32. package/output/components/dropdown-menu.d.ts.map +1 -0
  33. package/output/components/dropdown-menu.js +78 -0
  34. package/output/components/field.d.ts +39 -0
  35. package/output/components/field.d.ts.map +1 -0
  36. package/output/components/field.js +102 -0
  37. package/output/components/index.d.ts +36 -0
  38. package/output/components/index.d.ts.map +1 -0
  39. package/output/components/index.js +35 -0
  40. package/output/components/input-group.d.ts +17 -0
  41. package/output/components/input-group.d.ts.map +1 -0
  42. package/output/components/input-group.js +75 -0
  43. package/output/components/input.d.ts +3 -0
  44. package/output/components/input.d.ts.map +1 -0
  45. package/output/components/input.js +19 -0
  46. package/output/components/item.d.ts +25 -0
  47. package/output/components/item.d.ts.map +1 -0
  48. package/output/components/item.js +89 -0
  49. package/output/components/json-view/index.d.ts +11 -0
  50. package/output/components/json-view/index.d.ts.map +1 -0
  51. package/output/components/json-view/index.js +52 -0
  52. package/output/components/kbd.d.ts +4 -0
  53. package/output/components/kbd.d.ts.map +1 -0
  54. package/output/components/kbd.js +22 -0
  55. package/output/components/label.d.ts +5 -0
  56. package/output/components/label.d.ts.map +1 -0
  57. package/output/components/label.js +20 -0
  58. package/output/components/loading-button/index.d.ts.map +1 -0
  59. package/output/{loading-button → components/loading-button}/index.js +2 -6
  60. package/output/components/loading-button/index.test.d.ts.map +1 -0
  61. package/output/{loading-button → components/loading-button}/index.test.js +3 -3
  62. package/output/components/native-select.d.ts +9 -0
  63. package/output/components/native-select.d.ts.map +1 -0
  64. package/output/components/native-select.js +28 -0
  65. package/output/components/page-state.d.ts +19 -0
  66. package/output/components/page-state.d.ts.map +1 -0
  67. package/output/components/page-state.js +13 -0
  68. package/output/components/popover.d.ts +11 -0
  69. package/output/components/popover.d.ts.map +1 -0
  70. package/output/components/popover.js +44 -0
  71. package/output/components/progress.d.ts +5 -0
  72. package/output/components/progress.d.ts.map +1 -0
  73. package/output/components/progress.js +21 -0
  74. package/output/components/radio-group.d.ts +6 -0
  75. package/output/components/radio-group.d.ts.map +1 -0
  76. package/output/components/radio-group.js +25 -0
  77. package/output/components/scroll-area.d.ts +6 -0
  78. package/output/components/scroll-area.d.ts.map +1 -0
  79. package/output/components/scroll-area.js +24 -0
  80. package/output/components/select.d.ts +17 -0
  81. package/output/components/select.d.ts.map +1 -0
  82. package/output/components/select.js +58 -0
  83. package/output/components/separator.d.ts +6 -0
  84. package/output/components/separator.d.ts.map +1 -0
  85. package/output/components/separator.js +19 -0
  86. package/output/components/sheet.d.ts +16 -0
  87. package/output/components/sheet.d.ts.map +1 -0
  88. package/output/components/sheet.js +58 -0
  89. package/output/components/size-provider/index.d.ts.map +1 -0
  90. package/output/components/size-provider/size-provider.d.ts.map +1 -0
  91. package/output/components/skeleton.d.ts +3 -0
  92. package/output/components/skeleton.d.ts.map +1 -0
  93. package/output/{catalyst/divider.js → components/skeleton.js} +4 -4
  94. package/output/components/sonner.d.ts +8 -0
  95. package/output/components/sonner.d.ts.map +1 -0
  96. package/output/components/sonner.js +34 -0
  97. package/output/components/spinner.d.ts +3 -0
  98. package/output/components/spinner.d.ts.map +1 -0
  99. package/output/components/spinner.js +19 -0
  100. package/output/components/switch.d.ts +8 -0
  101. package/output/components/switch.d.ts.map +1 -0
  102. package/output/components/switch.js +20 -0
  103. package/output/components/table.d.ts +11 -0
  104. package/output/components/table.d.ts.map +1 -0
  105. package/output/components/table.js +47 -0
  106. package/output/components/tabs.d.ts +12 -0
  107. package/output/components/tabs.d.ts.map +1 -0
  108. package/output/components/tabs.js +45 -0
  109. package/output/components/textarea.d.ts +4 -0
  110. package/output/components/textarea.d.ts.map +1 -0
  111. package/output/components/textarea.js +19 -0
  112. package/output/components/theme-switcher.d.ts +6 -0
  113. package/output/components/theme-switcher.d.ts.map +1 -0
  114. package/output/components/theme-switcher.js +19 -0
  115. package/output/{toaster → components/toaster}/index.d.ts +1 -1
  116. package/output/components/toaster/index.d.ts.map +1 -0
  117. package/output/components/tooltip.d.ts +8 -0
  118. package/output/components/tooltip.d.ts.map +1 -0
  119. package/output/components/tooltip.js +33 -0
  120. package/output/features/confirm-dialog/index.d.ts +39 -0
  121. package/output/features/confirm-dialog/index.d.ts.map +1 -0
  122. package/output/features/confirm-dialog/index.js +19 -0
  123. package/output/features/input-confirm/index.d.ts +26 -0
  124. package/output/features/input-confirm/index.d.ts.map +1 -0
  125. package/output/features/input-confirm/index.js +59 -0
  126. package/output/features/signin/context.d.ts.map +1 -0
  127. package/output/{signin → features/signin}/index.d.ts +1 -1
  128. package/output/features/signin/index.d.ts.map +1 -0
  129. package/output/{signin → features/signin}/index.js +1 -1
  130. package/output/features/signin/service.d.ts.map +1 -0
  131. package/output/features/signin/service.test.d.ts.map +1 -0
  132. package/output/features/signin/types.d.ts.map +1 -0
  133. package/output/hooks/use-loading/index.d.ts +6 -0
  134. package/output/hooks/use-loading/index.d.ts.map +1 -0
  135. package/output/hooks/use-loading/index.js +14 -0
  136. package/output/hooks/use-loading/index.test.d.ts +2 -0
  137. package/output/hooks/use-loading/index.test.d.ts.map +1 -0
  138. package/output/hooks/use-loading/index.test.js +24 -0
  139. package/output/hooks/use-mobile/index.d.ts +2 -0
  140. package/output/hooks/use-mobile/index.d.ts.map +1 -0
  141. package/output/hooks/use-mobile/index.js +16 -0
  142. package/output/hooks/use-observer/index.d.ts.map +1 -0
  143. package/output/hooks/use-observer/use-observer.d.ts.map +1 -0
  144. package/output/hooks/use-observer/use-observer.test.d.ts.map +1 -0
  145. package/output/hooks/use-offline/index.d.ts +6 -0
  146. package/output/hooks/use-offline/index.d.ts.map +1 -0
  147. package/output/hooks/use-offline/index.js +19 -0
  148. package/output/layout/app/index.d.ts +34 -0
  149. package/output/layout/app/index.d.ts.map +1 -0
  150. package/output/layout/app/index.js +34 -0
  151. package/output/layout/dashboard/index.d.ts +58 -0
  152. package/output/layout/dashboard/index.d.ts.map +1 -0
  153. package/output/layout/dashboard/index.js +71 -0
  154. package/output/layout/index.d.ts +3 -0
  155. package/output/layout/index.d.ts.map +1 -0
  156. package/output/layout/index.js +2 -0
  157. package/output/services/cache-api/cache-api.d.ts.map +1 -0
  158. package/output/services/cache-api/cache-api.test.d.ts.map +1 -0
  159. package/output/services/cache-api/index.d.ts.map +1 -0
  160. package/output/services/service/index.d.ts.map +1 -0
  161. package/output/services/service/service.d.ts +135 -0
  162. package/output/services/service/service.d.ts.map +1 -0
  163. package/output/services/service/service.js +276 -0
  164. package/output/services/service/service.test.d.ts.map +1 -0
  165. package/output/{service → services/service}/service.test.js +0 -1
  166. package/output/services/side-cache/index.d.ts.map +1 -0
  167. package/output/services/side-cache/side-cache.d.ts.map +1 -0
  168. package/output/services/side-cache/side-cache.test.d.ts.map +1 -0
  169. package/output/utils/avatar/index.d.ts +28 -0
  170. package/output/utils/avatar/index.d.ts.map +1 -0
  171. package/output/utils/avatar/index.js +35 -0
  172. package/output/utils/delay/index.d.ts +2 -0
  173. package/output/utils/delay/index.d.ts.map +1 -0
  174. package/output/utils/delay/index.js +5 -0
  175. package/output/utils/form/index.d.ts +3 -0
  176. package/output/utils/form/index.d.ts.map +1 -0
  177. package/output/utils/form/index.js +1 -0
  178. package/output/utils/merge-class-names/index.d.ts +3 -0
  179. package/output/utils/merge-class-names/index.d.ts.map +1 -0
  180. package/output/utils/merge-class-names/index.js +5 -0
  181. package/output/utils/merge-class-names/index.test.d.ts +2 -0
  182. package/output/utils/merge-class-names/index.test.d.ts.map +1 -0
  183. package/output/utils/merge-class-names/index.test.js +7 -0
  184. package/output/utils/performance/index.d.ts +82 -0
  185. package/output/utils/performance/index.d.ts.map +1 -0
  186. package/output/utils/performance/index.js +129 -0
  187. package/output/utils/redirect/index.d.ts +6 -0
  188. package/output/utils/redirect/index.d.ts.map +1 -0
  189. package/output/utils/redirect/index.js +32 -0
  190. package/package.json +71 -24
  191. package/styles.css +123 -0
  192. package/output/cache-api/cache-api.d.ts.map +0 -1
  193. package/output/cache-api/cache-api.test.d.ts.map +0 -1
  194. package/output/cache-api/index.d.ts.map +0 -1
  195. package/output/catalyst/alert.d.ts +0 -29
  196. package/output/catalyst/alert.d.ts.map +0 -1
  197. package/output/catalyst/alert.js +0 -46
  198. package/output/catalyst/auth-layout.d.ts +0 -5
  199. package/output/catalyst/auth-layout.d.ts.map +0 -1
  200. package/output/catalyst/auth-layout.js +0 -4
  201. package/output/catalyst/avatar.d.ts +0 -16
  202. package/output/catalyst/avatar.d.ts.map +0 -1
  203. package/output/catalyst/avatar.js +0 -30
  204. package/output/catalyst/badge.d.ts +0 -35
  205. package/output/catalyst/badge.d.ts.map +0 -1
  206. package/output/catalyst/badge.js +0 -46
  207. package/output/catalyst/button.d.ts +0 -59
  208. package/output/catalyst/button.d.ts.map +0 -1
  209. package/output/catalyst/button.js +0 -181
  210. package/output/catalyst/checkbox.d.ts +0 -37
  211. package/output/catalyst/checkbox.d.ts.map +0 -1
  212. package/output/catalyst/checkbox.js +0 -97
  213. package/output/catalyst/combobox.d.ts +0 -20
  214. package/output/catalyst/combobox.d.ts.map +0 -1
  215. package/output/catalyst/combobox.js +0 -101
  216. package/output/catalyst/description-list.d.ts +0 -4
  217. package/output/catalyst/description-list.d.ts.map +0 -1
  218. package/output/catalyst/description-list.js +0 -25
  219. package/output/catalyst/dialog.d.ts +0 -29
  220. package/output/catalyst/dialog.d.ts.map +0 -1
  221. package/output/catalyst/dialog.js +0 -46
  222. package/output/catalyst/divider.d.ts +0 -4
  223. package/output/catalyst/divider.d.ts.map +0 -1
  224. package/output/catalyst/dropdown.d.ts +0 -33
  225. package/output/catalyst/dropdown.d.ts.map +0 -1
  226. package/output/catalyst/dropdown.js +0 -99
  227. package/output/catalyst/fieldset.d.ts +0 -22
  228. package/output/catalyst/fieldset.d.ts.map +0 -1
  229. package/output/catalyst/fieldset.js +0 -42
  230. package/output/catalyst/heading.d.ts +0 -7
  231. package/output/catalyst/heading.d.ts.map +0 -1
  232. package/output/catalyst/heading.js +0 -23
  233. package/output/catalyst/input.d.ts +0 -11
  234. package/output/catalyst/input.d.ts.map +0 -1
  235. package/output/catalyst/input.js +0 -70
  236. package/output/catalyst/link.d.ts +0 -13
  237. package/output/catalyst/link.d.ts.map +0 -1
  238. package/output/catalyst/link.js +0 -37
  239. package/output/catalyst/listbox.d.ts +0 -16
  240. package/output/catalyst/listbox.d.ts.map +0 -1
  241. package/output/catalyst/listbox.js +0 -99
  242. package/output/catalyst/navbar.d.ts +0 -16
  243. package/output/catalyst/navbar.d.ts.map +0 -1
  244. package/output/catalyst/navbar.js +0 -59
  245. package/output/catalyst/pagination.d.ts +0 -18
  246. package/output/catalyst/pagination.d.ts.map +0 -1
  247. package/output/catalyst/pagination.js +0 -35
  248. package/output/catalyst/radio.d.ts +0 -38
  249. package/output/catalyst/radio.d.ts.map +0 -1
  250. package/output/catalyst/radio.js +0 -100
  251. package/output/catalyst/select.d.ts +0 -6
  252. package/output/catalyst/select.d.ts.map +0 -1
  253. package/output/catalyst/select.js +0 -52
  254. package/output/catalyst/sidebar-layout.d.ts +0 -6
  255. package/output/catalyst/sidebar-layout.d.ts.map +0 -1
  256. package/output/catalyst/sidebar-layout.js +0 -18
  257. package/output/catalyst/sidebar.d.ts +0 -20
  258. package/output/catalyst/sidebar.d.ts.map +0 -1
  259. package/output/catalyst/sidebar.js +0 -77
  260. package/output/catalyst/stacked-layout.d.ts +0 -6
  261. package/output/catalyst/stacked-layout.d.ts.map +0 -1
  262. package/output/catalyst/stacked-layout.js +0 -18
  263. package/output/catalyst/switch.d.ts +0 -37
  264. package/output/catalyst/switch.d.ts.map +0 -1
  265. package/output/catalyst/switch.js +0 -160
  266. package/output/catalyst/table.d.ts +0 -17
  267. package/output/catalyst/table.d.ts.map +0 -1
  268. package/output/catalyst/table.js +0 -56
  269. package/output/catalyst/text.d.ts +0 -6
  270. package/output/catalyst/text.d.ts.map +0 -1
  271. package/output/catalyst/text.js +0 -30
  272. package/output/catalyst/textarea.d.ts +0 -7
  273. package/output/catalyst/textarea.d.ts.map +0 -1
  274. package/output/catalyst/textarea.js +0 -48
  275. package/output/catalyst-config/catalyst-config.d.ts +0 -14
  276. package/output/catalyst-config/catalyst-config.d.ts.map +0 -1
  277. package/output/catalyst-config/catalyst-config.js +0 -14
  278. package/output/catalyst-config/index.d.ts +0 -3
  279. package/output/catalyst-config/index.d.ts.map +0 -1
  280. package/output/catalyst-config/index.js +0 -1
  281. package/output/loading-button/LoadingButtonDemo.d.ts +0 -5
  282. package/output/loading-button/LoadingButtonDemo.d.ts.map +0 -1
  283. package/output/loading-button/LoadingButtonDemo.js +0 -33
  284. package/output/loading-button/index.d.ts.map +0 -1
  285. package/output/loading-button/index.test.d.ts.map +0 -1
  286. package/output/service/index.d.ts.map +0 -1
  287. package/output/service/service.d.ts +0 -286
  288. package/output/service/service.d.ts.map +0 -1
  289. package/output/service/service.js +0 -352
  290. package/output/service/service.test.d.ts.map +0 -1
  291. package/output/side-cache/index.d.ts.map +0 -1
  292. package/output/side-cache/side-cache.d.ts.map +0 -1
  293. package/output/side-cache/side-cache.test.d.ts.map +0 -1
  294. package/output/signin/context.d.ts.map +0 -1
  295. package/output/signin/index.d.ts.map +0 -1
  296. package/output/signin/service.d.ts.map +0 -1
  297. package/output/signin/service.test.d.ts.map +0 -1
  298. package/output/signin/types.d.ts.map +0 -1
  299. package/output/size-provider/index.d.ts.map +0 -1
  300. package/output/size-provider/size-provider.d.ts.map +0 -1
  301. package/output/toaster/index.d.ts.map +0 -1
  302. package/output/use-observer/index.d.ts.map +0 -1
  303. package/output/use-observer/use-observer.d.ts.map +0 -1
  304. package/output/use-observer/use-observer.test.d.ts.map +0 -1
  305. /package/output/{loading-button → components/loading-button}/index.d.ts +0 -0
  306. /package/output/{loading-button → components/loading-button}/index.test.d.ts +0 -0
  307. /package/output/{size-provider → components/size-provider}/index.d.ts +0 -0
  308. /package/output/{size-provider → components/size-provider}/index.js +0 -0
  309. /package/output/{size-provider → components/size-provider}/size-provider.d.ts +0 -0
  310. /package/output/{size-provider → components/size-provider}/size-provider.js +0 -0
  311. /package/output/{toaster → components/toaster}/index.js +0 -0
  312. /package/output/{signin → features/signin}/context.d.ts +0 -0
  313. /package/output/{signin → features/signin}/context.js +0 -0
  314. /package/output/{signin → features/signin}/service.d.ts +0 -0
  315. /package/output/{signin → features/signin}/service.js +0 -0
  316. /package/output/{service → features/signin}/service.test.d.ts +0 -0
  317. /package/output/{signin → features/signin}/service.test.js +0 -0
  318. /package/output/{signin → features/signin}/types.d.ts +0 -0
  319. /package/output/{signin → features/signin}/types.js +0 -0
  320. /package/output/{use-observer → hooks/use-observer}/index.d.ts +0 -0
  321. /package/output/{use-observer → hooks/use-observer}/index.js +0 -0
  322. /package/output/{use-observer → hooks/use-observer}/use-observer.d.ts +0 -0
  323. /package/output/{use-observer → hooks/use-observer}/use-observer.js +0 -0
  324. /package/output/{use-observer → hooks/use-observer}/use-observer.test.d.ts +0 -0
  325. /package/output/{use-observer → hooks/use-observer}/use-observer.test.js +0 -0
  326. /package/output/{cache-api → services/cache-api}/cache-api.d.ts +0 -0
  327. /package/output/{cache-api → services/cache-api}/cache-api.js +0 -0
  328. /package/output/{cache-api → services/cache-api}/cache-api.test.d.ts +0 -0
  329. /package/output/{cache-api → services/cache-api}/cache-api.test.js +0 -0
  330. /package/output/{cache-api → services/cache-api}/index.d.ts +0 -0
  331. /package/output/{cache-api → services/cache-api}/index.js +0 -0
  332. /package/output/{service → services/service}/index.d.ts +0 -0
  333. /package/output/{service → services/service}/index.js +0 -0
  334. /package/output/{signin → services/service}/service.test.d.ts +0 -0
  335. /package/output/{side-cache → services/side-cache}/index.d.ts +0 -0
  336. /package/output/{side-cache → services/side-cache}/index.js +0 -0
  337. /package/output/{side-cache → services/side-cache}/side-cache.d.ts +0 -0
  338. /package/output/{side-cache → services/side-cache}/side-cache.js +0 -0
  339. /package/output/{side-cache → services/side-cache}/side-cache.test.d.ts +0 -0
  340. /package/output/{side-cache → services/side-cache}/side-cache.test.js +0 -0
@@ -0,0 +1,276 @@
1
+ /**
2
+ * React 服务层集成工具
3
+ *
4
+ * 本模块提供了在 React 应用中集成服务和状态管理(Mobx)的工具函数和组件。
5
+ * 主要功能包括:
6
+ * - 通过 useService Hook 使用全局注册的服务,并自动响应状态变化
7
+ * - 通过 useLocalService Hook 创建局部服务实例,避免全局状态污染
8
+ * - 通过 ServiceProvider 组件提供服务注册表上下文
9
+ * - 自动初始化服务实例
10
+ *
11
+ * @example
12
+ * ```tsx
13
+ * // 1. 定义服务
14
+ * class UserService extends Service {
15
+ * @observable users: User[] = []
16
+ * async init() { return true }
17
+ * }
18
+ *
19
+ * // 2. 在根组件提供服务
20
+ * function App() {
21
+ * return (
22
+ * <ServiceProvider services={[UserService]}>
23
+ * <UserList />
24
+ * </ServiceProvider>
25
+ * )
26
+ * }
27
+ *
28
+ * // 3. 在组件中使用服务
29
+ * function UserList() {
30
+ * const users = useService(UserService, s => s.users)
31
+ * return <div>{users.map(user => <div key={user.id}>{user.name}</div>)}</div>
32
+ * }
33
+ * ```
34
+ */
35
+ import { reaction, runInAction } from 'mobx';
36
+ import { Service } from '@taicode/common-base';
37
+ import React, { useContext, useEffect, useMemo, useState, useRef, createElement } from 'react';
38
+ const INIT_RETURN_ERROR_MESSAGE = 'Service init must return a boolean value.';
39
+ class InvalidInitReturnError extends Error {
40
+ constructor() {
41
+ super(INIT_RETURN_ERROR_MESSAGE);
42
+ this.name = 'InvalidInitReturnError';
43
+ }
44
+ }
45
+ /**
46
+ * 轻量级服务注册表,替代 DI 容器。
47
+ * 以服务类为键,按需创建并缓存单例实例,支持父子级联查找。
48
+ */
49
+ class ServiceRegistry {
50
+ constructor(parent) {
51
+ this.values = new Map();
52
+ this.singletons = new Map();
53
+ this.parent = parent;
54
+ }
55
+ /** 注册值注入的服务实例 */
56
+ setValue(provide, value) {
57
+ this.values.set(provide, value);
58
+ }
59
+ /** 在当前及父级注册表中查找已存在的实例 */
60
+ find(provide) {
61
+ var _a;
62
+ const value = this.values.get(provide);
63
+ if (value !== undefined)
64
+ return value;
65
+ const singleton = this.singletons.get(provide);
66
+ if (singleton !== undefined)
67
+ return singleton;
68
+ return (_a = this.parent) === null || _a === void 0 ? void 0 : _a.find(provide);
69
+ }
70
+ /** 获取服务实例;若不存在则惰性创建并缓存单例 */
71
+ get(ServiceClass) {
72
+ const found = this.find(ServiceClass);
73
+ if (found !== undefined)
74
+ return found;
75
+ const instance = new ServiceClass();
76
+ this.singletons.set(ServiceClass, instance);
77
+ return instance;
78
+ }
79
+ /** 创建全新的局部实例(不受父级影响),并缓存到当前注册表 */
80
+ createLocal(ServiceClass) {
81
+ const instance = new ServiceClass();
82
+ this.singletons.set(ServiceClass, instance);
83
+ return instance;
84
+ }
85
+ }
86
+ /**
87
+ * React Context 用于传递服务注册表实例
88
+ * 通过上下文在组件树中共享服务实例
89
+ */
90
+ const ctx = React.createContext(undefined);
91
+ function setServiceInited(target, value) {
92
+ runInAction(() => {
93
+ if (target != null && typeof target === 'object') {
94
+ target.inited = value;
95
+ }
96
+ });
97
+ }
98
+ function applyInitResult(target, result) {
99
+ if (typeof result === 'boolean') {
100
+ setServiceInited(target, result);
101
+ return;
102
+ }
103
+ setServiceInited(target, false);
104
+ throw new InvalidInitReturnError();
105
+ }
106
+ async function initializeServiceInstance(target) {
107
+ if (target == null ||
108
+ typeof target !== 'object' ||
109
+ !('init' in target) ||
110
+ typeof target.init !== 'function') {
111
+ return;
112
+ }
113
+ const service = target;
114
+ if (service.inited === true) {
115
+ return;
116
+ }
117
+ try {
118
+ const result = await service.init();
119
+ applyInitResult(service, result);
120
+ }
121
+ catch (error) {
122
+ if (error instanceof InvalidInitReturnError) {
123
+ throw error;
124
+ }
125
+ setServiceInited(service, false);
126
+ }
127
+ }
128
+ export function useService(target, selector) {
129
+ const registry = useContext(ctx);
130
+ const [, refresh] = useState({});
131
+ useEffect(() => {
132
+ if (registry == null)
133
+ return;
134
+ const service = registry.get(target);
135
+ const dispose = reaction(() => {
136
+ if (!selector) {
137
+ return service;
138
+ }
139
+ const result = selector(service);
140
+ if (result === service) {
141
+ return service;
142
+ }
143
+ return result;
144
+ }, () => refresh({}));
145
+ return dispose;
146
+ }, [registry, selector, target]);
147
+ if (registry == null) {
148
+ throw new Error('Must be a child of ServiceProvider.');
149
+ }
150
+ const service = registry.get(target);
151
+ return selector ? selector(service) : service;
152
+ }
153
+ export function useLocalService(ServiceClass, selector) {
154
+ const parentRegistry = useContext(ctx);
155
+ const serviceInstanceRef = useRef(null);
156
+ const [, refresh] = useState({});
157
+ const [initError, setInitError] = useState(null);
158
+ if (parentRegistry == null) {
159
+ throw new Error('Must be a child of ServiceProvider.');
160
+ }
161
+ if (serviceInstanceRef.current == null) {
162
+ const localRegistry = new ServiceRegistry(parentRegistry);
163
+ serviceInstanceRef.current = localRegistry.createLocal(ServiceClass);
164
+ }
165
+ useEffect(() => {
166
+ if (parentRegistry == null)
167
+ return;
168
+ const service = serviceInstanceRef.current;
169
+ const dispose = reaction(() => {
170
+ if (!selector) {
171
+ return service;
172
+ }
173
+ const result = selector(service);
174
+ if (result === service) {
175
+ return service;
176
+ }
177
+ return result;
178
+ }, () => refresh({}));
179
+ return dispose;
180
+ }, [parentRegistry, selector, ServiceClass]);
181
+ useEffect(() => {
182
+ if (parentRegistry == null)
183
+ return;
184
+ let disposed = false;
185
+ setInitError(prev => (prev === null ? prev : null));
186
+ const service = serviceInstanceRef.current;
187
+ initializeServiceInstance(service).catch((error) => {
188
+ if (error instanceof InvalidInitReturnError && !disposed) {
189
+ setInitError(error);
190
+ }
191
+ });
192
+ return () => {
193
+ disposed = true;
194
+ };
195
+ }, [parentRegistry, ServiceClass]);
196
+ if (initError) {
197
+ throw initError;
198
+ }
199
+ return selector ? selector(serviceInstanceRef.current) : serviceInstanceRef.current;
200
+ }
201
+ /**
202
+ * 服务提供者组件
203
+ *
204
+ * 为其子组件树提供服务注册表上下文,自动复用父级注册表并创建子注册表。
205
+ * 支持容器层级结构,子注册表可以覆盖父级的服务。
206
+ *
207
+ * @param props 组件属性
208
+ */
209
+ export function ServiceProvider(props) {
210
+ const parentRegistry = useContext(ctx);
211
+ const { children, services } = props;
212
+ const registry = useMemo(() => {
213
+ const currentRegistry = new ServiceRegistry(parentRegistry);
214
+ services.forEach(service => {
215
+ if (service != null && typeof service === 'object' && 'provide' in service && 'useValue' in service) {
216
+ currentRegistry.setValue(service.provide, service.useValue);
217
+ }
218
+ });
219
+ return currentRegistry;
220
+ }, [parentRegistry, services]);
221
+ const init = createElement(InitService, { services, children });
222
+ return createElement(ctx.Provider, { value: registry, children: registry ? init : null });
223
+ }
224
+ /**
225
+ * 服务初始化组件
226
+ *
227
+ * 在组件挂载时自动初始化实现了 Service 接口的类注入服务实例。
228
+ *
229
+ * 初始化流程:
230
+ * 1. 跳过值注入的服务(它们应该在传入前已经初始化)
231
+ * 2. 从注册表获取服务实例(仅类注入的服务)
232
+ * 3. 检查服务是否实现了 Service 接口(有 init 方法)
233
+ * 4. 检查服务是否已经初始化(inited 属性)
234
+ * 5. 如果未初始化,则调用 init 方法并更新状态
235
+ *
236
+ * @param props 组件属性,与 ServiceProviderProps 相同
237
+ */
238
+ function InitService(props) {
239
+ const { services, children } = props;
240
+ const registry = useContext(ctx);
241
+ const [initError, setInitError] = useState(null);
242
+ useEffect(() => {
243
+ if (registry == null)
244
+ return;
245
+ if (services.length == 0)
246
+ return;
247
+ let disposed = false;
248
+ setInitError(prev => (prev === null ? prev : null));
249
+ const initPromises = services.map(async (service) => {
250
+ if (service != null && typeof service === 'object' && 'provide' in service && 'useValue' in service) {
251
+ return;
252
+ }
253
+ const instance = registry.get(service);
254
+ if (instance != null && typeof instance === 'object') {
255
+ const serviceInstance = instance;
256
+ if (typeof serviceInstance.init === 'function') {
257
+ if (serviceInstance.inited !== true) {
258
+ await initializeServiceInstance(serviceInstance).catch((error) => {
259
+ if (error instanceof InvalidInitReturnError && !disposed) {
260
+ setInitError(error);
261
+ }
262
+ });
263
+ }
264
+ }
265
+ }
266
+ });
267
+ void Promise.all(initPromises);
268
+ return () => {
269
+ disposed = true;
270
+ };
271
+ }, [registry, services]);
272
+ if (initError) {
273
+ throw initError;
274
+ }
275
+ return children;
276
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.test.d.ts","sourceRoot":"","sources":["../../../source/services/service/service.test.tsx"],"names":[],"mappings":""}
@@ -49,7 +49,6 @@ import { Service } from '@taicode/common-base';
49
49
  import { describe, it, expect, vi, beforeEach } from 'vitest';
50
50
  import { render, renderHook, waitFor } from '@testing-library/react';
51
51
  import { observable, makeObservable, runInAction, action } from 'mobx';
52
- import { inject } from '@needle-di/core';
53
52
  import { useService, useLocalService, ServiceProvider } from './service.js';
54
53
  class TestErrorBoundary extends React.Component {
55
54
  constructor() {
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../source/services/side-cache/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"side-cache.d.ts","sourceRoot":"","sources":["../../../source/services/side-cache/side-cache.ts"],"names":[],"mappings":"AAWA,qBAAa,SAAS,CAAC,CAAC;IAEtB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAgB;IAG1C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAoB;IAG/C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAoC;IAE1D,IACW,KAAK,IAAI,CAAC,GAAG,SAAS,CAIhC;IAED,IACW,KAAK,IAAI,OAAO,CAE1B;IAED,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC;IACvE,MAAM,CAAC,CAAC,SAAS,CAAC,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC;IAyC1F,KAAK,CAAC,GAAG,CAAC,EAAE,OAAO,GAAG,IAAI;CAoBlC"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"side-cache.test.d.ts","sourceRoot":"","sources":["../../../source/services/side-cache/side-cache.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Avatar 工具函数
3
+ * 用于生成用户头像 URL
4
+ */
5
+ /**
6
+ * 使用原生 Web Crypto API 生成 SHA-256 哈希
7
+ * @param input 输入字符串
8
+ * @returns SHA-256 哈希值(十六进制字符串)
9
+ */
10
+ export type GetUserAvatarUrlParams = {
11
+ username: string;
12
+ avatar?: string | null;
13
+ };
14
+ /**
15
+ * 生成 Gravatar 头像 URL
16
+ * @param email 用户邮箱
17
+ * @param size 头像大小(像素)
18
+ * @returns Gravatar URL
19
+ */
20
+ export declare function getGravatarUrl(email: string, size?: number): Promise<string>;
21
+ /**
22
+ * 生成用户头像 URL
23
+ * @param user 用户对象
24
+ * @param size 头像大小(像素)
25
+ * @returns 头像 URL(如果有自定义头像返回自定义,否则返回 Gravatar)
26
+ */
27
+ export declare function getUserAvatarUrl(user: GetUserAvatarUrlParams, size?: number): Promise<string>;
28
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../source/utils/avatar/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;GAIG;AACH,MAAM,MAAM,sBAAsB,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAA;AAWjF;;;;;GAKG;AACH,wBAAsB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,SAAK,GAAG,OAAO,CAAC,MAAM,CAAC,CAK9E;AAED;;;;;GAKG;AACH,wBAAsB,gBAAgB,CAAC,IAAI,EAAE,sBAAsB,EAAE,IAAI,SAAK,GAAG,OAAO,CAAC,MAAM,CAAC,CAK/F"}
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Avatar 工具函数
3
+ * 用于生成用户头像 URL
4
+ */
5
+ async function sha256(input) {
6
+ const encoder = new TextEncoder();
7
+ const data = encoder.encode(input);
8
+ const hashBuffer = await crypto.subtle.digest('SHA-256', data);
9
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
10
+ return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
11
+ }
12
+ /**
13
+ * 生成 Gravatar 头像 URL
14
+ * @param email 用户邮箱
15
+ * @param size 头像大小(像素)
16
+ * @returns Gravatar URL
17
+ */
18
+ export async function getGravatarUrl(email, size = 80) {
19
+ const trimmedEmail = email.trim();
20
+ const lowerEmail = trimmedEmail.toLowerCase();
21
+ const hash = await sha256(lowerEmail);
22
+ return `https://www.gravatar.com/avatar/${hash}?d=mp&s=${size}`;
23
+ }
24
+ /**
25
+ * 生成用户头像 URL
26
+ * @param user 用户对象
27
+ * @param size 头像大小(像素)
28
+ * @returns 头像 URL(如果有自定义头像返回自定义,否则返回 Gravatar)
29
+ */
30
+ export async function getUserAvatarUrl(user, size = 80) {
31
+ if (user.avatar) {
32
+ return user.avatar;
33
+ }
34
+ return getGravatarUrl(user.username, size);
35
+ }
@@ -0,0 +1,2 @@
1
+ export declare function delay(ms: number): Promise<unknown>;
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../source/utils/delay/index.ts"],"names":[],"mappings":"AAAA,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,oBAI/B"}
@@ -0,0 +1,5 @@
1
+ export function delay(ms) {
2
+ return new Promise(resolve => {
3
+ setTimeout(resolve, ms);
4
+ });
5
+ }
@@ -0,0 +1,3 @@
1
+ export type FormValidateResult<E extends string> = E | undefined | Promise<E | undefined>;
2
+ export type FormValidate<T extends object, K extends keyof T, E extends string> = (field: T[K], form: T) => FormValidateResult<E>;
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../source/utils/form/index.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,kBAAkB,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,GAAG,SAAS,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC,CAAA;AACzF,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,CAAC,SAAS,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,KAAK,kBAAkB,CAAC,CAAC,CAAC,CAAA"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import { type ClassValue } from 'clsx';
2
+ export declare function mergeClassNames(...inputs: ClassValue[]): string;
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../source/utils/merge-class-names/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,UAAU,EAAE,MAAM,MAAM,CAAA;AAG5C,wBAAgB,eAAe,CAAC,GAAG,MAAM,EAAE,UAAU,EAAE,UAEtD"}
@@ -0,0 +1,5 @@
1
+ import { clsx } from 'clsx';
2
+ import { twMerge } from 'tailwind-merge';
3
+ export function mergeClassNames(...inputs) {
4
+ return twMerge(clsx(inputs));
5
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../../../source/utils/merge-class-names/index.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { mergeClassNames } from './index.js';
3
+ describe('mergeClassNames', () => {
4
+ it('merges conditional and conflicting Tailwind classes', () => {
5
+ expect(mergeClassNames('px-2', false && 'text-muted-foreground', 'px-4')).toBe('px-4');
6
+ });
7
+ });
@@ -0,0 +1,82 @@
1
+ import { type Metric } from 'web-vitals';
2
+ /**
3
+ * 性能指标类型
4
+ */
5
+ export interface PerformanceMetrics {
6
+ cls?: Metric;
7
+ fcp?: Metric;
8
+ inp?: Metric;
9
+ lcp?: Metric;
10
+ ttfb?: Metric;
11
+ }
12
+ /**
13
+ * 性能监控配置
14
+ */
15
+ export interface PerformanceMonitorConfig {
16
+ /**
17
+ * 是否启用性能监控
18
+ */
19
+ enabled: boolean;
20
+ /**
21
+ * 是否在控制台输出日志
22
+ */
23
+ logToConsole: boolean;
24
+ /**
25
+ * 性能指标上报回调
26
+ */
27
+ onMetric?: (metric: Metric) => void;
28
+ /**
29
+ * 错误追踪回调
30
+ */
31
+ onError?: (error: Error) => void;
32
+ }
33
+ /**
34
+ * 性能监控器类
35
+ */
36
+ export declare class PerformanceMonitor {
37
+ private config;
38
+ private metrics;
39
+ private isInitialized;
40
+ constructor(config?: Partial<PerformanceMonitorConfig>);
41
+ /**
42
+ * 初始化性能监控
43
+ */
44
+ init(): void;
45
+ /**
46
+ * 处理性能指标
47
+ */
48
+ private handleMetric;
49
+ /**
50
+ * 输出性能指标到控制台
51
+ */
52
+ private logMetric;
53
+ /**
54
+ * 设置错误追踪
55
+ */
56
+ private setupErrorTracking;
57
+ /**
58
+ * 处理错误
59
+ */
60
+ private handleError;
61
+ /**
62
+ * 获取当前收集的所有性能指标
63
+ */
64
+ getMetrics(): PerformanceMetrics;
65
+ /**
66
+ * 手动记录自定义性能指标
67
+ */
68
+ recordCustomMetric(name: string, value: number, metadata?: Record<string, unknown>): void;
69
+ /**
70
+ * 手动报告错误
71
+ */
72
+ reportError(error: Error | string): void;
73
+ }
74
+ /**
75
+ * 获取性能监控器单例
76
+ */
77
+ export declare function getPerformanceMonitor(config?: Partial<PerformanceMonitorConfig>): PerformanceMonitor;
78
+ /**
79
+ * 初始化性能监控(便捷函数)
80
+ */
81
+ export declare function initPerformanceMonitoring(config?: Partial<PerformanceMonitorConfig>): void;
82
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../source/utils/performance/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsC,KAAK,MAAM,EAAE,MAAM,YAAY,CAAA;AAE5E;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC;;OAEG;IACH,OAAO,EAAE,OAAO,CAAA;IAChB;;OAEG;IACH,YAAY,EAAE,OAAO,CAAA;IACrB;;OAEG;IACH,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAA;IACnC;;OAEG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAA;CACjC;AAUD;;GAEG;AACH,qBAAa,kBAAkB;IAC7B,OAAO,CAAC,MAAM,CAA0B;IACxC,OAAO,CAAC,OAAO,CAAyB;IACxC,OAAO,CAAC,aAAa,CAAQ;gBAEjB,MAAM,CAAC,EAAE,OAAO,CAAC,wBAAwB,CAAC;IAItD;;OAEG;IACH,IAAI,IAAI,IAAI;IAsBZ;;OAEG;IACH,OAAO,CAAC,YAAY;IAYpB;;OAEG;IACH,OAAO,CAAC,SAAS;IAWjB;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAU1B;;OAEG;IACH,OAAO,CAAC,WAAW;IAUnB;;OAEG;IACH,UAAU,IAAI,kBAAkB;IAIhC;;OAEG;IACH,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI;IAkBzF;;OAEG;IACH,WAAW,CAAC,KAAK,EAAE,KAAK,GAAG,MAAM,GAAG,IAAI;CAIzC;AAKD;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,wBAAwB,CAAC,GAAG,kBAAkB,CAKpG;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,wBAAwB,CAAC,GAAG,IAAI,CAG1F"}
@@ -0,0 +1,129 @@
1
+ import { onCLS, onFCP, onINP, onLCP, onTTFB } from 'web-vitals';
2
+ /**
3
+ * 默认配置
4
+ */
5
+ const defaultConfig = {
6
+ enabled: true,
7
+ logToConsole: true,
8
+ };
9
+ /**
10
+ * 性能监控器类
11
+ */
12
+ export class PerformanceMonitor {
13
+ constructor(config) {
14
+ this.metrics = {};
15
+ this.isInitialized = false;
16
+ this.config = Object.assign(Object.assign({}, defaultConfig), config);
17
+ }
18
+ /**
19
+ * 初始化性能监控
20
+ */
21
+ init() {
22
+ if (this.isInitialized || !this.config.enabled) {
23
+ return;
24
+ }
25
+ this.isInitialized = true;
26
+ // 注册 Core Web Vitals 监听器
27
+ onCLS(this.handleMetric.bind(this));
28
+ onFCP(this.handleMetric.bind(this));
29
+ onINP(this.handleMetric.bind(this));
30
+ onLCP(this.handleMetric.bind(this));
31
+ onTTFB(this.handleMetric.bind(this));
32
+ // 注册全局错误监听器
33
+ this.setupErrorTracking();
34
+ if (this.config.logToConsole) {
35
+ console.log('[PerformanceMonitor] 性能监控已初始化');
36
+ }
37
+ }
38
+ /**
39
+ * 处理性能指标
40
+ */
41
+ handleMetric(metric) {
42
+ this.metrics[metric.name.toLowerCase()] = metric;
43
+ if (this.config.logToConsole) {
44
+ this.logMetric(metric);
45
+ }
46
+ if (this.config.onMetric) {
47
+ this.config.onMetric(metric);
48
+ }
49
+ }
50
+ /**
51
+ * 输出性能指标到控制台
52
+ */
53
+ logMetric(metric) {
54
+ const { name, value, rating } = metric;
55
+ const color = rating === 'good' ? '#22c55e' : rating === 'needs-improvement' ? '#eab308' : '#ef4444';
56
+ console.log(`%c[Performance] ${name}: ${value.toFixed(2)} (${rating})`, `color: ${color}; font-weight: bold;`);
57
+ }
58
+ /**
59
+ * 设置错误追踪
60
+ */
61
+ setupErrorTracking() {
62
+ window.addEventListener('error', (event) => {
63
+ this.handleError(event.error || new Error(event.message));
64
+ });
65
+ window.addEventListener('unhandledrejection', (event) => {
66
+ this.handleError(new Error(`Unhandled Promise Rejection: ${event.reason}`));
67
+ });
68
+ }
69
+ /**
70
+ * 处理错误
71
+ */
72
+ handleError(error) {
73
+ if (this.config.logToConsole) {
74
+ console.error('[PerformanceMonitor] 捕获到错误:', error);
75
+ }
76
+ if (this.config.onError) {
77
+ this.config.onError(error);
78
+ }
79
+ }
80
+ /**
81
+ * 获取当前收集的所有性能指标
82
+ */
83
+ getMetrics() {
84
+ return Object.assign({}, this.metrics);
85
+ }
86
+ /**
87
+ * 手动记录自定义性能指标
88
+ */
89
+ recordCustomMetric(name, value, metadata) {
90
+ const customMetric = {
91
+ name,
92
+ value,
93
+ rating: value < 100 ? 'good' : value < 300 ? 'needs-improvement' : 'poor',
94
+ delta: value,
95
+ entries: [],
96
+ id: `custom-${Date.now()}`,
97
+ navigationType: 'navigate',
98
+ };
99
+ this.handleMetric(customMetric);
100
+ if (this.config.logToConsole && metadata) {
101
+ console.log('[PerformanceMonitor] 自定义指标元数据:', metadata);
102
+ }
103
+ }
104
+ /**
105
+ * 手动报告错误
106
+ */
107
+ reportError(error) {
108
+ const errorObj = typeof error === 'string' ? new Error(error) : error;
109
+ this.handleError(errorObj);
110
+ }
111
+ }
112
+ // 创建单例实例
113
+ let monitorInstance = null;
114
+ /**
115
+ * 获取性能监控器单例
116
+ */
117
+ export function getPerformanceMonitor(config) {
118
+ if (!monitorInstance) {
119
+ monitorInstance = new PerformanceMonitor(config);
120
+ }
121
+ return monitorInstance;
122
+ }
123
+ /**
124
+ * 初始化性能监控(便捷函数)
125
+ */
126
+ export function initPerformanceMonitoring(config) {
127
+ const monitor = getPerformanceMonitor(config);
128
+ monitor.init();
129
+ }
@@ -0,0 +1,6 @@
1
+ interface ResolvedRedirect {
2
+ target: string;
3
+ }
4
+ export declare function resolveSafeRedirect(redirect: string | undefined, locale: string): ResolvedRedirect | null;
5
+ export {};
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../source/utils/redirect/index.ts"],"names":[],"mappings":"AAEA,UAAU,gBAAgB;IACxB,MAAM,EAAE,MAAM,CAAA;CACf;AAgBD,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAAE,MAAM,EAAE,MAAM,GAAG,gBAAgB,GAAG,IAAI,CAwBzG"}