@wooksjs/event-core 0.4.9 → 0.4.10
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 +180 -180
- package/dist/index.mjs +180 -180
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -3,196 +3,196 @@
|
|
|
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 = Object.assign({}, 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
|
-
var _a;
|
|
35
|
-
if (!currentContext) {
|
|
36
|
-
throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
37
|
-
}
|
|
38
|
-
const cc = currentContext;
|
|
39
|
-
if (expectedTypes || typeof expectedTypes === 'string') {
|
|
40
|
-
const type = (_a = cc.event) === null || _a === void 0 ? void 0 : _a.type;
|
|
41
|
-
const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
|
|
42
|
-
if (!types.includes(type))
|
|
43
|
-
new Error(`Event context type mismatch: expected ${types
|
|
44
|
-
.map((t) => `"${t}"`)
|
|
45
|
-
.join(', ')}, received "${type}"`);
|
|
46
|
-
}
|
|
47
|
-
return _getCtxHelpers(cc);
|
|
48
|
-
}
|
|
49
|
-
function _getCtxHelpers(cc) {
|
|
50
|
-
/**
|
|
51
|
-
* Hook to an event store property
|
|
52
|
-
*
|
|
53
|
-
* @param key store property key
|
|
54
|
-
* @returns a hook { value: <prop value>, hook: (key2: keyof <prop value>) => { value: <nested prop value> }, ... }
|
|
55
|
-
*/
|
|
56
|
-
function store(key) {
|
|
57
|
-
const obj = {
|
|
58
|
-
value: null,
|
|
59
|
-
hook,
|
|
60
|
-
init,
|
|
61
|
-
set: setNested,
|
|
62
|
-
get: getNested,
|
|
63
|
-
has: hasNested,
|
|
64
|
-
del: delNested,
|
|
65
|
-
entries,
|
|
66
|
-
clear,
|
|
67
|
-
};
|
|
68
|
-
attachHook(obj, {
|
|
69
|
-
set: (v) => set(key, v),
|
|
70
|
-
get: () => get(key),
|
|
71
|
-
});
|
|
72
|
-
function init(key2, getter) {
|
|
73
|
-
if (hasNested(key2))
|
|
74
|
-
return getNested(key2);
|
|
75
|
-
return setNested(key2, getter());
|
|
76
|
-
}
|
|
77
|
-
function hook(key2) {
|
|
78
|
-
const obj = {
|
|
79
|
-
value: null,
|
|
80
|
-
isDefined: null,
|
|
81
|
-
};
|
|
82
|
-
attachHook(obj, {
|
|
83
|
-
set: (v) => setNested(key2, v),
|
|
84
|
-
get: () => getNested(key2),
|
|
85
|
-
});
|
|
86
|
-
attachHook(obj, {
|
|
87
|
-
get: () => hasNested(key2),
|
|
88
|
-
}, 'isDefined');
|
|
89
|
-
return obj;
|
|
90
|
-
}
|
|
91
|
-
function setNested(key2, v) {
|
|
92
|
-
if (typeof obj.value === 'undefined') {
|
|
93
|
-
obj.value = {};
|
|
94
|
-
}
|
|
95
|
-
obj.value[key2] = v;
|
|
96
|
-
return v;
|
|
97
|
-
}
|
|
98
|
-
function delNested(key2) {
|
|
99
|
-
setNested(key2, undefined);
|
|
100
|
-
}
|
|
101
|
-
function getNested(key2) {
|
|
102
|
-
return (obj.value || {})[key2];
|
|
103
|
-
}
|
|
104
|
-
function hasNested(key2) {
|
|
105
|
-
return typeof (obj.value || {})[key2] !== 'undefined';
|
|
106
|
-
}
|
|
107
|
-
function entries() {
|
|
108
|
-
return Object.entries(obj.value || {});
|
|
109
|
-
}
|
|
110
|
-
function clear() {
|
|
111
|
-
obj.value = {};
|
|
112
|
-
}
|
|
113
|
-
return obj;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Get event context object
|
|
117
|
-
*
|
|
118
|
-
* @returns whole context object
|
|
119
|
-
*/
|
|
120
|
-
function getCtx() {
|
|
121
|
-
return cc;
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Get value of event store property
|
|
125
|
-
*
|
|
126
|
-
* @param key property name
|
|
127
|
-
* @returns value of property by name
|
|
128
|
-
*/
|
|
129
|
-
function get(key) {
|
|
130
|
-
return getCtx()[key];
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Set value of event store property
|
|
134
|
-
*
|
|
135
|
-
* @param key property name
|
|
136
|
-
* @param v property value
|
|
137
|
-
*/
|
|
138
|
-
function set(key, v) {
|
|
139
|
-
getCtx()[key] = v;
|
|
140
|
-
}
|
|
141
|
-
return {
|
|
142
|
-
getCtx,
|
|
143
|
-
restoreCtx: () => (currentContext = cc),
|
|
144
|
-
clearCtx: () => cc === currentContext ? (currentContext = null) : null,
|
|
145
|
-
store,
|
|
146
|
-
getStore: get,
|
|
147
|
-
setStore: set,
|
|
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 = Object.assign({}, 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
|
+
var _a;
|
|
35
|
+
if (!currentContext) {
|
|
36
|
+
throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
37
|
+
}
|
|
38
|
+
const cc = currentContext;
|
|
39
|
+
if (expectedTypes || typeof expectedTypes === 'string') {
|
|
40
|
+
const type = (_a = cc.event) === null || _a === void 0 ? void 0 : _a.type;
|
|
41
|
+
const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
|
|
42
|
+
if (!types.includes(type))
|
|
43
|
+
new Error(`Event context type mismatch: expected ${types
|
|
44
|
+
.map((t) => `"${t}"`)
|
|
45
|
+
.join(', ')}, received "${type}"`);
|
|
46
|
+
}
|
|
47
|
+
return _getCtxHelpers(cc);
|
|
48
|
+
}
|
|
49
|
+
function _getCtxHelpers(cc) {
|
|
50
|
+
/**
|
|
51
|
+
* Hook to an event store property
|
|
52
|
+
*
|
|
53
|
+
* @param key store property key
|
|
54
|
+
* @returns a hook { value: <prop value>, hook: (key2: keyof <prop value>) => { value: <nested prop value> }, ... }
|
|
55
|
+
*/
|
|
56
|
+
function store(key) {
|
|
57
|
+
const obj = {
|
|
58
|
+
value: null,
|
|
59
|
+
hook,
|
|
60
|
+
init,
|
|
61
|
+
set: setNested,
|
|
62
|
+
get: getNested,
|
|
63
|
+
has: hasNested,
|
|
64
|
+
del: delNested,
|
|
65
|
+
entries,
|
|
66
|
+
clear,
|
|
67
|
+
};
|
|
68
|
+
attachHook(obj, {
|
|
69
|
+
set: (v) => set(key, v),
|
|
70
|
+
get: () => get(key),
|
|
71
|
+
});
|
|
72
|
+
function init(key2, getter) {
|
|
73
|
+
if (hasNested(key2))
|
|
74
|
+
return getNested(key2);
|
|
75
|
+
return setNested(key2, getter());
|
|
76
|
+
}
|
|
77
|
+
function hook(key2) {
|
|
78
|
+
const obj = {
|
|
79
|
+
value: null,
|
|
80
|
+
isDefined: null,
|
|
81
|
+
};
|
|
82
|
+
attachHook(obj, {
|
|
83
|
+
set: (v) => setNested(key2, v),
|
|
84
|
+
get: () => getNested(key2),
|
|
85
|
+
});
|
|
86
|
+
attachHook(obj, {
|
|
87
|
+
get: () => hasNested(key2),
|
|
88
|
+
}, 'isDefined');
|
|
89
|
+
return obj;
|
|
90
|
+
}
|
|
91
|
+
function setNested(key2, v) {
|
|
92
|
+
if (typeof obj.value === 'undefined') {
|
|
93
|
+
obj.value = {};
|
|
94
|
+
}
|
|
95
|
+
obj.value[key2] = v;
|
|
96
|
+
return v;
|
|
97
|
+
}
|
|
98
|
+
function delNested(key2) {
|
|
99
|
+
setNested(key2, undefined);
|
|
100
|
+
}
|
|
101
|
+
function getNested(key2) {
|
|
102
|
+
return (obj.value || {})[key2];
|
|
103
|
+
}
|
|
104
|
+
function hasNested(key2) {
|
|
105
|
+
return typeof (obj.value || {})[key2] !== 'undefined';
|
|
106
|
+
}
|
|
107
|
+
function entries() {
|
|
108
|
+
return Object.entries(obj.value || {});
|
|
109
|
+
}
|
|
110
|
+
function clear() {
|
|
111
|
+
obj.value = {};
|
|
112
|
+
}
|
|
113
|
+
return obj;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Get event context object
|
|
117
|
+
*
|
|
118
|
+
* @returns whole context object
|
|
119
|
+
*/
|
|
120
|
+
function getCtx() {
|
|
121
|
+
return cc;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Get value of event store property
|
|
125
|
+
*
|
|
126
|
+
* @param key property name
|
|
127
|
+
* @returns value of property by name
|
|
128
|
+
*/
|
|
129
|
+
function get(key) {
|
|
130
|
+
return getCtx()[key];
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Set value of event store property
|
|
134
|
+
*
|
|
135
|
+
* @param key property name
|
|
136
|
+
* @param v property value
|
|
137
|
+
*/
|
|
138
|
+
function set(key, v) {
|
|
139
|
+
getCtx()[key] = v;
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
getCtx,
|
|
143
|
+
restoreCtx: () => (currentContext = cc),
|
|
144
|
+
clearCtx: () => cc === currentContext ? (currentContext = null) : null,
|
|
145
|
+
store,
|
|
146
|
+
getStore: get,
|
|
147
|
+
setStore: set,
|
|
148
|
+
};
|
|
149
149
|
}
|
|
150
150
|
|
|
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
|
-
};
|
|
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
|
+
};
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
-
function useEventId() {
|
|
164
|
-
const { store } = useEventContext();
|
|
165
|
-
const { init } = store('event');
|
|
166
|
-
const getId = () => init('id', () => crypto.randomUUID());
|
|
167
|
-
return { getId };
|
|
163
|
+
function useEventId() {
|
|
164
|
+
const { store } = useEventContext();
|
|
165
|
+
const { init } = store('event');
|
|
166
|
+
const getId = () => init('id', () => crypto.randomUUID());
|
|
167
|
+
return { getId };
|
|
168
168
|
}
|
|
169
169
|
|
|
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) => (Object.assign(Object.assign({}, msg), { eventId }));
|
|
177
|
-
}
|
|
178
|
-
if (!_opts.transports) {
|
|
179
|
-
_opts.transports = [
|
|
180
|
-
logger.createConsoleTransort({
|
|
181
|
-
format: logger.coloredConsole,
|
|
182
|
-
}),
|
|
183
|
-
];
|
|
184
|
-
}
|
|
185
|
-
super(_opts, (opts === null || opts === void 0 ? void 0 : opts.topic) || 'event');
|
|
186
|
-
}
|
|
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) => (Object.assign(Object.assign({}, msg), { eventId }));
|
|
177
|
+
}
|
|
178
|
+
if (!_opts.transports) {
|
|
179
|
+
_opts.transports = [
|
|
180
|
+
logger.createConsoleTransort({
|
|
181
|
+
format: logger.coloredConsole,
|
|
182
|
+
}),
|
|
183
|
+
];
|
|
184
|
+
}
|
|
185
|
+
super(_opts, (opts === null || opts === void 0 ? void 0 : opts.topic) || 'event');
|
|
186
|
+
}
|
|
187
187
|
}
|
|
188
188
|
|
|
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', () => { var _a; return new EventLogger(getId(), (_a = ctx.options) === null || _a === void 0 ? void 0 : _a.eventLogger); });
|
|
195
|
-
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', () => { var _a; return new EventLogger(getId(), (_a = ctx.options) === null || _a === void 0 ? void 0 : _a.eventLogger); });
|
|
195
|
+
return topic ? get().createTopic(topic) : get();
|
|
196
196
|
}
|
|
197
197
|
|
|
198
198
|
exports.EventLogger = EventLogger;
|
package/dist/index.mjs
CHANGED
|
@@ -1,196 +1,196 @@
|
|
|
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 = Object.assign({}, 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
|
-
var _a;
|
|
33
|
-
if (!currentContext) {
|
|
34
|
-
throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
35
|
-
}
|
|
36
|
-
const cc = currentContext;
|
|
37
|
-
if (expectedTypes || typeof expectedTypes === 'string') {
|
|
38
|
-
const type = (_a = cc.event) === null || _a === void 0 ? void 0 : _a.type;
|
|
39
|
-
const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
|
|
40
|
-
if (!types.includes(type))
|
|
41
|
-
new Error(`Event context type mismatch: expected ${types
|
|
42
|
-
.map((t) => `"${t}"`)
|
|
43
|
-
.join(', ')}, received "${type}"`);
|
|
44
|
-
}
|
|
45
|
-
return _getCtxHelpers(cc);
|
|
46
|
-
}
|
|
47
|
-
function _getCtxHelpers(cc) {
|
|
48
|
-
/**
|
|
49
|
-
* Hook to an event store property
|
|
50
|
-
*
|
|
51
|
-
* @param key store property key
|
|
52
|
-
* @returns a hook { value: <prop value>, hook: (key2: keyof <prop value>) => { value: <nested prop value> }, ... }
|
|
53
|
-
*/
|
|
54
|
-
function store(key) {
|
|
55
|
-
const obj = {
|
|
56
|
-
value: null,
|
|
57
|
-
hook,
|
|
58
|
-
init,
|
|
59
|
-
set: setNested,
|
|
60
|
-
get: getNested,
|
|
61
|
-
has: hasNested,
|
|
62
|
-
del: delNested,
|
|
63
|
-
entries,
|
|
64
|
-
clear,
|
|
65
|
-
};
|
|
66
|
-
attachHook(obj, {
|
|
67
|
-
set: (v) => set(key, v),
|
|
68
|
-
get: () => get(key),
|
|
69
|
-
});
|
|
70
|
-
function init(key2, getter) {
|
|
71
|
-
if (hasNested(key2))
|
|
72
|
-
return getNested(key2);
|
|
73
|
-
return setNested(key2, getter());
|
|
74
|
-
}
|
|
75
|
-
function hook(key2) {
|
|
76
|
-
const obj = {
|
|
77
|
-
value: null,
|
|
78
|
-
isDefined: null,
|
|
79
|
-
};
|
|
80
|
-
attachHook(obj, {
|
|
81
|
-
set: (v) => setNested(key2, v),
|
|
82
|
-
get: () => getNested(key2),
|
|
83
|
-
});
|
|
84
|
-
attachHook(obj, {
|
|
85
|
-
get: () => hasNested(key2),
|
|
86
|
-
}, 'isDefined');
|
|
87
|
-
return obj;
|
|
88
|
-
}
|
|
89
|
-
function setNested(key2, v) {
|
|
90
|
-
if (typeof obj.value === 'undefined') {
|
|
91
|
-
obj.value = {};
|
|
92
|
-
}
|
|
93
|
-
obj.value[key2] = v;
|
|
94
|
-
return v;
|
|
95
|
-
}
|
|
96
|
-
function delNested(key2) {
|
|
97
|
-
setNested(key2, undefined);
|
|
98
|
-
}
|
|
99
|
-
function getNested(key2) {
|
|
100
|
-
return (obj.value || {})[key2];
|
|
101
|
-
}
|
|
102
|
-
function hasNested(key2) {
|
|
103
|
-
return typeof (obj.value || {})[key2] !== 'undefined';
|
|
104
|
-
}
|
|
105
|
-
function entries() {
|
|
106
|
-
return Object.entries(obj.value || {});
|
|
107
|
-
}
|
|
108
|
-
function clear() {
|
|
109
|
-
obj.value = {};
|
|
110
|
-
}
|
|
111
|
-
return obj;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Get event context object
|
|
115
|
-
*
|
|
116
|
-
* @returns whole context object
|
|
117
|
-
*/
|
|
118
|
-
function getCtx() {
|
|
119
|
-
return cc;
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Get value of event store property
|
|
123
|
-
*
|
|
124
|
-
* @param key property name
|
|
125
|
-
* @returns value of property by name
|
|
126
|
-
*/
|
|
127
|
-
function get(key) {
|
|
128
|
-
return getCtx()[key];
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Set value of event store property
|
|
132
|
-
*
|
|
133
|
-
* @param key property name
|
|
134
|
-
* @param v property value
|
|
135
|
-
*/
|
|
136
|
-
function set(key, v) {
|
|
137
|
-
getCtx()[key] = v;
|
|
138
|
-
}
|
|
139
|
-
return {
|
|
140
|
-
getCtx,
|
|
141
|
-
restoreCtx: () => (currentContext = cc),
|
|
142
|
-
clearCtx: () => cc === currentContext ? (currentContext = null) : null,
|
|
143
|
-
store,
|
|
144
|
-
getStore: get,
|
|
145
|
-
setStore: set,
|
|
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 = Object.assign({}, 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
|
+
var _a;
|
|
33
|
+
if (!currentContext) {
|
|
34
|
+
throw new Error('Event context does not exist. Use event context synchronously within the runtime of the event.');
|
|
35
|
+
}
|
|
36
|
+
const cc = currentContext;
|
|
37
|
+
if (expectedTypes || typeof expectedTypes === 'string') {
|
|
38
|
+
const type = (_a = cc.event) === null || _a === void 0 ? void 0 : _a.type;
|
|
39
|
+
const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
|
|
40
|
+
if (!types.includes(type))
|
|
41
|
+
new Error(`Event context type mismatch: expected ${types
|
|
42
|
+
.map((t) => `"${t}"`)
|
|
43
|
+
.join(', ')}, received "${type}"`);
|
|
44
|
+
}
|
|
45
|
+
return _getCtxHelpers(cc);
|
|
46
|
+
}
|
|
47
|
+
function _getCtxHelpers(cc) {
|
|
48
|
+
/**
|
|
49
|
+
* Hook to an event store property
|
|
50
|
+
*
|
|
51
|
+
* @param key store property key
|
|
52
|
+
* @returns a hook { value: <prop value>, hook: (key2: keyof <prop value>) => { value: <nested prop value> }, ... }
|
|
53
|
+
*/
|
|
54
|
+
function store(key) {
|
|
55
|
+
const obj = {
|
|
56
|
+
value: null,
|
|
57
|
+
hook,
|
|
58
|
+
init,
|
|
59
|
+
set: setNested,
|
|
60
|
+
get: getNested,
|
|
61
|
+
has: hasNested,
|
|
62
|
+
del: delNested,
|
|
63
|
+
entries,
|
|
64
|
+
clear,
|
|
65
|
+
};
|
|
66
|
+
attachHook(obj, {
|
|
67
|
+
set: (v) => set(key, v),
|
|
68
|
+
get: () => get(key),
|
|
69
|
+
});
|
|
70
|
+
function init(key2, getter) {
|
|
71
|
+
if (hasNested(key2))
|
|
72
|
+
return getNested(key2);
|
|
73
|
+
return setNested(key2, getter());
|
|
74
|
+
}
|
|
75
|
+
function hook(key2) {
|
|
76
|
+
const obj = {
|
|
77
|
+
value: null,
|
|
78
|
+
isDefined: null,
|
|
79
|
+
};
|
|
80
|
+
attachHook(obj, {
|
|
81
|
+
set: (v) => setNested(key2, v),
|
|
82
|
+
get: () => getNested(key2),
|
|
83
|
+
});
|
|
84
|
+
attachHook(obj, {
|
|
85
|
+
get: () => hasNested(key2),
|
|
86
|
+
}, 'isDefined');
|
|
87
|
+
return obj;
|
|
88
|
+
}
|
|
89
|
+
function setNested(key2, v) {
|
|
90
|
+
if (typeof obj.value === 'undefined') {
|
|
91
|
+
obj.value = {};
|
|
92
|
+
}
|
|
93
|
+
obj.value[key2] = v;
|
|
94
|
+
return v;
|
|
95
|
+
}
|
|
96
|
+
function delNested(key2) {
|
|
97
|
+
setNested(key2, undefined);
|
|
98
|
+
}
|
|
99
|
+
function getNested(key2) {
|
|
100
|
+
return (obj.value || {})[key2];
|
|
101
|
+
}
|
|
102
|
+
function hasNested(key2) {
|
|
103
|
+
return typeof (obj.value || {})[key2] !== 'undefined';
|
|
104
|
+
}
|
|
105
|
+
function entries() {
|
|
106
|
+
return Object.entries(obj.value || {});
|
|
107
|
+
}
|
|
108
|
+
function clear() {
|
|
109
|
+
obj.value = {};
|
|
110
|
+
}
|
|
111
|
+
return obj;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Get event context object
|
|
115
|
+
*
|
|
116
|
+
* @returns whole context object
|
|
117
|
+
*/
|
|
118
|
+
function getCtx() {
|
|
119
|
+
return cc;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Get value of event store property
|
|
123
|
+
*
|
|
124
|
+
* @param key property name
|
|
125
|
+
* @returns value of property by name
|
|
126
|
+
*/
|
|
127
|
+
function get(key) {
|
|
128
|
+
return getCtx()[key];
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Set value of event store property
|
|
132
|
+
*
|
|
133
|
+
* @param key property name
|
|
134
|
+
* @param v property value
|
|
135
|
+
*/
|
|
136
|
+
function set(key, v) {
|
|
137
|
+
getCtx()[key] = v;
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
getCtx,
|
|
141
|
+
restoreCtx: () => (currentContext = cc),
|
|
142
|
+
clearCtx: () => cc === currentContext ? (currentContext = null) : null,
|
|
143
|
+
store,
|
|
144
|
+
getStore: get,
|
|
145
|
+
setStore: set,
|
|
146
|
+
};
|
|
147
147
|
}
|
|
148
148
|
|
|
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
|
-
};
|
|
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
|
+
};
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
-
function useEventId() {
|
|
162
|
-
const { store } = useEventContext();
|
|
163
|
-
const { init } = store('event');
|
|
164
|
-
const getId = () => init('id', () => randomUUID());
|
|
165
|
-
return { getId };
|
|
161
|
+
function useEventId() {
|
|
162
|
+
const { store } = useEventContext();
|
|
163
|
+
const { init } = store('event');
|
|
164
|
+
const getId = () => init('id', () => randomUUID());
|
|
165
|
+
return { getId };
|
|
166
166
|
}
|
|
167
167
|
|
|
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) => (Object.assign(Object.assign({}, msg), { eventId }));
|
|
175
|
-
}
|
|
176
|
-
if (!_opts.transports) {
|
|
177
|
-
_opts.transports = [
|
|
178
|
-
createConsoleTransort({
|
|
179
|
-
format: coloredConsole,
|
|
180
|
-
}),
|
|
181
|
-
];
|
|
182
|
-
}
|
|
183
|
-
super(_opts, (opts === null || opts === void 0 ? void 0 : opts.topic) || 'event');
|
|
184
|
-
}
|
|
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) => (Object.assign(Object.assign({}, msg), { eventId }));
|
|
175
|
+
}
|
|
176
|
+
if (!_opts.transports) {
|
|
177
|
+
_opts.transports = [
|
|
178
|
+
createConsoleTransort({
|
|
179
|
+
format: coloredConsole,
|
|
180
|
+
}),
|
|
181
|
+
];
|
|
182
|
+
}
|
|
183
|
+
super(_opts, (opts === null || opts === void 0 ? void 0 : opts.topic) || 'event');
|
|
184
|
+
}
|
|
185
185
|
}
|
|
186
186
|
|
|
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', () => { var _a; return new EventLogger(getId(), (_a = ctx.options) === null || _a === void 0 ? void 0 : _a.eventLogger); });
|
|
193
|
-
return topic ? get().createTopic(topic) : get();
|
|
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', () => { var _a; return new EventLogger(getId(), (_a = ctx.options) === null || _a === void 0 ? void 0 : _a.eventLogger); });
|
|
193
|
+
return topic ? get().createTopic(topic) : get();
|
|
194
194
|
}
|
|
195
195
|
|
|
196
196
|
export { EventLogger, attachHook, createEventContext, useEventContext, useEventId, useEventLogger, useRouteParams };
|