@vanyamate/sec 0.2.5 → 0.2.6

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 CHANGED
@@ -17,13 +17,53 @@ npm i @vanyamate/sec-react
17
17
  npm i @vanyamate/sec-solidjs
18
18
  ```
19
19
 
20
- For vue (like old 0.1.5 version)
20
+ ## Documentation:
21
21
 
22
- ```
23
- npm i @vanyamate/sec-vue
24
- ```
22
+ ### Example
25
23
 
26
- ## Documentation:
24
+ ```tsx
25
+
26
+ import { effect, store, marker, pending, to } from '@vanyamate/sec';
27
+ import { useStore } from '@vanyamate/sec-react';
28
+ import { result } from './index';
29
+
30
+
31
+ const logout = async function () {
32
+ return api('v1/auth/logout', { method: 'POST' });
33
+ };
34
+
35
+ const getPosts = async function (userId: number): Promise<Array<Post>> {
36
+ return api(`v1/posts/byUserId/${ userId }`);
37
+ };
38
+
39
+ const getPostsForUserPageEffect = effect(getPosts);
40
+ const logoutEffect = effect(logout);
41
+
42
+ const disableMarker = marker('afterAll')
43
+ .on('onBefore', logoutEffect);
44
+
45
+ const $userPagePostsPending = pending([ getPostsForUserPageEffect ])
46
+ .disableOn(disableMarker, false);
47
+ const $userPagePosts = store<Array<Post>>([])
48
+ .disableOn(disableMarker, [])
49
+ .on('onBefore', getPostsForUserPageEffect, to([]))
50
+ .on('onSuccess', getPostsForUserPageEffect, result());
51
+
52
+ const UserPage = function (userId: number) {
53
+ const postsPending = useStore($userPagePostsPending);
54
+ const posts = useStore($userPagePosts);
55
+
56
+ useLayoutEffect(() => {
57
+ getPostsForUserPageEffect(userId);
58
+ }, [ userId ]);
59
+
60
+ if (postsPending) {
61
+ return <Loader/>;
62
+ }
63
+
64
+ return posts.map((post) => <Post key={ post.id } post={ post }/>);
65
+ };
66
+ ```
27
67
 
28
68
  ### effect
29
69
 
@@ -85,6 +125,15 @@ to(123); // Return () => 123
85
125
  // after .on(logoutEffect, 'onSuccess', to([]));
86
126
  ```
87
127
 
128
+ ### result
129
+
130
+ Just helper. Returns a function that returns the returned value from action
131
+
132
+ ```typescript
133
+ // before .on(logoutEffect, 'onSuccess', (_, { result ) => result);
134
+ // after .on(logoutEffect, 'onSuccess', result());
135
+ ```
136
+
88
137
  ### pending
89
138
 
90
139
  Just helper. wrapper over store. returns a bool value and is used to create a pending-store.
@@ -102,9 +151,9 @@ Just helper. wrapper over store. returns a bool value and is used to create a pe
102
151
  */
103
152
 
104
153
  const postsIsPending = pending([
105
- getPostsForUser,
106
- createPostEffect,
107
- ]);
154
+ getPostsForUser,
155
+ createPostEffect,
156
+ ]);
108
157
  ```
109
158
 
110
159
  ### store
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=function(s,n){s&&n()},a=function(s,n=!0){const f=[],c={on:(o,t,e)=>(t==="onBefore"?o.onBefore((...r)=>u(n,()=>c.set(e(s,{args:r})))):t==="onSuccess"?o.onSuccess((r,...l)=>u(n,()=>c.set(e(s,{result:r,args:l})))):t==="onError"?o.onError((r,...l)=>u(n,()=>c.set(e(s,{error:r,args:l})))):o.onFinally((...r)=>u(n,()=>c.set(e(s,{args:r})))),c),get(){return s},set(o){s=o,f.forEach(t=>t(s))},subscribe(o){return f.push(o),()=>{const t=f.indexOf(o);~t&&f.splice(t,1)}},enableOn(o){return o.subscribe(()=>n=!0),c},disableOn(o){return o.subscribe(()=>n=!1),c}};return c},i=function(){return{afterAll:[],beforeAll:[],other:[]}},b=function(s){const n=i(),f=i(),c=i(),o=i(),t=async function(...e){return n.beforeAll.forEach(r=>r(...e)),n.other.forEach(r=>r(...e)),n.afterAll.forEach(r=>r(...e)),s(...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 c.beforeAll.forEach(l=>l(r,...e)),c.other.forEach(l=>l(r,...e)),c.afterAll.forEach(l=>l(r,...e)),r}).finally(()=>{o.beforeAll.forEach(r=>r(...e)),o.other.forEach(r=>r(...e)),o.afterAll.forEach(r=>r(...e))})};return t.onBefore=(e,r)=>{switch(r){case"beforeAll":n.beforeAll.push(e);break;case"afterAll":n.afterAll.push(e);break;default:n.other.push(e)}},t.onSuccess=(e,r)=>{switch(r){case"beforeAll":f.beforeAll.push(e);break;case"afterAll":f.afterAll.push(e);break;default:f.other.push(e)}},t.onError=(e,r)=>{switch(r){case"beforeAll":c.beforeAll.push(e);break;case"afterAll":c.afterAll.push(e);break;default:c.other.push(e)}},t.onFinally=(e,r)=>{switch(r){case"beforeAll":o.beforeAll.push(e);break;case"afterAll":o.afterAll.push(e);break;default:o.other.push(e)}},t},A=function(s,n,f=!0){console.log("Combine",s,n,f);let c=n(s);const o=[];s.forEach(e=>{e.subscribe(()=>{u(f,()=>{c=n(s),o.forEach(r=>r(c))})})});const t={on:()=>{throw new Error("Cannot call 'on' on combined store")},get(){return c},set(){throw new Error("Cannot call 'set' on combined store")},subscribe(e){return o.push(e),()=>{const r=o.indexOf(e);~r&&o.splice(r,1)}},enableOn(e){return e.subscribe(()=>f=!0),t},disableOn(e){return e.subscribe(()=>f=!1),t}};return t},E=function(s){const n=[],f={on:(c,o)=>(c==="onBefore"?o.onBefore(()=>n.forEach(t=>t()),s):c==="onSuccess"?o.onSuccess(()=>n.forEach(t=>t()),s):c==="onError"?o.onError(()=>n.forEach(t=>t()),s):o.onFinally(()=>n.forEach(t=>t()),s),f),subscribe:c=>{n.push(c)}};return f},h=function(s){return()=>s},p=function(s){const n=a(!1);return s.forEach(f=>{n.on(f,"onBefore",h(!0)),n.on(f,"onFinally",h(!1))}),n};exports.combine=A;exports.effect=b;exports.enableCheck=u;exports.marker=E;exports.pending=p;exports.store=a;exports.to=h;
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, 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>) => Store<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(t, n) {
2
- t && n();
3
- }, a = function(t, n = !0) {
4
- const f = [], c = {
5
- on: (o, s, e) => (s === "onBefore" ? o.onBefore(
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
- () => c.set(
9
- e(t, { args: r })
8
+ () => t.set(
9
+ e(c, { args: r })
10
10
  )
11
11
  )
12
- ) : s === "onSuccess" ? o.onSuccess(
12
+ ) : o === "onSuccess" ? s.onSuccess(
13
13
  (r, ...l) => u(
14
14
  n,
15
- () => c.set(
16
- e(t, {
15
+ () => t.set(
16
+ e(c, {
17
17
  result: r,
18
18
  args: l
19
19
  })
20
20
  )
21
21
  )
22
- ) : s === "onError" ? o.onError(
22
+ ) : o === "onError" ? s.onError(
23
23
  (r, ...l) => u(
24
24
  n,
25
- () => c.set(
26
- e(t, {
25
+ () => t.set(
26
+ e(c, {
27
27
  error: r,
28
28
  args: l
29
29
  })
30
30
  )
31
31
  )
32
- ) : o.onFinally(
32
+ ) : s.onFinally(
33
33
  (...r) => u(
34
34
  n,
35
- () => c.set(
36
- e(t, { args: r })
35
+ () => t.set(
36
+ e(c, { args: r })
37
37
  )
38
38
  )
39
- ), c),
39
+ ), t),
40
40
  get() {
41
- return t;
41
+ return c;
42
42
  },
43
- set(o) {
44
- t = o, f.forEach((s) => s(t));
43
+ set(s) {
44
+ c = s, f.forEach((o) => o(c));
45
45
  },
46
- subscribe(o) {
47
- return f.push(o), () => {
48
- const s = f.indexOf(o);
49
- ~s && f.splice(s, 1);
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 o.subscribe(() => n = !0), c;
52
+ enableOn(s, o) {
53
+ return s.subscribe(() => n = !0), o !== void 0 && t.set(o), t;
54
54
  },
55
- disableOn(o) {
56
- return o.subscribe(() => n = !1), c;
55
+ disableOn(s, o) {
56
+ return s.subscribe(() => n = !1), o !== void 0 && t.set(o), t;
57
57
  }
58
58
  };
59
- return c;
60
- }, h = function() {
59
+ return t;
60
+ }, i = function() {
61
61
  return {
62
62
  afterAll: [],
63
63
  beforeAll: [],
64
64
  other: []
65
65
  };
66
- }, b = function(t) {
67
- const n = h(), f = h(), c = h(), o = h(), s = async function(...e) {
68
- return n.beforeAll.forEach((r) => r(...e)), n.other.forEach((r) => r(...e)), n.afterAll.forEach((r) => r(...e)), t(...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 c.beforeAll.forEach((l) => l(r, ...e)), c.other.forEach((l) => l(r, ...e)), c.afterAll.forEach((l) => l(r, ...e)), r;
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
- o.beforeAll.forEach((r) => r(...e)), o.other.forEach((r) => r(...e)), o.afterAll.forEach((r) => r(...e));
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 s.onBefore = (e, r) => {
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
- }, s.onSuccess = (e, r) => {
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
- }, s.onError = (e, r) => {
96
+ }, o.onError = (e, r) => {
97
97
  switch (r) {
98
98
  case "beforeAll":
99
- c.beforeAll.push(e);
99
+ t.beforeAll.push(e);
100
100
  break;
101
101
  case "afterAll":
102
- c.afterAll.push(e);
102
+ t.afterAll.push(e);
103
103
  break;
104
104
  default:
105
- c.other.push(e);
105
+ t.other.push(e);
106
106
  }
107
- }, s.onFinally = (e, r) => {
107
+ }, o.onFinally = (e, r) => {
108
108
  switch (r) {
109
109
  case "beforeAll":
110
- o.beforeAll.push(e);
110
+ s.beforeAll.push(e);
111
111
  break;
112
112
  case "afterAll":
113
- o.afterAll.push(e);
113
+ s.afterAll.push(e);
114
114
  break;
115
115
  default:
116
- o.other.push(e);
116
+ s.other.push(e);
117
117
  }
118
- }, s;
119
- }, A = function(t, n, f = !0) {
120
- console.log("Combine", t, n, f);
121
- let c = n(t);
122
- const o = [];
123
- t.forEach((e) => {
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
- c = n(t), o.forEach((r) => r(c));
126
+ t = n(c), s.forEach((r) => r(t));
127
127
  });
128
128
  });
129
129
  });
130
- const s = {
130
+ const o = {
131
131
  on: () => {
132
132
  throw new Error("Cannot call 'on' on combined store");
133
133
  },
134
134
  get() {
135
- return c;
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 o.push(e), () => {
142
- const r = o.indexOf(e);
143
- ~r && o.splice(r, 1);
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), s;
147
+ return e.subscribe(() => f = !0), o;
148
148
  },
149
149
  disableOn(e) {
150
- return e.subscribe(() => f = !1), s;
150
+ return e.subscribe(() => f = !1), o;
151
151
  }
152
152
  };
153
- return s;
154
- }, E = function(t) {
153
+ return o;
154
+ }, E = function(c) {
155
155
  const n = [], f = {
156
- on: (c, o) => (c === "onBefore" ? o.onBefore(() => n.forEach((s) => s()), t) : c === "onSuccess" ? o.onSuccess(() => n.forEach((s) => s()), t) : c === "onError" ? o.onError(() => n.forEach((s) => s()), t) : o.onFinally(() => n.forEach((s) => s()), t), f),
157
- subscribe: (c) => {
158
- n.push(c);
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
- }, i = function(t) {
163
- return () => t;
164
- }, p = function(t) {
162
+ }, h = function(c) {
163
+ return () => c;
164
+ }, p = function(c) {
165
165
  const n = a(!1);
166
- return t.forEach((f) => {
167
- n.on(f, "onBefore", i(!0)), n.on(f, "onFinally", i(!1));
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
- i as to
180
+ h as to
178
181
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanyamate/sec",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "SEC. Store, Effect, Combine. Tiny state manager",
5
5
  "scripts": {
6
6
  "build": "vite build",