bakit 1.0.0-beta.4 → 1.0.0-beta.5
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/dist/index.d.ts +19 -25
- package/dist/index.js +73 -18
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -18,52 +18,45 @@ declare class StateBox {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
type EventsLike = Record<keyof unknown, unknown[]>;
|
|
21
|
-
type EventKey<E extends EventsLike> = keyof E;
|
|
22
21
|
declare enum ListenerHookExecutionState {
|
|
23
22
|
Main = "main",
|
|
24
23
|
Pre = "pre",
|
|
25
24
|
Post = "post",
|
|
26
25
|
Error = "error"
|
|
27
26
|
}
|
|
28
|
-
type MainListenerHookMethod<
|
|
29
|
-
type ErrorListenerHookMethod<
|
|
30
|
-
interface
|
|
31
|
-
state: ListenerHookExecutionState
|
|
32
|
-
method: MainListenerHookMethod<E
|
|
27
|
+
type MainListenerHookMethod<Args extends unknown[]> = (...args: Args) => Awaitable<void>;
|
|
28
|
+
type ErrorListenerHookMethod<Args extends unknown[]> = (error: unknown, ...args: Args) => Awaitable<void>;
|
|
29
|
+
interface ListenerHook<E extends EventsLike, K extends keyof E> {
|
|
30
|
+
state: ListenerHookExecutionState;
|
|
31
|
+
method: MainListenerHookMethod<E[K] & unknown[]> | ErrorListenerHookMethod<E[K] & unknown[]>;
|
|
33
32
|
entry: ListenerEntry<E, K>;
|
|
34
33
|
}
|
|
35
|
-
interface
|
|
36
|
-
state: ListenerHookExecutionState.Error;
|
|
37
|
-
method: ErrorListenerHookMethod<E, K>;
|
|
38
|
-
entry: ListenerEntry<E, K>;
|
|
39
|
-
}
|
|
40
|
-
type ListenerHook<E extends EventsLike, K extends EventKey<E>> = MainListenerHook<E, K> | ErrorListenerHook<E, K>;
|
|
41
|
-
interface ListenerEntryOptions<E extends EventsLike, K extends EventKey<E>> {
|
|
34
|
+
interface ListenerEntryOptions<E extends EventsLike, K extends keyof E> {
|
|
42
35
|
name: K;
|
|
43
36
|
once: boolean;
|
|
44
37
|
emitter?: EventEmitter;
|
|
45
38
|
}
|
|
46
|
-
declare class ListenerEntry<E extends EventsLike, K extends
|
|
39
|
+
declare class ListenerEntry<E extends EventsLike, K extends keyof E> {
|
|
47
40
|
options: ListenerEntryOptions<E, K>;
|
|
48
41
|
static hooksKey: symbol;
|
|
49
42
|
private static cache;
|
|
50
|
-
main: <T extends MainListenerHookMethod<E
|
|
51
|
-
pre: <T extends MainListenerHookMethod<E
|
|
52
|
-
post: <T extends MainListenerHookMethod<E
|
|
53
|
-
error: <T extends ErrorListenerHookMethod<E
|
|
43
|
+
main: <T extends MainListenerHookMethod<E[K] & unknown[]>>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
|
|
44
|
+
pre: <T extends MainListenerHookMethod<E[K] & unknown[]>>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
|
|
45
|
+
post: <T extends MainListenerHookMethod<E[K] & unknown[]>>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
|
|
46
|
+
error: <T extends ErrorListenerHookMethod<E[K] & unknown[]>>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
|
|
54
47
|
constructor(options: ListenerEntryOptions<E, K>);
|
|
55
|
-
static getHooks<E extends EventsLike, K extends
|
|
56
|
-
static getHooks<E extends EventsLike, K extends EventKey<E>>(constructor: ListenerConstructor, init: true): ListenerHook<E, K>[];
|
|
48
|
+
static getHooks<E extends EventsLike, K extends keyof E>(constructor: ListenerConstructor, init?: boolean): ListenerHook<E, K>[];
|
|
57
49
|
private static createMainHookDecorator;
|
|
58
50
|
private static createErrorHookDecorator;
|
|
59
51
|
private static addHook;
|
|
60
52
|
}
|
|
61
53
|
|
|
62
54
|
type ListenerConstructor = new (...args: unknown[]) => object;
|
|
63
|
-
type CreateListenerOptions<E extends EventsLike, K extends
|
|
64
|
-
declare function ListenerFactory<E extends EventsLike = ClientEvents, K extends
|
|
65
|
-
declare
|
|
66
|
-
declare function
|
|
55
|
+
type CreateListenerOptions<E extends EventsLike, K extends keyof E> = SetOptional<ListenerEntryOptions<E, K>, "once"> | K;
|
|
56
|
+
declare function ListenerFactory<E extends EventsLike = ClientEvents, K extends keyof E = keyof E>(options: CreateListenerOptions<E, K>): ListenerEntry<E, K>;
|
|
57
|
+
declare const LISTENER_ENTRY_KEY: unique symbol;
|
|
58
|
+
declare function use<E extends EventsLike = ClientEvents>(listener: ListenerEntry<E, keyof E>): (target: ListenerConstructor) => void;
|
|
59
|
+
declare function getEntry<E extends EventsLike = ClientEvents, K extends keyof E = keyof E>(constructor: ListenerConstructor): ListenerEntry<E, K> | undefined;
|
|
67
60
|
declare const Listener: typeof ListenerFactory & {
|
|
68
61
|
use: typeof use;
|
|
69
62
|
getEntry: typeof getEntry;
|
|
@@ -76,10 +69,11 @@ declare function setClient(newClient: BakitClient): void;
|
|
|
76
69
|
declare const ListenerRegistry: {
|
|
77
70
|
constructors: Set<ListenerConstructor>;
|
|
78
71
|
instances: WeakMap<ListenerConstructor, object>;
|
|
72
|
+
executors: WeakMap<object, (...args: unknown[]) => Promise<void>>;
|
|
79
73
|
add: typeof add;
|
|
80
74
|
setClient: typeof setClient;
|
|
81
75
|
remove: typeof remove;
|
|
82
76
|
removeAll: typeof removeAll;
|
|
83
77
|
};
|
|
84
78
|
|
|
85
|
-
export { BakitClient, type CreateListenerOptions, type
|
|
79
|
+
export { BakitClient, type CreateListenerOptions, type ErrorListenerHookMethod, type EventsLike, LISTENER_ENTRY_KEY, Listener, type ListenerConstructor, ListenerEntry, type ListenerEntryOptions, ListenerFactory, type ListenerHook, ListenerHookExecutionState, ListenerRegistry, type MainListenerHookMethod, StateBox, type States, extractId };
|
package/dist/index.js
CHANGED
|
@@ -831,15 +831,19 @@ var ListenerEntry = class _ListenerEntry {
|
|
|
831
831
|
post = _ListenerEntry.createMainHookDecorator("post" /* Post */, this);
|
|
832
832
|
error = _ListenerEntry.createErrorHookDecorator("error" /* Error */, this);
|
|
833
833
|
static getHooks(constructor, init = false) {
|
|
834
|
-
|
|
834
|
+
const { cache: cache2 } = this;
|
|
835
|
+
let hooks = cache2.get(constructor) ?? Reflect.getMetadata(this.hooksKey, constructor);
|
|
835
836
|
if (!hooks) {
|
|
836
837
|
hooks = [];
|
|
837
838
|
if (init) {
|
|
838
839
|
Reflect.defineMetadata(this.hooksKey, hooks, constructor);
|
|
839
|
-
this.cache.set(
|
|
840
|
+
this.cache.set(
|
|
841
|
+
constructor,
|
|
842
|
+
hooks
|
|
843
|
+
);
|
|
840
844
|
}
|
|
841
845
|
}
|
|
842
|
-
return init ? hooks :
|
|
846
|
+
return init ? hooks : [...hooks];
|
|
843
847
|
}
|
|
844
848
|
static createMainHookDecorator(state, entry) {
|
|
845
849
|
return (target, _key, descriptor) => {
|
|
@@ -873,23 +877,17 @@ var ListenerEntry = class _ListenerEntry {
|
|
|
873
877
|
|
|
874
878
|
// src/listener/Listener.ts
|
|
875
879
|
function ListenerFactory(options) {
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
name: options,
|
|
879
|
-
once: false
|
|
880
|
-
};
|
|
881
|
-
}
|
|
882
|
-
options.once = Boolean(options.once);
|
|
883
|
-
return new ListenerEntry(options);
|
|
880
|
+
const normalizedOptions = typeof options === "object" ? { once: false, ...options } : { name: options, once: false };
|
|
881
|
+
return new ListenerEntry(normalizedOptions);
|
|
884
882
|
}
|
|
885
|
-
var
|
|
883
|
+
var LISTENER_ENTRY_KEY = Symbol("entry");
|
|
886
884
|
function use2(listener) {
|
|
887
885
|
return (target) => {
|
|
888
|
-
Reflect.defineMetadata(
|
|
886
|
+
Reflect.defineMetadata(LISTENER_ENTRY_KEY, listener, target);
|
|
889
887
|
};
|
|
890
888
|
}
|
|
891
889
|
function getEntry(constructor) {
|
|
892
|
-
return Reflect.getMetadata(
|
|
890
|
+
return Reflect.getMetadata(LISTENER_ENTRY_KEY, constructor);
|
|
893
891
|
}
|
|
894
892
|
var Listener = Object.assign(ListenerFactory, {
|
|
895
893
|
use: use2,
|
|
@@ -900,6 +898,7 @@ var Listener = Object.assign(ListenerFactory, {
|
|
|
900
898
|
var client;
|
|
901
899
|
var constructors = /* @__PURE__ */ new Set();
|
|
902
900
|
var instances = /* @__PURE__ */ new WeakMap();
|
|
901
|
+
var executors = /* @__PURE__ */ new WeakMap();
|
|
903
902
|
function add(constructor) {
|
|
904
903
|
const entry = Listener.getEntry(constructor);
|
|
905
904
|
if (!entry) {
|
|
@@ -912,8 +911,14 @@ function add(constructor) {
|
|
|
912
911
|
}
|
|
913
912
|
options.emitter = client;
|
|
914
913
|
}
|
|
914
|
+
const instance = new constructor();
|
|
915
915
|
constructors.add(constructor);
|
|
916
|
-
instances.set(constructor,
|
|
916
|
+
instances.set(constructor, instance);
|
|
917
|
+
const executor = createExecutor(constructor, instance);
|
|
918
|
+
executors.set(instance, executor);
|
|
919
|
+
options.emitter[options.once ? "once" : "on"](options.name, (...args) => {
|
|
920
|
+
void executor(...args);
|
|
921
|
+
});
|
|
917
922
|
}
|
|
918
923
|
function remove(constructor) {
|
|
919
924
|
const entry = Listener.getEntry(constructor);
|
|
@@ -921,13 +926,18 @@ function remove(constructor) {
|
|
|
921
926
|
return;
|
|
922
927
|
}
|
|
923
928
|
constructors.delete(constructor);
|
|
929
|
+
const instance = instances.get(constructor);
|
|
930
|
+
if (!instance) {
|
|
931
|
+
return;
|
|
932
|
+
}
|
|
924
933
|
instances.delete(constructor);
|
|
925
|
-
const
|
|
926
|
-
if (!
|
|
934
|
+
const executor = executors.get(instance);
|
|
935
|
+
if (!executor) {
|
|
927
936
|
return;
|
|
928
937
|
}
|
|
929
938
|
const { name, emitter } = entry.options;
|
|
930
|
-
emitter?.removeListener(name,
|
|
939
|
+
emitter?.removeListener(name, executor);
|
|
940
|
+
executors.delete(instance);
|
|
931
941
|
}
|
|
932
942
|
function removeAll() {
|
|
933
943
|
for (const constructor of constructors) {
|
|
@@ -937,9 +947,53 @@ function removeAll() {
|
|
|
937
947
|
function setClient(newClient) {
|
|
938
948
|
client = newClient;
|
|
939
949
|
}
|
|
950
|
+
function createExecutor(constructor, instance) {
|
|
951
|
+
const entry = Listener.getEntry(constructor);
|
|
952
|
+
if (!entry) {
|
|
953
|
+
throw new Error("Missing listener entry");
|
|
954
|
+
}
|
|
955
|
+
const hooks = ListenerEntry.getHooks(constructor).filter((hook) => hook.entry === entry);
|
|
956
|
+
const hookGroup = makeHookGroup(hooks);
|
|
957
|
+
return async function(...args) {
|
|
958
|
+
if (!hookGroup.main) {
|
|
959
|
+
return;
|
|
960
|
+
}
|
|
961
|
+
try {
|
|
962
|
+
if (hookGroup.pre) {
|
|
963
|
+
const preMethod = hookGroup.pre.method;
|
|
964
|
+
await preMethod.call(instance, ...args);
|
|
965
|
+
}
|
|
966
|
+
await hookGroup.main.method.call(instance, ...args);
|
|
967
|
+
if (hookGroup.post) {
|
|
968
|
+
const postMethod = hookGroup.post.method;
|
|
969
|
+
await postMethod.call(instance, ...args);
|
|
970
|
+
}
|
|
971
|
+
} catch (error) {
|
|
972
|
+
if (hookGroup.error) {
|
|
973
|
+
const errorMethod = hookGroup.error.method;
|
|
974
|
+
await errorMethod.call(instance, error, ...args);
|
|
975
|
+
} else {
|
|
976
|
+
throw error;
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
};
|
|
980
|
+
}
|
|
981
|
+
function makeHookGroup(hooks) {
|
|
982
|
+
const hooksByType = {
|
|
983
|
+
["pre" /* Pre */]: void 0,
|
|
984
|
+
["main" /* Main */]: void 0,
|
|
985
|
+
["post" /* Post */]: void 0,
|
|
986
|
+
["error" /* Error */]: void 0
|
|
987
|
+
};
|
|
988
|
+
for (const hook of hooks) {
|
|
989
|
+
hooksByType[hook.state] = hook;
|
|
990
|
+
}
|
|
991
|
+
return hooksByType;
|
|
992
|
+
}
|
|
940
993
|
var ListenerRegistry = {
|
|
941
994
|
constructors,
|
|
942
995
|
instances,
|
|
996
|
+
executors,
|
|
943
997
|
add,
|
|
944
998
|
setClient,
|
|
945
999
|
remove,
|
|
@@ -1159,6 +1213,7 @@ export {
|
|
|
1159
1213
|
CommandSyntaxError,
|
|
1160
1214
|
CommandSyntaxErrorType,
|
|
1161
1215
|
HOOKS_KEY,
|
|
1216
|
+
LISTENER_ENTRY_KEY,
|
|
1162
1217
|
Listener,
|
|
1163
1218
|
ListenerEntry,
|
|
1164
1219
|
ListenerFactory,
|