@wooksjs/event-core 0.4.30 → 0.4.32
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.cjs +13 -4
- package/dist/index.d.ts +5 -0
- package/dist/index.mjs +13 -4
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -21,14 +21,19 @@ function useEventContext(expectedTypes) {
|
|
|
21
21
|
if (!currentContext) {
|
|
22
22
|
throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
23
23
|
}
|
|
24
|
-
|
|
24
|
+
let cc = currentContext;
|
|
25
25
|
if (expectedTypes || typeof expectedTypes === 'string') {
|
|
26
26
|
const type = cc.event.type;
|
|
27
27
|
const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
|
|
28
28
|
if (!types.includes(type)) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
if (cc.parentCtx?.event.type && types.includes(cc.parentCtx.event.type)) {
|
|
30
|
+
cc = cc.parentCtx;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
throw new Error(`Event context type mismatch: expected ${types
|
|
34
|
+
.map(t => `"${t}"`)
|
|
35
|
+
.join(', ')}, received "${type}"`);
|
|
36
|
+
}
|
|
32
37
|
}
|
|
33
38
|
}
|
|
34
39
|
return _getCtxHelpers(cc);
|
|
@@ -112,6 +117,10 @@ function _getCtxHelpers(cc) {
|
|
|
112
117
|
store,
|
|
113
118
|
getStore: get,
|
|
114
119
|
setStore: set,
|
|
120
|
+
setParentCtx: (parentCtx) => {
|
|
121
|
+
cc.parentCtx = parentCtx;
|
|
122
|
+
},
|
|
123
|
+
restoreParentCtx: () => (currentContext = cc.parentCtx || null),
|
|
115
124
|
};
|
|
116
125
|
}
|
|
117
126
|
|
package/dist/index.d.ts
CHANGED
|
@@ -21,6 +21,7 @@ interface TEventOptions {
|
|
|
21
21
|
interface TGenericContextStore<CustomEventType = TEmpty> {
|
|
22
22
|
event: CustomEventType & TGenericEvent;
|
|
23
23
|
options: TEventOptions;
|
|
24
|
+
parentCtx?: TGenericContextStore;
|
|
24
25
|
routeParams?: Record<string, string | string[]>;
|
|
25
26
|
}
|
|
26
27
|
/**
|
|
@@ -49,6 +50,8 @@ declare function createEventContext<S = TEmpty, EventTypeToCreate = TEmpty>(data
|
|
|
49
50
|
};
|
|
50
51
|
getStore: <K_1 extends keyof S | keyof TGenericContextStore<EventTypeToCreate>>(key: K_1) => (S & TGenericContextStore<EventTypeToCreate>)[K_1];
|
|
51
52
|
setStore: <K_2 extends keyof S | keyof TGenericContextStore<EventTypeToCreate>>(key: K_2, v: (S & TGenericContextStore<EventTypeToCreate>)[K_2]) => void;
|
|
53
|
+
setParentCtx: (parentCtx: unknown) => void;
|
|
54
|
+
restoreParentCtx: () => TGenericContextStore<TEmpty> | null;
|
|
52
55
|
};
|
|
53
56
|
/**
|
|
54
57
|
* Use existing event context
|
|
@@ -77,6 +80,8 @@ declare function useEventContext<S = TEmpty, EventType = TEmpty>(expectedTypes?:
|
|
|
77
80
|
};
|
|
78
81
|
getStore: <K_1 extends keyof S | keyof TGenericContextStore<EventType>>(key: K_1) => (S & TGenericContextStore<EventType>)[K_1];
|
|
79
82
|
setStore: <K_2 extends keyof S | keyof TGenericContextStore<EventType>>(key: K_2, v: (S & TGenericContextStore<EventType>)[K_2]) => void;
|
|
83
|
+
setParentCtx: (parentCtx: unknown) => void;
|
|
84
|
+
restoreParentCtx: () => TGenericContextStore<TEmpty> | null;
|
|
80
85
|
};
|
|
81
86
|
|
|
82
87
|
interface TEventLoggerData {
|
package/dist/index.mjs
CHANGED
|
@@ -19,14 +19,19 @@ function useEventContext(expectedTypes) {
|
|
|
19
19
|
if (!currentContext) {
|
|
20
20
|
throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
21
21
|
}
|
|
22
|
-
|
|
22
|
+
let cc = currentContext;
|
|
23
23
|
if (expectedTypes || typeof expectedTypes === 'string') {
|
|
24
24
|
const type = cc.event.type;
|
|
25
25
|
const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
|
|
26
26
|
if (!types.includes(type)) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
if (cc.parentCtx?.event.type && types.includes(cc.parentCtx.event.type)) {
|
|
28
|
+
cc = cc.parentCtx;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
throw new Error(`Event context type mismatch: expected ${types
|
|
32
|
+
.map(t => `"${t}"`)
|
|
33
|
+
.join(', ')}, received "${type}"`);
|
|
34
|
+
}
|
|
30
35
|
}
|
|
31
36
|
}
|
|
32
37
|
return _getCtxHelpers(cc);
|
|
@@ -110,6 +115,10 @@ function _getCtxHelpers(cc) {
|
|
|
110
115
|
store,
|
|
111
116
|
getStore: get,
|
|
112
117
|
setStore: set,
|
|
118
|
+
setParentCtx: (parentCtx) => {
|
|
119
|
+
cc.parentCtx = parentCtx;
|
|
120
|
+
},
|
|
121
|
+
restoreParentCtx: () => (currentContext = cc.parentCtx || null),
|
|
113
122
|
};
|
|
114
123
|
}
|
|
115
124
|
|