alemonjs 2.0.0-rc.99 → 2.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.
- package/bin/alemonc.js +15 -18
- package/bin/run.js +7 -7
- package/bin/start.js +10 -10
- package/bin/updateConfig.js +1 -1
- package/lib/app/event-bot.d.ts +8 -3
- package/lib/app/event-bot.js +8 -2
- package/lib/app/event-middleware.d.ts +8 -3
- package/lib/app/event-middleware.js +12 -3
- package/lib/app/event-processor-cycle.js +2 -2
- package/lib/app/event-processor-event.js +8 -7
- package/lib/app/event-processor-middleware.js +8 -7
- package/lib/app/event-processor.js +1 -7
- package/lib/app/event-utlis.d.ts +7 -2
- package/lib/app/event-utlis.js +9 -1
- package/lib/app/hook-use-api.d.ts +13 -1
- package/lib/app/hook-use-api.js +15 -3
- package/lib/app/hook-use-state.d.ts +11 -1
- package/lib/app/hook-use-state.js +29 -17
- package/lib/app/load.js +54 -42
- package/lib/app/utils.d.ts +7 -2
- package/lib/app/utils.js +19 -9
- package/lib/global.d.ts +29 -2
- package/lib/index.d.ts +7 -7
- package/lib/index.js +13 -12
- package/lib/jsx.d.ts +93 -0
- package/lib/jsx.js +143 -0
- package/lib/typing/cycle/index.d.ts +6 -1
- package/lib/typing/event/index.d.ts +30 -1
- package/lib/typing/message/index.d.ts +1 -1
- package/lib/typing/store/res.d.ts +10 -0
- package/package.json +2 -1
package/bin/alemonc.js
CHANGED
|
@@ -2,68 +2,65 @@
|
|
|
2
2
|
import { updateConfig } from './updateConfig.js'
|
|
3
3
|
import { run } from './run.js'
|
|
4
4
|
import { start } from './start.js'
|
|
5
|
-
import { Command } from 'commander'
|
|
6
|
-
const program = new Command()
|
|
5
|
+
import { Command } from 'commander'
|
|
6
|
+
const program = new Command()
|
|
7
7
|
|
|
8
|
-
program
|
|
9
|
-
.name('alemonc')
|
|
10
|
-
.description('CLI to some alemonc actions and scripts')
|
|
11
|
-
.version('1.0.0');
|
|
8
|
+
program.name('alemonc').description('CLI to some alemonc actions and scripts').version('1.0.0')
|
|
12
9
|
|
|
13
10
|
program
|
|
14
11
|
.command('add <key> [values...]')
|
|
15
12
|
.description('给key为数据的值添加元素')
|
|
16
13
|
.action((key, values) => {
|
|
17
14
|
updateConfig('add', key, values)
|
|
18
|
-
})
|
|
15
|
+
})
|
|
19
16
|
|
|
20
17
|
program
|
|
21
18
|
.command('remove <key> [values...]')
|
|
22
19
|
.description('给key为数据的值移除元素')
|
|
23
20
|
.action((key, values) => {
|
|
24
21
|
updateConfig('remove', key, values)
|
|
25
|
-
})
|
|
22
|
+
})
|
|
26
23
|
|
|
27
24
|
program
|
|
28
25
|
.command('set <key> [values...]')
|
|
29
26
|
.description('给某个key设置值')
|
|
30
27
|
.action((key, values) => {
|
|
31
28
|
updateConfig('set', key, values)
|
|
32
|
-
})
|
|
29
|
+
})
|
|
33
30
|
|
|
34
31
|
program
|
|
35
32
|
.command('del <key>')
|
|
36
33
|
.description('删除指定配置')
|
|
37
|
-
.action(
|
|
34
|
+
.action(key => {
|
|
38
35
|
updateConfig('del', key)
|
|
39
|
-
})
|
|
36
|
+
})
|
|
40
37
|
|
|
41
38
|
program
|
|
42
39
|
.command('get <key>')
|
|
43
40
|
.description('获取指定配置')
|
|
44
|
-
.action(
|
|
41
|
+
.action(key => {
|
|
45
42
|
updateConfig('get', key)
|
|
46
|
-
})
|
|
43
|
+
})
|
|
47
44
|
|
|
48
45
|
program
|
|
49
46
|
.command('run [script]')
|
|
50
47
|
.description('运行指定脚本')
|
|
51
|
-
.action(
|
|
48
|
+
.action(script => {
|
|
52
49
|
run(script)
|
|
53
|
-
})
|
|
50
|
+
})
|
|
54
51
|
|
|
55
52
|
program
|
|
56
53
|
.command('start')
|
|
57
54
|
.description('启动 package.json 中的 main 入口')
|
|
58
55
|
.action(() => {
|
|
59
56
|
start()
|
|
60
|
-
})
|
|
57
|
+
})
|
|
61
58
|
|
|
62
59
|
program
|
|
63
60
|
.command('help')
|
|
64
61
|
.description('获取帮助')
|
|
65
62
|
.action(() => {
|
|
66
63
|
program.help()
|
|
67
|
-
})
|
|
64
|
+
})
|
|
68
65
|
|
|
69
|
-
program.parse(process.argv)
|
|
66
|
+
program.parse(process.argv)
|
package/bin/run.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
* @param {*} dir
|
|
4
|
+
*
|
|
5
|
+
* @param {*} dir
|
|
6
6
|
*/
|
|
7
|
-
export const run =
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
7
|
+
export const run = dir => {
|
|
8
|
+
import('../lib/index.js').then(res => {
|
|
9
|
+
res.start(dir)
|
|
10
|
+
})
|
|
11
|
+
}
|
package/bin/start.js
CHANGED
|
@@ -4,15 +4,15 @@ import { readFileSync } from 'fs'
|
|
|
4
4
|
import { join } from 'path'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
* @param {*} dir
|
|
7
|
+
*
|
|
8
|
+
* @param {*} dir
|
|
9
9
|
*/
|
|
10
10
|
export const start = () => {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
11
|
+
// 读取配置文件
|
|
12
|
+
const dir = join(process.cwd(), 'package.json')
|
|
13
|
+
const start = readFileSync(dir, 'utf-8')
|
|
14
|
+
const { main } = JSON.parse(start) ?? {}
|
|
15
|
+
import('../lib/index.js').then(res => {
|
|
16
|
+
res.start(main)
|
|
17
|
+
})
|
|
18
|
+
}
|
package/bin/updateConfig.js
CHANGED
package/lib/app/event-bot.d.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import '../global.js';
|
|
2
|
-
import {
|
|
2
|
+
import { DefinePlatform } from '../typing/event/index.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* 定义机器人
|
|
6
6
|
* @param callback
|
|
7
7
|
* @returns
|
|
8
8
|
*/
|
|
9
|
-
declare const
|
|
9
|
+
declare const definePlatform: DefinePlatform;
|
|
10
|
+
/**
|
|
11
|
+
* 废弃
|
|
12
|
+
* @deprecated
|
|
13
|
+
*/
|
|
14
|
+
declare const defineBot: DefinePlatform;
|
|
10
15
|
|
|
11
|
-
export { defineBot };
|
|
16
|
+
export { defineBot, definePlatform };
|
package/lib/app/event-bot.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @param callback
|
|
4
4
|
* @returns
|
|
5
5
|
*/
|
|
6
|
-
const
|
|
6
|
+
const definePlatform = callback => {
|
|
7
7
|
// 判断是否是函数
|
|
8
8
|
if (typeof callback !== 'function') {
|
|
9
9
|
throw new Error('Invalid callback: callback must be a function');
|
|
@@ -13,6 +13,12 @@ const defineBot = callback => {
|
|
|
13
13
|
callback
|
|
14
14
|
};
|
|
15
15
|
};
|
|
16
|
+
global.definePlatform = definePlatform;
|
|
17
|
+
/**
|
|
18
|
+
* 废弃
|
|
19
|
+
* @deprecated
|
|
20
|
+
*/
|
|
21
|
+
const defineBot = definePlatform;
|
|
16
22
|
global.defineBot = defineBot;
|
|
17
23
|
|
|
18
|
-
export { defineBot };
|
|
24
|
+
export { defineBot, definePlatform };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import '../global.js';
|
|
2
|
-
import { OnMiddlewareType } from '../typing/event/index.js';
|
|
2
|
+
import { OnMiddlewareType, OnMiddlewareReversalType } from '../typing/event/index.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @fileoverview 中间件
|
|
@@ -7,12 +7,17 @@ import { OnMiddlewareType } from '../typing/event/index.js';
|
|
|
7
7
|
* @author ningmengchongshui
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* 废弃
|
|
12
|
+
* @deprecated
|
|
13
|
+
*/
|
|
14
|
+
declare const OnMiddleware: OnMiddlewareType;
|
|
10
15
|
/**
|
|
11
16
|
* 中间件
|
|
12
17
|
* @param callback
|
|
13
18
|
* @param select
|
|
14
19
|
* @returns
|
|
15
20
|
*/
|
|
16
|
-
declare const
|
|
21
|
+
declare const onMiddleware: OnMiddlewareReversalType;
|
|
17
22
|
|
|
18
|
-
export { OnMiddleware };
|
|
23
|
+
export { OnMiddleware, onMiddleware };
|
|
@@ -1,10 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 废弃
|
|
3
|
+
* @deprecated
|
|
4
|
+
*/
|
|
5
|
+
const OnMiddleware = (callback, select) => ({ select, current: callback });
|
|
6
|
+
global.OnMiddleware = OnMiddleware;
|
|
1
7
|
/**
|
|
2
8
|
* 中间件
|
|
3
9
|
* @param callback
|
|
4
10
|
* @param select
|
|
5
11
|
* @returns
|
|
6
12
|
*/
|
|
7
|
-
const
|
|
8
|
-
|
|
13
|
+
const onMiddleware = (select, callback) => ({
|
|
14
|
+
select,
|
|
15
|
+
current: callback
|
|
16
|
+
});
|
|
17
|
+
global.onMiddleware = onMiddleware;
|
|
9
18
|
|
|
10
|
-
export { OnMiddleware };
|
|
19
|
+
export { OnMiddleware, onMiddleware };
|
|
@@ -7,7 +7,7 @@ import { expendSubscribeCreate, expendSubscribeMount, expendSubscribeUnmount } f
|
|
|
7
7
|
* @param event
|
|
8
8
|
* @param select
|
|
9
9
|
*/
|
|
10
|
-
const
|
|
10
|
+
const showLog = (event, select) => {
|
|
11
11
|
const logs = [`[${select}]`];
|
|
12
12
|
if (typeof event['ChannelId'] == 'string' && event['ChannelId'] != '') {
|
|
13
13
|
logs.push(`[${event['ChannelId']}]`);
|
|
@@ -63,7 +63,7 @@ const expendCycle = async (valueEvent, select) => {
|
|
|
63
63
|
}
|
|
64
64
|
expendMiddleware(valueEvent, select, nextMount);
|
|
65
65
|
};
|
|
66
|
-
const log =
|
|
66
|
+
const log = showLog(valueEvent, select);
|
|
67
67
|
logger.info(log.join(''));
|
|
68
68
|
// create
|
|
69
69
|
expendSubscribeCreate(valueEvent, select, nextCreate);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isAsyncFunction } from 'util/types';
|
|
2
2
|
import { useState } from './hook-use-state.js';
|
|
3
|
-
import {
|
|
3
|
+
import { showErrorModule } from './utils.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @fileoverview 消息处理快
|
|
@@ -76,8 +76,8 @@ const expendEvent = async (valueEvent, select, next) => {
|
|
|
76
76
|
return;
|
|
77
77
|
}
|
|
78
78
|
// 检查状态
|
|
79
|
-
if (
|
|
80
|
-
const [state] = useState(
|
|
79
|
+
if (file?.state) {
|
|
80
|
+
const [state] = useState(file?.state);
|
|
81
81
|
if (state == false) {
|
|
82
82
|
// 继续
|
|
83
83
|
await nextEvent();
|
|
@@ -114,6 +114,7 @@ const expendEvent = async (valueEvent, select, next) => {
|
|
|
114
114
|
dir: file.dir,
|
|
115
115
|
path: file.path,
|
|
116
116
|
name: file.name,
|
|
117
|
+
node: file.node,
|
|
117
118
|
value: {
|
|
118
119
|
select: app.default.select
|
|
119
120
|
}
|
|
@@ -162,7 +163,7 @@ const expendEvent = async (valueEvent, select, next) => {
|
|
|
162
163
|
}
|
|
163
164
|
}
|
|
164
165
|
catch (err) {
|
|
165
|
-
|
|
166
|
+
showErrorModule(err);
|
|
166
167
|
}
|
|
167
168
|
};
|
|
168
169
|
/**
|
|
@@ -193,8 +194,8 @@ const expendEvent = async (valueEvent, select, next) => {
|
|
|
193
194
|
return;
|
|
194
195
|
}
|
|
195
196
|
// 检查状态
|
|
196
|
-
if (
|
|
197
|
-
const [state] = useState(
|
|
197
|
+
if (file?.state) {
|
|
198
|
+
const [state] = useState(file?.state);
|
|
198
199
|
if (state == false) {
|
|
199
200
|
// 继续
|
|
200
201
|
await nextEvent();
|
|
@@ -244,7 +245,7 @@ const expendEvent = async (valueEvent, select, next) => {
|
|
|
244
245
|
//
|
|
245
246
|
}
|
|
246
247
|
catch (err) {
|
|
247
|
-
|
|
248
|
+
showErrorModule(err);
|
|
248
249
|
}
|
|
249
250
|
//
|
|
250
251
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isAsyncFunction } from 'util/types';
|
|
2
2
|
import { useState } from './hook-use-state.js';
|
|
3
|
-
import {
|
|
3
|
+
import { showErrorModule } from './utils.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* @fileoverview 消息处理快
|
|
@@ -75,8 +75,8 @@ const expendMiddleware = async (valueEvent, select, next) => {
|
|
|
75
75
|
return;
|
|
76
76
|
}
|
|
77
77
|
// 检查状态
|
|
78
|
-
if (
|
|
79
|
-
const [state] = useState(
|
|
78
|
+
if (file?.state) {
|
|
79
|
+
const [state] = useState(file?.state);
|
|
80
80
|
if (state == false) {
|
|
81
81
|
// 继续
|
|
82
82
|
await nextMiddleware();
|
|
@@ -113,6 +113,7 @@ const expendMiddleware = async (valueEvent, select, next) => {
|
|
|
113
113
|
dir: file.dir,
|
|
114
114
|
path: file.path,
|
|
115
115
|
name: file.name,
|
|
116
|
+
node: file.node,
|
|
116
117
|
value: {
|
|
117
118
|
select: app.default?.select ?? select
|
|
118
119
|
}
|
|
@@ -150,7 +151,7 @@ const expendMiddleware = async (valueEvent, select, next) => {
|
|
|
150
151
|
}
|
|
151
152
|
}
|
|
152
153
|
catch (err) {
|
|
153
|
-
|
|
154
|
+
showErrorModule(err);
|
|
154
155
|
}
|
|
155
156
|
};
|
|
156
157
|
/**
|
|
@@ -180,8 +181,8 @@ const expendMiddleware = async (valueEvent, select, next) => {
|
|
|
180
181
|
await nextMiddleware();
|
|
181
182
|
return;
|
|
182
183
|
}
|
|
183
|
-
if (
|
|
184
|
-
const [state] = useState(
|
|
184
|
+
if (file?.state) {
|
|
185
|
+
const [state] = useState(file?.state);
|
|
185
186
|
if (state == false) {
|
|
186
187
|
// 继续
|
|
187
188
|
await nextMiddleware();
|
|
@@ -219,7 +220,7 @@ const expendMiddleware = async (valueEvent, select, next) => {
|
|
|
219
220
|
}
|
|
220
221
|
}
|
|
221
222
|
catch (err) {
|
|
222
|
-
|
|
223
|
+
showErrorModule(err);
|
|
223
224
|
}
|
|
224
225
|
//
|
|
225
226
|
};
|
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
import './event-bot.js';
|
|
2
|
-
import './event-chidren.js';
|
|
3
|
-
import './event-middleware.js';
|
|
4
|
-
import './event-utlis.js';
|
|
5
|
-
import './hook-use-state.js';
|
|
6
|
-
import { createHash } from './utils.js';
|
|
7
1
|
import { getConfigValue } from '../config.js';
|
|
8
|
-
import '../global.js';
|
|
9
2
|
import { expendCycle } from './event-processor-cycle.js';
|
|
3
|
+
import { createHash } from './utils.js';
|
|
10
4
|
|
|
11
5
|
const MIN_TIME = 3000;
|
|
12
6
|
const MAX_TIME = 10000;
|
package/lib/app/event-utlis.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import '../global.js';
|
|
2
|
-
import { OnResponseType } from '../typing/event/index.js';
|
|
2
|
+
import { OnResponseType, OnResponseReversalType } from '../typing/event/index.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* 处理响应事件
|
|
@@ -9,5 +9,10 @@ import { OnResponseType } from '../typing/event/index.js';
|
|
|
9
9
|
* @returns 回调函数的执行结果
|
|
10
10
|
*/
|
|
11
11
|
declare const OnResponse: OnResponseType;
|
|
12
|
+
/**
|
|
13
|
+
* 废弃
|
|
14
|
+
* @deprecated
|
|
15
|
+
*/
|
|
16
|
+
declare const onResponse: OnResponseReversalType;
|
|
12
17
|
|
|
13
|
-
export { OnResponse };
|
|
18
|
+
export { OnResponse, onResponse };
|
package/lib/app/event-utlis.js
CHANGED
|
@@ -9,5 +9,13 @@ const OnResponse = (callback, select) => {
|
|
|
9
9
|
return { current: callback, select };
|
|
10
10
|
};
|
|
11
11
|
global.OnResponse = OnResponse;
|
|
12
|
+
/**
|
|
13
|
+
* 废弃
|
|
14
|
+
* @deprecated
|
|
15
|
+
*/
|
|
16
|
+
const onResponse = (select, callback) => {
|
|
17
|
+
return { current: callback, select };
|
|
18
|
+
};
|
|
19
|
+
global.onResponse = onResponse;
|
|
12
20
|
|
|
13
|
-
export { OnResponse };
|
|
21
|
+
export { OnResponse, onResponse };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import '../global.js';
|
|
2
|
+
import { Events } from '../typing/event/map.js';
|
|
2
3
|
import { User } from '../typing/event/base/user.js';
|
|
3
4
|
import { DataEnums } from '../typing/message/index.js';
|
|
4
5
|
|
|
@@ -25,6 +26,17 @@ declare const useSend: (event: {
|
|
|
25
26
|
* @param {string} mainDir - 模块的主目录路径。
|
|
26
27
|
* @throws {Error} - 如果 mainDir 无效,抛出错误。
|
|
27
28
|
*/
|
|
29
|
+
declare const unChildren: (mainDir: string) => void;
|
|
30
|
+
/**
|
|
31
|
+
* 废弃,请使用unChildren
|
|
32
|
+
* @deprecated
|
|
33
|
+
*/
|
|
28
34
|
declare const unMount: (mainDir: string) => void;
|
|
35
|
+
/**
|
|
36
|
+
*
|
|
37
|
+
* @param values
|
|
38
|
+
* @returns
|
|
39
|
+
*/
|
|
40
|
+
declare const createSelects: <T extends (keyof Events)[] | keyof Events>(values: T) => T;
|
|
29
41
|
|
|
30
|
-
export { unMount, useMention, useSend };
|
|
42
|
+
export { createSelects, unChildren, unMount, useMention, useSend };
|
package/lib/app/hook-use-api.js
CHANGED
|
@@ -28,7 +28,8 @@ const useSend = (event) => {
|
|
|
28
28
|
}
|
|
29
29
|
return async (...val) => {
|
|
30
30
|
if (!val || val.length === 0) {
|
|
31
|
-
|
|
31
|
+
logger.error('Invalid val: val must be a non-empty array');
|
|
32
|
+
return;
|
|
32
33
|
}
|
|
33
34
|
try {
|
|
34
35
|
return await alemonjsBot.api.use.send(event, val);
|
|
@@ -46,7 +47,7 @@ const useSend = (event) => {
|
|
|
46
47
|
* @param {string} mainDir - 模块的主目录路径。
|
|
47
48
|
* @throws {Error} - 如果 mainDir 无效,抛出错误。
|
|
48
49
|
*/
|
|
49
|
-
const
|
|
50
|
+
const unChildren = (mainDir) => {
|
|
50
51
|
if (!mainDir || typeof mainDir !== 'string') {
|
|
51
52
|
throw new Error('Invalid mainDir: mainDir must be a non-empty string');
|
|
52
53
|
}
|
|
@@ -78,5 +79,16 @@ const unMount = (mainDir) => {
|
|
|
78
79
|
}
|
|
79
80
|
}
|
|
80
81
|
};
|
|
82
|
+
/**
|
|
83
|
+
* 废弃,请使用unChildren
|
|
84
|
+
* @deprecated
|
|
85
|
+
*/
|
|
86
|
+
const unMount = unChildren;
|
|
87
|
+
/**
|
|
88
|
+
*
|
|
89
|
+
* @param values
|
|
90
|
+
* @returns
|
|
91
|
+
*/
|
|
92
|
+
const createSelects = (values) => values;
|
|
81
93
|
|
|
82
|
-
export { unMount, useMention, useSend };
|
|
94
|
+
export { createSelects, unChildren, unMount, useMention, useSend };
|
|
@@ -16,12 +16,22 @@ declare const useState: <T extends string>(name: T, defaultValue?: boolean) => [
|
|
|
16
16
|
* @param name 功能名
|
|
17
17
|
* @param callback 回调函数
|
|
18
18
|
*/
|
|
19
|
+
declare const onState: <T extends string>(name: T, callback: (value: boolean) => void) => void;
|
|
20
|
+
/**
|
|
21
|
+
* 废弃
|
|
22
|
+
* @deprecated
|
|
23
|
+
*/
|
|
19
24
|
declare const eventState: <T extends string>(name: T, callback: (value: boolean) => void) => void;
|
|
20
25
|
/**
|
|
21
26
|
* 取消订阅状态变化
|
|
22
27
|
* @param name 功能名
|
|
23
28
|
* @param callback 回调函数
|
|
24
29
|
*/
|
|
30
|
+
declare const unState: <T extends string>(name: T, callback: (value: boolean) => void) => void;
|
|
31
|
+
/**
|
|
32
|
+
* 废弃
|
|
33
|
+
* @deprecated
|
|
34
|
+
*/
|
|
25
35
|
declare const unEventState: <T extends string>(name: T, callback: (value: boolean) => void) => void;
|
|
26
36
|
|
|
27
|
-
export { eventState, unEventState, useState };
|
|
37
|
+
export { eventState, onState, unEventState, unState, useState };
|
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
// 存储订阅者
|
|
2
2
|
const subscriptions = {};
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
target
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
callback
|
|
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
|
+
}
|
|
14
16
|
}
|
|
17
|
+
return true; // 表示设置成功
|
|
15
18
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
});
|
|
19
|
+
});
|
|
20
|
+
}
|
|
19
21
|
/**
|
|
20
22
|
* 获取指定功能是启动还是关闭
|
|
21
23
|
* ***
|
|
@@ -44,7 +46,7 @@ const useState = (name, defaultValue = true) => {
|
|
|
44
46
|
* @param name 功能名
|
|
45
47
|
* @param callback 回调函数
|
|
46
48
|
*/
|
|
47
|
-
const
|
|
49
|
+
const onState = (name, callback) => {
|
|
48
50
|
if (typeof callback !== 'function') {
|
|
49
51
|
throw new Error('Callback must be a function');
|
|
50
52
|
}
|
|
@@ -53,15 +55,25 @@ const eventState = (name, callback) => {
|
|
|
53
55
|
}
|
|
54
56
|
subscriptions[name].push(callback);
|
|
55
57
|
};
|
|
58
|
+
/**
|
|
59
|
+
* 废弃
|
|
60
|
+
* @deprecated
|
|
61
|
+
*/
|
|
62
|
+
const eventState = onState;
|
|
56
63
|
/**
|
|
57
64
|
* 取消订阅状态变化
|
|
58
65
|
* @param name 功能名
|
|
59
66
|
* @param callback 回调函数
|
|
60
67
|
*/
|
|
61
|
-
const
|
|
68
|
+
const unState = (name, callback) => {
|
|
62
69
|
if (subscriptions[name]) {
|
|
63
70
|
subscriptions[name] = subscriptions[name].filter(cb => cb !== callback);
|
|
64
71
|
}
|
|
65
72
|
};
|
|
73
|
+
/**
|
|
74
|
+
* 废弃
|
|
75
|
+
* @deprecated
|
|
76
|
+
*/
|
|
77
|
+
const unEventState = unState;
|
|
66
78
|
|
|
67
|
-
export { eventState, unEventState, useState };
|
|
79
|
+
export { eventState, onState, unEventState, unState, useState };
|