@vanyamate/sec 0.2.2 → 0.2.4

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(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;
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;
package/dist/index.js CHANGED
@@ -1,126 +1,128 @@
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
- }
1
+ const u = function(c, n) {
2
+ c && n();
3
+ }, a = function(c, n = !0) {
4
+ const f = [], t = {
5
+ on: (o, s, e) => (s === "onBefore" ? o.onBefore(
6
+ (...r) => u(
7
+ n,
8
+ () => t.set(
9
+ e(c, { args: r })
10
+ )
11
11
  )
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
- }
12
+ ) : s === "onSuccess" ? o.onSuccess(
13
+ (r, ...l) => u(
14
+ n,
15
+ () => t.set(
16
+ e(c, {
17
+ result: r,
18
+ args: l
19
+ })
20
+ )
21
21
  )
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
- }
22
+ ) : s === "onError" ? o.onError(
23
+ (r, ...l) => u(
24
+ n,
25
+ () => t.set(
26
+ e(c, {
27
+ error: r,
28
+ args: l
29
+ })
30
+ )
31
31
  )
32
- ) : n.onFinally(
33
- (...e) => u(
34
- c,
35
- () => o = r(o, { args: e })
32
+ ) : o.onFinally(
33
+ (...r) => u(
34
+ n,
35
+ () => t.set(
36
+ e(c, { args: r })
37
+ )
36
38
  )
37
- ), l),
39
+ ), t),
38
40
  get() {
39
- return o;
41
+ return c;
40
42
  },
41
- set(n) {
42
- o = n;
43
+ set(o) {
44
+ c = o, f.forEach((s) => s(c));
43
45
  },
44
- subscribe(n) {
45
- return f.push(n), () => {
46
- const s = f.indexOf(n);
46
+ subscribe(o) {
47
+ return f.push(o), () => {
48
+ const s = f.indexOf(o);
47
49
  ~s && f.splice(s, 1);
48
50
  };
49
51
  },
50
- enableOn(n) {
51
- return n.subscribe(() => c = !0), l;
52
+ enableOn(o) {
53
+ return o.subscribe(() => n = !0), t;
52
54
  },
53
- disableOn(n) {
54
- return n.subscribe(() => c = !1), l;
55
+ disableOn(o) {
56
+ return o.subscribe(() => n = !1), t;
55
57
  }
56
58
  };
57
- return l;
59
+ return t;
58
60
  }, h = function() {
59
61
  return {
60
62
  afterAll: [],
61
63
  beforeAll: [],
62
64
  other: []
63
65
  };
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;
66
+ }, b = function(c) {
67
+ const n = h(), f = h(), t = h(), o = h(), s = 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;
68
70
  }).finally(() => {
69
- n.beforeAll.forEach((e) => e(...r)), n.other.forEach((e) => e(...r)), n.afterAll.forEach((e) => e(...r));
71
+ o.beforeAll.forEach((r) => r(...e)), o.other.forEach((r) => r(...e)), o.afterAll.forEach((r) => r(...e));
70
72
  });
71
73
  };
72
- return s.onBefore = (r, e) => {
73
- switch (e) {
74
+ return s.onBefore = (e, r) => {
75
+ switch (r) {
74
76
  case "beforeAll":
75
- c.beforeAll.push(r);
77
+ n.beforeAll.push(e);
76
78
  break;
77
79
  case "afterAll":
78
- c.afterAll.push(r);
80
+ n.afterAll.push(e);
79
81
  break;
80
82
  default:
81
- c.other.push(r);
83
+ n.other.push(e);
82
84
  }
83
- }, s.onSuccess = (r, e) => {
84
- switch (e) {
85
+ }, s.onSuccess = (e, r) => {
86
+ switch (r) {
85
87
  case "beforeAll":
86
- f.beforeAll.push(r);
88
+ f.beforeAll.push(e);
87
89
  break;
88
90
  case "afterAll":
89
- f.afterAll.push(r);
91
+ f.afterAll.push(e);
90
92
  break;
91
93
  default:
92
- f.other.push(r);
94
+ f.other.push(e);
93
95
  }
94
- }, s.onError = (r, e) => {
95
- switch (e) {
96
+ }, s.onError = (e, r) => {
97
+ switch (r) {
96
98
  case "beforeAll":
97
- l.beforeAll.push(r);
99
+ t.beforeAll.push(e);
98
100
  break;
99
101
  case "afterAll":
100
- l.afterAll.push(r);
102
+ t.afterAll.push(e);
101
103
  break;
102
104
  default:
103
- l.other.push(r);
105
+ t.other.push(e);
104
106
  }
105
- }, s.onFinally = (r, e) => {
106
- switch (e) {
107
+ }, s.onFinally = (e, r) => {
108
+ switch (r) {
107
109
  case "beforeAll":
108
- n.beforeAll.push(r);
110
+ o.beforeAll.push(e);
109
111
  break;
110
112
  case "afterAll":
111
- n.afterAll.push(r);
113
+ o.afterAll.push(e);
112
114
  break;
113
115
  default:
114
- n.other.push(r);
116
+ o.other.push(e);
115
117
  }
116
118
  }, s;
117
- }, E = function(o, c, f = !0) {
118
- let l = c(...o);
119
- const n = [];
120
- o.forEach((r) => {
121
- r.subscribe(() => {
119
+ }, A = function(c, n, f = !0) {
120
+ let t = n(...c);
121
+ const o = [];
122
+ c.forEach((e) => {
123
+ e.subscribe(() => {
122
124
  u(f, () => {
123
- l = c(...o), n.forEach((e) => e(l));
125
+ t = n(...c), o.forEach((r) => r(t));
124
126
  });
125
127
  });
126
128
  });
@@ -129,47 +131,47 @@ const u = function(o, c) {
129
131
  throw new Error("Cannot call 'on' on combined store");
130
132
  },
131
133
  get() {
132
- return l;
134
+ return t;
133
135
  },
134
136
  set() {
135
137
  throw new Error("Cannot call 'set' on combined store");
136
138
  },
137
- subscribe(r) {
138
- return n.push(r), () => {
139
- const e = n.indexOf(r);
140
- ~e && n.splice(e, 1);
139
+ subscribe(e) {
140
+ return o.push(e), () => {
141
+ const r = o.indexOf(e);
142
+ ~r && o.splice(r, 1);
141
143
  };
142
144
  },
143
- enableOn(r) {
144
- return r.subscribe(() => f = !1), s;
145
+ enableOn(e) {
146
+ return e.subscribe(() => f = !1), s;
145
147
  },
146
- disableOn(r) {
147
- return r.subscribe(() => f = !0), s;
148
+ disableOn(e) {
149
+ return e.subscribe(() => f = !0), s;
148
150
  }
149
151
  };
150
152
  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);
153
+ }, E = function(c) {
154
+ 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
+ subscribe: (t) => {
157
+ n.push(t);
156
158
  }
157
159
  };
158
160
  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;
161
+ }, i = function(c) {
162
+ return () => c;
163
+ }, p = function(c) {
164
+ const n = a(!1);
165
+ return c.forEach((f) => {
166
+ n.on(f, "onBefore", i(!0)), n.on(f, "onFinally", i(!1));
167
+ }), n;
166
168
  };
167
169
  export {
168
- E as combine,
169
- A as effect,
170
+ A as combine,
171
+ b as effect,
170
172
  u as enableCheck,
171
- p as marker,
172
- w as pending,
173
- b as store,
174
- a as to
173
+ E as marker,
174
+ p as pending,
175
+ a as store,
176
+ i as to
175
177
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanyamate/sec",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "SEC. Store, Effect, Combine. Tiny state manager",
5
5
  "scripts": {
6
6
  "build": "vite build",
@@ -16,6 +16,11 @@
16
16
  "require": "./dist/index.cjs"
17
17
  }
18
18
  },
19
+ "files": [
20
+ "dist",
21
+ "package.json",
22
+ "README.md"
23
+ ],
19
24
  "repository": {
20
25
  "type": "git",
21
26
  "url": "git+https://github.com/VanyaMate/sec.git"
@@ -1,55 +0,0 @@
1
- import {
2
- enableCheck,
3
- Store, StoreListener,
4
- } from '../store';
5
- import { Marker } from '../marker';
6
-
7
-
8
- export const combine = function <State, States extends Array<any>> (
9
- stores: { [Index in keyof States]: Store<States[Index]> },
10
- callback: (...stores: { [K in keyof States]: Store<States[K]> }) => State,
11
- enabled: boolean = true,
12
- ): Store<State> {
13
- let combinedState: State = callback(...stores);
14
- const listeners: Array<StoreListener<State>> = [];
15
-
16
- stores.forEach((store) => {
17
- store.subscribe(() => {
18
- enableCheck(enabled, () => {
19
- combinedState = callback(...stores);
20
- listeners.forEach((listener) => listener(combinedState));
21
- });
22
- });
23
- });
24
-
25
- const storeApi: Store<State> = {
26
- on: () => {
27
- throw new Error(`Cannot call 'on' on combined store`);
28
- },
29
- get (): State {
30
- return combinedState;
31
- },
32
- set () {
33
- throw new Error(`Cannot call 'set' on combined store`);
34
- },
35
- subscribe (listener: StoreListener<State>) {
36
- listeners.push(listener);
37
- return () => {
38
- const index = listeners.indexOf(listener);
39
- if (~index) {
40
- listeners.splice(index, 1);
41
- }
42
- };
43
- },
44
- enableOn (marker: Marker<State>) {
45
- marker.subscribe(() => enabled = false);
46
- return storeApi;
47
- },
48
- disableOn (marker: Marker<State>) {
49
- marker.subscribe(() => enabled = true);
50
- return storeApi;
51
- },
52
- };
53
-
54
- return storeApi;
55
- };
@@ -1,115 +0,0 @@
1
- export type EffectAction = (...args: Array<any>) => Promise<any>;
2
- export type EffectSuccessCallback<AsyncAction extends EffectAction> = (result: Awaited<ReturnType<AsyncAction>>, ...args: Parameters<AsyncAction>) => void;
3
- export type EffectErrorCallback<AsyncAction extends EffectAction> = (error: unknown, ...args: Parameters<AsyncAction>) => void;
4
- export type EffectBeforeCallback<AsyncAction extends EffectAction> = (...args: Parameters<AsyncAction>) => void;
5
- export type EffectFinallyCallback<AsyncAction extends EffectAction> = (...args: Parameters<AsyncAction>) => void;
6
-
7
- export type EffectSubscribePosition =
8
- 'beforeAll'
9
- | 'afterAll'
10
- | undefined;
11
-
12
- export type Effect<AsyncAction extends EffectAction> = {
13
- (...args: Parameters<AsyncAction>): Promise<Awaited<ReturnType<AsyncAction>>>;
14
- onSuccess: (callback: EffectSuccessCallback<AsyncAction>, position?: EffectSubscribePosition) => void;
15
- onError: (callback: EffectErrorCallback<AsyncAction>, position?: EffectSubscribePosition) => void;
16
- onBefore: (callback: EffectBeforeCallback<AsyncAction>, position?: EffectSubscribePosition) => void;
17
- onFinally: (callback: EffectFinallyCallback<AsyncAction>, position?: EffectSubscribePosition) => void;
18
- }
19
-
20
- export type EffectCallbackList<Type> = {
21
- beforeAll: Array<Type>;
22
- afterAll: Array<Type>;
23
- other: Array<Type>;
24
- }
25
-
26
- const getCallbacksList = function <Type> (): EffectCallbackList<Type> {
27
- return {
28
- afterAll : [],
29
- beforeAll: [],
30
- other : [],
31
- };
32
- };
33
-
34
- export const effect = function <Action extends EffectAction> (action: Action): Effect<Action> {
35
- const beforeCallbacks: EffectCallbackList<EffectBeforeCallback<Action>> = getCallbacksList<EffectBeforeCallback<Action>>();
36
- const successCallbacks: EffectCallbackList<EffectSuccessCallback<Action>> = getCallbacksList<EffectSuccessCallback<Action>>();
37
- const errorCallbacks: EffectCallbackList<EffectErrorCallback<Action>> = getCallbacksList<EffectErrorCallback<Action>>();
38
- const finallyCallbacks: EffectCallbackList<EffectFinallyCallback<Action>> = getCallbacksList<EffectFinallyCallback<Action>>();
39
-
40
- const effectApi: Effect<Action> = async function (...args) {
41
- beforeCallbacks.beforeAll.forEach((callback) => callback(...args));
42
- beforeCallbacks.other.forEach((callback) => callback(...args));
43
- beforeCallbacks.afterAll.forEach((callback) => callback(...args));
44
-
45
- return action(...args)
46
- .then((result: Awaited<ReturnType<Action>>) => {
47
- successCallbacks.beforeAll.forEach((callback) => callback(result, ...args));
48
- successCallbacks.other.forEach((callback) => callback(result, ...args));
49
- successCallbacks.afterAll.forEach((callback) => callback(result, ...args));
50
- return result;
51
- })
52
- .catch((error: unknown) => {
53
- errorCallbacks.beforeAll.forEach((callback) => callback(error, ...args));
54
- errorCallbacks.other.forEach((callback) => callback(error, ...args));
55
- errorCallbacks.afterAll.forEach((callback) => callback(error, ...args));
56
- throw error;
57
- })
58
- .finally(() => {
59
- finallyCallbacks.beforeAll.forEach((callback) => callback(...args));
60
- finallyCallbacks.other.forEach((callback) => callback(...args));
61
- finallyCallbacks.afterAll.forEach((callback) => callback(...args));
62
- });
63
- } as Effect<Action>;
64
-
65
- effectApi.onBefore = (callback: EffectBeforeCallback<Action>, position?: EffectSubscribePosition) => {
66
- switch (position) {
67
- case 'beforeAll':
68
- beforeCallbacks.beforeAll.push(callback);
69
- break;
70
- case 'afterAll':
71
- beforeCallbacks.afterAll.push(callback);
72
- break;
73
- default:
74
- beforeCallbacks.other.push(callback);
75
- }
76
- };
77
- effectApi.onSuccess = (callback: EffectSuccessCallback<Action>, position?: EffectSubscribePosition) => {
78
- switch (position) {
79
- case 'beforeAll':
80
- successCallbacks.beforeAll.push(callback);
81
- break;
82
- case 'afterAll':
83
- successCallbacks.afterAll.push(callback);
84
- break;
85
- default:
86
- successCallbacks.other.push(callback);
87
- }
88
- };
89
- effectApi.onError = (callback: EffectErrorCallback<Action>, position?: EffectSubscribePosition) => {
90
- switch (position) {
91
- case 'beforeAll':
92
- errorCallbacks.beforeAll.push(callback);
93
- break;
94
- case 'afterAll':
95
- errorCallbacks.afterAll.push(callback);
96
- break;
97
- default:
98
- errorCallbacks.other.push(callback);
99
- }
100
- };
101
- effectApi.onFinally = (callback: EffectFinallyCallback<Action>, position?: EffectSubscribePosition) => {
102
- switch (position) {
103
- case 'beforeAll':
104
- finallyCallbacks.beforeAll.push(callback);
105
- break;
106
- case 'afterAll':
107
- finallyCallbacks.afterAll.push(callback);
108
- break;
109
- default:
110
- finallyCallbacks.other.push(callback);
111
- }
112
- };
113
-
114
- return effectApi;
115
- };
package/src/index.ts DELETED
@@ -1,6 +0,0 @@
1
- export * from './store';
2
- export * from './effect';
3
- export * from './combine';
4
- export * from './marker';
5
- export * from './pending';
6
- export * from './to';
@@ -1,34 +0,0 @@
1
- import { EffectAction, Effect, EffectSubscribePosition } from '../effect';
2
- import { StoreHandlerMap } from '../store';
3
-
4
-
5
- export type MarkerListener = () => void;
6
-
7
- export type Marker<State> = {
8
- on: <Action extends EffectAction>(event: keyof StoreHandlerMap<State, Action>, effect: Effect<Action>, position?: EffectSubscribePosition) => Marker<State>;
9
- subscribe: (listener: MarkerListener) => void;
10
- }
11
-
12
- export const marker = function <State> (position?: EffectSubscribePosition): Marker<State> {
13
- const listeners: Array<MarkerListener> = [];
14
-
15
- const markerApi: Marker<State> = {
16
- on : (event, effect) => {
17
- if (event === 'onBefore') {
18
- effect.onBefore(() => listeners.forEach((listener) => listener()), position);
19
- } else if (event === 'onSuccess') {
20
- effect.onSuccess(() => listeners.forEach((listener) => listener()), position);
21
- } else if (event === 'onError') {
22
- effect.onError(() => listeners.forEach((listener) => listener()), position);
23
- } else {
24
- effect.onFinally(() => listeners.forEach((listener) => listener()), position);
25
- }
26
- return markerApi;
27
- },
28
- subscribe: (listener: MarkerListener) => {
29
- listeners.push(listener);
30
- },
31
- };
32
-
33
- return markerApi;
34
- };
@@ -1,85 +0,0 @@
1
- import { beforeAll, describe, expect, test, vi } from 'vitest';
2
- import { marker } from './index';
3
- import { effect } from '../effect';
4
- import { store } from '../store';
5
-
6
-
7
- const createPostAction = async (id: number) => {
8
- return new Promise<{ id: number }>((resolve) => {
9
- setTimeout(() => {
10
- resolve({ id });
11
- }, 500);
12
- });
13
- };
14
- const loginAction = async () => {
15
- };
16
- const logoutAction = async () => {
17
- };
18
- const loginEffect = effect(loginAction);
19
- const logoutEffect = effect(logoutAction);
20
- const createPostEffect = effect(createPostAction);
21
- const loginMarker = marker('beforeAll').on('onSuccess', loginEffect);
22
- const logoutMarker = marker('afterAll').on('onSuccess', logoutEffect);
23
-
24
- const postsStore = store<Array<{ id: number }>>([], true)
25
- .enableOn(loginMarker)
26
- .disableOn(logoutMarker)
27
- .on(createPostEffect, 'onSuccess', (state, { result }) => state.concat(result));
28
-
29
- describe('marker', () => {
30
- beforeAll(() => {
31
- postsStore.set([]);
32
- });
33
-
34
- test('check with effect', async () => {
35
- const mockFunction = vi.fn();
36
- loginMarker.subscribe(mockFunction);
37
- await loginEffect().finally(() => {
38
- expect(mockFunction).toBeCalledTimes(1);
39
- });
40
- await loginEffect().finally(() => {
41
- expect(mockFunction).toBeCalledTimes(2);
42
- });
43
- });
44
-
45
- test('check with effect + store', () => {
46
- createPostEffect(1)
47
- .finally(() => {
48
- expect(postsStore.get()).deep.eq([]);
49
- });
50
-
51
- loginEffect().finally(async () => {
52
- await createPostEffect(2).finally(() => {
53
- expect(postsStore.get()).deep.eq([ { id: 2 } ]);
54
- });
55
- createPostEffect(3)
56
- .then(() => {
57
- expect(postsStore.get()).deep.eq([ { id: 2 } ]);
58
- });
59
- logoutEffect();
60
- });
61
- });
62
-
63
- test('check queue', () => {
64
- const queue: Array<number> = [];
65
- const logoutEffect = async function () {
66
- return new Promise(resolve => setTimeout(resolve, 100));
67
- };
68
- const beforeLogout = effect(logoutEffect);
69
- const middleLogout = effect(logoutEffect);
70
- const afterLogout = effect(logoutEffect);
71
-
72
- store<Array<number>>([]).on(middleLogout, 'onSuccess', (state) => {
73
- queue.push(2);
74
- return state;
75
- });
76
-
77
- marker('afterAll').on('onSuccess', afterLogout).subscribe(() => queue.push(3));
78
- marker('beforeAll').on('onSuccess', beforeLogout).subscribe(() => queue.push(1));
79
-
80
- logoutEffect()
81
- .finally(() => {
82
- expect(queue).deep.eq([ 1, 2, 3 ]);
83
- });
84
- });
85
- });
@@ -1,13 +0,0 @@
1
- import { store } from '../store';
2
- import { to } from '../to';
3
- import { Effect } from '../effect';
4
-
5
-
6
- export const pending = function (effects: Array<Effect<any>>) {
7
- const _store = store<boolean>(false);
8
- effects.forEach((effect) => {
9
- _store.on(effect, 'onBefore', to(true));
10
- _store.on(effect, 'onFinally', to(false));
11
- });
12
- return _store;
13
- };
@@ -1,24 +0,0 @@
1
- import { describe, expect, test } from 'vitest';
2
- import { effect } from '../effect';
3
- import { pending } from './index';
4
-
5
-
6
- describe('pending', () => {
7
- test('valid', async () => {
8
- const getPostsAction = async function () {
9
- return new Promise<void>((resolve) => setTimeout(resolve, 200));
10
- };
11
-
12
- const getPostsEffect = effect(getPostsAction);
13
-
14
- const postsPending = pending([ getPostsEffect ]);
15
-
16
- expect(postsPending.get()).equal(false);
17
-
18
- getPostsEffect();
19
- await new Promise(resolve => setTimeout(resolve, 100));
20
- expect(postsPending.get()).equal(true);
21
- await new Promise(resolve => setTimeout(resolve, 150));
22
- expect(postsPending.get()).equal(false);
23
- });
24
- });
@@ -1,134 +0,0 @@
1
- import { EffectAction, Effect, effect } from '../effect';
2
- import { Marker } from '../marker';
3
-
4
-
5
- export type StoreOnBeforeHandler<State, Action extends EffectAction> = (state: State, data: {
6
- args: Parameters<Action>
7
- }) => State;
8
- export type StoreOnSuccessHandler<State, Action extends EffectAction> = (state: State, data: {
9
- args: Parameters<Action>
10
- result: Awaited<ReturnType<Action>>;
11
- }) => State;
12
- export type StoreOnErrorHandler<State, Action extends EffectAction> = (state: State, data: {
13
- args: Parameters<Action>
14
- error: unknown;
15
- }) => State;
16
- export type StoreOnFinallyHandler<State, Action extends EffectAction> = (state: State, data: {
17
- args: Parameters<Action>
18
- }) => State;
19
-
20
- export type StoreHandlerMap<State, Action extends EffectAction> = {
21
- onBefore: StoreOnBeforeHandler<State, Action>;
22
- onSuccess: StoreOnSuccessHandler<State, Action>;
23
- onError: StoreOnErrorHandler<State, Action>;
24
- onFinally: StoreOnFinallyHandler<State, Action>;
25
- };
26
-
27
- export type StoreEffectSubscribe<State> =
28
- <
29
- Action extends EffectAction,
30
- Event extends keyof StoreHandlerMap<State, Action>
31
- > (
32
- effect: Effect<Action>,
33
- event: Event,
34
- handler: StoreHandlerMap<State, Action>[Event],
35
- ) => Store<State>;
36
-
37
- export type StoreListener<State> = (state: State) => void;
38
- export type StoreMarkerSubscribe<State> = (marker: Marker<State>) => Store<State>;
39
-
40
- export type Store<State> = {
41
- on: StoreEffectSubscribe<State>;
42
- enableOn: StoreMarkerSubscribe<State>;
43
- disableOn: StoreMarkerSubscribe<State>;
44
- get: () => State;
45
- set: (data: State) => void;
46
- subscribe: (listener: StoreListener<State>) => () => void;
47
- }
48
-
49
- export const enableCheck = function (enabled: boolean, callback: () => void) {
50
- if (enabled) {
51
- callback();
52
- }
53
- };
54
-
55
- export const store = function <State extends any> (state: State, enabled: boolean = true): Store<State> {
56
- const listeners: Array<StoreListener<State>> = [];
57
-
58
- const storeApi: Store<State> = {
59
- on: <
60
- Action extends EffectAction,
61
- Event extends keyof StoreHandlerMap<State, Action>
62
- > (effect: Effect<Action>, event: Event, handler: StoreHandlerMap<State, Action>[Event]) => {
63
- if (event === 'onBefore') {
64
- effect.onBefore((...args) =>
65
- enableCheck(
66
- enabled,
67
- () => {
68
- state = (handler as StoreOnBeforeHandler<State, Action>)(state, { args });
69
- listeners.forEach((listener) => listener(state));
70
- },
71
- ),
72
- );
73
- } else if (event === 'onSuccess') {
74
- effect.onSuccess((result, ...args) =>
75
- enableCheck(
76
- enabled,
77
- () => {
78
- state = (handler as StoreOnSuccessHandler<State, Action>)(state, {
79
- result, args,
80
- });
81
- listeners.forEach((listener) => listener(state));
82
- },
83
- ),
84
- );
85
- } else if (event === 'onError') {
86
- effect.onError((error, ...args) =>
87
- enableCheck(
88
- enabled,
89
- () => {
90
- state = (handler as StoreOnErrorHandler<State, Action>)(state, {
91
- error, args,
92
- });
93
- listeners.forEach((listener) => listener(state));
94
- },
95
- ),
96
- );
97
- } else {
98
- effect.onFinally((...args) =>
99
- enableCheck(
100
- enabled,
101
- () => state = (handler as StoreOnFinallyHandler<State, Action>)(state, { args }),
102
- ),
103
- );
104
- }
105
-
106
- return storeApi;
107
- },
108
- get (): State {
109
- return state;
110
- },
111
- set (value: State) {
112
- state = value;
113
- },
114
- subscribe (listener: StoreListener<State>) {
115
- listeners.push(listener);
116
- return () => {
117
- const index = listeners.indexOf(listener);
118
- if (~index) {
119
- listeners.splice(index, 1);
120
- }
121
- };
122
- },
123
- enableOn (marker: Marker<State>) {
124
- marker.subscribe(() => enabled = true);
125
- return storeApi;
126
- },
127
- disableOn (marker: Marker<State>) {
128
- marker.subscribe(() => enabled = false);
129
- return storeApi;
130
- },
131
- };
132
-
133
- return storeApi;
134
- };
package/src/to/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export const to = function <State> (state: State) {
2
- return () => state;
3
- };
package/src/to/to.test.ts DELETED
@@ -1,23 +0,0 @@
1
- import { describe, expect, test } from 'vitest';
2
- import { to } from './index';
3
-
4
-
5
- describe('to', () => {
6
- test('valid', () => {
7
- const arr: Array<number> = [];
8
- const obj = {};
9
-
10
- expect(to(1)()).equal(1);
11
- expect(to(arr)()).equal(arr);
12
- expect(to(obj)()).equal(obj);
13
- });
14
-
15
- test('invalid', () => {
16
- const arr: Array<number> = [];
17
- const obj = {};
18
-
19
- expect(to(1)()).not.equal(2);
20
- expect(to(arr)()).not.equal(obj);
21
- expect(to(obj)()).not.equal(arr);
22
- });
23
- });
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "skipLibCheck": true,
4
- "strict": true,
5
- "moduleResolution": "node",
6
- "outDir": "dist",
7
- "declaration": true,
8
- "declarationMap": true,
9
- "lib": [
10
- "ESNext",
11
- "DOM"
12
- ]
13
- }
14
- }
package/vite.config.ts DELETED
@@ -1,23 +0,0 @@
1
- import { defineConfig } from 'vitest/config';
2
- import dtsPlugin from 'vite-plugin-dts';
3
-
4
-
5
- export default defineConfig({
6
- plugins: [
7
- dtsPlugin({
8
- outDir : 'dist',
9
- entryRoot : 'src',
10
- rollupTypes: true,
11
- }),
12
- ],
13
- build : {
14
- outDir : './dist',
15
- target : 'esnext',
16
- emptyOutDir: true,
17
- lib : {
18
- entry : './src/index.ts',
19
- fileName: 'index',
20
- formats : [ 'es', 'cjs' ],
21
- },
22
- },
23
- });