@composy/store-core 0.0.1

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 (272) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/README.md +172 -0
  3. package/dist/index.js +1852 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/index.min.js +1082 -0
  6. package/dist/index.min.js.map +1 -0
  7. package/es/core/async-action.d.ts +95 -0
  8. package/es/core/async-action.d.ts.map +1 -0
  9. package/es/core/async-action.js +116 -0
  10. package/es/core/async-action.js.map +1 -0
  11. package/es/core/base-store.d.ts +85 -0
  12. package/es/core/base-store.d.ts.map +1 -0
  13. package/es/core/base-store.js +185 -0
  14. package/es/core/base-store.js.map +1 -0
  15. package/es/core/batch-manager.d.ts +46 -0
  16. package/es/core/batch-manager.d.ts.map +1 -0
  17. package/es/core/batch-manager.js +64 -0
  18. package/es/core/batch-manager.js.map +1 -0
  19. package/es/core/create-store.d.ts +66 -0
  20. package/es/core/create-store.d.ts.map +1 -0
  21. package/es/core/create-store.js +29 -0
  22. package/es/core/create-store.js.map +1 -0
  23. package/es/core/event-bus.d.ts +34 -0
  24. package/es/core/event-bus.d.ts.map +1 -0
  25. package/es/core/event-bus.js +71 -0
  26. package/es/core/event-bus.js.map +1 -0
  27. package/es/core/index.d.ts +14 -0
  28. package/es/core/index.d.ts.map +1 -0
  29. package/es/core/index.js +17 -0
  30. package/es/core/pub-sub.d.ts +41 -0
  31. package/es/core/pub-sub.d.ts.map +1 -0
  32. package/es/core/pub-sub.js +71 -0
  33. package/es/core/pub-sub.js.map +1 -0
  34. package/es/engine/index.d.ts +10 -0
  35. package/es/engine/index.d.ts.map +1 -0
  36. package/es/engine/index.js +12 -0
  37. package/es/engine/plugin.d.ts +20 -0
  38. package/es/engine/plugin.d.ts.map +1 -0
  39. package/es/engine/plugin.js +153 -0
  40. package/es/engine/plugin.js.map +1 -0
  41. package/es/engine/types.d.ts +81 -0
  42. package/es/engine/types.d.ts.map +1 -0
  43. package/es/engine/types.js +10 -0
  44. package/es/errors/index.d.ts +208 -0
  45. package/es/errors/index.d.ts.map +1 -0
  46. package/es/errors/index.js +217 -0
  47. package/es/errors/index.js.map +1 -0
  48. package/es/index.d.ts +27 -0
  49. package/es/index.d.ts.map +1 -0
  50. package/es/index.js +34 -0
  51. package/es/index.js.map +1 -0
  52. package/es/middleware/index.d.ts +13 -0
  53. package/es/middleware/index.d.ts.map +1 -0
  54. package/es/middleware/index.js +16 -0
  55. package/es/middleware/logger.d.ts +67 -0
  56. package/es/middleware/logger.d.ts.map +1 -0
  57. package/es/middleware/logger.js +136 -0
  58. package/es/middleware/logger.js.map +1 -0
  59. package/es/middleware/middleware-manager.d.ts +40 -0
  60. package/es/middleware/middleware-manager.d.ts.map +1 -0
  61. package/es/middleware/middleware-manager.js +79 -0
  62. package/es/middleware/middleware-manager.js.map +1 -0
  63. package/es/middleware/persist.d.ts +34 -0
  64. package/es/middleware/persist.d.ts.map +1 -0
  65. package/es/middleware/persist.js +102 -0
  66. package/es/middleware/persist.js.map +1 -0
  67. package/es/middleware/time-travel.d.ts +30 -0
  68. package/es/middleware/time-travel.d.ts.map +1 -0
  69. package/es/middleware/time-travel.js +151 -0
  70. package/es/middleware/time-travel.js.map +1 -0
  71. package/es/middleware/validation.d.ts +100 -0
  72. package/es/middleware/validation.d.ts.map +1 -0
  73. package/es/middleware/validation.js +74 -0
  74. package/es/middleware/validation.js.map +1 -0
  75. package/es/plugins/define-plugin.d.ts +74 -0
  76. package/es/plugins/define-plugin.d.ts.map +1 -0
  77. package/es/plugins/define-plugin.js +52 -0
  78. package/es/plugins/define-plugin.js.map +1 -0
  79. package/es/plugins/index.d.ts +8 -0
  80. package/es/plugins/index.d.ts.map +1 -0
  81. package/es/plugins/index.js +13 -0
  82. package/es/plugins/plugin-manager.d.ts +35 -0
  83. package/es/plugins/plugin-manager.d.ts.map +1 -0
  84. package/es/plugins/plugin-manager.js +77 -0
  85. package/es/plugins/plugin-manager.js.map +1 -0
  86. package/es/types/index.d.ts +12 -0
  87. package/es/types/index.d.ts.map +1 -0
  88. package/es/types/index.js +10 -0
  89. package/es/types/middleware.d.ts +172 -0
  90. package/es/types/middleware.d.ts.map +1 -0
  91. package/es/types/middleware.js +10 -0
  92. package/es/types/plugin.d.ts +136 -0
  93. package/es/types/plugin.d.ts.map +1 -0
  94. package/es/types/plugin.js +10 -0
  95. package/es/types/store.d.ts +198 -0
  96. package/es/types/store.d.ts.map +1 -0
  97. package/es/types/store.js +10 -0
  98. package/es/types/subscription.d.ts +158 -0
  99. package/es/types/subscription.d.ts.map +1 -0
  100. package/es/types/subscription.js +10 -0
  101. package/es/types/utility.d.ts +312 -0
  102. package/es/types/utility.d.ts.map +1 -0
  103. package/es/types/utility.js +10 -0
  104. package/es/utils/debounce-throttle.d.ts +86 -0
  105. package/es/utils/debounce-throttle.d.ts.map +1 -0
  106. package/es/utils/debounce-throttle.js +86 -0
  107. package/es/utils/debounce-throttle.js.map +1 -0
  108. package/es/utils/deep-clone.d.ts +143 -0
  109. package/es/utils/deep-clone.d.ts.map +1 -0
  110. package/es/utils/deep-clone.js +193 -0
  111. package/es/utils/deep-clone.js.map +1 -0
  112. package/es/utils/index.d.ts +13 -0
  113. package/es/utils/index.d.ts.map +1 -0
  114. package/es/utils/index.js +14 -0
  115. package/es/utils/selector.d.ts +213 -0
  116. package/es/utils/selector.d.ts.map +1 -0
  117. package/es/utils/selector.js +151 -0
  118. package/es/utils/selector.js.map +1 -0
  119. package/esm/core/async-action.d.ts +95 -0
  120. package/esm/core/async-action.js +116 -0
  121. package/esm/core/async-action.js.map +1 -0
  122. package/esm/core/base-store.d.ts +85 -0
  123. package/esm/core/base-store.js +185 -0
  124. package/esm/core/base-store.js.map +1 -0
  125. package/esm/core/batch-manager.d.ts +46 -0
  126. package/esm/core/batch-manager.js +64 -0
  127. package/esm/core/batch-manager.js.map +1 -0
  128. package/esm/core/create-store.d.ts +66 -0
  129. package/esm/core/create-store.js +29 -0
  130. package/esm/core/create-store.js.map +1 -0
  131. package/esm/core/event-bus.d.ts +34 -0
  132. package/esm/core/event-bus.js +71 -0
  133. package/esm/core/event-bus.js.map +1 -0
  134. package/esm/core/index.d.ts +14 -0
  135. package/esm/core/index.js +17 -0
  136. package/esm/core/pub-sub.d.ts +41 -0
  137. package/esm/core/pub-sub.js +71 -0
  138. package/esm/core/pub-sub.js.map +1 -0
  139. package/esm/engine/index.d.ts +10 -0
  140. package/esm/engine/index.js +12 -0
  141. package/esm/engine/plugin.d.ts +20 -0
  142. package/esm/engine/plugin.js +153 -0
  143. package/esm/engine/plugin.js.map +1 -0
  144. package/esm/engine/types.d.ts +81 -0
  145. package/esm/errors/index.d.ts +208 -0
  146. package/esm/errors/index.js +217 -0
  147. package/esm/errors/index.js.map +1 -0
  148. package/esm/index.d.ts +27 -0
  149. package/esm/index.js +36 -0
  150. package/esm/index.js.map +1 -0
  151. package/esm/middleware/index.d.ts +13 -0
  152. package/esm/middleware/logger.d.ts +67 -0
  153. package/esm/middleware/logger.js +119 -0
  154. package/esm/middleware/logger.js.map +1 -0
  155. package/esm/middleware/middleware-manager.d.ts +40 -0
  156. package/esm/middleware/middleware-manager.js +79 -0
  157. package/esm/middleware/middleware-manager.js.map +1 -0
  158. package/esm/middleware/persist.d.ts +34 -0
  159. package/esm/middleware/persist.js +102 -0
  160. package/esm/middleware/persist.js.map +1 -0
  161. package/esm/middleware/time-travel.d.ts +30 -0
  162. package/esm/middleware/time-travel.js +151 -0
  163. package/esm/middleware/time-travel.js.map +1 -0
  164. package/esm/middleware/validation.d.ts +100 -0
  165. package/esm/middleware/validation.js +74 -0
  166. package/esm/middleware/validation.js.map +1 -0
  167. package/esm/plugins/define-plugin.d.ts +74 -0
  168. package/esm/plugins/define-plugin.js +52 -0
  169. package/esm/plugins/define-plugin.js.map +1 -0
  170. package/esm/plugins/index.d.ts +8 -0
  171. package/esm/plugins/plugin-manager.d.ts +35 -0
  172. package/esm/plugins/plugin-manager.js +77 -0
  173. package/esm/plugins/plugin-manager.js.map +1 -0
  174. package/esm/types/index.d.ts +12 -0
  175. package/esm/types/middleware.d.ts +172 -0
  176. package/esm/types/plugin.d.ts +136 -0
  177. package/esm/types/store.d.ts +198 -0
  178. package/esm/types/subscription.d.ts +158 -0
  179. package/esm/types/utility.d.ts +312 -0
  180. package/esm/utils/debounce-throttle.d.ts +86 -0
  181. package/esm/utils/debounce-throttle.js +86 -0
  182. package/esm/utils/debounce-throttle.js.map +1 -0
  183. package/esm/utils/deep-clone.d.ts +143 -0
  184. package/esm/utils/deep-clone.js +193 -0
  185. package/esm/utils/deep-clone.js.map +1 -0
  186. package/esm/utils/index.d.ts +13 -0
  187. package/esm/utils/selector.d.ts +213 -0
  188. package/esm/utils/selector.js +151 -0
  189. package/esm/utils/selector.js.map +1 -0
  190. package/lib/core/async-action.cjs +118 -0
  191. package/lib/core/async-action.cjs.map +1 -0
  192. package/lib/core/async-action.d.ts +95 -0
  193. package/lib/core/base-store.cjs +187 -0
  194. package/lib/core/base-store.cjs.map +1 -0
  195. package/lib/core/base-store.d.ts +85 -0
  196. package/lib/core/batch-manager.cjs +69 -0
  197. package/lib/core/batch-manager.cjs.map +1 -0
  198. package/lib/core/batch-manager.d.ts +46 -0
  199. package/lib/core/create-store.cjs +31 -0
  200. package/lib/core/create-store.cjs.map +1 -0
  201. package/lib/core/create-store.d.ts +66 -0
  202. package/lib/core/event-bus.cjs +74 -0
  203. package/lib/core/event-bus.cjs.map +1 -0
  204. package/lib/core/event-bus.d.ts +34 -0
  205. package/lib/core/index.cjs +29 -0
  206. package/lib/core/index.d.ts +14 -0
  207. package/lib/core/pub-sub.cjs +74 -0
  208. package/lib/core/pub-sub.cjs.map +1 -0
  209. package/lib/core/pub-sub.d.ts +41 -0
  210. package/lib/engine/index.cjs +16 -0
  211. package/lib/engine/index.d.ts +10 -0
  212. package/lib/engine/plugin.cjs +157 -0
  213. package/lib/engine/plugin.cjs.map +1 -0
  214. package/lib/engine/plugin.d.ts +20 -0
  215. package/lib/engine/types.cjs +12 -0
  216. package/lib/engine/types.d.ts +81 -0
  217. package/lib/errors/index.cjs +233 -0
  218. package/lib/errors/index.cjs.map +1 -0
  219. package/lib/errors/index.d.ts +208 -0
  220. package/lib/index.cjs +101 -0
  221. package/lib/index.cjs.map +1 -0
  222. package/lib/index.d.ts +27 -0
  223. package/lib/middleware/index.cjs +28 -0
  224. package/lib/middleware/index.d.ts +13 -0
  225. package/lib/middleware/logger.cjs +140 -0
  226. package/lib/middleware/logger.cjs.map +1 -0
  227. package/lib/middleware/logger.d.ts +67 -0
  228. package/lib/middleware/middleware-manager.cjs +83 -0
  229. package/lib/middleware/middleware-manager.cjs.map +1 -0
  230. package/lib/middleware/middleware-manager.d.ts +40 -0
  231. package/lib/middleware/persist.cjs +105 -0
  232. package/lib/middleware/persist.cjs.map +1 -0
  233. package/lib/middleware/persist.d.ts +34 -0
  234. package/lib/middleware/time-travel.cjs +154 -0
  235. package/lib/middleware/time-travel.cjs.map +1 -0
  236. package/lib/middleware/time-travel.d.ts +30 -0
  237. package/lib/middleware/validation.cjs +76 -0
  238. package/lib/middleware/validation.cjs.map +1 -0
  239. package/lib/middleware/validation.d.ts +100 -0
  240. package/lib/package.json +3 -0
  241. package/lib/plugins/define-plugin.cjs +56 -0
  242. package/lib/plugins/define-plugin.cjs.map +1 -0
  243. package/lib/plugins/define-plugin.d.ts +74 -0
  244. package/lib/plugins/index.cjs +20 -0
  245. package/lib/plugins/index.d.ts +8 -0
  246. package/lib/plugins/plugin-manager.cjs +81 -0
  247. package/lib/plugins/plugin-manager.cjs.map +1 -0
  248. package/lib/plugins/plugin-manager.d.ts +35 -0
  249. package/lib/types/index.cjs +12 -0
  250. package/lib/types/index.d.ts +12 -0
  251. package/lib/types/middleware.cjs +12 -0
  252. package/lib/types/middleware.d.ts +172 -0
  253. package/lib/types/plugin.cjs +12 -0
  254. package/lib/types/plugin.d.ts +136 -0
  255. package/lib/types/store.cjs +12 -0
  256. package/lib/types/store.d.ts +198 -0
  257. package/lib/types/subscription.cjs +12 -0
  258. package/lib/types/subscription.d.ts +158 -0
  259. package/lib/types/utility.cjs +12 -0
  260. package/lib/types/utility.d.ts +312 -0
  261. package/lib/utils/debounce-throttle.cjs +89 -0
  262. package/lib/utils/debounce-throttle.cjs.map +1 -0
  263. package/lib/utils/debounce-throttle.d.ts +86 -0
  264. package/lib/utils/deep-clone.cjs +202 -0
  265. package/lib/utils/deep-clone.cjs.map +1 -0
  266. package/lib/utils/deep-clone.d.ts +143 -0
  267. package/lib/utils/index.cjs +34 -0
  268. package/lib/utils/index.d.ts +13 -0
  269. package/lib/utils/selector.cjs +161 -0
  270. package/lib/utils/selector.cjs.map +1 -0
  271. package/lib/utils/selector.d.ts +213 -0
  272. package/package.json +155 -0
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @lkit/store-core 类型定义
3
+ *
4
+ * 提供状态管理的核心类型定义
5
+ * @module types
6
+ */
7
+ export * from './store';
8
+ export * from './plugin';
9
+ export * from './middleware';
10
+ export * from './subscription';
11
+ export * from './utility';
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,172 @@
1
+ /**
2
+ * 中间件系统类型定义
3
+ *
4
+ * @module types/middleware
5
+ */
6
+ import type { StateTree, StoreId } from './store';
7
+ /**
8
+ * 中间件上下文
9
+ * @template S - 状态类型
10
+ */
11
+ export interface MiddlewareContext<S extends StateTree = StateTree> {
12
+ /** Store ID */
13
+ storeId: StoreId;
14
+ /** 当前状态 */
15
+ state: S;
16
+ /** 操作类型 */
17
+ type: MiddlewareActionType;
18
+ /** 操作数据 */
19
+ payload?: unknown;
20
+ /** 元数据 */
21
+ meta?: Record<string, unknown>;
22
+ }
23
+ /**
24
+ * 中间件操作类型
25
+ */
26
+ export type MiddlewareActionType = 'state:init' | 'state:patch' | 'state:reset' | 'action:before' | 'action:after' | 'action:error';
27
+ /**
28
+ * 中间件函数类型
29
+ * @template S - 状态类型
30
+ */
31
+ export type MiddlewareFunction<S extends StateTree = StateTree> = (context: MiddlewareContext<S>, next: () => void | Promise<void>) => void | Promise<void>;
32
+ /**
33
+ * 中间件接口
34
+ * @template S - 状态类型
35
+ */
36
+ export interface Middleware<S extends StateTree = StateTree> {
37
+ /** 中间件名称 */
38
+ readonly name: string;
39
+ /** 中间件优先级(数字越小越先执行) */
40
+ readonly priority?: number;
41
+ /** 中间件处理函数 */
42
+ handler: MiddlewareFunction<S>;
43
+ }
44
+ /**
45
+ * 中间件管理器接口
46
+ */
47
+ export interface MiddlewareManager {
48
+ /**
49
+ * 添加中间件
50
+ * @param middleware - 中间件实例
51
+ */
52
+ use: (middleware: Middleware) => void;
53
+ /**
54
+ * 移除中间件
55
+ * @param name - 中间件名称
56
+ */
57
+ remove: (name: string) => void;
58
+ /**
59
+ * 获取中间件
60
+ * @param name - 中间件名称
61
+ */
62
+ get: (name: string) => Middleware | undefined;
63
+ /**
64
+ * 是否存在中间件
65
+ * @param name - 中间件名称
66
+ */
67
+ has: (name: string) => boolean;
68
+ /**
69
+ * 执行中间件链
70
+ * @param context - 上下文
71
+ */
72
+ execute: (context: MiddlewareContext) => Promise<void>;
73
+ /**
74
+ * 获取所有中间件
75
+ */
76
+ getAll: () => Middleware[];
77
+ /**
78
+ * 清空所有中间件
79
+ */
80
+ clear: () => void;
81
+ }
82
+ /**
83
+ * 日志中间件选项
84
+ */
85
+ export interface LoggerMiddlewareOptions {
86
+ /** 是否记录状态变化 */
87
+ logState?: boolean;
88
+ /** 是否记录 action */
89
+ logAction?: boolean;
90
+ /** 是否折叠日志 */
91
+ collapsed?: boolean;
92
+ /** 是否显示时间戳 */
93
+ timestamp?: boolean;
94
+ /** 日志级别 */
95
+ level?: 'log' | 'info' | 'warn' | 'debug';
96
+ /** 自定义日志器 */
97
+ logger?: {
98
+ log: (...args: unknown[]) => void;
99
+ info: (...args: unknown[]) => void;
100
+ warn: (...args: unknown[]) => void;
101
+ debug: (...args: unknown[]) => void;
102
+ error?: (...args: unknown[]) => void;
103
+ group?: (...args: unknown[]) => void;
104
+ groupCollapsed?: (...args: unknown[]) => void;
105
+ groupEnd?: () => void;
106
+ };
107
+ /** 自定义过滤器 */
108
+ filter?: (context: MiddlewareContext) => boolean;
109
+ }
110
+ /**
111
+ * 持久化中间件选项
112
+ */
113
+ export interface PersistMiddlewareOptions {
114
+ /** 存储键名前缀 */
115
+ keyPrefix?: string;
116
+ /** 存储类型 */
117
+ storage?: 'localStorage' | 'sessionStorage';
118
+ /** 要持久化的 Store ID 列表(空则全部持久化) */
119
+ stores?: StoreId[];
120
+ /** 要持久化的状态路径 */
121
+ paths?: Record<StoreId, string[]>;
122
+ /** 序列化函数 */
123
+ serialize?: (value: unknown) => string;
124
+ /** 反序列化函数 */
125
+ deserialize?: (value: string) => unknown;
126
+ /** 防抖延迟(毫秒) */
127
+ debounce?: number;
128
+ }
129
+ /**
130
+ * 时间旅行中间件选项
131
+ */
132
+ export interface TimeTravelMiddlewareOptions {
133
+ /** 最大历史记录数 */
134
+ maxHistory?: number;
135
+ /** 是否启用 */
136
+ enabled?: boolean;
137
+ }
138
+ /**
139
+ * 时间旅行控制器
140
+ */
141
+ export interface TimeTravelController {
142
+ /** 撤销 */
143
+ undo: () => void;
144
+ /** 重做 */
145
+ redo: () => void;
146
+ /** 跳转到指定历史 */
147
+ goto: (index: number) => void;
148
+ /** 获取历史记录 */
149
+ getHistory: () => StateSnapshot[];
150
+ /** 当前历史索引 */
151
+ getCurrentIndex: () => number;
152
+ /** 是否可撤销 */
153
+ canUndo: () => boolean;
154
+ /** 是否可重做 */
155
+ canRedo: () => boolean;
156
+ /** 清空历史 */
157
+ clear: () => void;
158
+ }
159
+ /**
160
+ * 状态快照
161
+ */
162
+ export interface StateSnapshot {
163
+ /** 快照时间 */
164
+ timestamp: number;
165
+ /** 状态数据 */
166
+ state: StateTree;
167
+ /** 操作类型 */
168
+ type: MiddlewareActionType;
169
+ /** Store ID */
170
+ storeId: StoreId;
171
+ }
172
+ //# sourceMappingURL=middleware.d.ts.map
@@ -0,0 +1,136 @@
1
+ /**
2
+ * 插件系统类型定义
3
+ *
4
+ * @module types/plugin
5
+ */
6
+ import type { StateTree, StoreId } from './store';
7
+ /**
8
+ * 插件上下文
9
+ */
10
+ export interface PluginContext {
11
+ /** Store 管理器实例 */
12
+ storeManager: StoreManagerLike;
13
+ /** 当前 Store ID */
14
+ storeId: StoreId;
15
+ /** 当前 Store 实例 */
16
+ store: unknown;
17
+ /** 插件配置选项 */
18
+ options: Record<string, unknown>;
19
+ }
20
+ /**
21
+ * Store 管理器接口(简化版)
22
+ */
23
+ export interface StoreManagerLike {
24
+ /** 获取 Store */
25
+ getStore: (id: StoreId) => unknown;
26
+ /** 是否存在 Store */
27
+ hasStore: (id: StoreId) => boolean;
28
+ /** 获取所有 Store ID */
29
+ getStoreIds: () => StoreId[];
30
+ }
31
+ /**
32
+ * 插件接口
33
+ * @template Options - 插件选项类型
34
+ */
35
+ export interface StorePlugin<Options = Record<string, unknown>> {
36
+ /** 插件名称 */
37
+ readonly name: string;
38
+ /** 插件版本 */
39
+ readonly version?: string;
40
+ /** 插件描述 */
41
+ readonly description?: string;
42
+ /**
43
+ * 安装插件
44
+ * @param context - 插件上下文
45
+ * @param options - 插件选项
46
+ */
47
+ install: (context: PluginContext, options?: Options) => void | Promise<void>;
48
+ /**
49
+ * 卸载插件
50
+ * @param context - 插件上下文
51
+ */
52
+ uninstall?: (context: PluginContext) => void | Promise<void>;
53
+ /**
54
+ * Store 创建时的钩子
55
+ * @param store - Store 实例
56
+ */
57
+ onStoreCreated?: (store: unknown) => void;
58
+ /**
59
+ * Store 销毁时的钩子
60
+ * @param store - Store 实例
61
+ */
62
+ onStoreDisposed?: (store: unknown) => void;
63
+ /**
64
+ * 状态变化时的钩子
65
+ * @param storeId - Store ID
66
+ * @param state - 新状态
67
+ * @param oldState - 旧状态
68
+ */
69
+ onStateChange?: <S extends StateTree>(storeId: StoreId, state: S, oldState: S) => void;
70
+ }
71
+ /**
72
+ * 插件工厂函数类型
73
+ * @template Options - 插件选项类型
74
+ */
75
+ export type PluginFactory<Options = Record<string, unknown>> = (options?: Options) => StorePlugin<Options>;
76
+ /**
77
+ * 定义插件选项
78
+ * @template Options - 插件选项类型
79
+ */
80
+ export interface DefinePluginOptions<Options = Record<string, unknown>> {
81
+ /** 插件名称 */
82
+ name: string;
83
+ /** 插件版本 */
84
+ version?: string;
85
+ /** 插件描述 */
86
+ description?: string;
87
+ /** 安装函数 */
88
+ install: StorePlugin<Options>['install'];
89
+ /** 卸载函数 */
90
+ uninstall?: StorePlugin<Options>['uninstall'];
91
+ /** Store 创建钩子 */
92
+ onStoreCreated?: StorePlugin<Options>['onStoreCreated'];
93
+ /** Store 销毁钩子 */
94
+ onStoreDisposed?: StorePlugin<Options>['onStoreDisposed'];
95
+ /** 状态变化钩子 */
96
+ onStateChange?: StorePlugin<Options>['onStateChange'];
97
+ }
98
+ /**
99
+ * 插件管理器接口
100
+ */
101
+ export interface PluginManager {
102
+ /**
103
+ * 注册插件
104
+ * @param plugin - 插件实例
105
+ * @param options - 插件选项
106
+ */
107
+ use: <Options>(plugin: StorePlugin<Options>, options?: Options) => void;
108
+ /**
109
+ * 移除插件
110
+ * @param name - 插件名称
111
+ */
112
+ remove: (name: string) => void;
113
+ /**
114
+ * 获取插件
115
+ * @param name - 插件名称
116
+ */
117
+ get: (name: string) => StorePlugin | undefined;
118
+ /**
119
+ * 是否已注册插件
120
+ * @param name - 插件名称
121
+ */
122
+ has: (name: string) => boolean;
123
+ /**
124
+ * 获取所有插件
125
+ */
126
+ getAll: () => Map<string, StorePlugin>;
127
+ /**
128
+ * 获取插件数量
129
+ */
130
+ size: () => number;
131
+ /**
132
+ * 清空所有插件
133
+ */
134
+ clear: () => void;
135
+ }
136
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1,198 @@
1
+ /**
2
+ * Store 核心类型定义
3
+ *
4
+ * @module types/store
5
+ */
6
+ /**
7
+ * 状态对象类型
8
+ */
9
+ export type StateTree = Record<string | number | symbol, unknown>;
10
+ /**
11
+ * Store ID 类型
12
+ */
13
+ export type StoreId = string;
14
+ /**
15
+ * Getter 函数类型
16
+ * @template S - 状态类型
17
+ * @template R - 返回值类型
18
+ */
19
+ export type StoreGetter<S extends StateTree, R> = (state: S) => R;
20
+ /**
21
+ * Getter 集合类型
22
+ * @template S - 状态类型
23
+ */
24
+ export type StoreGetters<S extends StateTree> = Record<string, StoreGetter<S, unknown>>;
25
+ /**
26
+ * Action 函数类型
27
+ *
28
+ * 允许 action 通过 `this` 访问当前 Store 实例;未使用 `this` 的普通函数依旧兼容。
29
+ */
30
+ export type StoreAction = (...args: unknown[]) => unknown;
31
+ /**
32
+ * Action 集合类型
33
+ */
34
+ export type StoreActions = Record<string, StoreAction>;
35
+ /**
36
+ * Store 定义选项
37
+ * @template Id - Store ID 类型
38
+ * @template S - 状态类型
39
+ * @template G - Getters 类型
40
+ * @template A - Actions 类型
41
+ */
42
+ export interface StoreDefinition<Id extends StoreId = StoreId, S extends StateTree = StateTree, G extends StoreGetters<S> = StoreGetters<S>, A extends StoreActions = StoreActions> {
43
+ /** Store 唯一标识 */
44
+ id: Id;
45
+ /** 初始状态工厂函数 */
46
+ state: () => S;
47
+ /** Getters 定义 */
48
+ getters?: G;
49
+ /** Actions 定义 */
50
+ actions?: A;
51
+ }
52
+ /**
53
+ * Store 实例接口
54
+ * @template Id - Store ID 类型
55
+ * @template S - 状态类型
56
+ * @template G - Getters 类型
57
+ * @template A - Actions 类型
58
+ */
59
+ export interface StoreInstance<Id extends StoreId = StoreId, S extends StateTree = StateTree, G extends StoreGetters<S> = StoreGetters<S>, A extends StoreActions = StoreActions> {
60
+ /** Store 唯一标识 */
61
+ $id: Id;
62
+ /** 当前状态 */
63
+ $state: S;
64
+ /** Getter 计算结果 */
65
+ getters: {
66
+ [K in keyof G]: ReturnType<G[K]>;
67
+ };
68
+ /** Action 方法集合 */
69
+ actions: A;
70
+ /**
71
+ * 重置状态到初始值
72
+ */
73
+ $reset: () => void;
74
+ /**
75
+ * 批量更新状态
76
+ * @param partialState - 部分状态或更新函数
77
+ */
78
+ $patch: (partialState: Partial<S> | ((state: S) => void)) => void;
79
+ /**
80
+ * 订阅状态变化
81
+ * @param callback - 回调函数
82
+ * @param options - 订阅选项
83
+ * @returns 取消订阅函数
84
+ */
85
+ $subscribe: (callback: StateSubscriber<S>, options?: SubscribeOptions) => () => void;
86
+ /**
87
+ * 订阅 action 调用
88
+ * @param callback - 回调函数
89
+ * @returns 取消订阅函数
90
+ */
91
+ $onAction: (callback: ActionSubscriber<A>) => () => void;
92
+ /**
93
+ * 销毁 store
94
+ */
95
+ $dispose: () => void;
96
+ }
97
+ /**
98
+ * 状态变化订阅器
99
+ * @template S - 状态类型
100
+ */
101
+ export type StateSubscriber<S extends StateTree> = (mutation: StateMutation<S>, state: S) => void;
102
+ /**
103
+ * 状态变化信息
104
+ * @template S - 状态类型
105
+ */
106
+ export interface StateMutation<S extends StateTree> {
107
+ /** 变化类型 */
108
+ type: 'direct' | 'patch object' | 'patch function';
109
+ /** Store ID */
110
+ storeId: StoreId;
111
+ /** 变化的键路径 */
112
+ events?: string[];
113
+ /** 变化前的状态快照 */
114
+ oldState?: Partial<S>;
115
+ }
116
+ /**
117
+ * 订阅选项
118
+ */
119
+ export interface SubscribeOptions {
120
+ /** 是否立即执行一次 */
121
+ immediate?: boolean;
122
+ /** 是否深度监听 */
123
+ deep?: boolean;
124
+ /** 是否在组件卸载后继续监听 */
125
+ detached?: boolean;
126
+ }
127
+ /**
128
+ * Action 订阅器
129
+ * @template A - Actions 类型
130
+ */
131
+ export type ActionSubscriber<A extends StoreActions> = (context: ActionContext<A>) => void;
132
+ /**
133
+ * Action 上下文
134
+ * @template A - Actions 类型
135
+ */
136
+ export interface ActionContext<A extends StoreActions> {
137
+ /** Action 名称 */
138
+ name: keyof A;
139
+ /** Store ID */
140
+ storeId: StoreId;
141
+ /** 调用参数 */
142
+ args: unknown[];
143
+ /** 执行后回调,异步 action 会在 Promise resolve 后触发 */
144
+ after: (callback: (result: unknown) => void | Promise<void>) => void;
145
+ /** 错误回调,异步 action 会在 Promise reject 后触发 */
146
+ onError: (callback: (error: unknown) => void | Promise<void>) => void;
147
+ }
148
+ /**
149
+ * Store 配置选项
150
+ */
151
+ export interface StoreOptions {
152
+ /** 是否启用严格模式(只能通过 action 修改状态) */
153
+ strict?: boolean;
154
+ /** 是否启用开发工具 */
155
+ devtools?: boolean;
156
+ /** 是否启用持久化 */
157
+ persist?: boolean;
158
+ /** 持久化配置 */
159
+ persistOptions?: PersistOptions;
160
+ }
161
+ /**
162
+ * 持久化选项
163
+ */
164
+ export interface PersistOptions {
165
+ /** 存储键名 */
166
+ key?: string;
167
+ /** 存储类型 */
168
+ storage?: 'localStorage' | 'sessionStorage' | 'custom';
169
+ /** 自定义存储适配器 */
170
+ customStorage?: StorageAdapter;
171
+ /** 要持久化的路径 */
172
+ paths?: string[];
173
+ /** 序列化函数 */
174
+ serializer?: {
175
+ serialize: (value: unknown) => string;
176
+ deserialize: (value: string) => unknown;
177
+ };
178
+ /** 恢复前回调 */
179
+ beforeRestore?: (context: {
180
+ store: unknown;
181
+ }) => void;
182
+ /** 恢复后回调 */
183
+ afterRestore?: (context: {
184
+ store: unknown;
185
+ }) => void;
186
+ }
187
+ /**
188
+ * 存储适配器接口
189
+ */
190
+ export interface StorageAdapter {
191
+ /** 获取数据 */
192
+ getItem: (key: string) => string | null | Promise<string | null>;
193
+ /** 设置数据 */
194
+ setItem: (key: string, value: string) => void | Promise<void>;
195
+ /** 删除数据 */
196
+ removeItem: (key: string) => void | Promise<void>;
197
+ }
198
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1,158 @@
1
+ /**
2
+ * 订阅系统类型定义
3
+ *
4
+ * @module types/subscription
5
+ */
6
+ import type { StateTree, StoreId } from './store';
7
+ /**
8
+ * 订阅回调类型
9
+ * @template T - 数据类型
10
+ */
11
+ export type Subscriber<T = unknown> = (value: T, oldValue?: T) => void;
12
+ /**
13
+ * 取消订阅函数类型
14
+ */
15
+ export type Unsubscribe = () => void;
16
+ /**
17
+ * 订阅选项
18
+ */
19
+ export interface SubscriptionOptions {
20
+ /** 是否立即执行 */
21
+ immediate?: boolean;
22
+ /** 是否在 store 销毁后保持订阅 */
23
+ detached?: boolean;
24
+ }
25
+ /**
26
+ * 发布订阅管理器接口
27
+ */
28
+ export interface PubSub<T = unknown> {
29
+ /**
30
+ * 订阅
31
+ * @param callback - 回调函数
32
+ * @param options - 订阅选项
33
+ * @returns 取消订阅函数
34
+ */
35
+ subscribe: (callback: Subscriber<T>, options?: SubscriptionOptions) => Unsubscribe;
36
+ /**
37
+ * 发布
38
+ * @param value - 新值
39
+ * @param oldValue - 旧值
40
+ */
41
+ publish: (value: T, oldValue?: T) => void;
42
+ /**
43
+ * 清空所有订阅
44
+ */
45
+ clear: () => void;
46
+ /**
47
+ * 获取订阅者数量
48
+ */
49
+ size: () => number;
50
+ }
51
+ /**
52
+ * 事件类型
53
+ */
54
+ export type EventType = 'store:created' | 'store:disposed' | 'state:changed' | 'action:called' | 'action:completed' | 'action:error' | 'plugin:installed' | 'plugin:uninstalled';
55
+ /**
56
+ * 事件数据
57
+ */
58
+ export interface EventData {
59
+ /** 事件类型 */
60
+ type: EventType;
61
+ /** Store ID */
62
+ storeId?: StoreId;
63
+ /** 事件数据 */
64
+ payload?: unknown;
65
+ /** 时间戳 */
66
+ timestamp: number;
67
+ }
68
+ /**
69
+ * 事件监听器类型
70
+ */
71
+ export type EventListener = (data: EventData) => void;
72
+ /**
73
+ * 事件总线接口
74
+ */
75
+ export interface EventBus {
76
+ /**
77
+ * 监听事件
78
+ * @param type - 事件类型
79
+ * @param listener - 监听器
80
+ * @returns 取消监听函数
81
+ */
82
+ on: (type: EventType | '*', listener: EventListener) => Unsubscribe;
83
+ /**
84
+ * 监听一次
85
+ * @param type - 事件类型
86
+ * @param listener - 监听器
87
+ * @returns 取消监听函数
88
+ */
89
+ once: (type: EventType, listener: EventListener) => Unsubscribe;
90
+ /**
91
+ * 取消监听
92
+ * @param type - 事件类型
93
+ * @param listener - 监听器
94
+ */
95
+ off: (type: EventType | '*', listener?: EventListener) => void;
96
+ /**
97
+ * 发送事件
98
+ * @param type - 事件类型
99
+ * @param payload - 事件数据
100
+ */
101
+ emit: (type: EventType, payload?: unknown) => void;
102
+ /**
103
+ * 清空所有监听器
104
+ */
105
+ clear: () => void;
106
+ }
107
+ /**
108
+ * 选择器函数类型
109
+ * @template S - 状态类型
110
+ * @template R - 返回值类型
111
+ */
112
+ export type Selector<S extends StateTree, R> = (state: S) => R;
113
+ /**
114
+ * 选择器订阅选项
115
+ */
116
+ export interface SelectorOptions<R> {
117
+ /** 是否立即执行 */
118
+ immediate?: boolean;
119
+ /** 相等性比较函数 */
120
+ equalityFn?: (a: R, b: R) => boolean;
121
+ }
122
+ /**
123
+ * 批量更新上下文
124
+ */
125
+ export interface BatchContext {
126
+ /** 是否正在批量更新 */
127
+ isBatching: boolean;
128
+ /** 待更新的 Store ID 列表 */
129
+ pendingStores: Set<StoreId>;
130
+ }
131
+ /**
132
+ * 批量更新管理器接口
133
+ */
134
+ export interface BatchManager {
135
+ /**
136
+ * 开始批量更新
137
+ */
138
+ start: () => void;
139
+ /**
140
+ * 结束批量更新并通知所有订阅者
141
+ */
142
+ end: () => void;
143
+ /**
144
+ * 标记 Store 需要更新
145
+ * @param storeId - Store ID
146
+ */
147
+ markDirty: (storeId: StoreId) => void;
148
+ /**
149
+ * 在批量上下文中执行
150
+ * @param fn - 执行函数
151
+ */
152
+ batch: <T>(fn: () => T) => T;
153
+ /**
154
+ * 是否正在批量更新
155
+ */
156
+ isBatching: () => boolean;
157
+ }
158
+ //# sourceMappingURL=subscription.d.ts.map