@vanyamate/sec 0.2.4 → 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(c,n){c&&n()},a=function(c,n=!0){const f=[],s={on:(o,t,e)=>(t==="onBefore"?o.onBefore((...r)=>u(n,()=>s.set(e(c,{args:r})))):t==="onSuccess"?o.onSuccess((r,...l)=>u(n,()=>s.set(e(c,{result:r,args:l})))):t==="onError"?o.onError((r,...l)=>u(n,()=>s.set(e(c,{error:r,args:l})))):o.onFinally((...r)=>u(n,()=>s.set(e(c,{args:r})))),s),get(){return c},set(o){c=o,f.forEach(t=>t(c))},subscribe(o){return f.push(o),()=>{const t=f.indexOf(o);~t&&f.splice(t,1)}},enableOn(o){return o.subscribe(()=>n=!0),s},disableOn(o){return o.subscribe(()=>n=!1),s}};return s},i=function(){return{afterAll:[],beforeAll:[],other:[]}},b=function(c){const n=i(),f=i(),s=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)),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(()=>{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":s.beforeAll.push(e);break;case"afterAll":s.afterAll.push(e);break;default:s.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(c,n,f=!0){let s=n(...c);const o=[];c.forEach(e=>{e.subscribe(()=>{u(f,()=>{s=n(...c),o.forEach(r=>r(s))})})});const t={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 o.push(e),()=>{const r=o.indexOf(e);~r&&o.splice(r,1)}},enableOn(e){return e.subscribe(()=>f=!1),t},disableOn(e){return e.subscribe(()=>f=!0),t}};return t},E=function(c){const n=[],f={on:(s,o)=>(s==="onBefore"?o.onBefore(()=>n.forEach(t=>t()),c):s==="onSuccess"?o.onSuccess(()=>n.forEach(t=>t()),c):s==="onError"?o.onError(()=>n.forEach(t=>t()),c):o.onFinally(()=>n.forEach(t=>t()),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};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
@@ -1,4 +1,4 @@
1
- export declare const combine: <State, States extends Array<any>>(stores: { [Index in keyof States]: Store<States[Index]>; }, callback: (...stores: { [K in keyof States]: Store<States[K]>; }) => State, enabled?: boolean) => Store<State>;
1
+ export declare const combine: <State, States extends Array<any>>(stores: { [Index in keyof States]: Store<States[Index]>; }, callback: (stores: { [K in keyof States]: Store<States[K]>; }) => State, enabled?: boolean) => Store<State>;
2
2
 
3
3
  export declare type Effect<AsyncAction extends EffectAction> = {
4
4
  (...args: Parameters<AsyncAction>): Promise<Awaited<ReturnType<AsyncAction>>>;
@@ -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
@@ -2,14 +2,14 @@ const u = function(c, n) {
2
2
  c && n();
3
3
  }, a = function(c, n = !0) {
4
4
  const f = [], t = {
5
- on: (o, s, e) => (s === "onBefore" ? o.onBefore(
5
+ on: (s, o, e) => (o === "onBefore" ? s.onBefore(
6
6
  (...r) => u(
7
7
  n,
8
8
  () => t.set(
9
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
15
  () => t.set(
@@ -19,7 +19,7 @@ const u = function(c, n) {
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
25
  () => t.set(
@@ -29,7 +29,7 @@ const u = function(c, n) {
29
29
  })
30
30
  )
31
31
  )
32
- ) : o.onFinally(
32
+ ) : s.onFinally(
33
33
  (...r) => u(
34
34
  n,
35
35
  () => t.set(
@@ -40,38 +40,38 @@ const u = function(c, n) {
40
40
  get() {
41
41
  return c;
42
42
  },
43
- set(o) {
44
- c = o, f.forEach((s) => s(c));
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), t;
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), t;
55
+ disableOn(s, o) {
56
+ return s.subscribe(() => n = !1), o !== void 0 && t.set(o), t;
57
57
  }
58
58
  };
59
59
  return t;
60
- }, h = function() {
60
+ }, i = function() {
61
61
  return {
62
62
  afterAll: [],
63
63
  beforeAll: [],
64
64
  other: []
65
65
  };
66
66
  }, b = function(c) {
67
- const n = h(), f = h(), t = h(), o = h(), s = async function(...e) {
67
+ const n = i(), f = i(), t = i(), s = i(), o = async function(...e) {
68
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
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(c, 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,7 +93,7 @@ const u = function(c, 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
99
  t.beforeAll.push(e);
@@ -104,29 +104,30 @@ const u = function(c, n) {
104
104
  default:
105
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;
118
+ }, o;
119
119
  }, A = function(c, n, f = !0) {
120
- let t = n(...c);
121
- const o = [];
120
+ console.log("Combine", c, n, f);
121
+ let t = n(c);
122
+ const s = [];
122
123
  c.forEach((e) => {
123
124
  e.subscribe(() => {
124
125
  u(f, () => {
125
- t = n(...c), o.forEach((r) => r(t));
126
+ t = n(c), s.forEach((r) => r(t));
126
127
  });
127
128
  });
128
129
  });
129
- const s = {
130
+ const o = {
130
131
  on: () => {
131
132
  throw new Error("Cannot call 'on' on combined store");
132
133
  },
@@ -137,34 +138,36 @@ const u = function(c, n) {
137
138
  throw new Error("Cannot call 'set' on combined store");
138
139
  },
139
140
  subscribe(e) {
140
- return o.push(e), () => {
141
- const r = o.indexOf(e);
142
- ~r && o.splice(r, 1);
141
+ return s.push(e), () => {
142
+ const r = s.indexOf(e);
143
+ ~r && s.splice(r, 1);
143
144
  };
144
145
  },
145
146
  enableOn(e) {
146
- return e.subscribe(() => f = !1), s;
147
+ return e.subscribe(() => f = !0), o;
147
148
  },
148
149
  disableOn(e) {
149
- return e.subscribe(() => f = !0), s;
150
+ return e.subscribe(() => f = !1), o;
150
151
  }
151
152
  };
152
- return s;
153
+ return o;
153
154
  }, E = function(c) {
154
155
  const n = [], f = {
155
- on: (t, o) => (t === "onBefore" ? o.onBefore(() => n.forEach((s) => s()), c) : t === "onSuccess" ? o.onSuccess(() => n.forEach((s) => s()), c) : t === "onError" ? o.onError(() => n.forEach((s) => s()), c) : o.onFinally(() => n.forEach((s) => s()), c), f),
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),
156
157
  subscribe: (t) => {
157
158
  n.push(t);
158
159
  }
159
160
  };
160
161
  return f;
161
- }, i = function(c) {
162
+ }, h = function(c) {
162
163
  return () => c;
163
164
  }, p = function(c) {
164
165
  const n = a(!1);
165
166
  return c.forEach((f) => {
166
- n.on(f, "onBefore", i(!0)), n.on(f, "onFinally", i(!1));
167
+ n.on(f, "onBefore", h(!0)), n.on(f, "onFinally", h(!1));
167
168
  }), n;
169
+ }, w = function() {
170
+ return (c, { result: n }) => n;
168
171
  };
169
172
  export {
170
173
  A as combine,
@@ -172,6 +175,7 @@ export {
172
175
  u as enableCheck,
173
176
  E as marker,
174
177
  p as pending,
178
+ w as result,
175
179
  a as store,
176
- i as to
180
+ h as to
177
181
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanyamate/sec",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "SEC. Store, Effect, Combine. Tiny state manager",
5
5
  "scripts": {
6
6
  "build": "vite build",