@vanyamate/sec 0.3.0 → 0.3.1-batch-beta.0

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
@@ -161,8 +161,10 @@ const postsIsPending = pending([
161
161
 
162
162
  Store stores data and subscribes to effect.
163
163
 
164
- When initializing, the store takes 2 values. The first value is the initialization data. The second value is optional -
165
- enable/disable. When disabled, the store will not be updated.
164
+ When initializing, the store takes 2 values. The first value is the initialization data. The second value is settings.
165
+
166
+ - `enabled` - default is `true`. When disabled, the store will not be updated.
167
+ - `instantListenerExecution` - default is `false`. When enabled, a listener will instant executed when subscribe.
166
168
 
167
169
  The store has an api.
168
170
 
@@ -186,7 +188,7 @@ The store has an api.
186
188
 
187
189
  ```typescript
188
190
 
189
- const authIsPending = store<boolean>(false)
191
+ const authIsPending = store<boolean>(false, { enabled: true, instantListenerExecution: true })
190
192
  .on(loginEffect, 'onBefore', () => true)
191
193
  .on(loginEffect, 'onFinally', to(false)); // instead of () => false
192
194
 
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=function(n,t){n&&t()},A=function(n,t={enabled:!0,instantListenerExecution:!1}){const l=new Set;let{enabled:s=!0,instantListenerExecution:c=!1}=t;const o={on:(e,r,f)=>(r==="onBefore"?e.onBefore((...u)=>i(s,()=>o.set(f(n,{args:u})))):r==="onSuccess"?e.onSuccess((u,...h)=>i(s,()=>o.set(f(n,{result:u,args:h})))):r==="onError"?e.onError((u,...h)=>i(s,()=>o.set(f(n,{error:u,args:h})))):e.onFinally((...u)=>i(s,()=>o.set(f(n,{args:u})))),o),get(){return n},set(e){n=e,l.forEach(r=>r(n))},subscribe(e){return l.add(e),c&&e(n),()=>l.delete(e)},enableOn(e,r){return e.subscribe(()=>s=!0),r!==void 0&&o.set(r),o},disableOn(e,r){return e.subscribe(()=>s=!1),r!==void 0&&o.set(r),o}};return o},a=function(){return{afterAll:[],beforeAll:[],other:[]}},E=function(n){const t=a(),l=a(),s=a(),c=a(),o=async function(...e){return t.beforeAll.forEach(r=>r(...e)),t.other.forEach(r=>r(...e)),t.afterAll.forEach(r=>r(...e)),n(...e).then(r=>(l.beforeAll.forEach(f=>f(r,...e)),l.other.forEach(f=>f(r,...e)),l.afterAll.forEach(f=>f(r,...e)),r)).catch(r=>{throw s.beforeAll.forEach(f=>f(r,...e)),s.other.forEach(f=>f(r,...e)),s.afterAll.forEach(f=>f(r,...e)),r}).finally(()=>{c.beforeAll.forEach(r=>r(...e)),c.other.forEach(r=>r(...e)),c.afterAll.forEach(r=>r(...e))})};return o.onBefore=(e,r)=>{switch(r){case"beforeAll":t.beforeAll.push(e);break;case"afterAll":t.afterAll.push(e);break;default:t.other.push(e)}},o.onSuccess=(e,r)=>{switch(r){case"beforeAll":l.beforeAll.push(e);break;case"afterAll":l.afterAll.push(e);break;default:l.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":c.beforeAll.push(e);break;case"afterAll":c.afterAll.push(e);break;default:c.other.push(e)}},o},p=function(n,t,l=!0){console.log("Combine",n,t,l);let s=t(n);const c=[];n.forEach(e=>{e.subscribe(()=>{i(l,()=>{s=t(n),c.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 c.push(e),()=>{const r=c.indexOf(e);~r&&c.splice(r,1)}},enableOn(e){return e.subscribe(()=>l=!0),o},disableOn(e){return e.subscribe(()=>l=!1),o}};return o},d=function(n){const t=[],l={on:(s,c)=>(s==="onBefore"?c.onBefore(()=>t.forEach(o=>o()),n):s==="onSuccess"?c.onSuccess(()=>t.forEach(o=>o()),n):s==="onError"?c.onError(()=>t.forEach(o=>o()),n):c.onFinally(()=>t.forEach(o=>o()),n),l),subscribe:s=>{t.push(s)}};return l},b=function(n){return()=>n},m=function(n){const t=A(!1);return n.forEach(l=>{t.on(l,"onBefore",b(!0)),t.on(l,"onFinally",b(!1))}),t},w=function(){return(n,{result:t})=>t};exports.combine=p;exports.effect=E;exports.enableCheck=i;exports.marker=d;exports.pending=m;exports.result=w;exports.store=A;exports.to=b;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});let m=0,A=[];const E=()=>m+=1,d=()=>{m-=1,w()&&B()},w=()=>m==0,k=o=>{w()?o():A.push(o)},B=()=>{A.forEach(o=>o()),A.length=0},i=function(o,n){o&&n()},y=function(o,n={enabled:!0,instantListenerExecution:!1}){const c=new Set;let l,{enabled:t=!0,instantListenerExecution:f=!1}=n;const e={on:(r,s,a)=>(s==="onBefore"?r.onBefore((...u)=>i(t,()=>e.set(a(o,{args:u})))):s==="onSuccess"?r.onSuccess((u,...b)=>i(t,()=>e.set(a(o,{result:u,args:b})))):s==="onError"?r.onError((u,...b)=>i(t,()=>e.set(a(o,{error:u,args:b})))):r.onFinally((...u)=>i(t,()=>e.set(a(o,{args:u})))),e),get(){return o},set(r){o=r,k(()=>{l!=o&&(l=o,c.forEach(s=>s(o)))})},subscribe(r){return c.add(r),f&&r(o),()=>c.delete(r)},enableOn(r,s){return r.subscribe(()=>{t=!0,s!==void 0&&(E(),queueMicrotask(()=>{d(),e.set(s)}))}),e},disableOn(r,s){return r.subscribe(()=>{t=!1,s!==void 0&&(E(),queueMicrotask(()=>{d(),e.set(s)}))}),e}};return e},h=function(){return{afterAll:[],beforeAll:[],other:[]}},C=function(o){const n=h(),c=h(),l=h(),t=h(),f=async function(...e){E();try{return n.beforeAll.forEach(r=>r(...e)),n.other.forEach(r=>r(...e)),n.afterAll.forEach(r=>r(...e)),await o(...e).then(r=>(c.beforeAll.forEach(s=>s(r,...e)),c.other.forEach(s=>s(r,...e)),c.afterAll.forEach(s=>s(r,...e)),r)).catch(r=>{throw l.beforeAll.forEach(s=>s(r,...e)),l.other.forEach(s=>s(r,...e)),l.afterAll.forEach(s=>s(r,...e)),r}).finally(()=>{t.beforeAll.forEach(r=>r(...e)),t.other.forEach(r=>r(...e)),t.afterAll.forEach(r=>r(...e))})}finally{d()}};return f.onBefore=(e,r)=>{switch(r){case"beforeAll":n.beforeAll.push(e);break;case"afterAll":n.afterAll.push(e);break;default:n.other.push(e)}},f.onSuccess=(e,r)=>{switch(r){case"beforeAll":c.beforeAll.push(e);break;case"afterAll":c.afterAll.push(e);break;default:c.other.push(e)}},f.onError=(e,r)=>{switch(r){case"beforeAll":l.beforeAll.push(e);break;case"afterAll":l.afterAll.push(e);break;default:l.other.push(e)}},f.onFinally=(e,r)=>{switch(r){case"beforeAll":t.beforeAll.push(e);break;case"afterAll":t.afterAll.push(e);break;default:t.other.push(e)}},f},S=function(o,n,c=!0){console.log("Combine",o,n,c);let l=n(o);const t=[];o.forEach(e=>{e.subscribe(()=>{i(c,()=>{l=n(o),t.forEach(r=>r(l))})})});const f={on:()=>{throw new Error("Cannot call 'on' on combined store")},get(){return l},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(()=>c=!0),f},disableOn(e){return e.subscribe(()=>c=!1),f}};return f},F=function(o){const n=[],c={on:(l,t)=>(l==="onBefore"?t.onBefore(()=>n.forEach(f=>f()),o):l==="onSuccess"?t.onSuccess(()=>n.forEach(f=>f()),o):l==="onError"?t.onError(()=>n.forEach(f=>f()),o):t.onFinally(()=>n.forEach(f=>f()),o),c),subscribe:l=>{n.push(l)}};return c},p=function(o){return()=>o},O=function(o){const n=y(!1);return o.forEach(c=>{n.on(c,"onBefore",p(!0)),n.on(c,"onFinally",p(!1))}),n},x=function(){return(o,{result:n})=>n};exports.combine=S;exports.effect=C;exports.enableCheck=i;exports.marker=F;exports.pending=O;exports.result=x;exports.store=y;exports.to=p;
package/dist/index.js CHANGED
@@ -1,180 +1,202 @@
1
- const i = function(n, t) {
2
- n && t();
3
- }, A = function(n, t = { enabled: !0, instantListenerExecution: !1 }) {
4
- const l = /* @__PURE__ */ new Set();
5
- let { enabled: s = !0, instantListenerExecution: c = !1 } = t;
6
- const o = {
7
- on: (e, r, f) => (r === "onBefore" ? e.onBefore(
1
+ let d = 0, A = [];
2
+ const E = () => d += 1, p = () => {
3
+ d -= 1, B() && C();
4
+ }, B = () => d == 0, y = (o) => {
5
+ B() ? o() : A.push(o);
6
+ }, C = () => {
7
+ A.forEach((o) => o()), A.length = 0;
8
+ }, i = function(o, n) {
9
+ o && n();
10
+ }, k = function(o, n = { enabled: !0, instantListenerExecution: !1 }) {
11
+ const c = /* @__PURE__ */ new Set();
12
+ let l, { enabled: t = !0, instantListenerExecution: f = !1 } = n;
13
+ const e = {
14
+ on: (r, s, a) => (s === "onBefore" ? r.onBefore(
8
15
  (...u) => i(
9
- s,
10
- () => o.set(
11
- f(n, { args: u })
16
+ t,
17
+ () => e.set(
18
+ a(o, { args: u })
12
19
  )
13
20
  )
14
- ) : r === "onSuccess" ? e.onSuccess(
15
- (u, ...a) => i(
16
- s,
17
- () => o.set(
18
- f(n, {
21
+ ) : s === "onSuccess" ? r.onSuccess(
22
+ (u, ...b) => i(
23
+ t,
24
+ () => e.set(
25
+ a(o, {
19
26
  result: u,
20
- args: a
27
+ args: b
21
28
  })
22
29
  )
23
30
  )
24
- ) : r === "onError" ? e.onError(
25
- (u, ...a) => i(
26
- s,
27
- () => o.set(
28
- f(n, {
31
+ ) : s === "onError" ? r.onError(
32
+ (u, ...b) => i(
33
+ t,
34
+ () => e.set(
35
+ a(o, {
29
36
  error: u,
30
- args: a
37
+ args: b
31
38
  })
32
39
  )
33
40
  )
34
- ) : e.onFinally(
41
+ ) : r.onFinally(
35
42
  (...u) => i(
36
- s,
37
- () => o.set(
38
- f(n, { args: u })
43
+ t,
44
+ () => e.set(
45
+ a(o, { args: u })
39
46
  )
40
47
  )
41
- ), o),
48
+ ), e),
42
49
  get() {
43
- return n;
50
+ return o;
44
51
  },
45
- set(e) {
46
- n = e, l.forEach((r) => r(n));
52
+ set(r) {
53
+ o = r, y(() => {
54
+ l != o && (l = o, c.forEach((s) => s(o)));
55
+ });
47
56
  },
48
- subscribe(e) {
49
- return l.add(e), c && e(n), () => l.delete(e);
57
+ subscribe(r) {
58
+ return c.add(r), f && r(o), () => c.delete(r);
50
59
  },
51
- enableOn(e, r) {
52
- return e.subscribe(() => s = !0), r !== void 0 && o.set(r), o;
60
+ enableOn(r, s) {
61
+ return r.subscribe(() => {
62
+ t = !0, s !== void 0 && (E(), queueMicrotask(() => {
63
+ p(), e.set(s);
64
+ }));
65
+ }), e;
53
66
  },
54
- disableOn(e, r) {
55
- return e.subscribe(() => s = !1), r !== void 0 && o.set(r), o;
67
+ disableOn(r, s) {
68
+ return r.subscribe(() => {
69
+ t = !1, s !== void 0 && (E(), queueMicrotask(() => {
70
+ p(), e.set(s);
71
+ }));
72
+ }), e;
56
73
  }
57
74
  };
58
- return o;
75
+ return e;
59
76
  }, h = function() {
60
77
  return {
61
78
  afterAll: [],
62
79
  beforeAll: [],
63
80
  other: []
64
81
  };
65
- }, E = function(n) {
66
- const t = h(), l = h(), s = h(), c = h(), o = async function(...e) {
67
- return t.beforeAll.forEach((r) => r(...e)), t.other.forEach((r) => r(...e)), t.afterAll.forEach((r) => r(...e)), n(...e).then((r) => (l.beforeAll.forEach((f) => f(r, ...e)), l.other.forEach((f) => f(r, ...e)), l.afterAll.forEach((f) => f(r, ...e)), r)).catch((r) => {
68
- throw s.beforeAll.forEach((f) => f(r, ...e)), s.other.forEach((f) => f(r, ...e)), s.afterAll.forEach((f) => f(r, ...e)), r;
69
- }).finally(() => {
70
- c.beforeAll.forEach((r) => r(...e)), c.other.forEach((r) => r(...e)), c.afterAll.forEach((r) => r(...e));
71
- });
82
+ }, m = function(o) {
83
+ const n = h(), c = h(), l = h(), t = h(), f = async function(...e) {
84
+ E();
85
+ try {
86
+ return n.beforeAll.forEach((r) => r(...e)), n.other.forEach((r) => r(...e)), n.afterAll.forEach((r) => r(...e)), await o(...e).then((r) => (c.beforeAll.forEach((s) => s(r, ...e)), c.other.forEach((s) => s(r, ...e)), c.afterAll.forEach((s) => s(r, ...e)), r)).catch((r) => {
87
+ throw l.beforeAll.forEach((s) => s(r, ...e)), l.other.forEach((s) => s(r, ...e)), l.afterAll.forEach((s) => s(r, ...e)), r;
88
+ }).finally(() => {
89
+ t.beforeAll.forEach((r) => r(...e)), t.other.forEach((r) => r(...e)), t.afterAll.forEach((r) => r(...e));
90
+ });
91
+ } finally {
92
+ p();
93
+ }
72
94
  };
73
- return o.onBefore = (e, r) => {
95
+ return f.onBefore = (e, r) => {
74
96
  switch (r) {
75
97
  case "beforeAll":
76
- t.beforeAll.push(e);
98
+ n.beforeAll.push(e);
77
99
  break;
78
100
  case "afterAll":
79
- t.afterAll.push(e);
101
+ n.afterAll.push(e);
80
102
  break;
81
103
  default:
82
- t.other.push(e);
104
+ n.other.push(e);
83
105
  }
84
- }, o.onSuccess = (e, r) => {
106
+ }, f.onSuccess = (e, r) => {
85
107
  switch (r) {
86
108
  case "beforeAll":
87
- l.beforeAll.push(e);
109
+ c.beforeAll.push(e);
88
110
  break;
89
111
  case "afterAll":
90
- l.afterAll.push(e);
112
+ c.afterAll.push(e);
91
113
  break;
92
114
  default:
93
- l.other.push(e);
115
+ c.other.push(e);
94
116
  }
95
- }, o.onError = (e, r) => {
117
+ }, f.onError = (e, r) => {
96
118
  switch (r) {
97
119
  case "beforeAll":
98
- s.beforeAll.push(e);
120
+ l.beforeAll.push(e);
99
121
  break;
100
122
  case "afterAll":
101
- s.afterAll.push(e);
123
+ l.afterAll.push(e);
102
124
  break;
103
125
  default:
104
- s.other.push(e);
126
+ l.other.push(e);
105
127
  }
106
- }, o.onFinally = (e, r) => {
128
+ }, f.onFinally = (e, r) => {
107
129
  switch (r) {
108
130
  case "beforeAll":
109
- c.beforeAll.push(e);
131
+ t.beforeAll.push(e);
110
132
  break;
111
133
  case "afterAll":
112
- c.afterAll.push(e);
134
+ t.afterAll.push(e);
113
135
  break;
114
136
  default:
115
- c.other.push(e);
137
+ t.other.push(e);
116
138
  }
117
- }, o;
118
- }, p = function(n, t, l = !0) {
119
- console.log("Combine", n, t, l);
120
- let s = t(n);
121
- const c = [];
122
- n.forEach((e) => {
139
+ }, f;
140
+ }, S = function(o, n, c = !0) {
141
+ console.log("Combine", o, n, c);
142
+ let l = n(o);
143
+ const t = [];
144
+ o.forEach((e) => {
123
145
  e.subscribe(() => {
124
- i(l, () => {
125
- s = t(n), c.forEach((r) => r(s));
146
+ i(c, () => {
147
+ l = n(o), t.forEach((r) => r(l));
126
148
  });
127
149
  });
128
150
  });
129
- const o = {
151
+ const f = {
130
152
  on: () => {
131
153
  throw new Error("Cannot call 'on' on combined store");
132
154
  },
133
155
  get() {
134
- return s;
156
+ return l;
135
157
  },
136
158
  set() {
137
159
  throw new Error("Cannot call 'set' on combined store");
138
160
  },
139
161
  subscribe(e) {
140
- return c.push(e), () => {
141
- const r = c.indexOf(e);
142
- ~r && c.splice(r, 1);
162
+ return t.push(e), () => {
163
+ const r = t.indexOf(e);
164
+ ~r && t.splice(r, 1);
143
165
  };
144
166
  },
145
167
  enableOn(e) {
146
- return e.subscribe(() => l = !0), o;
168
+ return e.subscribe(() => c = !0), f;
147
169
  },
148
170
  disableOn(e) {
149
- return e.subscribe(() => l = !1), o;
171
+ return e.subscribe(() => c = !1), f;
150
172
  }
151
173
  };
152
- return o;
153
- }, d = function(n) {
154
- const t = [], l = {
155
- on: (s, c) => (s === "onBefore" ? c.onBefore(() => t.forEach((o) => o()), n) : s === "onSuccess" ? c.onSuccess(() => t.forEach((o) => o()), n) : s === "onError" ? c.onError(() => t.forEach((o) => o()), n) : c.onFinally(() => t.forEach((o) => o()), n), l),
156
- subscribe: (s) => {
157
- t.push(s);
174
+ return f;
175
+ }, x = function(o) {
176
+ const n = [], c = {
177
+ on: (l, t) => (l === "onBefore" ? t.onBefore(() => n.forEach((f) => f()), o) : l === "onSuccess" ? t.onSuccess(() => n.forEach((f) => f()), o) : l === "onError" ? t.onError(() => n.forEach((f) => f()), o) : t.onFinally(() => n.forEach((f) => f()), o), c),
178
+ subscribe: (l) => {
179
+ n.push(l);
158
180
  }
159
181
  };
160
- return l;
161
- }, b = function(n) {
162
- return () => n;
163
- }, w = function(n) {
164
- const t = A(!1);
165
- return n.forEach((l) => {
166
- t.on(l, "onBefore", b(!0)), t.on(l, "onFinally", b(!1));
167
- }), t;
168
- }, C = function() {
169
- return (n, { result: t }) => t;
182
+ return c;
183
+ }, w = function(o) {
184
+ return () => o;
185
+ }, F = function(o) {
186
+ const n = k(!1);
187
+ return o.forEach((c) => {
188
+ n.on(c, "onBefore", w(!0)), n.on(c, "onFinally", w(!1));
189
+ }), n;
190
+ }, O = function() {
191
+ return (o, { result: n }) => n;
170
192
  };
171
193
  export {
172
- p as combine,
173
- E as effect,
194
+ S as combine,
195
+ m as effect,
174
196
  i as enableCheck,
175
- d as marker,
176
- w as pending,
177
- C as result,
178
- A as store,
179
- b as to
197
+ x as marker,
198
+ F as pending,
199
+ O as result,
200
+ k as store,
201
+ w as to
180
202
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanyamate/sec",
3
- "version": "0.3.0",
3
+ "version": "0.3.1-batch-beta.0",
4
4
  "description": "SEC. Store, Effect, Combine. Tiny state manager",
5
5
  "scripts": {
6
6
  "build": "vite build",