@wooksjs/event-core 0.1.0

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/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # @wooksjs/event-core
2
+
3
+ **!!! This is work-in-progress library, breaking changes are expected !!!**
4
+
5
+ <p align="center">
6
+ <img src="../../logo.png" width="128px"><br>
7
+ <a href="https://github.com/prostojs/wooks/blob/main/LICENSE">
8
+ <img src="https://img.shields.io/badge/License-MIT-green?style=for-the-badge" />
9
+ </a>
10
+ </p>
11
+
12
+ Core functionality of wooks context
package/dist/index.cjs ADDED
@@ -0,0 +1,162 @@
1
+ 'use strict';
2
+
3
+ const banner = () => `[${"@wooksjs/event-core"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
4
+
5
+ /* istanbul ignore file */
6
+ function logError(error) {
7
+ console.error('' + '' + banner() + error + '');
8
+ }
9
+
10
+ function panic(error) {
11
+ logError(error);
12
+ return new Error(error);
13
+ }
14
+
15
+ // eslint-disable-next-line @typescript-eslint/ban-types
16
+ function attachHook(target, opts, name) {
17
+ Object.defineProperty(target, name || 'value', {
18
+ get: opts.get,
19
+ set: opts.set,
20
+ });
21
+ return target;
22
+ }
23
+
24
+ let currentContext = null;
25
+ /**
26
+ * Create a new event context
27
+ *
28
+ * @param data
29
+ * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
30
+ */
31
+ function createEventContext(data) {
32
+ const newContext = Object.assign({}, data);
33
+ currentContext = newContext;
34
+ return _getCtxHelpers(newContext);
35
+ }
36
+ /**
37
+ * Use existing event context
38
+ *
39
+ * !Must be called syncronously while context is reachable
40
+ *
41
+ * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
42
+ */
43
+ function useEventContext(expectedTypes) {
44
+ var _a;
45
+ if (!currentContext) {
46
+ throw panic('Event context does not exist. Use event context synchronously within the runtime of the event.');
47
+ }
48
+ const cc = currentContext;
49
+ if (expectedTypes || typeof expectedTypes === 'string') {
50
+ const type = (_a = cc.event) === null || _a === void 0 ? void 0 : _a.type;
51
+ const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
52
+ if (!types.includes(type))
53
+ panic(`Event context type mismatch: expected ${types.map(t => `"${t}"`).join(', ')}, received "${type}"`);
54
+ }
55
+ return _getCtxHelpers(cc);
56
+ }
57
+ function _getCtxHelpers(cc) {
58
+ /**
59
+ * Hook to an event store property
60
+ *
61
+ * @param key store property key
62
+ * @returns a hook { value: <prop value>, hook: (key2: keyof <prop value>) => { value: <nested prop value> }, ... }
63
+ */
64
+ function store(key) {
65
+ const obj = {
66
+ value: null,
67
+ hook,
68
+ init,
69
+ set: setNested,
70
+ get: getNested,
71
+ has: hasNested,
72
+ del: delNested,
73
+ entries,
74
+ clear,
75
+ };
76
+ attachHook(obj, {
77
+ set: v => set(key, v),
78
+ get: () => get(key),
79
+ });
80
+ function init(key2, getter) {
81
+ if (hasNested(key2))
82
+ return getNested(key2);
83
+ return setNested(key2, getter());
84
+ }
85
+ function hook(key2) {
86
+ const obj = {
87
+ value: null,
88
+ isDefined: null,
89
+ };
90
+ attachHook(obj, {
91
+ set: v => setNested(key2, v),
92
+ get: () => getNested(key2),
93
+ });
94
+ attachHook(obj, {
95
+ get: () => hasNested(key2),
96
+ }, 'isDefined');
97
+ return obj;
98
+ }
99
+ function setNested(key2, v) {
100
+ if (typeof obj.value === 'undefined') {
101
+ obj.value = {};
102
+ }
103
+ obj.value[key2] = v;
104
+ return v;
105
+ }
106
+ function delNested(key2) {
107
+ setNested(key2, undefined);
108
+ }
109
+ function getNested(key2) { return (obj.value || {})[key2]; }
110
+ function hasNested(key2) { return typeof (obj.value || {})[key2] !== 'undefined'; }
111
+ function entries() { return Object.entries((obj.value || {})); }
112
+ function clear() { obj.value = {}; }
113
+ return obj;
114
+ }
115
+ /**
116
+ * Get event context object
117
+ *
118
+ * @returns whole context object
119
+ */
120
+ function getCtx() { return cc; }
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) { return getCtx()[key]; }
128
+ /**
129
+ * Set value of event store property
130
+ *
131
+ * @param key property name
132
+ * @param v property value
133
+ */
134
+ function set(key, v) {
135
+ (getCtx())[key] = v;
136
+ }
137
+ return {
138
+ getCtx,
139
+ restoreCtx: () => currentContext = cc,
140
+ clearCtx: () => cc === currentContext ? currentContext = null : null,
141
+ store,
142
+ getStore: get,
143
+ setStore: set,
144
+ };
145
+ }
146
+
147
+ function useRouteParams() {
148
+ const { store } = useEventContext();
149
+ const params = (store('routeParams').value || {});
150
+ function get(name) {
151
+ return params[name];
152
+ }
153
+ return {
154
+ params,
155
+ get,
156
+ };
157
+ }
158
+
159
+ exports.attachHook = attachHook;
160
+ exports.createEventContext = createEventContext;
161
+ exports.useEventContext = useEventContext;
162
+ exports.useRouteParams = useRouteParams;
@@ -0,0 +1,77 @@
1
+ export declare function attachHook<V = unknown, T extends (object | Function) = object, P extends PropertyKey = 'value'>(target: T, opts: {
2
+ get: () => V | undefined;
3
+ set?: (value: V) => void;
4
+ }, name?: P): T & { [name in P]?: V | undefined; };
5
+
6
+ /**
7
+ * Create a new event context
8
+ *
9
+ * @param data
10
+ * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
11
+ */
12
+ export declare function createEventContext<S extends TGenericContextStore>(data: S): {
13
+ getCtx: () => S;
14
+ restoreCtx: () => S;
15
+ clearCtx: () => null;
16
+ store: <K extends keyof S>(key: K) => {
17
+ value: S[K];
18
+ hook: <K2 extends keyof Required<S>[K]>(key2: K2) => {
19
+ value: Required<S>[K][K2];
20
+ isDefined: boolean;
21
+ };
22
+ init: <K2_1 extends keyof Required<S>[K]>(key2: K2_1, getter: () => Required<Required<S>[K]>[K2_1]) => Required<Required<S>[K]>[K2_1];
23
+ set: <K2_2 extends keyof Required<S>[K]>(key2: K2_2, v: Required<S[K]>[K2_2]) => Required<S[K]>[K2_2];
24
+ get: <K2_3 extends keyof Required<S>[K]>(key2: K2_3) => Required<S>[K][K2_3];
25
+ has: <K2_4 extends keyof Required<S>[K]>(key2: K2_4) => boolean;
26
+ del: <K2_5 extends keyof Required<S>[K]>(key2: K2_5) => void;
27
+ entries: () => [string, unknown][];
28
+ clear: () => void;
29
+ };
30
+ getStore: <K_1 extends keyof S>(key: K_1) => S[K_1];
31
+ setStore: <K_2 extends keyof S>(key: K_2, v: S[K_2]) => void;
32
+ };
33
+
34
+ export declare type TGenericContextStore<E extends TGenericEvent = TGenericEvent> = {
35
+ event: E;
36
+ routeParams?: Record<string, string | string[]>;
37
+ };
38
+
39
+ export declare interface TGenericEvent {
40
+ type: string;
41
+ }
42
+
43
+ /**
44
+ * Use existing event context
45
+ *
46
+ * !Must be called syncronously while context is reachable
47
+ *
48
+ * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
49
+ */
50
+ export declare function useEventContext<S extends TGenericContextStore>(expectedTypes?: string | string[]): {
51
+ getCtx: () => S;
52
+ restoreCtx: () => S;
53
+ clearCtx: () => null;
54
+ store: <K extends keyof S>(key: K) => {
55
+ value: S[K];
56
+ hook: <K2 extends keyof Required<S>[K]>(key2: K2) => {
57
+ value: Required<S>[K][K2];
58
+ isDefined: boolean;
59
+ };
60
+ init: <K2_1 extends keyof Required<S>[K]>(key2: K2_1, getter: () => Required<Required<S>[K]>[K2_1]) => Required<Required<S>[K]>[K2_1];
61
+ set: <K2_2 extends keyof Required<S>[K]>(key2: K2_2, v: Required<S[K]>[K2_2]) => Required<S[K]>[K2_2];
62
+ get: <K2_3 extends keyof Required<S>[K]>(key2: K2_3) => Required<S>[K][K2_3];
63
+ has: <K2_4 extends keyof Required<S>[K]>(key2: K2_4) => boolean;
64
+ del: <K2_5 extends keyof Required<S>[K]>(key2: K2_5) => void;
65
+ entries: () => [string, unknown][];
66
+ clear: () => void;
67
+ };
68
+ getStore: <K_1 extends keyof S>(key: K_1) => S[K_1];
69
+ setStore: <K_2 extends keyof S>(key: K_2, v: S[K_2]) => void;
70
+ };
71
+
72
+ export declare function useRouteParams<T extends object = Record<string, string | string[]>>(): {
73
+ params: T;
74
+ get: <K extends keyof T>(name: K) => T[K];
75
+ };
76
+
77
+ export { }
package/dist/index.mjs ADDED
@@ -0,0 +1,157 @@
1
+ const banner = () => `[${"@wooksjs/event-core"}][${new Date().toISOString().replace('T', ' ').replace(/\.\d{3}z$/i, '')}] `;
2
+
3
+ /* istanbul ignore file */
4
+ function logError(error) {
5
+ console.error('' + '' + banner() + error + '');
6
+ }
7
+
8
+ function panic(error) {
9
+ logError(error);
10
+ return new Error(error);
11
+ }
12
+
13
+ // eslint-disable-next-line @typescript-eslint/ban-types
14
+ function attachHook(target, opts, name) {
15
+ Object.defineProperty(target, name || 'value', {
16
+ get: opts.get,
17
+ set: opts.set,
18
+ });
19
+ return target;
20
+ }
21
+
22
+ let currentContext = null;
23
+ /**
24
+ * Create a new event context
25
+ *
26
+ * @param data
27
+ * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
28
+ */
29
+ function createEventContext(data) {
30
+ const newContext = Object.assign({}, data);
31
+ currentContext = newContext;
32
+ return _getCtxHelpers(newContext);
33
+ }
34
+ /**
35
+ * Use existing event context
36
+ *
37
+ * !Must be called syncronously while context is reachable
38
+ *
39
+ * @returns set of hooks { getCtx, restoreCtx, clearCtx, hookStore, getStore, setStore }
40
+ */
41
+ function useEventContext(expectedTypes) {
42
+ var _a;
43
+ if (!currentContext) {
44
+ throw panic('Event context does not exist. Use event context synchronously within the runtime of the event.');
45
+ }
46
+ const cc = currentContext;
47
+ if (expectedTypes || typeof expectedTypes === 'string') {
48
+ const type = (_a = cc.event) === null || _a === void 0 ? void 0 : _a.type;
49
+ const types = typeof expectedTypes === 'string' ? [expectedTypes] : expectedTypes;
50
+ if (!types.includes(type))
51
+ panic(`Event context type mismatch: expected ${types.map(t => `"${t}"`).join(', ')}, received "${type}"`);
52
+ }
53
+ return _getCtxHelpers(cc);
54
+ }
55
+ function _getCtxHelpers(cc) {
56
+ /**
57
+ * Hook to an event store property
58
+ *
59
+ * @param key store property key
60
+ * @returns a hook { value: <prop value>, hook: (key2: keyof <prop value>) => { value: <nested prop value> }, ... }
61
+ */
62
+ function store(key) {
63
+ const obj = {
64
+ value: null,
65
+ hook,
66
+ init,
67
+ set: setNested,
68
+ get: getNested,
69
+ has: hasNested,
70
+ del: delNested,
71
+ entries,
72
+ clear,
73
+ };
74
+ attachHook(obj, {
75
+ set: v => set(key, v),
76
+ get: () => get(key),
77
+ });
78
+ function init(key2, getter) {
79
+ if (hasNested(key2))
80
+ return getNested(key2);
81
+ return setNested(key2, getter());
82
+ }
83
+ function hook(key2) {
84
+ const obj = {
85
+ value: null,
86
+ isDefined: null,
87
+ };
88
+ attachHook(obj, {
89
+ set: v => setNested(key2, v),
90
+ get: () => getNested(key2),
91
+ });
92
+ attachHook(obj, {
93
+ get: () => hasNested(key2),
94
+ }, 'isDefined');
95
+ return obj;
96
+ }
97
+ function setNested(key2, v) {
98
+ if (typeof obj.value === 'undefined') {
99
+ obj.value = {};
100
+ }
101
+ obj.value[key2] = v;
102
+ return v;
103
+ }
104
+ function delNested(key2) {
105
+ setNested(key2, undefined);
106
+ }
107
+ function getNested(key2) { return (obj.value || {})[key2]; }
108
+ function hasNested(key2) { return typeof (obj.value || {})[key2] !== 'undefined'; }
109
+ function entries() { return Object.entries((obj.value || {})); }
110
+ function clear() { obj.value = {}; }
111
+ return obj;
112
+ }
113
+ /**
114
+ * Get event context object
115
+ *
116
+ * @returns whole context object
117
+ */
118
+ function getCtx() { return cc; }
119
+ /**
120
+ * Get value of event store property
121
+ *
122
+ * @param key property name
123
+ * @returns value of property by name
124
+ */
125
+ function get(key) { return getCtx()[key]; }
126
+ /**
127
+ * Set value of event store property
128
+ *
129
+ * @param key property name
130
+ * @param v property value
131
+ */
132
+ function set(key, v) {
133
+ (getCtx())[key] = v;
134
+ }
135
+ return {
136
+ getCtx,
137
+ restoreCtx: () => currentContext = cc,
138
+ clearCtx: () => cc === currentContext ? currentContext = null : null,
139
+ store,
140
+ getStore: get,
141
+ setStore: set,
142
+ };
143
+ }
144
+
145
+ function useRouteParams() {
146
+ const { store } = useEventContext();
147
+ const params = (store('routeParams').value || {});
148
+ function get(name) {
149
+ return params[name];
150
+ }
151
+ return {
152
+ params,
153
+ get,
154
+ };
155
+ }
156
+
157
+ export { attachHook, createEventContext, useEventContext, useRouteParams };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@wooksjs/event-core",
3
+ "version": "0.1.0",
4
+ "description": "@wooksjs/event-core",
5
+ "main": "dist/index.cjs",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/wooksjs/wooksjs.git",
14
+ "directory": "packages/event-core"
15
+ },
16
+ "keywords": [
17
+ "http",
18
+ "wooks",
19
+ "composables",
20
+ "web",
21
+ "framework",
22
+ "app",
23
+ "api",
24
+ "rest",
25
+ "restful",
26
+ "prostojs"
27
+ ],
28
+ "author": "Artem Maltsev",
29
+ "license": "MIT",
30
+ "bugs": {
31
+ "url": "https://github.com/wooksjs/wooksjs/issues"
32
+ },
33
+ "homepage": "https://github.com/wooksjs/wooksjs/tree/main/packages/event-core#readme"
34
+ }