@vanyamate/sec 0.2.7 → 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 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;
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.d.ts CHANGED
@@ -52,7 +52,7 @@ export declare type Store<State> = {
52
52
  subscribe: (listener: StoreListener<State>) => () => void;
53
53
  };
54
54
 
55
- export declare const store: <State extends any>(state: State, enabled?: boolean) => Store<State>;
55
+ export declare const store: <State extends any>(state: State, options?: StoreOptions) => Store<State>;
56
56
 
57
57
  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>;
58
58
 
@@ -85,6 +85,11 @@ export declare type StoreOnSuccessHandler<State, Action extends EffectAction> =
85
85
  result: Awaited<ReturnType<Action>>;
86
86
  }) => State;
87
87
 
88
+ export declare type StoreOptions = {
89
+ enabled?: boolean;
90
+ instantListenerExecution?: boolean;
91
+ };
92
+
88
93
  export declare const to: <State>(state: State) => () => State;
89
94
 
90
95
  export { }
package/dist/index.js CHANGED
@@ -1,77 +1,98 @@
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
- (...r) => u(
7
- n,
8
- () => t.set(
9
- e(c, { args: r })
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(
15
+ (...u) => i(
16
+ t,
17
+ () => e.set(
18
+ a(o, { args: u })
10
19
  )
11
20
  )
12
- ) : o === "onSuccess" ? s.onSuccess(
13
- (r, ...l) => u(
14
- n,
15
- () => t.set(
16
- e(c, {
17
- result: r,
18
- args: l
21
+ ) : s === "onSuccess" ? r.onSuccess(
22
+ (u, ...b) => i(
23
+ t,
24
+ () => e.set(
25
+ a(o, {
26
+ result: u,
27
+ args: b
19
28
  })
20
29
  )
21
30
  )
22
- ) : o === "onError" ? s.onError(
23
- (r, ...l) => u(
24
- n,
25
- () => t.set(
26
- e(c, {
27
- error: r,
28
- args: l
31
+ ) : s === "onError" ? r.onError(
32
+ (u, ...b) => i(
33
+ t,
34
+ () => e.set(
35
+ a(o, {
36
+ error: u,
37
+ args: b
29
38
  })
30
39
  )
31
40
  )
32
- ) : s.onFinally(
33
- (...r) => u(
34
- n,
35
- () => t.set(
36
- e(c, { args: r })
41
+ ) : r.onFinally(
42
+ (...u) => i(
43
+ t,
44
+ () => e.set(
45
+ a(o, { args: u })
37
46
  )
38
47
  )
39
- ), t),
48
+ ), e),
40
49
  get() {
41
- return c;
50
+ return o;
42
51
  },
43
- set(s) {
44
- c = s, f.forEach((o) => o(c));
52
+ set(r) {
53
+ o = r, y(() => {
54
+ l != o && (l = o, c.forEach((s) => s(o)));
55
+ });
45
56
  },
46
- subscribe(s) {
47
- return f.push(s), () => {
48
- const o = f.indexOf(s);
49
- ~o && f.splice(o, 1);
50
- };
57
+ subscribe(r) {
58
+ return c.add(r), f && r(o), () => c.delete(r);
51
59
  },
52
- enableOn(s, o) {
53
- return s.subscribe(() => n = !0), o !== void 0 && t.set(o), t;
60
+ enableOn(r, s) {
61
+ return r.subscribe(() => {
62
+ t = !0, s !== void 0 && (E(), queueMicrotask(() => {
63
+ p(), e.set(s);
64
+ }));
65
+ }), e;
54
66
  },
55
- disableOn(s, o) {
56
- return s.subscribe(() => n = !1), o !== void 0 && t.set(o), t;
67
+ disableOn(r, s) {
68
+ return r.subscribe(() => {
69
+ t = !1, s !== void 0 && (E(), queueMicrotask(() => {
70
+ p(), e.set(s);
71
+ }));
72
+ }), e;
57
73
  }
58
74
  };
59
- return t;
60
- }, i = function() {
75
+ return e;
76
+ }, h = function() {
61
77
  return {
62
78
  afterAll: [],
63
79
  beforeAll: [],
64
80
  other: []
65
81
  };
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
- }).finally(() => {
71
- s.beforeAll.forEach((r) => r(...e)), s.other.forEach((r) => r(...e)), s.afterAll.forEach((r) => r(...e));
72
- });
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
+ }
73
94
  };
74
- return o.onBefore = (e, r) => {
95
+ return f.onBefore = (e, r) => {
75
96
  switch (r) {
76
97
  case "beforeAll":
77
98
  n.beforeAll.push(e);
@@ -82,100 +103,100 @@ const u = function(c, n) {
82
103
  default:
83
104
  n.other.push(e);
84
105
  }
85
- }, o.onSuccess = (e, r) => {
106
+ }, f.onSuccess = (e, r) => {
86
107
  switch (r) {
87
108
  case "beforeAll":
88
- f.beforeAll.push(e);
109
+ c.beforeAll.push(e);
89
110
  break;
90
111
  case "afterAll":
91
- f.afterAll.push(e);
112
+ c.afterAll.push(e);
92
113
  break;
93
114
  default:
94
- f.other.push(e);
115
+ c.other.push(e);
95
116
  }
96
- }, o.onError = (e, r) => {
117
+ }, f.onError = (e, r) => {
97
118
  switch (r) {
98
119
  case "beforeAll":
99
- t.beforeAll.push(e);
120
+ l.beforeAll.push(e);
100
121
  break;
101
122
  case "afterAll":
102
- t.afterAll.push(e);
123
+ l.afterAll.push(e);
103
124
  break;
104
125
  default:
105
- t.other.push(e);
126
+ l.other.push(e);
106
127
  }
107
- }, o.onFinally = (e, r) => {
128
+ }, f.onFinally = (e, r) => {
108
129
  switch (r) {
109
130
  case "beforeAll":
110
- s.beforeAll.push(e);
131
+ t.beforeAll.push(e);
111
132
  break;
112
133
  case "afterAll":
113
- s.afterAll.push(e);
134
+ t.afterAll.push(e);
114
135
  break;
115
136
  default:
116
- s.other.push(e);
137
+ t.other.push(e);
117
138
  }
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) => {
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) => {
124
145
  e.subscribe(() => {
125
- u(f, () => {
126
- t = n(c), s.forEach((r) => r(t));
146
+ i(c, () => {
147
+ l = n(o), t.forEach((r) => r(l));
127
148
  });
128
149
  });
129
150
  });
130
- const o = {
151
+ const f = {
131
152
  on: () => {
132
153
  throw new Error("Cannot call 'on' on combined store");
133
154
  },
134
155
  get() {
135
- return t;
156
+ return l;
136
157
  },
137
158
  set() {
138
159
  throw new Error("Cannot call 'set' on combined store");
139
160
  },
140
161
  subscribe(e) {
141
- return s.push(e), () => {
142
- const r = s.indexOf(e);
143
- ~r && s.splice(r, 1);
162
+ return t.push(e), () => {
163
+ const r = t.indexOf(e);
164
+ ~r && t.splice(r, 1);
144
165
  };
145
166
  },
146
167
  enableOn(e) {
147
- return e.subscribe(() => f = !0), o;
168
+ return e.subscribe(() => c = !0), f;
148
169
  },
149
170
  disableOn(e) {
150
- return e.subscribe(() => f = !1), o;
171
+ return e.subscribe(() => c = !1), f;
151
172
  }
152
173
  };
153
- return o;
154
- }, E = function(c) {
155
- const n = [], 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),
157
- subscribe: (t) => {
158
- n.push(t);
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);
159
180
  }
160
181
  };
161
- return f;
162
- }, h = function(c) {
163
- return () => c;
164
- }, p = function(c) {
165
- const n = a(!1);
166
- return c.forEach((f) => {
167
- n.on(f, "onBefore", h(!0)), n.on(f, "onFinally", h(!1));
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));
168
189
  }), n;
169
- }, w = function() {
170
- return (c, { result: n }) => n;
190
+ }, O = function() {
191
+ return (o, { result: n }) => n;
171
192
  };
172
193
  export {
173
- A as combine,
174
- b as effect,
175
- u as enableCheck,
176
- E as marker,
177
- p as pending,
178
- w as result,
179
- a as store,
180
- h as to
194
+ S as combine,
195
+ m as effect,
196
+ i as enableCheck,
197
+ x as marker,
198
+ F as pending,
199
+ O as result,
200
+ k as store,
201
+ w as to
181
202
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanyamate/sec",
3
- "version": "0.2.7",
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",