@vanyamate/sec 0.2.0 → 0.2.1

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
@@ -46,7 +46,7 @@ const loginAction = async function (loginData: LoginData): Promise<UserData> {
46
46
  const loginEffect = effect(loginAction);
47
47
 
48
48
  // Anywhere in code
49
- loginEffect();
49
+ loginEffect({ login: 'VanyaMate', password: 'qwerty12345' });
50
50
 
51
51
  ```
52
52
 
@@ -68,6 +68,8 @@ const logoutMarker = marker('afterAll').on('onSuccess', logoutEffect);
68
68
 
69
69
  ```
70
70
 
71
+ order of execution: 'beforeAll' -> undefined -> 'afterAll' (within one type (for example `onSuccess`))
72
+
71
73
  ### to
72
74
 
73
75
  Just helper. Returns a function that returns the passed value.
@@ -78,10 +80,33 @@ to(123); // Return () => 123
78
80
 
79
81
  ```
80
82
 
83
+ ```typescript
84
+ // before .on(logoutEffect, 'onSuccess', () => []);
85
+ // after .on(logoutEffect, 'onSuccess', to([]));
86
+ ```
87
+
81
88
  ### pending
82
89
 
83
90
  Just helper. wrapper over store. returns a bool value and is used to create a pending-store.
84
91
 
92
+ ```typescript
93
+ /**
94
+ *
95
+ * instead of
96
+ * const postsIsPending = store<boolean>(false)
97
+ * .on(getPostsForUser, 'onBefore', () => true)
98
+ * .on(getPostsForUser, 'onFinally', () => false)
99
+ * .on(createPostEffect, 'onBefore', () => true)
100
+ * .on(createPostEffect, 'onFinally', () => false);
101
+ *
102
+ */
103
+
104
+ const postsIsPending = pending([
105
+ getPostsForUser,
106
+ createPostEffect,
107
+ ]);
108
+ ```
109
+
85
110
  ### store
86
111
 
87
112
  Store stores data and subscribes to effect.
@@ -172,21 +197,29 @@ const posts = store<Array<Post>>([])
172
197
 
173
198
  ### Types
174
199
 
175
- instead of writing everything in `.on` - you can take out the handlers separately, setting the required type for them. `StoreEffectEventMap` is a generic and takes 2 parameters, and then the type is selected.
200
+ #### StoreHandlerMap
201
+
202
+ instead of writing everything in `.on` - you can take out the handlers separately, setting the required type for them.
203
+ `StoreHandlerMap` is a generic and takes 2 parameters, and then the type is selected.
204
+ first is type of store value. second is action signature. and then select type of subscribe.
205
+
206
+ ```
207
+ StoreHandlerMap<number, typeof getRandomId>['onSuccess']
208
+ ```
176
209
 
177
210
  ```typescript
178
211
  // Example:
179
212
 
180
213
  const getRandomId = async function () {
181
- return { id: Math.random() };
214
+ return { id: Math.random() };
182
215
  };
183
216
 
184
217
  const getRandomEffect = effect(getRandomId);
185
218
 
186
219
  const handler: StoreHandlerMap<number, typeof getRandomId>['onSuccess'] = function (state, { result }) {
187
- return result.id;
220
+ return result.id;
188
221
  };
189
222
 
190
223
  const num = store<number>(0)
191
- .on(getRandomEffect, 'onSuccess', handler);
224
+ .on(getRandomEffect, 'onSuccess', handler);
192
225
  ```
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=function(s,o){s&&o()},A=function(s,o=!0){const c={on:(n,f,t)=>(f==="onBefore"?n.onBefore((...e)=>u(o,()=>s=t(s,{args:e}))):f==="onSuccess"?n.onSuccess((e,...r)=>u(o,()=>s=t(s,{result:e,args:r}))):f==="onError"?n.onError((e,...r)=>u(o,()=>s=t(s,{error:e,args:r}))):n.onFinally((...e)=>u(o,()=>s=t(s,{args:e}))),c),get(){return s},set(n){s=n},subscribe(n){return()=>{}},enableOn(n){return n.subscribe(()=>o=!1),c},disableOn(n){return n.subscribe(()=>()=>o=!0),c}};return c},h=function(){return{afterAll:[],beforeAll:[],other:[]}},E=function(s){const o=h(),c=h(),n=h(),f=h(),t=async function(...e){return o.beforeAll.forEach(r=>r(...e)),o.other.forEach(r=>r(...e)),o.afterAll.forEach(r=>r(...e)),s(...e).then(r=>(c.beforeAll.forEach(l=>l(r,...e)),c.other.forEach(l=>l(r,...e)),c.afterAll.forEach(l=>l(r,...e)),r)).catch(r=>{throw n.beforeAll.forEach(l=>l(r,...e)),n.other.forEach(l=>l(r,...e)),n.afterAll.forEach(l=>l(r,...e)),r}).finally(()=>{f.beforeAll.forEach(r=>r(...e)),f.other.forEach(r=>r(...e)),f.afterAll.forEach(r=>r(...e))})};return t.onBefore=(e,r)=>{switch(r){case"beforeAll":o.beforeAll.push(e);break;case"afterAll":o.afterAll.push(e);break;default:o.other.push(e)}},t.onSuccess=(e,r)=>{switch(r){case"beforeAll":c.beforeAll.push(e);break;case"afterAll":c.afterAll.push(e);break;default:c.other.push(e)}},t.onError=(e,r)=>{switch(r){case"beforeAll":n.beforeAll.push(e);break;case"afterAll":n.afterAll.push(e);break;default:n.other.push(e)}},t.onFinally=(e,r)=>{switch(r){case"beforeAll":f.beforeAll.push(e);break;case"afterAll":f.afterAll.push(e);break;default:f.other.push(e)}},t},i=function(s,o,c=!0){let n=o(...s);const f=[];s.forEach(e=>{e.subscribe(()=>{u(c,()=>{n=o(...s),f.forEach(r=>r(n))})})});const t={on:()=>{throw new Error("Cannot call 'on' on combined store")},get(){return n},set(){throw new Error("Cannot call 'set' on combined store")},subscribe(e){return f.push(e),()=>{const r=f.indexOf(e);~r&&f.splice(r,1)}},enableOn(e){return e.subscribe(()=>c=!1),t},disableOn(e){return e.subscribe(()=>c=!0),t}};return t},a=function(s){const o=[],c={on:(n,f)=>(n==="onBefore"?f.onBefore(()=>o.forEach(t=>t()),s):n==="onSuccess"?f.onSuccess(()=>o.forEach(t=>t()),s):n==="onError"?f.onError(()=>o.forEach(t=>t()),s):f.onFinally(()=>o.forEach(t=>t()),s),c),subscribe:n=>{o.push(n)}};return c},b=function(s){return()=>s},p=function(s){const o=A(!1);return s.forEach(c=>{o.on(c,"onBefore",b(!0)),o.on(c,"onFinally",b(!1))}),o};exports.combine=i;exports.effect=E;exports.enableCheck=u;exports.marker=a;exports.pending=p;exports.store=A;exports.to=b;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=function(t,o){t&&o()},a=function(t,o=!0){const c={on:(n,l,s)=>(l==="onBefore"?n.onBefore((...e)=>u(o,()=>t=s(t,{args:e}))):l==="onSuccess"?n.onSuccess((e,...r)=>u(o,()=>t=s(t,{result:e,args:r}))):l==="onError"?n.onError((e,...r)=>u(o,()=>t=s(t,{error:e,args:r}))):n.onFinally((...e)=>u(o,()=>t=s(t,{args:e}))),c),get(){return t},set(n){t=n},subscribe(n){return()=>{}},enableOn(n){return n.subscribe(()=>o=!0),c},disableOn(n){return n.subscribe(()=>o=!1),c}};return c},i=function(){return{afterAll:[],beforeAll:[],other:[]}},b=function(t){const o=i(),c=i(),n=i(),l=i(),s=async function(...e){return o.beforeAll.forEach(r=>r(...e)),o.other.forEach(r=>r(...e)),o.afterAll.forEach(r=>r(...e)),t(...e).then(r=>(c.beforeAll.forEach(f=>f(r,...e)),c.other.forEach(f=>f(r,...e)),c.afterAll.forEach(f=>f(r,...e)),r)).catch(r=>{throw n.beforeAll.forEach(f=>f(r,...e)),n.other.forEach(f=>f(r,...e)),n.afterAll.forEach(f=>f(r,...e)),r}).finally(()=>{l.beforeAll.forEach(r=>r(...e)),l.other.forEach(r=>r(...e)),l.afterAll.forEach(r=>r(...e))})};return s.onBefore=(e,r)=>{switch(r){case"beforeAll":o.beforeAll.push(e);break;case"afterAll":o.afterAll.push(e);break;default:o.other.push(e)}},s.onSuccess=(e,r)=>{switch(r){case"beforeAll":c.beforeAll.push(e);break;case"afterAll":c.afterAll.push(e);break;default:c.other.push(e)}},s.onError=(e,r)=>{switch(r){case"beforeAll":n.beforeAll.push(e);break;case"afterAll":n.afterAll.push(e);break;default:n.other.push(e)}},s.onFinally=(e,r)=>{switch(r){case"beforeAll":l.beforeAll.push(e);break;case"afterAll":l.afterAll.push(e);break;default:l.other.push(e)}},s},A=function(t,o,c=!0){let n=o(...t);const l=[];t.forEach(e=>{e.subscribe(()=>{u(c,()=>{n=o(...t),l.forEach(r=>r(n))})})});const s={on:()=>{throw new Error("Cannot call 'on' on combined store")},get(){return n},set(){throw new Error("Cannot call 'set' on combined store")},subscribe(e){return l.push(e),()=>{const r=l.indexOf(e);~r&&l.splice(r,1)}},enableOn(e){return e.subscribe(()=>c=!1),s},disableOn(e){return e.subscribe(()=>c=!0),s}};return s},E=function(t){const o=[],c={on:(n,l)=>(n==="onBefore"?l.onBefore(()=>o.forEach(s=>s()),t):n==="onSuccess"?l.onSuccess(()=>o.forEach(s=>s()),t):n==="onError"?l.onError(()=>o.forEach(s=>s()),t):l.onFinally(()=>o.forEach(s=>s()),t),c),subscribe:n=>{o.push(n)}};return c},h=function(t){return()=>t},p=function(t){const o=a(!1);return t.forEach(c=>{o.on(c,"onBefore",h(!0)),o.on(c,"onFinally",h(!1))}),o};exports.combine=A;exports.effect=b;exports.enableCheck=u;exports.marker=E;exports.pending=p;exports.store=a;exports.to=h;
package/dist/index.d.ts CHANGED
@@ -50,7 +50,7 @@ export declare type Store<State> = {
50
50
  subscribe: (listener: StoreListener<State>) => () => void;
51
51
  };
52
52
 
53
- export declare const store: <State extends any>(initialData: State, enabled?: boolean) => Store<State>;
53
+ export declare const store: <State extends any>(state: State, enabled?: boolean) => Store<State>;
54
54
 
55
55
  export declare type StoreEffectSubscribe<State> = <Action extends EffectAction, Event extends keyof StoreHandlerMap<State, Action>>(effect: Effect<Action>, event: Event, handler: StoreHandlerMap<State, Action>[Event]) => Store<State>;
56
56
 
package/dist/index.js CHANGED
@@ -1,32 +1,32 @@
1
- const l = function(s, o) {
1
+ const u = function(s, o) {
2
2
  s && o();
3
- }, A = function(s, o = !0) {
3
+ }, a = function(s, o = !0) {
4
4
  const c = {
5
5
  on: (n, f, t) => (f === "onBefore" ? n.onBefore(
6
- (...e) => l(
6
+ (...r) => u(
7
7
  o,
8
- () => s = t(s, { args: e })
8
+ () => s = t(s, { args: r })
9
9
  )
10
10
  ) : f === "onSuccess" ? n.onSuccess(
11
- (e, ...r) => l(
11
+ (r, ...e) => u(
12
12
  o,
13
13
  () => s = t(s, {
14
- result: e,
15
- args: r
14
+ result: r,
15
+ args: e
16
16
  })
17
17
  )
18
18
  ) : f === "onError" ? n.onError(
19
- (e, ...r) => l(
19
+ (r, ...e) => u(
20
20
  o,
21
21
  () => s = t(s, {
22
- error: e,
23
- args: r
22
+ error: r,
23
+ args: e
24
24
  })
25
25
  )
26
26
  ) : n.onFinally(
27
- (...e) => l(
27
+ (...r) => u(
28
28
  o,
29
- () => s = t(s, { args: e })
29
+ () => s = t(s, { args: r })
30
30
  )
31
31
  ), c),
32
32
  get() {
@@ -40,10 +40,10 @@ const l = function(s, o) {
40
40
  };
41
41
  },
42
42
  enableOn(n) {
43
- return n.subscribe(() => o = !1), c;
43
+ return n.subscribe(() => o = !0), c;
44
44
  },
45
45
  disableOn(n) {
46
- return n.subscribe(() => () => o = !0), c;
46
+ return n.subscribe(() => o = !1), c;
47
47
  }
48
48
  };
49
49
  return c;
@@ -53,66 +53,66 @@ const l = function(s, o) {
53
53
  beforeAll: [],
54
54
  other: []
55
55
  };
56
- }, E = function(s) {
57
- const o = h(), c = h(), n = h(), f = h(), t = async function(...e) {
58
- return o.beforeAll.forEach((r) => r(...e)), o.other.forEach((r) => r(...e)), o.afterAll.forEach((r) => r(...e)), s(...e).then((r) => (c.beforeAll.forEach((u) => u(r, ...e)), c.other.forEach((u) => u(r, ...e)), c.afterAll.forEach((u) => u(r, ...e)), r)).catch((r) => {
59
- throw n.beforeAll.forEach((u) => u(r, ...e)), n.other.forEach((u) => u(r, ...e)), n.afterAll.forEach((u) => u(r, ...e)), r;
56
+ }, b = function(s) {
57
+ const o = h(), c = h(), n = h(), f = h(), t = async function(...r) {
58
+ return o.beforeAll.forEach((e) => e(...r)), o.other.forEach((e) => e(...r)), o.afterAll.forEach((e) => e(...r)), s(...r).then((e) => (c.beforeAll.forEach((l) => l(e, ...r)), c.other.forEach((l) => l(e, ...r)), c.afterAll.forEach((l) => l(e, ...r)), e)).catch((e) => {
59
+ throw n.beforeAll.forEach((l) => l(e, ...r)), n.other.forEach((l) => l(e, ...r)), n.afterAll.forEach((l) => l(e, ...r)), e;
60
60
  }).finally(() => {
61
- f.beforeAll.forEach((r) => r(...e)), f.other.forEach((r) => r(...e)), f.afterAll.forEach((r) => r(...e));
61
+ f.beforeAll.forEach((e) => e(...r)), f.other.forEach((e) => e(...r)), f.afterAll.forEach((e) => e(...r));
62
62
  });
63
63
  };
64
- return t.onBefore = (e, r) => {
65
- switch (r) {
64
+ return t.onBefore = (r, e) => {
65
+ switch (e) {
66
66
  case "beforeAll":
67
- o.beforeAll.push(e);
67
+ o.beforeAll.push(r);
68
68
  break;
69
69
  case "afterAll":
70
- o.afterAll.push(e);
70
+ o.afterAll.push(r);
71
71
  break;
72
72
  default:
73
- o.other.push(e);
73
+ o.other.push(r);
74
74
  }
75
- }, t.onSuccess = (e, r) => {
76
- switch (r) {
75
+ }, t.onSuccess = (r, e) => {
76
+ switch (e) {
77
77
  case "beforeAll":
78
- c.beforeAll.push(e);
78
+ c.beforeAll.push(r);
79
79
  break;
80
80
  case "afterAll":
81
- c.afterAll.push(e);
81
+ c.afterAll.push(r);
82
82
  break;
83
83
  default:
84
- c.other.push(e);
84
+ c.other.push(r);
85
85
  }
86
- }, t.onError = (e, r) => {
87
- switch (r) {
86
+ }, t.onError = (r, e) => {
87
+ switch (e) {
88
88
  case "beforeAll":
89
- n.beforeAll.push(e);
89
+ n.beforeAll.push(r);
90
90
  break;
91
91
  case "afterAll":
92
- n.afterAll.push(e);
92
+ n.afterAll.push(r);
93
93
  break;
94
94
  default:
95
- n.other.push(e);
95
+ n.other.push(r);
96
96
  }
97
- }, t.onFinally = (e, r) => {
98
- switch (r) {
97
+ }, t.onFinally = (r, e) => {
98
+ switch (e) {
99
99
  case "beforeAll":
100
- f.beforeAll.push(e);
100
+ f.beforeAll.push(r);
101
101
  break;
102
102
  case "afterAll":
103
- f.afterAll.push(e);
103
+ f.afterAll.push(r);
104
104
  break;
105
105
  default:
106
- f.other.push(e);
106
+ f.other.push(r);
107
107
  }
108
108
  }, t;
109
- }, a = function(s, o, c = !0) {
109
+ }, A = function(s, o, c = !0) {
110
110
  let n = o(...s);
111
111
  const f = [];
112
- s.forEach((e) => {
113
- e.subscribe(() => {
114
- l(c, () => {
115
- n = o(...s), f.forEach((r) => r(n));
112
+ s.forEach((r) => {
113
+ r.subscribe(() => {
114
+ u(c, () => {
115
+ n = o(...s), f.forEach((e) => e(n));
116
116
  });
117
117
  });
118
118
  });
@@ -126,21 +126,21 @@ const l = function(s, o) {
126
126
  set() {
127
127
  throw new Error("Cannot call 'set' on combined store");
128
128
  },
129
- subscribe(e) {
130
- return f.push(e), () => {
131
- const r = f.indexOf(e);
132
- ~r && f.splice(r, 1);
129
+ subscribe(r) {
130
+ return f.push(r), () => {
131
+ const e = f.indexOf(r);
132
+ ~e && f.splice(e, 1);
133
133
  };
134
134
  },
135
- enableOn(e) {
136
- return e.subscribe(() => c = !1), t;
135
+ enableOn(r) {
136
+ return r.subscribe(() => c = !1), t;
137
137
  },
138
- disableOn(e) {
139
- return e.subscribe(() => c = !0), t;
138
+ disableOn(r) {
139
+ return r.subscribe(() => c = !0), t;
140
140
  }
141
141
  };
142
142
  return t;
143
- }, i = function(s) {
143
+ }, E = function(s) {
144
144
  const o = [], c = {
145
145
  on: (n, f) => (n === "onBefore" ? f.onBefore(() => o.forEach((t) => t()), s) : n === "onSuccess" ? f.onSuccess(() => o.forEach((t) => t()), s) : n === "onError" ? f.onError(() => o.forEach((t) => t()), s) : f.onFinally(() => o.forEach((t) => t()), s), c),
146
146
  subscribe: (n) => {
@@ -148,20 +148,20 @@ const l = function(s, o) {
148
148
  }
149
149
  };
150
150
  return c;
151
- }, b = function(s) {
151
+ }, i = function(s) {
152
152
  return () => s;
153
153
  }, p = function(s) {
154
- const o = A(!1);
154
+ const o = a(!1);
155
155
  return s.forEach((c) => {
156
- o.on(c, "onBefore", b(!0)), o.on(c, "onFinally", b(!1));
156
+ o.on(c, "onBefore", i(!0)), o.on(c, "onFinally", i(!1));
157
157
  }), o;
158
158
  };
159
159
  export {
160
- a as combine,
161
- E as effect,
162
- l as enableCheck,
163
- i as marker,
160
+ A as combine,
161
+ b as effect,
162
+ u as enableCheck,
163
+ E as marker,
164
164
  p as pending,
165
- A as store,
166
- b as to
165
+ a as store,
166
+ i as to
167
167
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanyamate/sec",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "SEC. Store, Effect, Combine. Tiny state manager",
5
5
  "scripts": {
6
6
  "build": "vite build",
@@ -52,7 +52,7 @@ export const enableCheck = function (enabled: boolean, callback: () => void) {
52
52
  }
53
53
  };
54
54
 
55
- export const store = function <State extends any> (initialData: State, enabled: boolean = true): Store<State> {
55
+ export const store = function <State extends any> (state: State, enabled: boolean = true): Store<State> {
56
56
  const listeners: Array<StoreListener<State>> = [];
57
57
 
58
58
  const storeApi: Store<State> = {
@@ -64,14 +64,14 @@ export const store = function <State extends any> (initialData: State, enabled:
64
64
  effect.onBefore((...args) =>
65
65
  enableCheck(
66
66
  enabled,
67
- () => initialData = (handler as StoreOnBeforeHandler<State, Action>)(initialData, { args }),
67
+ () => state = (handler as StoreOnBeforeHandler<State, Action>)(state, { args }),
68
68
  ),
69
69
  );
70
70
  } else if (event === 'onSuccess') {
71
71
  effect.onSuccess((result, ...args) =>
72
72
  enableCheck(
73
73
  enabled,
74
- () => initialData = (handler as StoreOnSuccessHandler<State, Action>)(initialData, {
74
+ () => state = (handler as StoreOnSuccessHandler<State, Action>)(state, {
75
75
  result, args,
76
76
  }),
77
77
  ),
@@ -80,7 +80,7 @@ export const store = function <State extends any> (initialData: State, enabled:
80
80
  effect.onError((error, ...args) =>
81
81
  enableCheck(
82
82
  enabled,
83
- () => initialData = (handler as StoreOnErrorHandler<State, Action>)(initialData, {
83
+ () => state = (handler as StoreOnErrorHandler<State, Action>)(state, {
84
84
  error, args,
85
85
  }),
86
86
  ),
@@ -89,7 +89,7 @@ export const store = function <State extends any> (initialData: State, enabled:
89
89
  effect.onFinally((...args) =>
90
90
  enableCheck(
91
91
  enabled,
92
- () => initialData = (handler as StoreOnFinallyHandler<State, Action>)(initialData, { args }),
92
+ () => state = (handler as StoreOnFinallyHandler<State, Action>)(state, { args }),
93
93
  ),
94
94
  );
95
95
  }
@@ -97,10 +97,10 @@ export const store = function <State extends any> (initialData: State, enabled:
97
97
  return storeApi;
98
98
  },
99
99
  get (): State {
100
- return initialData;
100
+ return state;
101
101
  },
102
102
  set (value: State) {
103
- initialData = value;
103
+ state = value;
104
104
  },
105
105
  subscribe (listener: StoreListener<State>) {
106
106
  listeners.push(listener);
@@ -112,11 +112,11 @@ export const store = function <State extends any> (initialData: State, enabled:
112
112
  };
113
113
  },
114
114
  enableOn (marker: Marker<State>) {
115
- marker.subscribe(() => enabled = false);
115
+ marker.subscribe(() => enabled = true);
116
116
  return storeApi;
117
117
  },
118
118
  disableOn (marker: Marker<State>) {
119
- marker.subscribe(() => () => enabled = true);
119
+ marker.subscribe(() => enabled = false);
120
120
  return storeApi;
121
121
  },
122
122
  };