alemonjs 2.0.2 → 2.0.4

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 (58) hide show
  1. package/bin/start.js +12 -12
  2. package/lib/app/define-bot.d.ts +15 -0
  3. package/lib/app/{event-bot.js → define-bot.js} +1 -1
  4. package/lib/app/define-chidren.d.ts +10 -0
  5. package/lib/app/{event-chidren.js → define-chidren.js} +1 -1
  6. package/lib/app/event-middleware.d.ts +9 -10
  7. package/lib/app/event-middleware.js +8 -8
  8. package/lib/app/event-processor-event.js +81 -122
  9. package/lib/app/event-processor-middleware.js +100 -112
  10. package/lib/app/event-processor-subscribe.js +6 -8
  11. package/lib/app/event-processor.d.ts +3 -4
  12. package/lib/app/event-response.d.ts +16 -0
  13. package/lib/app/{event-utlis.js → event-response.js} +6 -7
  14. package/lib/app/hook-use-api.d.ts +9 -16
  15. package/lib/app/hook-use-api.js +11 -37
  16. package/lib/app/hook-use-state.d.ts +2 -2
  17. package/lib/app/hook-use-state.js +11 -35
  18. package/lib/app/hook-use-subscribe.d.ts +3 -4
  19. package/lib/app/hook-use-subscribe.js +3 -5
  20. package/lib/app/load.d.ts +22 -0
  21. package/lib/app/load.js +74 -59
  22. package/lib/app/{api.d.ts → message-api.d.ts} +0 -1
  23. package/lib/app/message-format.d.ts +36 -0
  24. package/lib/app/store.d.ts +90 -0
  25. package/lib/app/store.js +272 -0
  26. package/lib/app/utils.d.ts +3 -3
  27. package/lib/app/utils.js +13 -12
  28. package/lib/config.d.ts +0 -1
  29. package/lib/config.js +1 -1
  30. package/lib/datastructure/SinglyLinkedList.d.ts +3 -3
  31. package/lib/datastructure/SinglyLinkedList.js +2 -2
  32. package/lib/global.d.ts +44 -31
  33. package/lib/global.js +6 -46
  34. package/lib/index.d.ts +19 -30
  35. package/lib/index.js +12 -87
  36. package/lib/jsx.d.ts +10 -12
  37. package/lib/jsx.js +9 -10
  38. package/lib/main.d.ts +14 -0
  39. package/lib/main.js +79 -0
  40. package/lib/typing/{global → client}/index.d.ts +3 -6
  41. package/lib/typing/cycle/index.d.ts +2 -2
  42. package/lib/typing/event/actions.d.ts +37 -0
  43. package/lib/typing/event/actions.js +72 -0
  44. package/lib/typing/event/base/user.d.ts +12 -0
  45. package/lib/typing/event/index.d.ts +54 -30
  46. package/lib/typing/event/map.d.ts +6 -4
  47. package/lib/typing/logger/index.d.ts +3 -3
  48. package/lib/typing/message/index.d.ts +14 -14
  49. package/lib/typing/store/res.d.ts +20 -10
  50. package/lib/typing/subscribe/index.d.ts +11 -2
  51. package/package.json +1 -1
  52. package/lib/app/event-bot.d.ts +0 -16
  53. package/lib/app/event-chidren.d.ts +0 -11
  54. package/lib/app/event-utlis.d.ts +0 -18
  55. package/lib/app/hook-message-format.d.ts +0 -37
  56. package/lib/logger.js +0 -84
  57. /package/lib/app/{api.js → message-api.js} +0 -0
  58. /package/lib/app/{hook-message-format.js → message-format.js} +0 -0
@@ -1,6 +1,8 @@
1
1
  import { isAsyncFunction } from 'util/types';
2
2
  import { useState } from './hook-use-state.js';
3
3
  import { showErrorModule } from './utils.js';
4
+ import { Middleware } from './store.js';
5
+ import { useSend } from './hook-use-api.js';
4
6
 
5
7
  /**
6
8
  * @fileoverview 消息处理快
@@ -15,12 +17,12 @@ import { showErrorModule } from './utils.js';
15
17
  * @param select
16
18
  */
17
19
  const expendMiddleware = async (valueEvent, select, next) => {
18
- // 得到所有 apps
19
- const mwFiles = [...alemonjsCore.storeMiddleware];
20
- // 得到对应类型的消息
21
- const mws = [...alemonjsCore.storeMiddlewareGather[select]];
20
+ const mw = new Middleware();
21
+ const Send = useSend(valueEvent);
22
+ // 得到所有 mws
23
+ const mwFiles = mw.value;
22
24
  let valueI = 0;
23
- let valueJ = 0;
25
+ // let valueJ = 0
24
26
  /**
25
27
  * 下一步
26
28
  * @returns
@@ -30,19 +32,13 @@ const expendMiddleware = async (valueEvent, select, next) => {
30
32
  next(...cns);
31
33
  return;
32
34
  }
33
- // i 结束了
35
+ // 结束了
34
36
  if (valueI >= mwFiles.length) {
35
- // j 结束了
36
- if (valueJ >= mws.length) {
37
- next();
38
- return;
39
- }
40
- // 走 j,检查所有分毫类型的
41
- await callj();
37
+ next();
42
38
  return;
43
39
  }
44
- // 走 i,检查所有 apps
45
- await calli();
40
+ // 检查所有
41
+ calli();
46
42
  };
47
43
  /**
48
44
  * 执行 i
@@ -52,37 +48,47 @@ const expendMiddleware = async (valueEvent, select, next) => {
52
48
  // 调用完了
53
49
  if (valueI >= mwFiles.length) {
54
50
  // 开始调用j
55
- await nextMiddleware();
51
+ nextMiddleware();
56
52
  return;
57
53
  }
58
54
  valueI++;
59
55
  const file = mwFiles[valueI - 1];
60
56
  if (!file?.path) {
61
57
  // 继续
62
- await nextMiddleware();
58
+ nextMiddleware();
63
59
  return;
64
60
  }
65
61
  try {
66
62
  const app = await import(`file://${file.path}`);
67
63
  if (!app?.default) {
68
64
  // 继续
69
- await nextMiddleware();
65
+ nextMiddleware();
70
66
  return;
71
67
  }
72
68
  if (!app.default.current) {
73
69
  // 继续
74
- await nextMiddleware();
70
+ nextMiddleware();
75
71
  return;
76
72
  }
77
73
  // 检查状态
78
- if (file?.state) {
79
- const [state] = useState(file?.state);
74
+ if (file?.stateKey) {
75
+ const [state] = useState(file?.stateKey);
80
76
  if (state == false) {
81
77
  // 继续
82
- await nextMiddleware();
78
+ nextMiddleware();
83
79
  return;
84
80
  }
85
81
  }
82
+ if (['message.create', 'private.message.create'].includes(select)) {
83
+ if (app?.regular) {
84
+ const reg = new RegExp(app.regular);
85
+ if (!reg.test(valueEvent['MessageText'])) {
86
+ // 继续
87
+ nextMiddleware();
88
+ return;
89
+ }
90
+ }
91
+ }
86
92
  // 是数组
87
93
  if (Array.isArray(app.default?.select)) {
88
94
  // 不包含
@@ -100,98 +106,40 @@ const expendMiddleware = async (valueEvent, select, next) => {
100
106
  nextMiddleware();
101
107
  return;
102
108
  }
103
- // 不是数组,进行分类存储
104
- // 判断是否已经存储
105
- if (!alemonjsCore.storeMiddlewareGather[select].find(v => v.path === file.path)) {
106
- // 索引
107
- const index = alemonjsCore.storeMiddleware.findIndex(v => v.path === file.path);
108
- // 去除
109
- alemonjsCore.storeMiddleware.splice(index, 1);
110
- // 存储
111
- alemonjsCore.storeMiddlewareGather[select].push({
112
- source: file.source,
113
- dir: file.dir,
114
- path: file.path,
115
- name: file.name,
116
- node: file.node,
117
- value: {
118
- select: app.default?.select ?? select
119
- }
120
- });
121
- }
122
109
  }
123
110
  if (Array.isArray(app.default.current)) {
124
111
  let i = 0;
125
112
  let T = true;
126
- const start = async () => {
127
- if (i >= app.default.current.length)
128
- return;
129
- // 不是真的
130
- if (!T)
113
+ let isNext = false;
114
+ /**
115
+ *
116
+ * @param res
117
+ * @returns
118
+ */
119
+ const onRes = (res) => {
120
+ if (isNext) {
121
+ // 被调用了next
131
122
  return;
132
- if (isAsyncFunction(app.default.current[i])) {
133
- T = await app.default.current[i](valueEvent, nextMiddleware);
134
123
  }
135
- else {
136
- T = await app.default.current[i](valueEvent, nextMiddleware);
124
+ if (typeof res === 'boolean') {
125
+ T = res;
126
+ }
127
+ else if (Array.isArray(res)) {
128
+ if (res.length > 0) {
129
+ // 发送数据
130
+ Send(...res);
131
+ }
132
+ }
133
+ else if (typeof res === 'object') {
134
+ if (typeof res?.allowGrouping === 'boolean') {
135
+ T = res.allowGrouping;
136
+ }
137
+ if (Array.isArray(res.data)) {
138
+ // 发送数据
139
+ Send(...res.data);
140
+ }
137
141
  }
138
- ++i;
139
- await start();
140
142
  };
141
- await start();
142
- }
143
- else {
144
- // 这里是否继续时 next 说了算
145
- if (isAsyncFunction(app.default?.current)) {
146
- app.default?.current(valueEvent, nextMiddleware);
147
- }
148
- else {
149
- app.default?.current(valueEvent, nextMiddleware);
150
- }
151
- }
152
- }
153
- catch (err) {
154
- showErrorModule(err);
155
- }
156
- };
157
- /**
158
- * 被分类好的
159
- * @returns
160
- */
161
- const callj = async () => {
162
- // 调用完了
163
- if (valueJ >= mws.length) {
164
- return;
165
- }
166
- valueJ++;
167
- const file = mws[valueJ - 1];
168
- if (!file?.path) {
169
- await nextMiddleware();
170
- return;
171
- }
172
- try {
173
- const app = await import(`file://${file.path}`);
174
- if (!app?.default) {
175
- // 继续
176
- await nextMiddleware();
177
- return;
178
- }
179
- if (!app.default.current) {
180
- // 继续
181
- await nextMiddleware();
182
- return;
183
- }
184
- if (file?.state) {
185
- const [state] = useState(file?.state);
186
- if (state == false) {
187
- // 继续
188
- await nextMiddleware();
189
- return;
190
- }
191
- }
192
- if (Array.isArray(app.default.current)) {
193
- let i = 0;
194
- let T = true;
195
143
  const start = async () => {
196
144
  if (i >= app.default.current.length)
197
145
  return;
@@ -199,10 +147,18 @@ const expendMiddleware = async (valueEvent, select, next) => {
199
147
  if (!T)
200
148
  return;
201
149
  if (isAsyncFunction(app.default.current[i])) {
202
- T = await app.default.current[i](valueEvent, nextMiddleware);
150
+ const res = await app.default.current[i](valueEvent, (...cns) => {
151
+ isNext = true;
152
+ nextMiddleware(...cns);
153
+ });
154
+ onRes(res);
203
155
  }
204
156
  else {
205
- T = await app.default.current[i](valueEvent, nextMiddleware);
157
+ const res = app.default.current[i](valueEvent, (...cns) => {
158
+ isNext = true;
159
+ nextMiddleware(...cns);
160
+ });
161
+ onRes(res);
206
162
  }
207
163
  ++i;
208
164
  await start();
@@ -210,22 +166,54 @@ const expendMiddleware = async (valueEvent, select, next) => {
210
166
  await start();
211
167
  }
212
168
  else {
169
+ let isNext = false;
170
+ const onRes = (res) => {
171
+ if (isNext) {
172
+ // 被调用了next
173
+ return;
174
+ }
175
+ if (typeof res === 'boolean') {
176
+ // T = res
177
+ }
178
+ else if (Array.isArray(res)) {
179
+ if (res.length > 0) {
180
+ // 发送数据
181
+ Send(...res);
182
+ }
183
+ }
184
+ else if (typeof res === 'object') {
185
+ if (typeof res?.allowGrouping === 'boolean') {
186
+ // T = res.allowGrouping
187
+ }
188
+ if (Array.isArray(res.data)) {
189
+ // 发送数据
190
+ Send(...res.data);
191
+ }
192
+ }
193
+ };
213
194
  // 这里是否继续时 next 说了算
214
195
  if (isAsyncFunction(app.default?.current)) {
215
- app.default?.current(valueEvent, nextMiddleware);
196
+ const res = await app.default?.current(valueEvent, (...cns) => {
197
+ isNext = true;
198
+ nextMiddleware(...cns);
199
+ });
200
+ onRes(res);
216
201
  }
217
202
  else {
218
- app.default?.current(valueEvent, nextMiddleware);
203
+ const res = app.default?.current(valueEvent, (...cns) => {
204
+ isNext = true;
205
+ nextMiddleware(...cns);
206
+ });
207
+ onRes(res);
219
208
  }
220
209
  }
221
210
  }
222
211
  catch (err) {
223
212
  showErrorModule(err);
224
213
  }
225
- //
226
214
  };
227
215
  // 开始修正模式
228
- await nextMiddleware();
216
+ nextMiddleware();
229
217
  };
230
218
 
231
219
  export { expendMiddleware };
@@ -1,4 +1,4 @@
1
- import { SinglyLinkedList } from '../datastructure/SinglyLinkedList.js';
1
+ import { SubscribeList } from './store.js';
2
2
 
3
3
  /**
4
4
  * 处理订阅
@@ -8,9 +8,7 @@ import { SinglyLinkedList } from '../datastructure/SinglyLinkedList.js';
8
8
  * @param chioce
9
9
  */
10
10
  const expendSubscribe = async (valueEvent, select, next, chioce) => {
11
- if (!alemonjsCore.storeSubscribeList[chioce][select]) {
12
- alemonjsCore.storeSubscribeList[chioce][select] = new SinglyLinkedList();
13
- }
11
+ const subList = new SubscribeList(chioce, select);
14
12
  /**
15
13
  * 观察者下一步
16
14
  * @returns
@@ -20,7 +18,7 @@ const expendSubscribe = async (valueEvent, select, next, chioce) => {
20
18
  next(...cns);
21
19
  return;
22
20
  }
23
- const item = alemonjsCore.storeSubscribeList[chioce][select].popNext(); // 弹出下一个节点
21
+ const item = subList.value.popNext(); // 弹出下一个节点
24
22
  // 可能是 undefined
25
23
  if (!item || !item.data.current) {
26
24
  // 继续 next
@@ -36,16 +34,16 @@ const expendSubscribe = async (valueEvent, select, next, chioce) => {
36
34
  }
37
35
  }
38
36
  // 订阅是执行则销毁
39
- alemonjsCore.storeSubscribeList[chioce][select].removeCurrent(); // 移除当前节点
37
+ subList.value.removeCurrent(); // 移除当前节点
40
38
  const Continue = (cn, ...cns) => {
41
39
  // next() 订阅继续
42
- alemonjsCore.storeSubscribeList[chioce][select].append(item.data); // 重新连接
40
+ subList.value.append(item.data); // 重新连接
43
41
  if (cn) {
44
42
  nextObserver(...cns);
45
43
  return;
46
44
  }
47
45
  if (typeof cn === 'boolean') {
48
- alemonjsCore.storeSubscribeList[chioce][select].removeCurrent(); // 移除当前节点
46
+ subList.value.removeCurrent(); // 移除当前节点
49
47
  nextObserver(...cns);
50
48
  return;
51
49
  }
@@ -1,5 +1,4 @@
1
- import '../global.js';
2
- import { Events } from '../typing/event/map.js';
1
+ import { EventKeys, Events } from '../typing/event/map.js';
3
2
 
4
3
  /**
5
4
  * 消息处理器
@@ -8,7 +7,7 @@ import { Events } from '../typing/event/map.js';
8
7
  * @param data
9
8
  * @returns
10
9
  */
11
- declare const onProcessor: <T extends keyof Events>(name: T, event: Events[T], data?: any) => void;
10
+ declare const onProcessor: <T extends EventKeys>(name: T, event: Events[T], data?: any) => void;
12
11
  /**
13
12
  * 消息处理器
14
13
  * @param event
@@ -16,6 +15,6 @@ declare const onProcessor: <T extends keyof Events>(name: T, event: Events[T], d
16
15
  * @deprecated 该方法已被弃用,请使用 onProcessor() 替代。
17
16
  * @returns
18
17
  */
19
- declare const OnProcessor: <T extends keyof Events>(event: Events[T], name: T) => void;
18
+ declare const OnProcessor: <T extends EventKeys>(event: Events[T], name: T) => void;
20
19
 
21
20
  export { OnProcessor, onProcessor };
@@ -0,0 +1,16 @@
1
+ import { OnResponseReversalFunc, OnResponseFunc } from '../typing/event/index.js';
2
+
3
+ /**
4
+ * 处理响应事件
5
+ * @param select 事件选择
6
+ * @param callback 回调函数,处理事件和 API
7
+ * @returns 回调函数的执行结果
8
+ */
9
+ declare const onResponse: OnResponseReversalFunc;
10
+ /**
11
+ * 废弃,请使用 onResponse
12
+ * @deprecated
13
+ */
14
+ declare const OnResponse: OnResponseFunc;
15
+
16
+ export { OnResponse, onResponse };
@@ -1,21 +1,20 @@
1
1
  /**
2
2
  * 处理响应事件
3
+ * @param select 事件选择
3
4
  * @param callback 回调函数,处理事件和 API
4
- * @param event 事件类型
5
- * @param reg
6
5
  * @returns 回调函数的执行结果
7
6
  */
8
- const OnResponse = (callback, select) => {
7
+ const onResponse = (select, callback) => {
9
8
  return { current: callback, select };
10
9
  };
11
- global.OnResponse = OnResponse;
10
+ global.onResponse = onResponse;
12
11
  /**
13
- * 废弃
12
+ * 废弃,请使用 onResponse
14
13
  * @deprecated
15
14
  */
16
- const onResponse = (select, callback) => {
15
+ const OnResponse = (callback, select) => {
17
16
  return { current: callback, select };
18
17
  };
19
- global.onResponse = onResponse;
18
+ global.OnResponse = OnResponse;
20
19
 
21
20
  export { OnResponse, onResponse };
@@ -1,42 +1,35 @@
1
- import '../global.js';
2
- import { Events } from '../typing/event/map.js';
1
+ import { EventKeys, Events } from '../typing/event/map.js';
3
2
  import { User } from '../typing/event/base/user.js';
4
3
  import { DataEnums } from '../typing/message/index.js';
4
+ import '../global.js';
5
5
 
6
6
  /**
7
7
  * 使用提及。
8
8
  * @param {Object} event - 事件对象,包含触发提及的相关信息。
9
- * @returns {any} - 返回提及操作的结果。
10
9
  */
11
- declare const useMention: (event: {
12
- [key: string]: any;
13
- }) => Promise<User[]>;
10
+ declare const useMention: <T extends EventKeys>(event: Events[T]) => Promise<User[]>;
14
11
  /**
15
12
  * 使用发送消息。
16
13
  * @param {Object} event - 事件对象,包含触发发送的相关信息。
17
14
  * @returns {Function} - 返回一个异步函数,用于发送消息。
18
15
  * @throws {Error} - 如果 event 无效,抛出错误。
19
16
  */
20
- declare const useSend: (event: {
21
- [key: string]: any;
22
- }) => (...val: DataEnums[]) => Promise<any[]>;
17
+ declare const useSend: <T extends EventKeys>(event: Events[T]) => (...val: DataEnums[]) => Promise<any[]>;
23
18
  /**
24
- * 卸载模块。
25
- * 从全局存储中移除与指定模块相关的数据。
26
- * @param {string} mainDir - 模块的主目录路径。
27
- * @throws {Error} - 如果 mainDir 无效,抛出错误。
19
+ * 卸载模块
20
+ * @param name
28
21
  */
29
- declare const unChildren: (mainDir: string) => void;
22
+ declare const unChildren: (name?: string) => void;
30
23
  /**
31
24
  * 废弃,请使用unChildren
32
25
  * @deprecated
33
26
  */
34
- declare const unMount: (mainDir: string) => void;
27
+ declare const unMount: () => void;
35
28
  /**
36
29
  *
37
30
  * @param values
38
31
  * @returns
39
32
  */
40
- declare const createSelects: <T extends (keyof Events)[] | keyof Events>(values: T) => T;
33
+ declare const createSelects: <T extends EventKeys[] | EventKeys>(values: T) => T;
41
34
 
42
35
  export { createSelects, unChildren, unMount, useMention, useSend };
@@ -1,7 +1,8 @@
1
+ import { ChildrenApp } from './store.js';
2
+
1
3
  /**
2
4
  * 使用提及。
3
5
  * @param {Object} event - 事件对象,包含触发提及的相关信息。
4
- * @returns {any} - 返回提及操作的结果。
5
6
  */
6
7
  const useMention = async (event) => {
7
8
  if (!event || typeof event !== 'object') {
@@ -42,48 +43,21 @@ const useSend = (event) => {
42
43
  };
43
44
  };
44
45
  /**
45
- * 卸载模块。
46
- * 从全局存储中移除与指定模块相关的数据。
47
- * @param {string} mainDir - 模块的主目录路径。
48
- * @throws {Error} - 如果 mainDir 无效,抛出错误。
46
+ * 卸载模块
47
+ * @param name
49
48
  */
50
- const unChildren = (mainDir) => {
51
- if (!mainDir || typeof mainDir !== 'string') {
52
- throw new Error('Invalid mainDir: mainDir must be a non-empty string');
53
- }
54
- // 从 storeMains 中移除
55
- const storeMainsIndex = alemonjsCore.storeMains.indexOf(mainDir);
56
- if (storeMainsIndex !== -1) {
57
- alemonjsCore.storeMains.splice(storeMainsIndex, 1);
58
- }
59
- // 从 storeResponse 中移除
60
- if (alemonjsCore.storeResponse.find(item => item.source == mainDir)) {
61
- alemonjsCore.storeResponse = alemonjsCore.storeResponse.filter(item => item.source !== mainDir);
62
- }
63
- // 从 storeMiddleware 中移除
64
- if (alemonjsCore.storeMiddleware.find(item => item.source == mainDir)) {
65
- alemonjsCore.storeMiddleware = alemonjsCore.storeMiddleware.filter(item => item.source !== mainDir);
66
- }
67
- // 从 storeResponseGather 中移除
68
- for (const key in alemonjsCore.storeResponseGather) {
69
- if (Array.isArray(alemonjsCore.storeResponseGather[key]) &&
70
- alemonjsCore.storeResponseGather[key].find(item => item.source == mainDir)) {
71
- alemonjsCore.storeResponseGather[key] = alemonjsCore.storeResponseGather[key].filter(item => item.source !== mainDir);
72
- }
73
- }
74
- // 从 storeMiddlewareGather 中移除
75
- for (const key in alemonjsCore.storeMiddlewareGather) {
76
- if (Array.isArray(alemonjsCore.storeMiddlewareGather[key]) &&
77
- alemonjsCore.storeMiddlewareGather[key].find(item => item.source == mainDir)) {
78
- alemonjsCore.storeMiddlewareGather[key] = alemonjsCore.storeMiddlewareGather[key].filter(item => item.source !== mainDir);
79
- }
80
- }
49
+ const unChildren = (name = 'main') => {
50
+ const app = new ChildrenApp(name);
51
+ app.un();
81
52
  };
82
53
  /**
83
54
  * 废弃,请使用unChildren
84
55
  * @deprecated
85
56
  */
86
- const unMount = unChildren;
57
+ const unMount = () => {
58
+ // 警告,已废弃。
59
+ console.warn('unMount 已废弃,请使用 unChildren');
60
+ };
87
61
  /**
88
62
  *
89
63
  * @param values
@@ -18,7 +18,7 @@ declare const useState: <T extends string>(name: T, defaultValue?: boolean) => [
18
18
  */
19
19
  declare const onState: <T extends string>(name: T, callback: (value: boolean) => void) => void;
20
20
  /**
21
- * 废弃
21
+ * 废弃,请使用 onState
22
22
  * @deprecated
23
23
  */
24
24
  declare const eventState: <T extends string>(name: T, callback: (value: boolean) => void) => void;
@@ -29,7 +29,7 @@ declare const eventState: <T extends string>(name: T, callback: (value: boolean)
29
29
  */
30
30
  declare const unState: <T extends string>(name: T, callback: (value: boolean) => void) => void;
31
31
  /**
32
- * 废弃
32
+ * 废弃,请使用 unState
33
33
  * @deprecated
34
34
  */
35
35
  declare const unEventState: <T extends string>(name: T, callback: (value: boolean) => void) => void;
@@ -1,23 +1,5 @@
1
- // 存储订阅者
2
- const subscriptions = {};
3
- if (!alemonjsCore.storeState) {
4
- // 初始化全局状态
5
- alemonjsCore.storeState = new Proxy({}, {
6
- get(target, prop) {
7
- return prop in target ? target[prop] : false;
8
- },
9
- set(target, prop, value) {
10
- target[prop] = value;
11
- // 通知所有订阅者
12
- if (subscriptions[prop]) {
13
- for (const callback of subscriptions[prop]) {
14
- callback(value);
15
- }
16
- }
17
- return true; // 表示设置成功
18
- }
19
- });
20
- }
1
+ import { State, StateSubscribe } from './store.js';
2
+
21
3
  /**
22
4
  * 获取指定功能是启动还是关闭
23
5
  * ***
@@ -31,15 +13,12 @@ if (!alemonjsCore.storeState) {
31
13
  * @returns 当前状态和设置函数
32
14
  */
33
15
  const useState = (name, defaultValue = true) => {
34
- // 如果不存在则设置默认值
35
- if (!(name in alemonjsCore.storeState)) {
36
- alemonjsCore.storeState[name] = defaultValue;
37
- }
16
+ const state = new State(name, defaultValue);
38
17
  // 设置值的函数
39
18
  const setValue = (value) => {
40
- alemonjsCore.storeState[name] = value;
19
+ state.value = value;
41
20
  };
42
- return [alemonjsCore.storeState[name], setValue];
21
+ return [state.value, setValue];
43
22
  };
44
23
  /**
45
24
  * 订阅状态变化
@@ -50,13 +29,11 @@ const onState = (name, callback) => {
50
29
  if (typeof callback !== 'function') {
51
30
  throw new Error('Callback must be a function');
52
31
  }
53
- if (!subscriptions[name]) {
54
- subscriptions[name] = [];
55
- }
56
- subscriptions[name].push(callback);
32
+ const sub = new StateSubscribe(name);
33
+ sub.on(callback);
57
34
  };
58
35
  /**
59
- * 废弃
36
+ * 废弃,请使用 onState
60
37
  * @deprecated
61
38
  */
62
39
  const eventState = onState;
@@ -66,12 +43,11 @@ const eventState = onState;
66
43
  * @param callback 回调函数
67
44
  */
68
45
  const unState = (name, callback) => {
69
- if (subscriptions[name]) {
70
- subscriptions[name] = subscriptions[name].filter(cb => cb !== callback);
71
- }
46
+ const sub = new StateSubscribe(name);
47
+ sub.un(callback);
72
48
  };
73
49
  /**
74
- * 废弃
50
+ * 废弃,请使用 unState
75
51
  * @deprecated
76
52
  */
77
53
  const unEventState = unState;
@@ -1,5 +1,4 @@
1
- import '../global.js';
2
- import { Events } from '../typing/event/map.js';
1
+ import { EventKeys, Events } from '../typing/event/map.js';
3
2
  import { Current } from '../typing/event/index.js';
4
3
 
5
4
  /**
@@ -8,13 +7,13 @@ import { Current } from '../typing/event/index.js';
8
7
  * @param option
9
8
  * @returns
10
9
  */
11
- declare const useSubscribe: <T extends keyof Events>(event: any, select: T) => ((callback: Current<T>, keys: (keyof Events[T])[]) => void)[];
10
+ declare const useSubscribe: <T extends EventKeys>(event: Events[T], select: T) => ((callback: Current<T>, keys: (keyof Events[T])[]) => void)[];
12
11
  /**
13
12
  * 使用观察者
14
13
  * @param event
15
14
  * @param option
16
15
  * @returns
17
16
  */
18
- declare const useObserver: <T extends keyof Events>(event: any, option: T) => (callback: Current<T>, keys: (keyof Events[T])[]) => void;
17
+ declare const useObserver: <T extends EventKeys>(event: Events[T], option: T) => (callback: Current<T>, keys: (keyof Events[T])[]) => void;
19
18
 
20
19
  export { useObserver, useSubscribe };