@vanyamate/sec 0.2.5 → 0.2.7
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 +58 -8
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +73 -70
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# SEC (Store, Effect, Combine)
|
|
2
2
|
|
|
3
|
+
https://sec-docs-minimal.lovable.app/
|
|
4
|
+
|
|
3
5
|
Tiny state manager
|
|
4
6
|
|
|
5
7
|
*It may change in the future*
|
|
@@ -17,13 +19,52 @@ npm i @vanyamate/sec-react
|
|
|
17
19
|
npm i @vanyamate/sec-solidjs
|
|
18
20
|
```
|
|
19
21
|
|
|
20
|
-
|
|
22
|
+
## Documentation:
|
|
21
23
|
|
|
22
|
-
|
|
23
|
-
npm i @vanyamate/sec-vue
|
|
24
|
-
```
|
|
24
|
+
### Example
|
|
25
25
|
|
|
26
|
-
|
|
26
|
+
```tsx
|
|
27
|
+
|
|
28
|
+
import { effect, store, marker, pending, to, result } from '@vanyamate/sec';
|
|
29
|
+
import { useStore } from '@vanyamate/sec-react';
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
const logout = async function () {
|
|
33
|
+
return api('v1/auth/logout', { method: 'POST' });
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const getPosts = async function (userId: number): Promise<Array<Post>> {
|
|
37
|
+
return api(`v1/posts/byUserId/${ userId }`);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const getPostsForUserPageEffect = effect(getPosts);
|
|
41
|
+
const logoutEffect = effect(logout);
|
|
42
|
+
|
|
43
|
+
const disableMarker = marker('afterAll')
|
|
44
|
+
.on('onBefore', logoutEffect);
|
|
45
|
+
|
|
46
|
+
const $userPagePostsPending = pending([ getPostsForUserPageEffect ])
|
|
47
|
+
.disableOn(disableMarker, false);
|
|
48
|
+
const $userPagePosts = store<Array<Post>>([])
|
|
49
|
+
.disableOn(disableMarker, [])
|
|
50
|
+
.on('onBefore', getPostsForUserPageEffect, to([]))
|
|
51
|
+
.on('onSuccess', getPostsForUserPageEffect, result());
|
|
52
|
+
|
|
53
|
+
const UserPage = function (userId: number) {
|
|
54
|
+
const postsPending = useStore($userPagePostsPending);
|
|
55
|
+
const posts = useStore($userPagePosts);
|
|
56
|
+
|
|
57
|
+
useLayoutEffect(() => {
|
|
58
|
+
getPostsForUserPageEffect(userId);
|
|
59
|
+
}, [ userId ]);
|
|
60
|
+
|
|
61
|
+
if (postsPending) {
|
|
62
|
+
return <Loader/>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return posts.map((post) => <Post key={ post.id } post={ post }/>);
|
|
66
|
+
};
|
|
67
|
+
```
|
|
27
68
|
|
|
28
69
|
### effect
|
|
29
70
|
|
|
@@ -85,6 +126,15 @@ to(123); // Return () => 123
|
|
|
85
126
|
// after .on(logoutEffect, 'onSuccess', to([]));
|
|
86
127
|
```
|
|
87
128
|
|
|
129
|
+
### result
|
|
130
|
+
|
|
131
|
+
Just helper. Returns a function that returns the returned value from action
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
// before .on(logoutEffect, 'onSuccess', (_, { result ) => result);
|
|
135
|
+
// after .on(logoutEffect, 'onSuccess', result());
|
|
136
|
+
```
|
|
137
|
+
|
|
88
138
|
### pending
|
|
89
139
|
|
|
90
140
|
Just helper. wrapper over store. returns a bool value and is used to create a pending-store.
|
|
@@ -102,9 +152,9 @@ Just helper. wrapper over store. returns a bool value and is used to create a pe
|
|
|
102
152
|
*/
|
|
103
153
|
|
|
104
154
|
const postsIsPending = pending([
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
]);
|
|
155
|
+
getPostsForUser,
|
|
156
|
+
createPostEffect,
|
|
157
|
+
]);
|
|
108
158
|
```
|
|
109
159
|
|
|
110
160
|
### store
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=function(
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=function(c,n){c&&n()},a=function(c,n=!0){const f=[],s={on:(t,o,e)=>(o==="onBefore"?t.onBefore((...r)=>u(n,()=>s.set(e(c,{args:r})))):o==="onSuccess"?t.onSuccess((r,...l)=>u(n,()=>s.set(e(c,{result:r,args:l})))):o==="onError"?t.onError((r,...l)=>u(n,()=>s.set(e(c,{error:r,args:l})))):t.onFinally((...r)=>u(n,()=>s.set(e(c,{args:r})))),s),get(){return c},set(t){c=t,f.forEach(o=>o(c))},subscribe(t){return f.push(t),()=>{const o=f.indexOf(t);~o&&f.splice(o,1)}},enableOn(t,o){return t.subscribe(()=>n=!0),o!==void 0&&s.set(o),s},disableOn(t,o){return t.subscribe(()=>n=!1),o!==void 0&&s.set(o),s}};return s},i=function(){return{afterAll:[],beforeAll:[],other:[]}},b=function(c){const n=i(),f=i(),s=i(),t=i(),o=async function(...e){return n.beforeAll.forEach(r=>r(...e)),n.other.forEach(r=>r(...e)),n.afterAll.forEach(r=>r(...e)),c(...e).then(r=>(f.beforeAll.forEach(l=>l(r,...e)),f.other.forEach(l=>l(r,...e)),f.afterAll.forEach(l=>l(r,...e)),r)).catch(r=>{throw s.beforeAll.forEach(l=>l(r,...e)),s.other.forEach(l=>l(r,...e)),s.afterAll.forEach(l=>l(r,...e)),r}).finally(()=>{t.beforeAll.forEach(r=>r(...e)),t.other.forEach(r=>r(...e)),t.afterAll.forEach(r=>r(...e))})};return o.onBefore=(e,r)=>{switch(r){case"beforeAll":n.beforeAll.push(e);break;case"afterAll":n.afterAll.push(e);break;default:n.other.push(e)}},o.onSuccess=(e,r)=>{switch(r){case"beforeAll":f.beforeAll.push(e);break;case"afterAll":f.afterAll.push(e);break;default:f.other.push(e)}},o.onError=(e,r)=>{switch(r){case"beforeAll":s.beforeAll.push(e);break;case"afterAll":s.afterAll.push(e);break;default:s.other.push(e)}},o.onFinally=(e,r)=>{switch(r){case"beforeAll":t.beforeAll.push(e);break;case"afterAll":t.afterAll.push(e);break;default:t.other.push(e)}},o},A=function(c,n,f=!0){console.log("Combine",c,n,f);let s=n(c);const t=[];c.forEach(e=>{e.subscribe(()=>{u(f,()=>{s=n(c),t.forEach(r=>r(s))})})});const o={on:()=>{throw new Error("Cannot call 'on' on combined store")},get(){return s},set(){throw new Error("Cannot call 'set' on combined store")},subscribe(e){return t.push(e),()=>{const r=t.indexOf(e);~r&&t.splice(r,1)}},enableOn(e){return e.subscribe(()=>f=!0),o},disableOn(e){return e.subscribe(()=>f=!1),o}};return o},E=function(c){const n=[],f={on:(s,t)=>(s==="onBefore"?t.onBefore(()=>n.forEach(o=>o()),c):s==="onSuccess"?t.onSuccess(()=>n.forEach(o=>o()),c):s==="onError"?t.onError(()=>n.forEach(o=>o()),c):t.onFinally(()=>n.forEach(o=>o()),c),f),subscribe:s=>{n.push(s)}};return f},h=function(c){return()=>c},p=function(c){const n=a(!1);return c.forEach(f=>{n.on(f,"onBefore",h(!0)),n.on(f,"onFinally",h(!1))}),n},m=function(){return(c,{result:n})=>n};exports.combine=A;exports.effect=b;exports.enableCheck=u;exports.marker=E;exports.pending=p;exports.result=m;exports.store=a;exports.to=h;
|
package/dist/index.d.ts
CHANGED
|
@@ -41,6 +41,8 @@ export declare type MarkerListener = () => void;
|
|
|
41
41
|
|
|
42
42
|
export declare const pending: (effects: Array<Effect<any>>) => Store<boolean>;
|
|
43
43
|
|
|
44
|
+
export declare const result: <State extends Awaited<ReturnType<Action>>, Action extends EffectAction>() => StoreHandlerMap<State, Action>["onSuccess"];
|
|
45
|
+
|
|
44
46
|
export declare type Store<State> = {
|
|
45
47
|
on: StoreEffectSubscribe<State>;
|
|
46
48
|
enableOn: StoreMarkerSubscribe<State>;
|
|
@@ -63,7 +65,7 @@ export declare type StoreHandlerMap<State, Action extends EffectAction> = {
|
|
|
63
65
|
|
|
64
66
|
export declare type StoreListener<State> = (state: State) => void;
|
|
65
67
|
|
|
66
|
-
export declare type StoreMarkerSubscribe<State> = (marker: Marker<State
|
|
68
|
+
export declare type StoreMarkerSubscribe<State> = (marker: Marker<State>, value?: State) => Store<State>;
|
|
67
69
|
|
|
68
70
|
export declare type StoreOnBeforeHandler<State, Action extends EffectAction> = (state: State, data: {
|
|
69
71
|
args: Parameters<Action>;
|
package/dist/index.js
CHANGED
|
@@ -1,77 +1,77 @@
|
|
|
1
|
-
const u = function(
|
|
2
|
-
|
|
3
|
-
}, a = function(
|
|
4
|
-
const f = [],
|
|
5
|
-
on: (
|
|
1
|
+
const u = function(c, n) {
|
|
2
|
+
c && n();
|
|
3
|
+
}, a = function(c, n = !0) {
|
|
4
|
+
const f = [], t = {
|
|
5
|
+
on: (s, o, e) => (o === "onBefore" ? s.onBefore(
|
|
6
6
|
(...r) => u(
|
|
7
7
|
n,
|
|
8
|
-
() =>
|
|
9
|
-
e(
|
|
8
|
+
() => t.set(
|
|
9
|
+
e(c, { args: r })
|
|
10
10
|
)
|
|
11
11
|
)
|
|
12
|
-
) :
|
|
12
|
+
) : o === "onSuccess" ? s.onSuccess(
|
|
13
13
|
(r, ...l) => u(
|
|
14
14
|
n,
|
|
15
|
-
() =>
|
|
16
|
-
e(
|
|
15
|
+
() => t.set(
|
|
16
|
+
e(c, {
|
|
17
17
|
result: r,
|
|
18
18
|
args: l
|
|
19
19
|
})
|
|
20
20
|
)
|
|
21
21
|
)
|
|
22
|
-
) :
|
|
22
|
+
) : o === "onError" ? s.onError(
|
|
23
23
|
(r, ...l) => u(
|
|
24
24
|
n,
|
|
25
|
-
() =>
|
|
26
|
-
e(
|
|
25
|
+
() => t.set(
|
|
26
|
+
e(c, {
|
|
27
27
|
error: r,
|
|
28
28
|
args: l
|
|
29
29
|
})
|
|
30
30
|
)
|
|
31
31
|
)
|
|
32
|
-
) :
|
|
32
|
+
) : s.onFinally(
|
|
33
33
|
(...r) => u(
|
|
34
34
|
n,
|
|
35
|
-
() =>
|
|
36
|
-
e(
|
|
35
|
+
() => t.set(
|
|
36
|
+
e(c, { args: r })
|
|
37
37
|
)
|
|
38
38
|
)
|
|
39
|
-
),
|
|
39
|
+
), t),
|
|
40
40
|
get() {
|
|
41
|
-
return
|
|
41
|
+
return c;
|
|
42
42
|
},
|
|
43
|
-
set(
|
|
44
|
-
|
|
43
|
+
set(s) {
|
|
44
|
+
c = s, f.forEach((o) => o(c));
|
|
45
45
|
},
|
|
46
|
-
subscribe(
|
|
47
|
-
return f.push(
|
|
48
|
-
const
|
|
49
|
-
~
|
|
46
|
+
subscribe(s) {
|
|
47
|
+
return f.push(s), () => {
|
|
48
|
+
const o = f.indexOf(s);
|
|
49
|
+
~o && f.splice(o, 1);
|
|
50
50
|
};
|
|
51
51
|
},
|
|
52
|
-
enableOn(o) {
|
|
53
|
-
return
|
|
52
|
+
enableOn(s, o) {
|
|
53
|
+
return s.subscribe(() => n = !0), o !== void 0 && t.set(o), t;
|
|
54
54
|
},
|
|
55
|
-
disableOn(o) {
|
|
56
|
-
return
|
|
55
|
+
disableOn(s, o) {
|
|
56
|
+
return s.subscribe(() => n = !1), o !== void 0 && t.set(o), t;
|
|
57
57
|
}
|
|
58
58
|
};
|
|
59
|
-
return
|
|
60
|
-
},
|
|
59
|
+
return t;
|
|
60
|
+
}, i = function() {
|
|
61
61
|
return {
|
|
62
62
|
afterAll: [],
|
|
63
63
|
beforeAll: [],
|
|
64
64
|
other: []
|
|
65
65
|
};
|
|
66
|
-
}, b = function(
|
|
67
|
-
const n =
|
|
68
|
-
return n.beforeAll.forEach((r) => r(...e)), n.other.forEach((r) => r(...e)), n.afterAll.forEach((r) => r(...e)),
|
|
69
|
-
throw
|
|
66
|
+
}, b = function(c) {
|
|
67
|
+
const n = i(), f = i(), t = i(), s = i(), o = async function(...e) {
|
|
68
|
+
return n.beforeAll.forEach((r) => r(...e)), n.other.forEach((r) => r(...e)), n.afterAll.forEach((r) => r(...e)), c(...e).then((r) => (f.beforeAll.forEach((l) => l(r, ...e)), f.other.forEach((l) => l(r, ...e)), f.afterAll.forEach((l) => l(r, ...e)), r)).catch((r) => {
|
|
69
|
+
throw t.beforeAll.forEach((l) => l(r, ...e)), t.other.forEach((l) => l(r, ...e)), t.afterAll.forEach((l) => l(r, ...e)), r;
|
|
70
70
|
}).finally(() => {
|
|
71
|
-
|
|
71
|
+
s.beforeAll.forEach((r) => r(...e)), s.other.forEach((r) => r(...e)), s.afterAll.forEach((r) => r(...e));
|
|
72
72
|
});
|
|
73
73
|
};
|
|
74
|
-
return
|
|
74
|
+
return o.onBefore = (e, r) => {
|
|
75
75
|
switch (r) {
|
|
76
76
|
case "beforeAll":
|
|
77
77
|
n.beforeAll.push(e);
|
|
@@ -82,7 +82,7 @@ const u = function(t, n) {
|
|
|
82
82
|
default:
|
|
83
83
|
n.other.push(e);
|
|
84
84
|
}
|
|
85
|
-
},
|
|
85
|
+
}, o.onSuccess = (e, r) => {
|
|
86
86
|
switch (r) {
|
|
87
87
|
case "beforeAll":
|
|
88
88
|
f.beforeAll.push(e);
|
|
@@ -93,79 +93,81 @@ const u = function(t, n) {
|
|
|
93
93
|
default:
|
|
94
94
|
f.other.push(e);
|
|
95
95
|
}
|
|
96
|
-
},
|
|
96
|
+
}, o.onError = (e, r) => {
|
|
97
97
|
switch (r) {
|
|
98
98
|
case "beforeAll":
|
|
99
|
-
|
|
99
|
+
t.beforeAll.push(e);
|
|
100
100
|
break;
|
|
101
101
|
case "afterAll":
|
|
102
|
-
|
|
102
|
+
t.afterAll.push(e);
|
|
103
103
|
break;
|
|
104
104
|
default:
|
|
105
|
-
|
|
105
|
+
t.other.push(e);
|
|
106
106
|
}
|
|
107
|
-
},
|
|
107
|
+
}, o.onFinally = (e, r) => {
|
|
108
108
|
switch (r) {
|
|
109
109
|
case "beforeAll":
|
|
110
|
-
|
|
110
|
+
s.beforeAll.push(e);
|
|
111
111
|
break;
|
|
112
112
|
case "afterAll":
|
|
113
|
-
|
|
113
|
+
s.afterAll.push(e);
|
|
114
114
|
break;
|
|
115
115
|
default:
|
|
116
|
-
|
|
116
|
+
s.other.push(e);
|
|
117
117
|
}
|
|
118
|
-
},
|
|
119
|
-
}, A = function(
|
|
120
|
-
console.log("Combine",
|
|
121
|
-
let
|
|
122
|
-
const
|
|
123
|
-
|
|
118
|
+
}, o;
|
|
119
|
+
}, A = function(c, n, f = !0) {
|
|
120
|
+
console.log("Combine", c, n, f);
|
|
121
|
+
let t = n(c);
|
|
122
|
+
const s = [];
|
|
123
|
+
c.forEach((e) => {
|
|
124
124
|
e.subscribe(() => {
|
|
125
125
|
u(f, () => {
|
|
126
|
-
|
|
126
|
+
t = n(c), s.forEach((r) => r(t));
|
|
127
127
|
});
|
|
128
128
|
});
|
|
129
129
|
});
|
|
130
|
-
const
|
|
130
|
+
const o = {
|
|
131
131
|
on: () => {
|
|
132
132
|
throw new Error("Cannot call 'on' on combined store");
|
|
133
133
|
},
|
|
134
134
|
get() {
|
|
135
|
-
return
|
|
135
|
+
return t;
|
|
136
136
|
},
|
|
137
137
|
set() {
|
|
138
138
|
throw new Error("Cannot call 'set' on combined store");
|
|
139
139
|
},
|
|
140
140
|
subscribe(e) {
|
|
141
|
-
return
|
|
142
|
-
const r =
|
|
143
|
-
~r &&
|
|
141
|
+
return s.push(e), () => {
|
|
142
|
+
const r = s.indexOf(e);
|
|
143
|
+
~r && s.splice(r, 1);
|
|
144
144
|
};
|
|
145
145
|
},
|
|
146
146
|
enableOn(e) {
|
|
147
|
-
return e.subscribe(() => f = !0),
|
|
147
|
+
return e.subscribe(() => f = !0), o;
|
|
148
148
|
},
|
|
149
149
|
disableOn(e) {
|
|
150
|
-
return e.subscribe(() => f = !1),
|
|
150
|
+
return e.subscribe(() => f = !1), o;
|
|
151
151
|
}
|
|
152
152
|
};
|
|
153
|
-
return
|
|
154
|
-
}, E = function(
|
|
153
|
+
return o;
|
|
154
|
+
}, E = function(c) {
|
|
155
155
|
const n = [], f = {
|
|
156
|
-
on: (
|
|
157
|
-
subscribe: (
|
|
158
|
-
n.push(
|
|
156
|
+
on: (t, s) => (t === "onBefore" ? s.onBefore(() => n.forEach((o) => o()), c) : t === "onSuccess" ? s.onSuccess(() => n.forEach((o) => o()), c) : t === "onError" ? s.onError(() => n.forEach((o) => o()), c) : s.onFinally(() => n.forEach((o) => o()), c), f),
|
|
157
|
+
subscribe: (t) => {
|
|
158
|
+
n.push(t);
|
|
159
159
|
}
|
|
160
160
|
};
|
|
161
161
|
return f;
|
|
162
|
-
},
|
|
163
|
-
return () =>
|
|
164
|
-
}, p = function(
|
|
162
|
+
}, h = function(c) {
|
|
163
|
+
return () => c;
|
|
164
|
+
}, p = function(c) {
|
|
165
165
|
const n = a(!1);
|
|
166
|
-
return
|
|
167
|
-
n.on(f, "onBefore",
|
|
166
|
+
return c.forEach((f) => {
|
|
167
|
+
n.on(f, "onBefore", h(!0)), n.on(f, "onFinally", h(!1));
|
|
168
168
|
}), n;
|
|
169
|
+
}, w = function() {
|
|
170
|
+
return (c, { result: n }) => n;
|
|
169
171
|
};
|
|
170
172
|
export {
|
|
171
173
|
A as combine,
|
|
@@ -173,6 +175,7 @@ export {
|
|
|
173
175
|
u as enableCheck,
|
|
174
176
|
E as marker,
|
|
175
177
|
p as pending,
|
|
178
|
+
w as result,
|
|
176
179
|
a as store,
|
|
177
|
-
|
|
180
|
+
h as to
|
|
178
181
|
};
|