@vanyamate/sec 0.1.2 → 0.1.3
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.js +109 -0
- package/package.json +5 -4
- package/tsconfig.json +20 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
export function effect(fn) {
|
|
2
|
+
let beforeListeners = [];
|
|
3
|
+
let successListeners = [];
|
|
4
|
+
let errorListeners = [];
|
|
5
|
+
let finallyListeners = [];
|
|
6
|
+
const wrappedEffect = async (...args) => {
|
|
7
|
+
beforeListeners.forEach(listener => listener(...args));
|
|
8
|
+
try {
|
|
9
|
+
const result = await fn(...args);
|
|
10
|
+
successListeners.forEach(listener => listener(result, ...args));
|
|
11
|
+
return result;
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
errorListeners.forEach(listener => listener(error, ...args));
|
|
15
|
+
throw error;
|
|
16
|
+
}
|
|
17
|
+
finally {
|
|
18
|
+
finallyListeners.forEach(listener => listener(...args));
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
wrappedEffect.onBefore = (callback) => {
|
|
22
|
+
beforeListeners.push(callback);
|
|
23
|
+
};
|
|
24
|
+
wrappedEffect.onSuccess = (callback) => {
|
|
25
|
+
successListeners.push(callback);
|
|
26
|
+
};
|
|
27
|
+
wrappedEffect.onError = (callback) => {
|
|
28
|
+
errorListeners.push(callback);
|
|
29
|
+
};
|
|
30
|
+
wrappedEffect.onFinally = (callback) => {
|
|
31
|
+
finallyListeners.push(callback);
|
|
32
|
+
};
|
|
33
|
+
return wrappedEffect;
|
|
34
|
+
}
|
|
35
|
+
export function store(initialState) {
|
|
36
|
+
let state = initialState;
|
|
37
|
+
let listeners = [];
|
|
38
|
+
const get = () => state;
|
|
39
|
+
const set = (newState) => {
|
|
40
|
+
state = newState;
|
|
41
|
+
listeners.forEach(listener => listener(state));
|
|
42
|
+
};
|
|
43
|
+
const on = (effect, event, handler) => {
|
|
44
|
+
const callback = (payload) => set(handler(state, payload));
|
|
45
|
+
if (event === 'onBefore') {
|
|
46
|
+
effect.onBefore((...args) => callback({ args }));
|
|
47
|
+
}
|
|
48
|
+
else if (event === 'onSuccess') {
|
|
49
|
+
effect.onSuccess((result, ...args) => callback({
|
|
50
|
+
result,
|
|
51
|
+
args,
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
else if (event === 'onError') {
|
|
55
|
+
effect.onError((error, ...args) => callback({ error, args }));
|
|
56
|
+
}
|
|
57
|
+
else if (event === 'onFinally') {
|
|
58
|
+
effect.onFinally((...args) => callback({ args }));
|
|
59
|
+
}
|
|
60
|
+
return storeApi;
|
|
61
|
+
};
|
|
62
|
+
const subscribe = (listener) => {
|
|
63
|
+
listeners.push(listener);
|
|
64
|
+
return () => {
|
|
65
|
+
const index = listeners.indexOf(listener);
|
|
66
|
+
if (index !== -1) {
|
|
67
|
+
listeners.splice(index, 1);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
const storeApi = {
|
|
72
|
+
get,
|
|
73
|
+
set,
|
|
74
|
+
on,
|
|
75
|
+
subscribe,
|
|
76
|
+
};
|
|
77
|
+
return storeApi;
|
|
78
|
+
}
|
|
79
|
+
export function combine(stores, callback) {
|
|
80
|
+
let combinedState = callback(...stores);
|
|
81
|
+
stores.forEach((store) => {
|
|
82
|
+
store.subscribe(() => {
|
|
83
|
+
combinedState = callback(...stores);
|
|
84
|
+
listeners.forEach(listener => listener(combinedState));
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
const listeners = [];
|
|
88
|
+
const get = () => combinedState;
|
|
89
|
+
const subscribe = (listener) => {
|
|
90
|
+
listeners.push(listener);
|
|
91
|
+
return () => {
|
|
92
|
+
const index = listeners.indexOf(listener);
|
|
93
|
+
if (index !== -1) {
|
|
94
|
+
listeners.splice(index, 1);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
const storeApi = {
|
|
99
|
+
get,
|
|
100
|
+
set: () => {
|
|
101
|
+
throw new Error('Cannot call \'set\' on combined store');
|
|
102
|
+
},
|
|
103
|
+
on: () => {
|
|
104
|
+
throw new Error('Cannot call \'on\' on combined store');
|
|
105
|
+
},
|
|
106
|
+
subscribe,
|
|
107
|
+
};
|
|
108
|
+
return storeApi;
|
|
109
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vanyamate/sec",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "SEC. Store, Effect, Combine. Tiny state manager",
|
|
5
5
|
"scripts": {
|
|
6
|
-
"g:type": "tsc index.ts --declaration --emitDeclarationOnly"
|
|
6
|
+
"g:type": "tsc index.ts --declaration --emitDeclarationOnly",
|
|
7
|
+
"build": "tsc"
|
|
7
8
|
},
|
|
8
|
-
"main": "./index.
|
|
9
|
-
"types": "./index.d.ts",
|
|
9
|
+
"main": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
10
11
|
"type": "module",
|
|
11
12
|
"repository": {
|
|
12
13
|
"type": "git",
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNext",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"outDir": "./dist",
|
|
7
|
+
"rootDir": "./",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"moduleResolution": "Node"
|
|
11
|
+
},
|
|
12
|
+
"include": [
|
|
13
|
+
"./index.ts"
|
|
14
|
+
],
|
|
15
|
+
"exclude": [
|
|
16
|
+
"node_modules",
|
|
17
|
+
"index.d.ts",
|
|
18
|
+
"index.js"
|
|
19
|
+
]
|
|
20
|
+
}
|
|
File without changes
|