@vanyamate/sec 0.2.1 → 0.2.2

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.cjs CHANGED
@@ -1 +1 @@
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;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=function(o,c){o&&c()},b=function(o,c=!0){const f=[],t={on:(n,s,e)=>(s==="onBefore"?n.onBefore((...r)=>u(c,()=>{o=e(o,{args:r}),f.forEach(l=>l(o))})):s==="onSuccess"?n.onSuccess((r,...l)=>u(c,()=>{o=e(o,{result:r,args:l}),f.forEach(i=>i(o))})):s==="onError"?n.onError((r,...l)=>u(c,()=>{o=e(o,{error:r,args:l}),f.forEach(i=>i(o))})):n.onFinally((...r)=>u(c,()=>o=e(o,{args:r}))),t),get(){return o},set(n){o=n},subscribe(n){return f.push(n),()=>{const s=f.indexOf(n);~s&&f.splice(s,1)}},enableOn(n){return n.subscribe(()=>c=!0),t},disableOn(n){return n.subscribe(()=>c=!1),t}};return t},h=function(){return{afterAll:[],beforeAll:[],other:[]}},A=function(o){const c=h(),f=h(),t=h(),n=h(),s=async function(...e){return c.beforeAll.forEach(r=>r(...e)),c.other.forEach(r=>r(...e)),c.afterAll.forEach(r=>r(...e)),o(...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 t.beforeAll.forEach(l=>l(r,...e)),t.other.forEach(l=>l(r,...e)),t.afterAll.forEach(l=>l(r,...e)),r}).finally(()=>{n.beforeAll.forEach(r=>r(...e)),n.other.forEach(r=>r(...e)),n.afterAll.forEach(r=>r(...e))})};return s.onBefore=(e,r)=>{switch(r){case"beforeAll":c.beforeAll.push(e);break;case"afterAll":c.afterAll.push(e);break;default:c.other.push(e)}},s.onSuccess=(e,r)=>{switch(r){case"beforeAll":f.beforeAll.push(e);break;case"afterAll":f.afterAll.push(e);break;default:f.other.push(e)}},s.onError=(e,r)=>{switch(r){case"beforeAll":t.beforeAll.push(e);break;case"afterAll":t.afterAll.push(e);break;default:t.other.push(e)}},s.onFinally=(e,r)=>{switch(r){case"beforeAll":n.beforeAll.push(e);break;case"afterAll":n.afterAll.push(e);break;default:n.other.push(e)}},s},E=function(o,c,f=!0){let t=c(...o);const n=[];o.forEach(e=>{e.subscribe(()=>{u(f,()=>{t=c(...o),n.forEach(r=>r(t))})})});const s={on:()=>{throw new Error("Cannot call 'on' on combined store")},get(){return t},set(){throw new Error("Cannot call 'set' on combined store")},subscribe(e){return n.push(e),()=>{const r=n.indexOf(e);~r&&n.splice(r,1)}},enableOn(e){return e.subscribe(()=>f=!1),s},disableOn(e){return e.subscribe(()=>f=!0),s}};return s},p=function(o){const c=[],f={on:(t,n)=>(t==="onBefore"?n.onBefore(()=>c.forEach(s=>s()),o):t==="onSuccess"?n.onSuccess(()=>c.forEach(s=>s()),o):t==="onError"?n.onError(()=>c.forEach(s=>s()),o):n.onFinally(()=>c.forEach(s=>s()),o),f),subscribe:t=>{c.push(t)}};return f},a=function(o){return()=>o},k=function(o){const c=b(!1);return o.forEach(f=>{c.on(f,"onBefore",a(!0)),c.on(f,"onFinally",a(!1))}),c};exports.combine=E;exports.effect=A;exports.enableCheck=u;exports.marker=p;exports.pending=k;exports.store=b;exports.to=a;
package/dist/index.js CHANGED
@@ -1,167 +1,175 @@
1
- const u = function(s, o) {
2
- s && o();
3
- }, a = function(s, o = !0) {
4
- const c = {
5
- on: (n, f, t) => (f === "onBefore" ? n.onBefore(
6
- (...r) => u(
7
- o,
8
- () => s = t(s, { args: r })
1
+ const u = function(o, c) {
2
+ o && c();
3
+ }, b = function(o, c = !0) {
4
+ const f = [], l = {
5
+ on: (n, s, r) => (s === "onBefore" ? n.onBefore(
6
+ (...e) => u(
7
+ c,
8
+ () => {
9
+ o = r(o, { args: e }), f.forEach((t) => t(o));
10
+ }
9
11
  )
10
- ) : f === "onSuccess" ? n.onSuccess(
11
- (r, ...e) => u(
12
- o,
13
- () => s = t(s, {
14
- result: r,
15
- args: e
16
- })
12
+ ) : s === "onSuccess" ? n.onSuccess(
13
+ (e, ...t) => u(
14
+ c,
15
+ () => {
16
+ o = r(o, {
17
+ result: e,
18
+ args: t
19
+ }), f.forEach((i) => i(o));
20
+ }
17
21
  )
18
- ) : f === "onError" ? n.onError(
19
- (r, ...e) => u(
20
- o,
21
- () => s = t(s, {
22
- error: r,
23
- args: e
24
- })
22
+ ) : s === "onError" ? n.onError(
23
+ (e, ...t) => u(
24
+ c,
25
+ () => {
26
+ o = r(o, {
27
+ error: e,
28
+ args: t
29
+ }), f.forEach((i) => i(o));
30
+ }
25
31
  )
26
32
  ) : n.onFinally(
27
- (...r) => u(
28
- o,
29
- () => s = t(s, { args: r })
33
+ (...e) => u(
34
+ c,
35
+ () => o = r(o, { args: e })
30
36
  )
31
- ), c),
37
+ ), l),
32
38
  get() {
33
- return s;
39
+ return o;
34
40
  },
35
41
  set(n) {
36
- s = n;
42
+ o = n;
37
43
  },
38
44
  subscribe(n) {
39
- return () => {
45
+ return f.push(n), () => {
46
+ const s = f.indexOf(n);
47
+ ~s && f.splice(s, 1);
40
48
  };
41
49
  },
42
50
  enableOn(n) {
43
- return n.subscribe(() => o = !0), c;
51
+ return n.subscribe(() => c = !0), l;
44
52
  },
45
53
  disableOn(n) {
46
- return n.subscribe(() => o = !1), c;
54
+ return n.subscribe(() => c = !1), l;
47
55
  }
48
56
  };
49
- return c;
57
+ return l;
50
58
  }, h = function() {
51
59
  return {
52
60
  afterAll: [],
53
61
  beforeAll: [],
54
62
  other: []
55
63
  };
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;
64
+ }, A = function(o) {
65
+ const c = h(), f = h(), l = h(), n = h(), s = async function(...r) {
66
+ return c.beforeAll.forEach((e) => e(...r)), c.other.forEach((e) => e(...r)), c.afterAll.forEach((e) => e(...r)), o(...r).then((e) => (f.beforeAll.forEach((t) => t(e, ...r)), f.other.forEach((t) => t(e, ...r)), f.afterAll.forEach((t) => t(e, ...r)), e)).catch((e) => {
67
+ throw l.beforeAll.forEach((t) => t(e, ...r)), l.other.forEach((t) => t(e, ...r)), l.afterAll.forEach((t) => t(e, ...r)), e;
60
68
  }).finally(() => {
61
- f.beforeAll.forEach((e) => e(...r)), f.other.forEach((e) => e(...r)), f.afterAll.forEach((e) => e(...r));
69
+ n.beforeAll.forEach((e) => e(...r)), n.other.forEach((e) => e(...r)), n.afterAll.forEach((e) => e(...r));
62
70
  });
63
71
  };
64
- return t.onBefore = (r, e) => {
72
+ return s.onBefore = (r, e) => {
65
73
  switch (e) {
66
74
  case "beforeAll":
67
- o.beforeAll.push(r);
75
+ c.beforeAll.push(r);
68
76
  break;
69
77
  case "afterAll":
70
- o.afterAll.push(r);
78
+ c.afterAll.push(r);
71
79
  break;
72
80
  default:
73
- o.other.push(r);
81
+ c.other.push(r);
74
82
  }
75
- }, t.onSuccess = (r, e) => {
83
+ }, s.onSuccess = (r, e) => {
76
84
  switch (e) {
77
85
  case "beforeAll":
78
- c.beforeAll.push(r);
86
+ f.beforeAll.push(r);
79
87
  break;
80
88
  case "afterAll":
81
- c.afterAll.push(r);
89
+ f.afterAll.push(r);
82
90
  break;
83
91
  default:
84
- c.other.push(r);
92
+ f.other.push(r);
85
93
  }
86
- }, t.onError = (r, e) => {
94
+ }, s.onError = (r, e) => {
87
95
  switch (e) {
88
96
  case "beforeAll":
89
- n.beforeAll.push(r);
97
+ l.beforeAll.push(r);
90
98
  break;
91
99
  case "afterAll":
92
- n.afterAll.push(r);
100
+ l.afterAll.push(r);
93
101
  break;
94
102
  default:
95
- n.other.push(r);
103
+ l.other.push(r);
96
104
  }
97
- }, t.onFinally = (r, e) => {
105
+ }, s.onFinally = (r, e) => {
98
106
  switch (e) {
99
107
  case "beforeAll":
100
- f.beforeAll.push(r);
108
+ n.beforeAll.push(r);
101
109
  break;
102
110
  case "afterAll":
103
- f.afterAll.push(r);
111
+ n.afterAll.push(r);
104
112
  break;
105
113
  default:
106
- f.other.push(r);
114
+ n.other.push(r);
107
115
  }
108
- }, t;
109
- }, A = function(s, o, c = !0) {
110
- let n = o(...s);
111
- const f = [];
112
- s.forEach((r) => {
116
+ }, s;
117
+ }, E = function(o, c, f = !0) {
118
+ let l = c(...o);
119
+ const n = [];
120
+ o.forEach((r) => {
113
121
  r.subscribe(() => {
114
- u(c, () => {
115
- n = o(...s), f.forEach((e) => e(n));
122
+ u(f, () => {
123
+ l = c(...o), n.forEach((e) => e(l));
116
124
  });
117
125
  });
118
126
  });
119
- const t = {
127
+ const s = {
120
128
  on: () => {
121
129
  throw new Error("Cannot call 'on' on combined store");
122
130
  },
123
131
  get() {
124
- return n;
132
+ return l;
125
133
  },
126
134
  set() {
127
135
  throw new Error("Cannot call 'set' on combined store");
128
136
  },
129
137
  subscribe(r) {
130
- return f.push(r), () => {
131
- const e = f.indexOf(r);
132
- ~e && f.splice(e, 1);
138
+ return n.push(r), () => {
139
+ const e = n.indexOf(r);
140
+ ~e && n.splice(e, 1);
133
141
  };
134
142
  },
135
143
  enableOn(r) {
136
- return r.subscribe(() => c = !1), t;
144
+ return r.subscribe(() => f = !1), s;
137
145
  },
138
146
  disableOn(r) {
139
- return r.subscribe(() => c = !0), t;
147
+ return r.subscribe(() => f = !0), s;
140
148
  }
141
149
  };
142
- return t;
143
- }, E = function(s) {
144
- const o = [], c = {
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
- subscribe: (n) => {
147
- o.push(n);
150
+ return s;
151
+ }, p = function(o) {
152
+ const c = [], f = {
153
+ on: (l, n) => (l === "onBefore" ? n.onBefore(() => c.forEach((s) => s()), o) : l === "onSuccess" ? n.onSuccess(() => c.forEach((s) => s()), o) : l === "onError" ? n.onError(() => c.forEach((s) => s()), o) : n.onFinally(() => c.forEach((s) => s()), o), f),
154
+ subscribe: (l) => {
155
+ c.push(l);
148
156
  }
149
157
  };
150
- return c;
151
- }, i = function(s) {
152
- return () => s;
153
- }, p = function(s) {
154
- const o = a(!1);
155
- return s.forEach((c) => {
156
- o.on(c, "onBefore", i(!0)), o.on(c, "onFinally", i(!1));
157
- }), o;
158
+ return f;
159
+ }, a = function(o) {
160
+ return () => o;
161
+ }, w = function(o) {
162
+ const c = b(!1);
163
+ return o.forEach((f) => {
164
+ c.on(f, "onBefore", a(!0)), c.on(f, "onFinally", a(!1));
165
+ }), c;
158
166
  };
159
167
  export {
160
- A as combine,
161
- b as effect,
168
+ E as combine,
169
+ A as effect,
162
170
  u as enableCheck,
163
- E as marker,
164
- p as pending,
165
- a as store,
166
- i as to
171
+ p as marker,
172
+ w as pending,
173
+ b as store,
174
+ a as to
167
175
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanyamate/sec",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "SEC. Store, Effect, Combine. Tiny state manager",
5
5
  "scripts": {
6
6
  "build": "vite build",
@@ -64,25 +64,34 @@ export const store = function <State extends any> (state: State, enabled: boolea
64
64
  effect.onBefore((...args) =>
65
65
  enableCheck(
66
66
  enabled,
67
- () => state = (handler as StoreOnBeforeHandler<State, Action>)(state, { args }),
67
+ () => {
68
+ state = (handler as StoreOnBeforeHandler<State, Action>)(state, { args });
69
+ listeners.forEach((listener) => listener(state));
70
+ },
68
71
  ),
69
72
  );
70
73
  } else if (event === 'onSuccess') {
71
74
  effect.onSuccess((result, ...args) =>
72
75
  enableCheck(
73
76
  enabled,
74
- () => state = (handler as StoreOnSuccessHandler<State, Action>)(state, {
75
- result, args,
76
- }),
77
+ () => {
78
+ state = (handler as StoreOnSuccessHandler<State, Action>)(state, {
79
+ result, args,
80
+ });
81
+ listeners.forEach((listener) => listener(state));
82
+ },
77
83
  ),
78
84
  );
79
85
  } else if (event === 'onError') {
80
86
  effect.onError((error, ...args) =>
81
87
  enableCheck(
82
88
  enabled,
83
- () => state = (handler as StoreOnErrorHandler<State, Action>)(state, {
84
- error, args,
85
- }),
89
+ () => {
90
+ state = (handler as StoreOnErrorHandler<State, Action>)(state, {
91
+ error, args,
92
+ });
93
+ listeners.forEach((listener) => listener(state));
94
+ },
86
95
  ),
87
96
  );
88
97
  } else {