@wooksjs/event-core 0.4.9 → 0.4.11
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 +182 -180
- package/dist/index.mjs +182 -180
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -3,196 +3,198 @@
|
|
|
3
3
|
var crypto = require('crypto');
|
|
4
4
|
var logger = require('@prostojs/logger');
|
|
5
5
|
|
|
6
|
-
function attachHook(target, opts, name) {
|
|
7
|
-
Object.defineProperty(target, name || 'value', {
|
|
8
|
-
get: opts.get,
|
|
9
|
-
set: opts.set,
|
|
10
|
-
});
|
|
11
|
-
return target;
|
|
6
|
+
function attachHook(target, opts, name) {
|
|
7
|
+
Object.defineProperty(target, name || 'value', {
|
|
8
|
+
get: opts.get,
|
|
9
|
+
set: opts.set,
|
|
10
|
+
});
|
|
11
|
+
return target;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
let currentContext = null;
|
|
15
|
-
/**
|
|
16
|
-
* Create a new event context
|
|
17
|
-
*
|
|
18
|
-
* @param data
|
|
19
|
-
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
20
|
-
*/
|
|
21
|
-
function createEventContext(data) {
|
|
22
|
-
const newContext =
|
|
23
|
-
currentContext = newContext;
|
|
24
|
-
return _getCtxHelpers(newContext);
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Use existing event context
|
|
28
|
-
*
|
|
29
|
-
* !Must be called syncronously while context is reachable
|
|
30
|
-
*
|
|
31
|
-
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
32
|
-
*/
|
|
33
|
-
function useEventContext(expectedTypes) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
.
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
* @
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
* @
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
* @param
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
};
|
|
14
|
+
let currentContext = null;
|
|
15
|
+
/**
|
|
16
|
+
* Create a new event context
|
|
17
|
+
*
|
|
18
|
+
* @param data
|
|
19
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
20
|
+
*/
|
|
21
|
+
function createEventContext(data) {
|
|
22
|
+
const newContext = { ...data };
|
|
23
|
+
currentContext = newContext;
|
|
24
|
+
return _getCtxHelpers(newContext);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Use existing event context
|
|
28
|
+
*
|
|
29
|
+
* !Must be called syncronously while context is reachable
|
|
30
|
+
*
|
|
31
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
32
|
+
*/
|
|
33
|
+
function useEventContext(expectedTypes) {
|
|
34
|
+
if (!currentContext) {
|
|
35
|
+
throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
36
|
+
}
|
|
37
|
+
const cc = currentContext;
|
|
38
|
+
if (expectedTypes || typeof expectedTypes === 'string') {
|
|
39
|
+
const type = cc.event?.type;
|
|
40
|
+
const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
|
|
41
|
+
if (!types.includes(type))
|
|
42
|
+
new Error(`Event context type mismatch: expected ${types
|
|
43
|
+
.map((t) => `"${t}"`)
|
|
44
|
+
.join(', ')}, received "${type}"`);
|
|
45
|
+
}
|
|
46
|
+
return _getCtxHelpers(cc);
|
|
47
|
+
}
|
|
48
|
+
function _getCtxHelpers(cc) {
|
|
49
|
+
/**
|
|
50
|
+
* Hook to an event store property
|
|
51
|
+
*
|
|
52
|
+
* @param key store property key
|
|
53
|
+
* @returns a hook { value: <prop value>, hook: (key2: keyof <prop value>) => { value: <nested prop value> }, ... }
|
|
54
|
+
*/
|
|
55
|
+
function store(key) {
|
|
56
|
+
const obj = {
|
|
57
|
+
value: null,
|
|
58
|
+
hook,
|
|
59
|
+
init,
|
|
60
|
+
set: setNested,
|
|
61
|
+
get: getNested,
|
|
62
|
+
has: hasNested,
|
|
63
|
+
del: delNested,
|
|
64
|
+
entries,
|
|
65
|
+
clear,
|
|
66
|
+
};
|
|
67
|
+
attachHook(obj, {
|
|
68
|
+
set: (v) => set(key, v),
|
|
69
|
+
get: () => get(key),
|
|
70
|
+
});
|
|
71
|
+
function init(key2, getter) {
|
|
72
|
+
if (hasNested(key2))
|
|
73
|
+
return getNested(key2);
|
|
74
|
+
return setNested(key2, getter());
|
|
75
|
+
}
|
|
76
|
+
function hook(key2) {
|
|
77
|
+
const obj = {
|
|
78
|
+
value: null,
|
|
79
|
+
isDefined: null,
|
|
80
|
+
};
|
|
81
|
+
attachHook(obj, {
|
|
82
|
+
set: (v) => setNested(key2, v),
|
|
83
|
+
get: () => getNested(key2),
|
|
84
|
+
});
|
|
85
|
+
attachHook(obj, {
|
|
86
|
+
get: () => hasNested(key2),
|
|
87
|
+
}, 'isDefined');
|
|
88
|
+
return obj;
|
|
89
|
+
}
|
|
90
|
+
function setNested(key2, v) {
|
|
91
|
+
if (typeof obj.value === 'undefined') {
|
|
92
|
+
obj.value = {};
|
|
93
|
+
}
|
|
94
|
+
obj.value[key2] = v;
|
|
95
|
+
return v;
|
|
96
|
+
}
|
|
97
|
+
function delNested(key2) {
|
|
98
|
+
setNested(key2, undefined);
|
|
99
|
+
}
|
|
100
|
+
function getNested(key2) {
|
|
101
|
+
return (obj.value || {})[key2];
|
|
102
|
+
}
|
|
103
|
+
function hasNested(key2) {
|
|
104
|
+
return typeof (obj.value || {})[key2] !== 'undefined';
|
|
105
|
+
}
|
|
106
|
+
function entries() {
|
|
107
|
+
return Object.entries(obj.value || {});
|
|
108
|
+
}
|
|
109
|
+
function clear() {
|
|
110
|
+
obj.value = {};
|
|
111
|
+
}
|
|
112
|
+
return obj;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Get event context object
|
|
116
|
+
*
|
|
117
|
+
* @returns whole context object
|
|
118
|
+
*/
|
|
119
|
+
function getCtx() {
|
|
120
|
+
return cc;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Get value of event store property
|
|
124
|
+
*
|
|
125
|
+
* @param key property name
|
|
126
|
+
* @returns value of property by name
|
|
127
|
+
*/
|
|
128
|
+
function get(key) {
|
|
129
|
+
return getCtx()[key];
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Set value of event store property
|
|
133
|
+
*
|
|
134
|
+
* @param key property name
|
|
135
|
+
* @param v property value
|
|
136
|
+
*/
|
|
137
|
+
function set(key, v) {
|
|
138
|
+
getCtx()[key] = v;
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
getCtx,
|
|
142
|
+
restoreCtx: () => (currentContext = cc),
|
|
143
|
+
clearCtx: () => cc === currentContext ? (currentContext = null) : null,
|
|
144
|
+
store,
|
|
145
|
+
getStore: get,
|
|
146
|
+
setStore: set,
|
|
147
|
+
};
|
|
149
148
|
}
|
|
150
149
|
|
|
151
|
-
function useRouteParams() {
|
|
152
|
-
const { store } = useEventContext();
|
|
153
|
-
const params = (store('routeParams').value || {});
|
|
154
|
-
function get(name) {
|
|
155
|
-
return params[name];
|
|
156
|
-
}
|
|
157
|
-
return {
|
|
158
|
-
params,
|
|
159
|
-
get,
|
|
160
|
-
};
|
|
150
|
+
function useRouteParams() {
|
|
151
|
+
const { store } = useEventContext();
|
|
152
|
+
const params = (store('routeParams').value || {});
|
|
153
|
+
function get(name) {
|
|
154
|
+
return params[name];
|
|
155
|
+
}
|
|
156
|
+
return {
|
|
157
|
+
params,
|
|
158
|
+
get,
|
|
159
|
+
};
|
|
161
160
|
}
|
|
162
161
|
|
|
163
|
-
function useEventId() {
|
|
164
|
-
const { store } = useEventContext();
|
|
165
|
-
const { init } = store('event');
|
|
166
|
-
const getId = () => init('id', () => crypto.randomUUID());
|
|
167
|
-
return { getId };
|
|
162
|
+
function useEventId() {
|
|
163
|
+
const { store } = useEventContext();
|
|
164
|
+
const { init } = store('event');
|
|
165
|
+
const getId = () => init('id', () => crypto.randomUUID());
|
|
166
|
+
return { getId };
|
|
168
167
|
}
|
|
169
168
|
|
|
170
|
-
class EventLogger extends logger.ProstoLogger {
|
|
171
|
-
constructor(eventId, opts) {
|
|
172
|
-
const _opts = opts || {
|
|
173
|
-
level: 4,
|
|
174
|
-
};
|
|
175
|
-
if (!_opts.mapper) {
|
|
176
|
-
_opts.mapper = (msg) => (
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
169
|
+
class EventLogger extends logger.ProstoLogger {
|
|
170
|
+
constructor(eventId, opts) {
|
|
171
|
+
const _opts = opts || {
|
|
172
|
+
level: 4,
|
|
173
|
+
};
|
|
174
|
+
if (!_opts.mapper) {
|
|
175
|
+
_opts.mapper = (msg) => ({
|
|
176
|
+
...msg,
|
|
177
|
+
eventId,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
if (!_opts.transports) {
|
|
181
|
+
_opts.transports = [
|
|
182
|
+
logger.createConsoleTransort({
|
|
183
|
+
format: logger.coloredConsole,
|
|
184
|
+
}),
|
|
185
|
+
];
|
|
186
|
+
}
|
|
187
|
+
super(_opts, opts?.topic || 'event');
|
|
188
|
+
}
|
|
187
189
|
}
|
|
188
190
|
|
|
189
|
-
function useEventLogger(topic) {
|
|
190
|
-
const { getId } = useEventId();
|
|
191
|
-
const { store, getCtx } = useEventContext();
|
|
192
|
-
const { init } = store('event');
|
|
193
|
-
const ctx = getCtx();
|
|
194
|
-
const get = () => init('logger', () =>
|
|
195
|
-
return topic ? get().createTopic(topic) : get();
|
|
191
|
+
function useEventLogger(topic) {
|
|
192
|
+
const { getId } = useEventId();
|
|
193
|
+
const { store, getCtx } = useEventContext();
|
|
194
|
+
const { init } = store('event');
|
|
195
|
+
const ctx = getCtx();
|
|
196
|
+
const get = () => init('logger', () => new EventLogger(getId(), ctx.options?.eventLogger));
|
|
197
|
+
return topic ? get().createTopic(topic) : get();
|
|
196
198
|
}
|
|
197
199
|
|
|
198
200
|
exports.EventLogger = EventLogger;
|
package/dist/index.mjs
CHANGED
|
@@ -1,196 +1,198 @@
|
|
|
1
1
|
import { randomUUID } from 'crypto';
|
|
2
2
|
import { ProstoLogger, createConsoleTransort, coloredConsole } from '@prostojs/logger';
|
|
3
3
|
|
|
4
|
-
function attachHook(target, opts, name) {
|
|
5
|
-
Object.defineProperty(target, name || 'value', {
|
|
6
|
-
get: opts.get,
|
|
7
|
-
set: opts.set,
|
|
8
|
-
});
|
|
9
|
-
return target;
|
|
4
|
+
function attachHook(target, opts, name) {
|
|
5
|
+
Object.defineProperty(target, name || 'value', {
|
|
6
|
+
get: opts.get,
|
|
7
|
+
set: opts.set,
|
|
8
|
+
});
|
|
9
|
+
return target;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
let currentContext = null;
|
|
13
|
-
/**
|
|
14
|
-
* Create a new event context
|
|
15
|
-
*
|
|
16
|
-
* @param data
|
|
17
|
-
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
18
|
-
*/
|
|
19
|
-
function createEventContext(data) {
|
|
20
|
-
const newContext =
|
|
21
|
-
currentContext = newContext;
|
|
22
|
-
return _getCtxHelpers(newContext);
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Use existing event context
|
|
26
|
-
*
|
|
27
|
-
* !Must be called syncronously while context is reachable
|
|
28
|
-
*
|
|
29
|
-
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
30
|
-
*/
|
|
31
|
-
function useEventContext(expectedTypes) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* @
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
* @
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
* @param
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
};
|
|
12
|
+
let currentContext = null;
|
|
13
|
+
/**
|
|
14
|
+
* Create a new event context
|
|
15
|
+
*
|
|
16
|
+
* @param data
|
|
17
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
18
|
+
*/
|
|
19
|
+
function createEventContext(data) {
|
|
20
|
+
const newContext = { ...data };
|
|
21
|
+
currentContext = newContext;
|
|
22
|
+
return _getCtxHelpers(newContext);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Use existing event context
|
|
26
|
+
*
|
|
27
|
+
* !Must be called syncronously while context is reachable
|
|
28
|
+
*
|
|
29
|
+
* @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
|
|
30
|
+
*/
|
|
31
|
+
function useEventContext(expectedTypes) {
|
|
32
|
+
if (!currentContext) {
|
|
33
|
+
throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
34
|
+
}
|
|
35
|
+
const cc = currentContext;
|
|
36
|
+
if (expectedTypes || typeof expectedTypes === 'string') {
|
|
37
|
+
const type = cc.event?.type;
|
|
38
|
+
const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
|
|
39
|
+
if (!types.includes(type))
|
|
40
|
+
new Error(`Event context type mismatch: expected ${types
|
|
41
|
+
.map((t) => `"${t}"`)
|
|
42
|
+
.join(', ')}, received "${type}"`);
|
|
43
|
+
}
|
|
44
|
+
return _getCtxHelpers(cc);
|
|
45
|
+
}
|
|
46
|
+
function _getCtxHelpers(cc) {
|
|
47
|
+
/**
|
|
48
|
+
* Hook to an event store property
|
|
49
|
+
*
|
|
50
|
+
* @param key store property key
|
|
51
|
+
* @returns a hook { value: <prop value>, hook: (key2: keyof <prop value>) => { value: <nested prop value> }, ... }
|
|
52
|
+
*/
|
|
53
|
+
function store(key) {
|
|
54
|
+
const obj = {
|
|
55
|
+
value: null,
|
|
56
|
+
hook,
|
|
57
|
+
init,
|
|
58
|
+
set: setNested,
|
|
59
|
+
get: getNested,
|
|
60
|
+
has: hasNested,
|
|
61
|
+
del: delNested,
|
|
62
|
+
entries,
|
|
63
|
+
clear,
|
|
64
|
+
};
|
|
65
|
+
attachHook(obj, {
|
|
66
|
+
set: (v) => set(key, v),
|
|
67
|
+
get: () => get(key),
|
|
68
|
+
});
|
|
69
|
+
function init(key2, getter) {
|
|
70
|
+
if (hasNested(key2))
|
|
71
|
+
return getNested(key2);
|
|
72
|
+
return setNested(key2, getter());
|
|
73
|
+
}
|
|
74
|
+
function hook(key2) {
|
|
75
|
+
const obj = {
|
|
76
|
+
value: null,
|
|
77
|
+
isDefined: null,
|
|
78
|
+
};
|
|
79
|
+
attachHook(obj, {
|
|
80
|
+
set: (v) => setNested(key2, v),
|
|
81
|
+
get: () => getNested(key2),
|
|
82
|
+
});
|
|
83
|
+
attachHook(obj, {
|
|
84
|
+
get: () => hasNested(key2),
|
|
85
|
+
}, 'isDefined');
|
|
86
|
+
return obj;
|
|
87
|
+
}
|
|
88
|
+
function setNested(key2, v) {
|
|
89
|
+
if (typeof obj.value === 'undefined') {
|
|
90
|
+
obj.value = {};
|
|
91
|
+
}
|
|
92
|
+
obj.value[key2] = v;
|
|
93
|
+
return v;
|
|
94
|
+
}
|
|
95
|
+
function delNested(key2) {
|
|
96
|
+
setNested(key2, undefined);
|
|
97
|
+
}
|
|
98
|
+
function getNested(key2) {
|
|
99
|
+
return (obj.value || {})[key2];
|
|
100
|
+
}
|
|
101
|
+
function hasNested(key2) {
|
|
102
|
+
return typeof (obj.value || {})[key2] !== 'undefined';
|
|
103
|
+
}
|
|
104
|
+
function entries() {
|
|
105
|
+
return Object.entries(obj.value || {});
|
|
106
|
+
}
|
|
107
|
+
function clear() {
|
|
108
|
+
obj.value = {};
|
|
109
|
+
}
|
|
110
|
+
return obj;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Get event context object
|
|
114
|
+
*
|
|
115
|
+
* @returns whole context object
|
|
116
|
+
*/
|
|
117
|
+
function getCtx() {
|
|
118
|
+
return cc;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get value of event store property
|
|
122
|
+
*
|
|
123
|
+
* @param key property name
|
|
124
|
+
* @returns value of property by name
|
|
125
|
+
*/
|
|
126
|
+
function get(key) {
|
|
127
|
+
return getCtx()[key];
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Set value of event store property
|
|
131
|
+
*
|
|
132
|
+
* @param key property name
|
|
133
|
+
* @param v property value
|
|
134
|
+
*/
|
|
135
|
+
function set(key, v) {
|
|
136
|
+
getCtx()[key] = v;
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
getCtx,
|
|
140
|
+
restoreCtx: () => (currentContext = cc),
|
|
141
|
+
clearCtx: () => cc === currentContext ? (currentContext = null) : null,
|
|
142
|
+
store,
|
|
143
|
+
getStore: get,
|
|
144
|
+
setStore: set,
|
|
145
|
+
};
|
|
147
146
|
}
|
|
148
147
|
|
|
149
|
-
function useRouteParams() {
|
|
150
|
-
const { store } = useEventContext();
|
|
151
|
-
const params = (store('routeParams').value || {});
|
|
152
|
-
function get(name) {
|
|
153
|
-
return params[name];
|
|
154
|
-
}
|
|
155
|
-
return {
|
|
156
|
-
params,
|
|
157
|
-
get,
|
|
158
|
-
};
|
|
148
|
+
function useRouteParams() {
|
|
149
|
+
const { store } = useEventContext();
|
|
150
|
+
const params = (store('routeParams').value || {});
|
|
151
|
+
function get(name) {
|
|
152
|
+
return params[name];
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
params,
|
|
156
|
+
get,
|
|
157
|
+
};
|
|
159
158
|
}
|
|
160
159
|
|
|
161
|
-
function useEventId() {
|
|
162
|
-
const { store } = useEventContext();
|
|
163
|
-
const { init } = store('event');
|
|
164
|
-
const getId = () => init('id', () => randomUUID());
|
|
165
|
-
return { getId };
|
|
160
|
+
function useEventId() {
|
|
161
|
+
const { store } = useEventContext();
|
|
162
|
+
const { init } = store('event');
|
|
163
|
+
const getId = () => init('id', () => randomUUID());
|
|
164
|
+
return { getId };
|
|
166
165
|
}
|
|
167
166
|
|
|
168
|
-
class EventLogger extends ProstoLogger {
|
|
169
|
-
constructor(eventId, opts) {
|
|
170
|
-
const _opts = opts || {
|
|
171
|
-
level: 4,
|
|
172
|
-
};
|
|
173
|
-
if (!_opts.mapper) {
|
|
174
|
-
_opts.mapper = (msg) => (
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
167
|
+
class EventLogger extends ProstoLogger {
|
|
168
|
+
constructor(eventId, opts) {
|
|
169
|
+
const _opts = opts || {
|
|
170
|
+
level: 4,
|
|
171
|
+
};
|
|
172
|
+
if (!_opts.mapper) {
|
|
173
|
+
_opts.mapper = (msg) => ({
|
|
174
|
+
...msg,
|
|
175
|
+
eventId,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
if (!_opts.transports) {
|
|
179
|
+
_opts.transports = [
|
|
180
|
+
createConsoleTransort({
|
|
181
|
+
format: coloredConsole,
|
|
182
|
+
}),
|
|
183
|
+
];
|
|
184
|
+
}
|
|
185
|
+
super(_opts, opts?.topic || 'event');
|
|
186
|
+
}
|
|
185
187
|
}
|
|
186
188
|
|
|
187
|
-
function useEventLogger(topic) {
|
|
188
|
-
const { getId } = useEventId();
|
|
189
|
-
const { store, getCtx } = useEventContext();
|
|
190
|
-
const { init } = store('event');
|
|
191
|
-
const ctx = getCtx();
|
|
192
|
-
const get = () => init('logger', () =>
|
|
193
|
-
return topic ? get().createTopic(topic) : get();
|
|
189
|
+
function useEventLogger(topic) {
|
|
190
|
+
const { getId } = useEventId();
|
|
191
|
+
const { store, getCtx } = useEventContext();
|
|
192
|
+
const { init } = store('event');
|
|
193
|
+
const ctx = getCtx();
|
|
194
|
+
const get = () => init('logger', () => new EventLogger(getId(), ctx.options?.eventLogger));
|
|
195
|
+
return topic ? get().createTopic(topic) : get();
|
|
194
196
|
}
|
|
195
197
|
|
|
196
198
|
export { EventLogger, attachHook, createEventContext, useEventContext, useEventId, useEventLogger, useRouteParams };
|