alemonjs 2.1.49 → 2.1.52
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/LICENSE +21 -0
- package/lib/app/event-format.d.ts +19 -0
- package/lib/app/event-format.js +128 -0
- package/lib/app/event-processor-callHandler.js +3 -2
- package/lib/app/event-utils.js +1 -1
- package/lib/app/hook-event-context.d.ts +2 -1
- package/lib/app/hook-event-context.js +7 -4
- package/lib/app/hook-use/client.js +21 -17
- package/lib/app/hook-use/common.d.ts +2 -2
- package/lib/app/hook-use/common.js +1 -0
- package/lib/app/hook-use/event.d.ts +33 -0
- package/lib/app/hook-use/event.js +51 -0
- package/lib/app/hook-use/index.d.ts +2 -0
- package/lib/app/hook-use/index.js +2 -0
- package/lib/app/hook-use/subscribe.d.ts +73 -0
- package/lib/app/hook-use/subscribe.js +112 -0
- package/lib/app/index.d.ts +1 -1
- package/lib/app/index.js +5 -3
- package/lib/app/load_modules/load.js +1 -1
- package/lib/app/load_modules/loadChild.js +3 -3
- package/lib/app/message-api.d.ts +2 -2
- package/lib/app/message-api.js +6 -38
- package/lib/app/message-format.d.ts +1 -13
- package/lib/app/message-format.js +1 -25
- package/lib/cbp/connects/platform.js +3 -3
- package/lib/cbp/server/main.js +6 -6
- package/lib/core/utils.js +5 -5
- package/lib/index.js +5 -3
- package/lib/process/module.js +3 -3
- package/lib/process/platform.js +3 -3
- package/lib/server/routers/router.js +59 -10
- package/lib/server/routers/utils.d.ts +2 -0
- package/lib/server/routers/utils.js +12 -1
- package/lib/types/event/base/platform.d.ts +1 -1
- package/lib/types/event/builder.d.ts +36 -0
- package/lib/types/event/builder.js +1 -0
- package/lib/types/event/channel/index.d.ts +4 -4
- package/lib/types/event/guild/index.d.ts +4 -4
- package/lib/types/event/interaction/index.d.ts +3 -3
- package/lib/types/event/member/index.d.ts +6 -6
- package/lib/types/event/message/message.d.ts +7 -7
- package/lib/types/event/message/private.message.d.ts +4 -4
- package/lib/types/event/notice/index.d.ts +3 -3
- package/lib/types/event/request/index.d.ts +4 -4
- package/lib/types/index.d.ts +1 -0
- package/lib/utils.js +1 -1
- package/package.json +3 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013-present, Yuxi (Evan) You
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIdED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Events, EventKeys, EventBuilder, ReservedEventKeys, User, Guild, Channel, Message, MessageText, MessageMedia, MessageOpen, Platform } from '../types';
|
|
2
|
+
export declare class FormatEvent<T extends EventKeys = EventKeys> {
|
|
3
|
+
#private;
|
|
4
|
+
private constructor();
|
|
5
|
+
static create<T extends EventKeys>(name: T): EventBuilder<T>;
|
|
6
|
+
addPlatform(params: Platform): this;
|
|
7
|
+
addGuild(params: Guild): this;
|
|
8
|
+
addChannel(params: Channel): this;
|
|
9
|
+
addUser(params: User): this;
|
|
10
|
+
addMessage(params: Message): this;
|
|
11
|
+
addText(params: MessageText): this;
|
|
12
|
+
addMedia(params: MessageMedia): this;
|
|
13
|
+
addOpen(params: MessageOpen): this;
|
|
14
|
+
add<E extends Record<string, unknown>>(fields: {
|
|
15
|
+
[K in keyof E]: K extends ReservedEventKeys ? never : E[K];
|
|
16
|
+
}): this;
|
|
17
|
+
get value(): Events[T];
|
|
18
|
+
}
|
|
19
|
+
export declare function wrapEvent<E extends Record<string, unknown>>(event: object): Readonly<E>;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
class FormatEvent {
|
|
2
|
+
#data;
|
|
3
|
+
constructor(name) {
|
|
4
|
+
this.#data = { name, Timestamp: Date.now() };
|
|
5
|
+
}
|
|
6
|
+
static create(name) {
|
|
7
|
+
return new FormatEvent(name);
|
|
8
|
+
}
|
|
9
|
+
addPlatform(params) {
|
|
10
|
+
const { value, ...otherParams } = params;
|
|
11
|
+
Object.assign(this.#data, otherParams);
|
|
12
|
+
Object.defineProperty(this.#data, 'value', {
|
|
13
|
+
value,
|
|
14
|
+
enumerable: false
|
|
15
|
+
});
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
addGuild(params) {
|
|
19
|
+
Object.assign(this.#data, {
|
|
20
|
+
GuildId: params.GuildId,
|
|
21
|
+
SpaceId: params.SpaceId
|
|
22
|
+
});
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
addChannel(params) {
|
|
26
|
+
Object.assign(this.#data, {
|
|
27
|
+
ChannelId: params.ChannelId
|
|
28
|
+
});
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
addUser(params) {
|
|
32
|
+
Object.assign(this.#data, {
|
|
33
|
+
UserId: params.UserId,
|
|
34
|
+
UserKey: params.UserKey,
|
|
35
|
+
IsMaster: params.IsMaster,
|
|
36
|
+
IsBot: params.IsBot,
|
|
37
|
+
...(params.UserName !== undefined && { UserName: params.UserName }),
|
|
38
|
+
...(params.UserAvatar !== undefined && { UserAvatar: params.UserAvatar })
|
|
39
|
+
});
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
addMessage(params) {
|
|
43
|
+
const assign = { MessageId: params.MessageId };
|
|
44
|
+
if (params.ReplyId !== undefined) {
|
|
45
|
+
assign.ReplyId = params.ReplyId;
|
|
46
|
+
}
|
|
47
|
+
Object.assign(this.#data, assign);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
addText(params) {
|
|
51
|
+
Object.assign(this.#data, {
|
|
52
|
+
MessageText: params.MessageText
|
|
53
|
+
});
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
addMedia(params) {
|
|
57
|
+
Object.assign(this.#data, {
|
|
58
|
+
MessageMedia: params.MessageMedia
|
|
59
|
+
});
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
addOpen(params) {
|
|
63
|
+
Object.assign(this.#data, {
|
|
64
|
+
OpenId: params.OpenId
|
|
65
|
+
});
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
add(fields) {
|
|
69
|
+
for (const key of Object.keys(fields)) {
|
|
70
|
+
this.#data[`_${key}`] = fields[key];
|
|
71
|
+
}
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
get value() {
|
|
75
|
+
return this.#data;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function wrapEvent(event) {
|
|
79
|
+
return new Proxy(event, {
|
|
80
|
+
get(target, prop, receiver) {
|
|
81
|
+
if (typeof prop === 'string' && !prop.startsWith('_')) {
|
|
82
|
+
const privateKey = `_${prop}`;
|
|
83
|
+
if (privateKey in target) {
|
|
84
|
+
return Reflect.get(target, privateKey, receiver);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return Reflect.get(target, prop, receiver);
|
|
88
|
+
},
|
|
89
|
+
has(target, prop) {
|
|
90
|
+
if (typeof prop === 'string' && !prop.startsWith('_')) {
|
|
91
|
+
if (`_${prop}` in target) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return Reflect.has(target, prop);
|
|
96
|
+
},
|
|
97
|
+
set() {
|
|
98
|
+
return false;
|
|
99
|
+
},
|
|
100
|
+
deleteProperty() {
|
|
101
|
+
return false;
|
|
102
|
+
},
|
|
103
|
+
ownKeys(target) {
|
|
104
|
+
return Reflect.ownKeys(target).map(key => {
|
|
105
|
+
if (typeof key === 'string' && key.startsWith('_')) {
|
|
106
|
+
return key.slice(1);
|
|
107
|
+
}
|
|
108
|
+
return key;
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
112
|
+
if (typeof prop === 'string' && !prop.startsWith('_')) {
|
|
113
|
+
const privateKey = `_${prop}`;
|
|
114
|
+
const desc = Reflect.getOwnPropertyDescriptor(target, privateKey);
|
|
115
|
+
if (desc) {
|
|
116
|
+
return { ...desc, configurable: true, writable: false };
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const desc = Reflect.getOwnPropertyDescriptor(target, prop);
|
|
120
|
+
if (desc) {
|
|
121
|
+
return { ...desc, writable: false };
|
|
122
|
+
}
|
|
123
|
+
return desc;
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { FormatEvent, wrapEvent };
|
|
@@ -20,10 +20,11 @@ const createCallHandler = valueEvent => {
|
|
|
20
20
|
return;
|
|
21
21
|
}
|
|
22
22
|
try {
|
|
23
|
-
const
|
|
23
|
+
const nextFn = (...cns) => {
|
|
24
24
|
isNext = true;
|
|
25
25
|
nextEvent(...cns);
|
|
26
|
-
}
|
|
26
|
+
};
|
|
27
|
+
const res = await withEventContext(valueEvent, nextFn, () => currents[index](valueEvent, nextFn));
|
|
27
28
|
if (res !== true) {
|
|
28
29
|
isClose = true;
|
|
29
30
|
}
|
package/lib/app/event-utils.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getConfig } from '../core/config.js';
|
|
2
|
-
import { ChildrenApp, State
|
|
2
|
+
import { StateSubscribe, ChildrenApp, State } from './store.js';
|
|
3
3
|
import { ResultCode } from '../core/variable.js';
|
|
4
4
|
|
|
5
5
|
const unChildren = (name = 'main') => {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { EventKeys, Events } from '../types';
|
|
2
|
-
export declare const withEventContext: <T extends EventKeys, R>(event: Events[T], runner: () => R) => R;
|
|
2
|
+
export declare const withEventContext: <T extends EventKeys, R>(event: Events[T], next: (...args: boolean[]) => void, runner: () => R) => R;
|
|
3
3
|
export declare const getCurrentEvent: <T extends EventKeys>() => Events[T] | undefined;
|
|
4
|
+
export declare const getCurrentNext: () => ((...args: boolean[]) => void) | undefined;
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
2
|
|
|
3
3
|
const eventStore = new AsyncLocalStorage();
|
|
4
|
-
const withEventContext = (event, runner) => {
|
|
5
|
-
return eventStore.run(event, runner);
|
|
4
|
+
const withEventContext = (event, next, runner) => {
|
|
5
|
+
return eventStore.run({ event, next }, runner);
|
|
6
6
|
};
|
|
7
7
|
const getCurrentEvent = () => {
|
|
8
|
-
return eventStore.getStore();
|
|
8
|
+
return eventStore.getStore()?.event;
|
|
9
|
+
};
|
|
10
|
+
const getCurrentNext = () => {
|
|
11
|
+
return eventStore.getStore()?.next;
|
|
9
12
|
};
|
|
10
13
|
|
|
11
|
-
export { getCurrentEvent, withEventContext };
|
|
14
|
+
export { getCurrentEvent, getCurrentNext, withEventContext };
|
|
@@ -10,6 +10,26 @@ import { sendAPI } from '../../cbp/processor/api.js';
|
|
|
10
10
|
import '../message-format-old.js';
|
|
11
11
|
import { getCurrentEvent } from '../hook-event-context.js';
|
|
12
12
|
|
|
13
|
+
const createDeepProxy = (event, path = []) => {
|
|
14
|
+
return new Proxy((() => { }), {
|
|
15
|
+
get(_target, prop) {
|
|
16
|
+
if (typeof prop === 'symbol') {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
return createDeepProxy(event, [...path, String(prop)]);
|
|
20
|
+
},
|
|
21
|
+
apply(_target, _thisArg, args) {
|
|
22
|
+
return sendAPI({
|
|
23
|
+
action: 'client.api',
|
|
24
|
+
payload: {
|
|
25
|
+
event,
|
|
26
|
+
key: path.join('.'),
|
|
27
|
+
params: args
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
};
|
|
13
33
|
function useClient(eventOrClass, _ApiClass) {
|
|
14
34
|
let valueEvent;
|
|
15
35
|
if (eventOrClass !== undefined && typeof eventOrClass === 'function') {
|
|
@@ -26,23 +46,7 @@ function useClient(eventOrClass, _ApiClass) {
|
|
|
26
46
|
});
|
|
27
47
|
throw new Error('Invalid event: event must be an object');
|
|
28
48
|
}
|
|
29
|
-
const client =
|
|
30
|
-
get(_target, prop) {
|
|
31
|
-
if (typeof prop === 'symbol') {
|
|
32
|
-
return undefined;
|
|
33
|
-
}
|
|
34
|
-
return (...args) => {
|
|
35
|
-
return sendAPI({
|
|
36
|
-
action: 'client.api',
|
|
37
|
-
payload: {
|
|
38
|
-
event: valueEvent,
|
|
39
|
-
key: String(prop),
|
|
40
|
-
params: args
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
});
|
|
49
|
+
const client = createDeepProxy(valueEvent);
|
|
46
50
|
return [client];
|
|
47
51
|
}
|
|
48
52
|
|
|
@@ -5,9 +5,9 @@ import { createResult, Result } from '../../core/utils';
|
|
|
5
5
|
import { sendAction } from '../../cbp/processor/actions';
|
|
6
6
|
import { sendAPI } from '../../cbp/processor/api';
|
|
7
7
|
import { Format } from '../message-format';
|
|
8
|
-
import { getCurrentEvent } from '../hook-event-context';
|
|
8
|
+
import { getCurrentEvent, getCurrentNext } from '../hook-event-context';
|
|
9
9
|
export type { DataEnums, EventKeys, Events, User, GuildInfo, ChannelInfo, MemberInfo, RoleInfo, PaginationParams, PaginatedResult, Result };
|
|
10
|
-
export { ResultCode, ChildrenApp, createResult, sendAction, sendAPI, Format, getCurrentEvent };
|
|
10
|
+
export { ResultCode, ChildrenApp, createResult, sendAction, sendAPI, Format, getCurrentEvent, getCurrentNext };
|
|
11
11
|
export type Options = {
|
|
12
12
|
UserId?: string;
|
|
13
13
|
UserKey?: string;
|
|
@@ -5,6 +5,7 @@ export { sendAction } from '../../cbp/processor/actions.js';
|
|
|
5
5
|
export { sendAPI } from '../../cbp/processor/api.js';
|
|
6
6
|
export { Format } from '../message-format.js';
|
|
7
7
|
import { getCurrentEvent } from '../hook-event-context.js';
|
|
8
|
+
export { getCurrentNext } from '../hook-event-context.js';
|
|
8
9
|
|
|
9
10
|
const getEventOrThrow = (event) => {
|
|
10
11
|
const currentEvent = event ?? getCurrentEvent();
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { EventKeys, Events } from '../../types';
|
|
2
|
+
type UseEventOptions<T extends EventKeys> = {
|
|
3
|
+
selects: T | T[];
|
|
4
|
+
regular?: RegExp;
|
|
5
|
+
prefix?: string;
|
|
6
|
+
exact?: string;
|
|
7
|
+
};
|
|
8
|
+
type UseEventResult<T extends EventKeys> = {
|
|
9
|
+
current: Events[T];
|
|
10
|
+
value: Events[T]['value'];
|
|
11
|
+
match: {
|
|
12
|
+
selects: boolean;
|
|
13
|
+
regular: boolean;
|
|
14
|
+
prefix: boolean;
|
|
15
|
+
exact: boolean;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export declare function useEvent<T extends EventKeys>(options?: UseEventOptions<T>): readonly [UseEventResult<T>, (...args: boolean[]) => void];
|
|
19
|
+
export declare function useEvent<T extends EventKeys>(event?: Events[T], options?: UseEventOptions<T>): readonly [UseEventResult<T>, (...args: boolean[]) => void];
|
|
20
|
+
export declare function createEvent<T extends EventKeys>(options: {
|
|
21
|
+
event: any;
|
|
22
|
+
selects: T | T[];
|
|
23
|
+
regular?: RegExp;
|
|
24
|
+
prefix?: string;
|
|
25
|
+
exact?: string;
|
|
26
|
+
}): Events[T] & {
|
|
27
|
+
value: Events[T]["value"];
|
|
28
|
+
selects: boolean;
|
|
29
|
+
regular: boolean;
|
|
30
|
+
prefix: boolean;
|
|
31
|
+
exact: boolean;
|
|
32
|
+
};
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { getEventOrThrow } from './common.js';
|
|
2
|
+
import { getCurrentNext } from '../hook-event-context.js';
|
|
3
|
+
|
|
4
|
+
const isOptions = (v) => {
|
|
5
|
+
return typeof v === 'object' && 'selects' in v;
|
|
6
|
+
};
|
|
7
|
+
function useEvent(eventOrOptions, options) {
|
|
8
|
+
let eventArg;
|
|
9
|
+
let opts;
|
|
10
|
+
if (eventOrOptions && isOptions(eventOrOptions)) {
|
|
11
|
+
opts = eventOrOptions;
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
eventArg = eventOrOptions;
|
|
15
|
+
opts = options;
|
|
16
|
+
}
|
|
17
|
+
const { selects, regular, prefix, exact } = opts ?? {};
|
|
18
|
+
const eventValue = getEventOrThrow(eventArg);
|
|
19
|
+
const { name, MessageText } = eventValue;
|
|
20
|
+
const selectsArr = Array.isArray(selects) ? selects : [selects];
|
|
21
|
+
const match = {
|
|
22
|
+
selects: selectsArr.includes(name),
|
|
23
|
+
exact: !!(exact && MessageText === exact),
|
|
24
|
+
prefix: !!(prefix && MessageText?.startsWith(prefix)),
|
|
25
|
+
regular: !!(regular && MessageText && regular.test(MessageText))
|
|
26
|
+
};
|
|
27
|
+
const r = {
|
|
28
|
+
current: { ...eventValue },
|
|
29
|
+
match
|
|
30
|
+
};
|
|
31
|
+
Object.defineProperty(r, 'value', {
|
|
32
|
+
get() {
|
|
33
|
+
return eventValue.value;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
const next = getCurrentNext() ?? (() => { });
|
|
37
|
+
return [r, next];
|
|
38
|
+
}
|
|
39
|
+
function createEvent(options) {
|
|
40
|
+
const { event, selects, regular, prefix, exact } = options;
|
|
41
|
+
const [eventRef] = useEvent(event, { selects, regular, prefix, exact });
|
|
42
|
+
const o = eventRef.match;
|
|
43
|
+
const e = eventRef.current;
|
|
44
|
+
return {
|
|
45
|
+
...e,
|
|
46
|
+
...o,
|
|
47
|
+
value: eventRef.value
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { createEvent, useEvent };
|
|
@@ -13,3 +13,5 @@ export { useReaction } from './reaction.js';
|
|
|
13
13
|
export { useRequest } from './request.js';
|
|
14
14
|
export { useRole } from './role.js';
|
|
15
15
|
export { useUser } from './user.js';
|
|
16
|
+
export { useObserver, useSubscribe } from './subscribe.js';
|
|
17
|
+
export { createEvent, useEvent } from './event.js';
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { EventCycleEnum, Current, Events, EventKeys } from '../../types';
|
|
2
|
+
export declare function useSubscribe<T extends EventKeys>(selects: T | T[]): readonly [
|
|
3
|
+
{
|
|
4
|
+
create: (callback: Current<T>, keys: (keyof Events[T])[]) => {
|
|
5
|
+
id: string;
|
|
6
|
+
selects: T[];
|
|
7
|
+
choose: EventCycleEnum;
|
|
8
|
+
};
|
|
9
|
+
mount: (callback: Current<T>, keys: (keyof Events[T])[]) => {
|
|
10
|
+
id: string;
|
|
11
|
+
selects: T[];
|
|
12
|
+
choose: EventCycleEnum;
|
|
13
|
+
};
|
|
14
|
+
unmount: (callback: Current<T>, keys: (keyof Events[T])[]) => {
|
|
15
|
+
id: string;
|
|
16
|
+
selects: T[];
|
|
17
|
+
choose: EventCycleEnum;
|
|
18
|
+
};
|
|
19
|
+
cancel: (value: {
|
|
20
|
+
id: string;
|
|
21
|
+
selects: T[];
|
|
22
|
+
choose: EventCycleEnum;
|
|
23
|
+
}) => void;
|
|
24
|
+
}
|
|
25
|
+
];
|
|
26
|
+
export declare function useSubscribe<T extends EventKeys>(event: Events[T] | undefined, selects: T | T[]): readonly [
|
|
27
|
+
{
|
|
28
|
+
create: (callback: Current<T>, keys: (keyof Events[T])[]) => {
|
|
29
|
+
id: string;
|
|
30
|
+
selects: T[];
|
|
31
|
+
choose: EventCycleEnum;
|
|
32
|
+
};
|
|
33
|
+
mount: (callback: Current<T>, keys: (keyof Events[T])[]) => {
|
|
34
|
+
id: string;
|
|
35
|
+
selects: T[];
|
|
36
|
+
choose: EventCycleEnum;
|
|
37
|
+
};
|
|
38
|
+
unmount: (callback: Current<T>, keys: (keyof Events[T])[]) => {
|
|
39
|
+
id: string;
|
|
40
|
+
selects: T[];
|
|
41
|
+
choose: EventCycleEnum;
|
|
42
|
+
};
|
|
43
|
+
cancel: (value: {
|
|
44
|
+
id: string;
|
|
45
|
+
selects: T[];
|
|
46
|
+
choose: EventCycleEnum;
|
|
47
|
+
}) => void;
|
|
48
|
+
}
|
|
49
|
+
];
|
|
50
|
+
export declare function useObserver<T extends EventKeys>(selects: T | T[]): readonly [
|
|
51
|
+
(callback: Current<T>, keys: (keyof Events[T])[]) => {
|
|
52
|
+
id: string;
|
|
53
|
+
selects: T[];
|
|
54
|
+
choose: EventCycleEnum;
|
|
55
|
+
},
|
|
56
|
+
(value: {
|
|
57
|
+
id: string;
|
|
58
|
+
selects: T[];
|
|
59
|
+
choose: EventCycleEnum;
|
|
60
|
+
}) => void
|
|
61
|
+
];
|
|
62
|
+
export declare function useObserver<T extends EventKeys>(event: Events[T] | undefined, selects: T | T[]): readonly [
|
|
63
|
+
(callback: Current<T>, keys: (keyof Events[T])[]) => {
|
|
64
|
+
id: string;
|
|
65
|
+
selects: T[];
|
|
66
|
+
choose: EventCycleEnum;
|
|
67
|
+
},
|
|
68
|
+
(value: {
|
|
69
|
+
id: string;
|
|
70
|
+
selects: T[];
|
|
71
|
+
choose: EventCycleEnum;
|
|
72
|
+
}) => void
|
|
73
|
+
];
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { ResultCode } from '../../core/variable.js';
|
|
2
|
+
import { SubscribeList } from '../store.js';
|
|
3
|
+
import { SubscribeStatus } from '../config.js';
|
|
4
|
+
import { getCurrentEvent } from '../hook-event-context.js';
|
|
5
|
+
|
|
6
|
+
function useSubscribe(eventOrSelects, maybeSelects) {
|
|
7
|
+
const selects = (maybeSelects === undefined ? eventOrSelects : maybeSelects);
|
|
8
|
+
const event = (maybeSelects === undefined ? undefined : eventOrSelects);
|
|
9
|
+
const valueEvent = event ?? getCurrentEvent();
|
|
10
|
+
if (typeof valueEvent !== 'object' || valueEvent === null) {
|
|
11
|
+
logger.error({
|
|
12
|
+
code: ResultCode.FailParams,
|
|
13
|
+
message: 'event is not object',
|
|
14
|
+
data: null
|
|
15
|
+
});
|
|
16
|
+
throw new Error('event is not object');
|
|
17
|
+
}
|
|
18
|
+
if (typeof selects !== 'string' && !Array.isArray(selects)) {
|
|
19
|
+
logger.error({
|
|
20
|
+
code: ResultCode.FailParams,
|
|
21
|
+
message: 'select is not string or array',
|
|
22
|
+
data: null
|
|
23
|
+
});
|
|
24
|
+
throw new Error('select is not string or array');
|
|
25
|
+
}
|
|
26
|
+
const register = (callback, keys, choose) => {
|
|
27
|
+
const curSelects = Array.isArray(selects) ? selects : [selects];
|
|
28
|
+
const ID = Date.now().toString(36) + Math.random().toString(36).substring(2, 15);
|
|
29
|
+
if (keys.length === 0) {
|
|
30
|
+
logger.warn({
|
|
31
|
+
code: ResultCode.FailParams,
|
|
32
|
+
message: 'subscribe keys is empty',
|
|
33
|
+
data: null
|
|
34
|
+
});
|
|
35
|
+
return { selects: curSelects, choose, id: ID };
|
|
36
|
+
}
|
|
37
|
+
for (const select of curSelects) {
|
|
38
|
+
const subList = new SubscribeList(choose, select);
|
|
39
|
+
const values = {};
|
|
40
|
+
for (const key of keys) {
|
|
41
|
+
if (typeof key === 'string' && (typeof valueEvent[key] === 'string' || typeof valueEvent[key] === 'number' || typeof valueEvent[key] === 'boolean')) {
|
|
42
|
+
values[key] = valueEvent[key];
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
logger.warn({
|
|
46
|
+
code: ResultCode.FailParams,
|
|
47
|
+
message: `Invalid key: ${key?.toString()} must be a string, number or boolean`,
|
|
48
|
+
data: null
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
subList.value.append({
|
|
53
|
+
choose,
|
|
54
|
+
selects: curSelects,
|
|
55
|
+
keys: values,
|
|
56
|
+
current: callback,
|
|
57
|
+
status: SubscribeStatus.active,
|
|
58
|
+
id: ID
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
selects: curSelects,
|
|
63
|
+
choose,
|
|
64
|
+
id: ID
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
const create = (callback, keys) => {
|
|
68
|
+
return register(callback, keys, 'create');
|
|
69
|
+
};
|
|
70
|
+
const mountBefore = (callback, keys) => {
|
|
71
|
+
return register(callback, keys, 'mount');
|
|
72
|
+
};
|
|
73
|
+
const unmount = (callback, keys) => {
|
|
74
|
+
return register(callback, keys, 'unmount');
|
|
75
|
+
};
|
|
76
|
+
const cancel = (value) => {
|
|
77
|
+
const selects = value.selects;
|
|
78
|
+
const ID = value.id;
|
|
79
|
+
for (const select of selects) {
|
|
80
|
+
const subList = new SubscribeList(value.choose, select);
|
|
81
|
+
subList.value.forEach(node => {
|
|
82
|
+
if (node.data.id === ID) {
|
|
83
|
+
node.data.status = SubscribeStatus.paused;
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
const subscribe = {
|
|
90
|
+
create,
|
|
91
|
+
mount: mountBefore,
|
|
92
|
+
unmount,
|
|
93
|
+
cancel
|
|
94
|
+
};
|
|
95
|
+
return [subscribe];
|
|
96
|
+
}
|
|
97
|
+
function useObserver(eventOrSelects, maybeSelects) {
|
|
98
|
+
const selects = (maybeSelects === undefined ? eventOrSelects : maybeSelects);
|
|
99
|
+
const event = (maybeSelects === undefined ? undefined : eventOrSelects);
|
|
100
|
+
if (selects === undefined) {
|
|
101
|
+
logger.error({
|
|
102
|
+
code: ResultCode.FailParams,
|
|
103
|
+
message: 'select is not string or array',
|
|
104
|
+
data: null
|
|
105
|
+
});
|
|
106
|
+
throw new Error('select is not string or array');
|
|
107
|
+
}
|
|
108
|
+
const [subscribe] = useSubscribe(event, selects);
|
|
109
|
+
return [subscribe.mount, subscribe.cancel];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export { useObserver, useSubscribe };
|
package/lib/app/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from './define-platform.js';
|
|
|
5
5
|
export * from './define-response.js';
|
|
6
6
|
export * from './define-middleware.js';
|
|
7
7
|
export * from './define-router.js';
|
|
8
|
+
export * from './event-format.js';
|
|
8
9
|
export * from './event-group.js';
|
|
9
10
|
export * from './event-middleware';
|
|
10
11
|
export * from './event-processor';
|
|
@@ -16,6 +17,5 @@ export * from './event-processor-subscribe.js';
|
|
|
16
17
|
export * from './hook-use/index.js';
|
|
17
18
|
export * from './hook-event-context.js';
|
|
18
19
|
export * from './event-utils.js';
|
|
19
|
-
export * from './hook-use-subscribe.js';
|
|
20
20
|
export * from './message-api.js';
|
|
21
21
|
export * from './message-format.js';
|
package/lib/app/index.js
CHANGED
|
@@ -6,6 +6,7 @@ export { definePlatform } from './define-platform.js';
|
|
|
6
6
|
export { defineResponse } from './define-response.js';
|
|
7
7
|
export { defineMiddleware } from './define-middleware.js';
|
|
8
8
|
export { defineRouter, lazy } from './define-router.js';
|
|
9
|
+
export { FormatEvent, wrapEvent } from './event-format.js';
|
|
9
10
|
export { onGroup } from './event-group.js';
|
|
10
11
|
export { OnMiddleware, onMiddleware } from './event-middleware.js';
|
|
11
12
|
export { OnProcessor, onProcessor } from './event-processor.js';
|
|
@@ -29,9 +30,10 @@ export { useReaction } from './hook-use/reaction.js';
|
|
|
29
30
|
export { useRequest } from './hook-use/request.js';
|
|
30
31
|
export { useRole } from './hook-use/role.js';
|
|
31
32
|
export { useUser } from './hook-use/user.js';
|
|
32
|
-
export {
|
|
33
|
+
export { useObserver, useSubscribe } from './hook-use/subscribe.js';
|
|
34
|
+
export { createEvent, useEvent } from './hook-use/event.js';
|
|
35
|
+
export { getCurrentEvent, getCurrentNext, withEventContext } from './hook-event-context.js';
|
|
33
36
|
export { createEventValue, createSelects, onSelects, onState, unChildren, unState, useState } from './event-utils.js';
|
|
34
|
-
export { useObserver, useSubscribe } from './hook-use-subscribe.js';
|
|
35
37
|
export { MessageDirect, createDataFormat, format, getMessageIntent, sendToChannel, sendToUser } from './message-api.js';
|
|
36
|
-
export { Format, FormatButtonGroup, FormatMarkDown
|
|
38
|
+
export { Format, FormatButtonGroup, FormatMarkDown } from './message-format.js';
|
|
37
39
|
export { Attachment, Audio, BT, Button, Image, ImageFile, ImageURL, Link, MD, Markdown, MarkdownOriginal, Mention, Text, Video } from './message-format-old.js';
|
|
@@ -8,7 +8,7 @@ const loadApps = () => {
|
|
|
8
8
|
const cfg = getConfig();
|
|
9
9
|
const apps = Array.isArray(cfg.value?.apps) ? cfg.value.apps : Object.keys(cfg.value?.apps ?? {}).filter(Boolean);
|
|
10
10
|
const uniqueApps = Array.from(new Set(apps));
|
|
11
|
-
|
|
11
|
+
uniqueApps.forEach(app => void loadChildrenFile(app));
|
|
12
12
|
};
|
|
13
13
|
const run = (input) => {
|
|
14
14
|
if (!input) {
|
|
@@ -3,11 +3,11 @@ import { existsSync } from 'fs';
|
|
|
3
3
|
import { showErrorModule, getRecursiveDirFiles, createEventName } from '../../core/utils.js';
|
|
4
4
|
import { ChildrenApp } from '../store.js';
|
|
5
5
|
import { ResultCode, fileSuffixMiddleware } from '../../core/variable.js';
|
|
6
|
-
import module from 'module';
|
|
6
|
+
import module$1 from 'module';
|
|
7
7
|
|
|
8
8
|
const initRequire = () => { };
|
|
9
9
|
initRequire.resolve = () => '';
|
|
10
|
-
const require = module?.createRequire?.(import.meta.url) ?? initRequire;
|
|
10
|
+
const require$1 = module$1?.createRequire?.(import.meta.url) ?? initRequire;
|
|
11
11
|
const loadChildren = async (mainPath, appName) => {
|
|
12
12
|
if (!mainPath || typeof mainPath !== 'string') {
|
|
13
13
|
logger.error({
|
|
@@ -168,7 +168,7 @@ const loadChildrenFile = (appName) => {
|
|
|
168
168
|
return;
|
|
169
169
|
}
|
|
170
170
|
try {
|
|
171
|
-
const mainPath = require.resolve(appName);
|
|
171
|
+
const mainPath = require$1.resolve(appName);
|
|
172
172
|
if (!existsSync(mainPath)) {
|
|
173
173
|
logger.error({
|
|
174
174
|
code: ResultCode.FailParams,
|
package/lib/app/message-api.d.ts
CHANGED
|
@@ -7,12 +7,12 @@ export declare class MessageDirect {
|
|
|
7
7
|
static create(): MessageDirect;
|
|
8
8
|
sendToChannel(params: {
|
|
9
9
|
SpaceId: string;
|
|
10
|
-
format: Format;
|
|
10
|
+
format: Format | DataEnums[];
|
|
11
11
|
replyId?: string;
|
|
12
12
|
}): Promise<Result[]>;
|
|
13
13
|
sendToUser(params: {
|
|
14
14
|
OpenID: string;
|
|
15
|
-
format: Format;
|
|
15
|
+
format: Format | DataEnums[];
|
|
16
16
|
}): Promise<Result[]>;
|
|
17
17
|
}
|
|
18
18
|
export declare const sendToChannel: (SpaceId: string, data: DataEnums[]) => Promise<Result[]>;
|