@vanyamate/sec 0.1.5 → 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 +210 -9
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +88 -29
- package/dist/index.js +166 -108
- package/package.json +38 -25
- package/src/combine/index.ts +55 -0
- package/src/effect/index.ts +115 -0
- package/src/index.ts +6 -0
- package/src/marker/index.ts +34 -0
- package/src/marker/marker.test.ts +85 -0
- package/src/pending/index.ts +13 -0
- package/src/pending/pending.test.ts +24 -0
- package/src/store/index.ts +125 -0
- package/src/to/index.ts +3 -0
- package/src/to/to.test.ts +23 -0
- package/tsconfig.json +10 -16
- package/vite.config.ts +23 -0
- package/index.ts +0 -166
package/README.md
CHANGED
|
@@ -2,23 +2,224 @@
|
|
|
2
2
|
|
|
3
3
|
Tiny state manager
|
|
4
4
|
|
|
5
|
+
*It may change in the future*
|
|
6
|
+
|
|
7
|
+
For react/preact/svelte/qwik/solid
|
|
8
|
+
|
|
5
9
|
```
|
|
6
10
|
npm i @vanyamate/sec
|
|
7
11
|
```
|
|
8
12
|
|
|
13
|
+
with
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
npm i @vanyamate/sec-react
|
|
17
|
+
npm i @vanyamate/sec-solidjs
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
For vue (like old 0.1.5 version)
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
npm i @vanyamate/sec-vue
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Documentation:
|
|
27
|
+
|
|
28
|
+
### effect
|
|
29
|
+
|
|
30
|
+
Effect is a wrapper around action. Action is any asynchronous action. Store subscribes to effect. Effect can be called
|
|
31
|
+
from anywhere in the code.
|
|
32
|
+
|
|
33
|
+
Effect takes one value - action.
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
|
|
37
|
+
const loginAction = async function (loginData: LoginData): Promise<UserData> {
|
|
38
|
+
// login action.. (fetch, etc.)
|
|
39
|
+
return fetch(`${ __API__ }/v1/auth`, {
|
|
40
|
+
method: 'POST',
|
|
41
|
+
body : JSON.stringify(loginData),
|
|
42
|
+
})
|
|
43
|
+
.then((response) => response.json());
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const loginEffect = effect(loginAction);
|
|
47
|
+
|
|
48
|
+
// Anywhere in code
|
|
49
|
+
loginEffect({ login: 'VanyaMate', password: 'qwerty12345' });
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### marker
|
|
54
|
+
|
|
55
|
+
markers are needed to enable/disable the store. markers also subscribe to the effect. marker takes 1 value of the
|
|
56
|
+
subscription type. `afterAll`, `beforeAll` or `undefined`
|
|
57
|
+
|
|
58
|
+
The marker has an api.
|
|
59
|
+
|
|
60
|
+
- `on` - subscribe to effect. takes 2 parameters. type (`onBefore`, `onSuccess`, `onError` and `onFinally`) and effect.
|
|
61
|
+
after effect is triggered, all listeners that were set via `subscribe` are triggered.
|
|
62
|
+
- `subscribe` - adds a handler that will be triggered when any effect of the required type is executed.
|
|
63
|
+
|
|
9
64
|
```typescript
|
|
10
|
-
import { store, effect, combine } from './index';
|
|
11
65
|
|
|
66
|
+
const loginMarker = marker('beforeAll').on('onSuccess', loginEffect);
|
|
67
|
+
const logoutMarker = marker('afterAll').on('onSuccess', logoutEffect);
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
order of execution: 'beforeAll' -> undefined -> 'afterAll' (within one type (for example `onSuccess`))
|
|
72
|
+
|
|
73
|
+
### to
|
|
74
|
+
|
|
75
|
+
Just helper. Returns a function that returns the passed value.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
|
|
79
|
+
to(123); // Return () => 123
|
|
80
|
+
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
// before .on(logoutEffect, 'onSuccess', () => []);
|
|
85
|
+
// after .on(logoutEffect, 'onSuccess', to([]));
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### pending
|
|
89
|
+
|
|
90
|
+
Just helper. wrapper over store. returns a bool value and is used to create a pending-store.
|
|
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
|
+
|
|
110
|
+
### store
|
|
111
|
+
|
|
112
|
+
Store stores data and subscribes to effect.
|
|
113
|
+
|
|
114
|
+
When initializing, the store takes 2 values. The first value is the initialization data. The second value is optional -
|
|
115
|
+
enable/disable. When disabled, the store will not be updated.
|
|
116
|
+
|
|
117
|
+
The store has an api.
|
|
118
|
+
|
|
119
|
+
- `on` - subscription to effect. takes 3 parameters. effect, type (`onBefore`, `onSuccess`, `onError` and `onFinally`)
|
|
120
|
+
and handler. handler has signature depending on type. handler is a function that takes 2 parameters and should always
|
|
121
|
+
return value of the type that is currently specified in store. first parameter is current state of store, and the
|
|
122
|
+
second is an object and depends on subscription type. for `onBefore` it is only `{ args }` where `args` is an array of
|
|
123
|
+
arguments of the function that we passed to effect. for `onSuccess` it is `{ args, result }` where `result` is result
|
|
124
|
+
of `Promise` execution. for `onError` `{ args, error }` where `error` is `unknown`. and for `onFinally` it is
|
|
125
|
+
`{ args }`.
|
|
126
|
+
- `onBefore` - `(state, { args }) => state`
|
|
127
|
+
- `onSuccess` - `(state, { args, result }) => state`
|
|
128
|
+
- `onError` - `(state, { args, error }) => state`
|
|
129
|
+
- `onFinally` - `(state, { args }) => state`
|
|
130
|
+
- `get` - get current store value.
|
|
131
|
+
- `set` - set new value
|
|
132
|
+
- `subscribe` - receives a function as input that will be executed when the store changes and returns an unsubscribe
|
|
133
|
+
function.
|
|
134
|
+
- `enableOn` - receives a marker that, when triggered, will enable the store.
|
|
135
|
+
- `disableOn` - receives a marker that, when triggered, will disable the store.
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
|
|
139
|
+
const authIsPending = store<boolean>(false)
|
|
140
|
+
.on(loginEffect, 'onBefore', () => true)
|
|
141
|
+
.on(loginEffect, 'onFinally', to(false)); // instead of () => false
|
|
142
|
+
|
|
143
|
+
const authError = store<Error | null>(null)
|
|
144
|
+
.on(loginEffect, 'onError', (_, { error }) => {
|
|
145
|
+
// For example
|
|
146
|
+
if (error instanceof Error) {
|
|
147
|
+
return error;
|
|
148
|
+
} else {
|
|
149
|
+
return new Error(error);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const authData = store<UserData | null>(null)
|
|
154
|
+
.on(loginEffect, 'onSuccess', (_, { result }) => result)
|
|
155
|
+
.on(logoutEffect, 'onSuccess', () => null);
|
|
156
|
+
|
|
157
|
+
const postsForUserId = store<number>(0)
|
|
158
|
+
.enableOn(loginMarker)
|
|
159
|
+
.disableOn(logoutMarker)
|
|
160
|
+
.on(getPostsForUser, 'onBefore', (_, { args: [ id ] }) => id);
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
*
|
|
164
|
+
* instead of
|
|
165
|
+
* const postsIsPending = store<boolean>(false)
|
|
166
|
+
* .on(getPostsForUser, 'onBefore', () => true)
|
|
167
|
+
* .on(getPostsForUser, 'onFinally', () => false)
|
|
168
|
+
* .on(createPostEffect, 'onBefore', () => true)
|
|
169
|
+
* .on(createPostEffect, 'onFinally', () => false);
|
|
170
|
+
*
|
|
171
|
+
*/
|
|
172
|
+
|
|
173
|
+
const postsIsPending = pending([
|
|
174
|
+
getPostsForUser,
|
|
175
|
+
createPostEffect,
|
|
176
|
+
]);
|
|
177
|
+
|
|
178
|
+
const posts = store<Array<Post>>([])
|
|
179
|
+
.enableOn(loginMarker)
|
|
180
|
+
.disableOn(logoutMarker)
|
|
181
|
+
.on(getPostsForUser, 'onSuccess', (state, { result, args: [ userId ] }) => {
|
|
182
|
+
if (postsForUserId.get() === userId) {
|
|
183
|
+
return state.concat(result);
|
|
184
|
+
} else {
|
|
185
|
+
return result;
|
|
186
|
+
}
|
|
187
|
+
})
|
|
188
|
+
.on(createPostEffect, 'onSuccess', (state, { result, args: [ userId ] }) => {
|
|
189
|
+
if (postsForUserId.get() === userId) {
|
|
190
|
+
return state.concat(result);
|
|
191
|
+
} else {
|
|
192
|
+
return state;
|
|
193
|
+
}
|
|
194
|
+
})
|
|
195
|
+
.on(logoutEffect, 'onSuccess', to([]));
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Types
|
|
199
|
+
|
|
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
|
+
```
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
// Example:
|
|
12
212
|
|
|
13
|
-
const
|
|
213
|
+
const getRandomId = async function () {
|
|
214
|
+
return { id: Math.random() };
|
|
215
|
+
};
|
|
14
216
|
|
|
15
|
-
|
|
16
|
-
const sumEffect = effect(sum);
|
|
217
|
+
const getRandomEffect = effect(getRandomId);
|
|
17
218
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
219
|
+
const handler: StoreHandlerMap<number, typeof getRandomId>['onSuccess'] = function (state, { result }) {
|
|
220
|
+
return result.id;
|
|
221
|
+
};
|
|
21
222
|
|
|
22
|
-
|
|
23
|
-
|
|
223
|
+
const num = store<number>(0)
|
|
224
|
+
.on(getRandomEffect, 'onSuccess', handler);
|
|
24
225
|
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +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;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,29 +1,88 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
export type Effect<
|
|
4
|
-
(...args:
|
|
5
|
-
onSuccess: (callback:
|
|
6
|
-
onError: (callback:
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
export declare
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
export declare
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
1
|
+
export declare const combine: <State, States extends Array<any>>(stores: { [Index in keyof States]: Store<States[Index]>; }, callback: (...stores: { [K in keyof States]: Store<States[K]>; }) => State, enabled?: boolean) => Store<State>;
|
|
2
|
+
|
|
3
|
+
export declare type Effect<AsyncAction extends EffectAction> = {
|
|
4
|
+
(...args: Parameters<AsyncAction>): Promise<Awaited<ReturnType<AsyncAction>>>;
|
|
5
|
+
onSuccess: (callback: EffectSuccessCallback<AsyncAction>, position?: EffectSubscribePosition) => void;
|
|
6
|
+
onError: (callback: EffectErrorCallback<AsyncAction>, position?: EffectSubscribePosition) => void;
|
|
7
|
+
onBefore: (callback: EffectBeforeCallback<AsyncAction>, position?: EffectSubscribePosition) => void;
|
|
8
|
+
onFinally: (callback: EffectFinallyCallback<AsyncAction>, position?: EffectSubscribePosition) => void;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export declare const effect: <Action extends EffectAction>(action: Action) => Effect<Action>;
|
|
12
|
+
|
|
13
|
+
export declare type EffectAction = (...args: Array<any>) => Promise<any>;
|
|
14
|
+
|
|
15
|
+
export declare type EffectBeforeCallback<AsyncAction extends EffectAction> = (...args: Parameters<AsyncAction>) => void;
|
|
16
|
+
|
|
17
|
+
export declare type EffectCallbackList<Type> = {
|
|
18
|
+
beforeAll: Array<Type>;
|
|
19
|
+
afterAll: Array<Type>;
|
|
20
|
+
other: Array<Type>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export declare type EffectErrorCallback<AsyncAction extends EffectAction> = (error: unknown, ...args: Parameters<AsyncAction>) => void;
|
|
24
|
+
|
|
25
|
+
export declare type EffectFinallyCallback<AsyncAction extends EffectAction> = (...args: Parameters<AsyncAction>) => void;
|
|
26
|
+
|
|
27
|
+
export declare type EffectSubscribePosition = 'beforeAll' | 'afterAll' | undefined;
|
|
28
|
+
|
|
29
|
+
export declare type EffectSuccessCallback<AsyncAction extends EffectAction> = (result: Awaited<ReturnType<AsyncAction>>, ...args: Parameters<AsyncAction>) => void;
|
|
30
|
+
|
|
31
|
+
export declare const enableCheck: (enabled: boolean, callback: () => void) => void;
|
|
32
|
+
|
|
33
|
+
export declare type Marker<State> = {
|
|
34
|
+
on: <Action extends EffectAction>(event: keyof StoreHandlerMap<State, Action>, effect: Effect<Action>, position?: EffectSubscribePosition) => Marker<State>;
|
|
35
|
+
subscribe: (listener: MarkerListener) => void;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export declare const marker: <State>(position?: EffectSubscribePosition) => Marker<State>;
|
|
39
|
+
|
|
40
|
+
export declare type MarkerListener = () => void;
|
|
41
|
+
|
|
42
|
+
export declare const pending: (effects: Array<Effect<any>>) => Store<boolean>;
|
|
43
|
+
|
|
44
|
+
export declare type Store<State> = {
|
|
45
|
+
on: StoreEffectSubscribe<State>;
|
|
46
|
+
enableOn: StoreMarkerSubscribe<State>;
|
|
47
|
+
disableOn: StoreMarkerSubscribe<State>;
|
|
48
|
+
get: () => State;
|
|
49
|
+
set: (data: State) => void;
|
|
50
|
+
subscribe: (listener: StoreListener<State>) => () => void;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export declare const store: <State extends any>(state: State, enabled?: boolean) => Store<State>;
|
|
54
|
+
|
|
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
|
+
|
|
57
|
+
export declare type StoreHandlerMap<State, Action extends EffectAction> = {
|
|
58
|
+
onBefore: StoreOnBeforeHandler<State, Action>;
|
|
59
|
+
onSuccess: StoreOnSuccessHandler<State, Action>;
|
|
60
|
+
onError: StoreOnErrorHandler<State, Action>;
|
|
61
|
+
onFinally: StoreOnFinallyHandler<State, Action>;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export declare type StoreListener<State> = (state: State) => void;
|
|
65
|
+
|
|
66
|
+
export declare type StoreMarkerSubscribe<State> = (marker: Marker<State>) => Store<State>;
|
|
67
|
+
|
|
68
|
+
export declare type StoreOnBeforeHandler<State, Action extends EffectAction> = (state: State, data: {
|
|
69
|
+
args: Parameters<Action>;
|
|
70
|
+
}) => State;
|
|
71
|
+
|
|
72
|
+
export declare type StoreOnErrorHandler<State, Action extends EffectAction> = (state: State, data: {
|
|
73
|
+
args: Parameters<Action>;
|
|
74
|
+
error: unknown;
|
|
75
|
+
}) => State;
|
|
76
|
+
|
|
77
|
+
export declare type StoreOnFinallyHandler<State, Action extends EffectAction> = (state: State, data: {
|
|
78
|
+
args: Parameters<Action>;
|
|
79
|
+
}) => State;
|
|
80
|
+
|
|
81
|
+
export declare type StoreOnSuccessHandler<State, Action extends EffectAction> = (state: State, data: {
|
|
82
|
+
args: Parameters<Action>;
|
|
83
|
+
result: Awaited<ReturnType<Action>>;
|
|
84
|
+
}) => State;
|
|
85
|
+
|
|
86
|
+
export declare const to: <State>(state: State) => () => State;
|
|
87
|
+
|
|
88
|
+
export { }
|
package/dist/index.js
CHANGED
|
@@ -1,109 +1,167 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const subscribe = (listener) => {
|
|
63
|
-
listeners.push(listener);
|
|
64
|
-
return () => {
|
|
65
|
-
const index = listeners.indexOf(listener);
|
|
66
|
-
if (index !== -1) {
|
|
67
|
-
listeners.splice(index, 1);
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
};
|
|
71
|
-
const storeApi = {
|
|
72
|
-
get,
|
|
73
|
-
set,
|
|
74
|
-
on,
|
|
75
|
-
subscribe,
|
|
76
|
-
};
|
|
77
|
-
return storeApi;
|
|
78
|
-
}
|
|
79
|
-
export function combine(stores, callback) {
|
|
80
|
-
let combinedState = callback(...stores);
|
|
81
|
-
stores.forEach((store) => {
|
|
82
|
-
store.subscribe(() => {
|
|
83
|
-
combinedState = callback(...stores);
|
|
84
|
-
listeners.forEach(listener => listener(combinedState));
|
|
85
|
-
});
|
|
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 })
|
|
9
|
+
)
|
|
10
|
+
) : f === "onSuccess" ? n.onSuccess(
|
|
11
|
+
(r, ...e) => u(
|
|
12
|
+
o,
|
|
13
|
+
() => s = t(s, {
|
|
14
|
+
result: r,
|
|
15
|
+
args: e
|
|
16
|
+
})
|
|
17
|
+
)
|
|
18
|
+
) : f === "onError" ? n.onError(
|
|
19
|
+
(r, ...e) => u(
|
|
20
|
+
o,
|
|
21
|
+
() => s = t(s, {
|
|
22
|
+
error: r,
|
|
23
|
+
args: e
|
|
24
|
+
})
|
|
25
|
+
)
|
|
26
|
+
) : n.onFinally(
|
|
27
|
+
(...r) => u(
|
|
28
|
+
o,
|
|
29
|
+
() => s = t(s, { args: r })
|
|
30
|
+
)
|
|
31
|
+
), c),
|
|
32
|
+
get() {
|
|
33
|
+
return s;
|
|
34
|
+
},
|
|
35
|
+
set(n) {
|
|
36
|
+
s = n;
|
|
37
|
+
},
|
|
38
|
+
subscribe(n) {
|
|
39
|
+
return () => {
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
enableOn(n) {
|
|
43
|
+
return n.subscribe(() => o = !0), c;
|
|
44
|
+
},
|
|
45
|
+
disableOn(n) {
|
|
46
|
+
return n.subscribe(() => o = !1), c;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
return c;
|
|
50
|
+
}, h = function() {
|
|
51
|
+
return {
|
|
52
|
+
afterAll: [],
|
|
53
|
+
beforeAll: [],
|
|
54
|
+
other: []
|
|
55
|
+
};
|
|
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
|
+
}).finally(() => {
|
|
61
|
+
f.beforeAll.forEach((e) => e(...r)), f.other.forEach((e) => e(...r)), f.afterAll.forEach((e) => e(...r));
|
|
86
62
|
});
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
63
|
+
};
|
|
64
|
+
return t.onBefore = (r, e) => {
|
|
65
|
+
switch (e) {
|
|
66
|
+
case "beforeAll":
|
|
67
|
+
o.beforeAll.push(r);
|
|
68
|
+
break;
|
|
69
|
+
case "afterAll":
|
|
70
|
+
o.afterAll.push(r);
|
|
71
|
+
break;
|
|
72
|
+
default:
|
|
73
|
+
o.other.push(r);
|
|
74
|
+
}
|
|
75
|
+
}, t.onSuccess = (r, e) => {
|
|
76
|
+
switch (e) {
|
|
77
|
+
case "beforeAll":
|
|
78
|
+
c.beforeAll.push(r);
|
|
79
|
+
break;
|
|
80
|
+
case "afterAll":
|
|
81
|
+
c.afterAll.push(r);
|
|
82
|
+
break;
|
|
83
|
+
default:
|
|
84
|
+
c.other.push(r);
|
|
85
|
+
}
|
|
86
|
+
}, t.onError = (r, e) => {
|
|
87
|
+
switch (e) {
|
|
88
|
+
case "beforeAll":
|
|
89
|
+
n.beforeAll.push(r);
|
|
90
|
+
break;
|
|
91
|
+
case "afterAll":
|
|
92
|
+
n.afterAll.push(r);
|
|
93
|
+
break;
|
|
94
|
+
default:
|
|
95
|
+
n.other.push(r);
|
|
96
|
+
}
|
|
97
|
+
}, t.onFinally = (r, e) => {
|
|
98
|
+
switch (e) {
|
|
99
|
+
case "beforeAll":
|
|
100
|
+
f.beforeAll.push(r);
|
|
101
|
+
break;
|
|
102
|
+
case "afterAll":
|
|
103
|
+
f.afterAll.push(r);
|
|
104
|
+
break;
|
|
105
|
+
default:
|
|
106
|
+
f.other.push(r);
|
|
107
|
+
}
|
|
108
|
+
}, t;
|
|
109
|
+
}, A = function(s, o, c = !0) {
|
|
110
|
+
let n = o(...s);
|
|
111
|
+
const f = [];
|
|
112
|
+
s.forEach((r) => {
|
|
113
|
+
r.subscribe(() => {
|
|
114
|
+
u(c, () => {
|
|
115
|
+
n = o(...s), f.forEach((e) => e(n));
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
const t = {
|
|
120
|
+
on: () => {
|
|
121
|
+
throw new Error("Cannot call 'on' on combined store");
|
|
122
|
+
},
|
|
123
|
+
get() {
|
|
124
|
+
return n;
|
|
125
|
+
},
|
|
126
|
+
set() {
|
|
127
|
+
throw new Error("Cannot call 'set' on combined store");
|
|
128
|
+
},
|
|
129
|
+
subscribe(r) {
|
|
130
|
+
return f.push(r), () => {
|
|
131
|
+
const e = f.indexOf(r);
|
|
132
|
+
~e && f.splice(e, 1);
|
|
133
|
+
};
|
|
134
|
+
},
|
|
135
|
+
enableOn(r) {
|
|
136
|
+
return r.subscribe(() => c = !1), t;
|
|
137
|
+
},
|
|
138
|
+
disableOn(r) {
|
|
139
|
+
return r.subscribe(() => c = !0), t;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
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);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
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
|
+
};
|
|
159
|
+
export {
|
|
160
|
+
A as combine,
|
|
161
|
+
b as effect,
|
|
162
|
+
u as enableCheck,
|
|
163
|
+
E as marker,
|
|
164
|
+
p as pending,
|
|
165
|
+
a as store,
|
|
166
|
+
i as to
|
|
167
|
+
};
|