@milkio/stargate 1.0.0-alpha.5 → 1.0.0-alpha.7
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/index.ts +5 -5
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -40,18 +40,18 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
40
40
|
const $fetch = stargateOptions.fetch ?? fetch;
|
|
41
41
|
const $abort = stargateOptions.abort ?? AbortController;
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
type StargateEvents = {
|
|
44
44
|
"milkio:executeBefore": { path: string; options: Mixin<ExecuteOptions, { headers: Record<string, string>; baseUrl: string }> };
|
|
45
45
|
"milkio:fetchBefore": { path: string; options: Mixin<ExecuteOptions, { headers: Record<string, string>; baseUrl: string; body: string }> };
|
|
46
46
|
"milkio:executeError": { path: string; options: Mixin<ExecuteOptions, { headers: Record<string, string>; baseUrl: string }>; error: Partial<Generated["rejectCode"]> };
|
|
47
|
-
}
|
|
47
|
+
};
|
|
48
48
|
|
|
49
49
|
const __initEventManager = () => {
|
|
50
50
|
const handlers = new Map<(event: any) => void, string>();
|
|
51
51
|
const indexed = new Map<string, Set<(event: any) => void>>();
|
|
52
52
|
|
|
53
53
|
const eventManager = {
|
|
54
|
-
on: <Key extends keyof
|
|
54
|
+
on: <Key extends keyof StargateEvents, Handler extends (event: StargateEvents[Key]) => void>(key: Key, handler: Handler) => {
|
|
55
55
|
handlers.set(handler, key as string);
|
|
56
56
|
if (indexed.has(key as string) === false) {
|
|
57
57
|
indexed.set(key as string, new Set());
|
|
@@ -65,13 +65,13 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
|
|
|
65
65
|
set.delete(handler);
|
|
66
66
|
};
|
|
67
67
|
},
|
|
68
|
-
off: <Key extends keyof
|
|
68
|
+
off: <Key extends keyof StargateEvents, Handler extends (event: StargateEvents[Key]) => void>(key: Key, handler: Handler) => {
|
|
69
69
|
const set = indexed.get(key as string);
|
|
70
70
|
if (!set) return;
|
|
71
71
|
handlers.delete(handler);
|
|
72
72
|
set.delete(handler);
|
|
73
73
|
},
|
|
74
|
-
emit: async <Key extends keyof
|
|
74
|
+
emit: async <Key extends keyof StargateEvents, Value extends StargateEvents[Key]>(key: Key, value: Value): Promise<void> => {
|
|
75
75
|
const h = indexed.get(key as string);
|
|
76
76
|
if (h) {
|
|
77
77
|
for (const handler of h) {
|