@vanyamate/sec 0.1.4 → 0.2.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
@@ -2,23 +2,191 @@
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();
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
+ ### to
72
+
73
+ Just helper. Returns a function that returns the passed value.
74
+
75
+ ```typescript
76
+
77
+ to(123); // Return () => 123
78
+
79
+ ```
80
+
81
+ ### pending
82
+
83
+ Just helper. wrapper over store. returns a bool value and is used to create a pending-store.
84
+
85
+ ### store
86
+
87
+ Store stores data and subscribes to effect.
88
+
89
+ When initializing, the store takes 2 values. The first value is the initialization data. The second value is optional -
90
+ enable/disable. When disabled, the store will not be updated.
91
+
92
+ The store has an api.
93
+
94
+ - `on` - subscription to effect. takes 3 parameters. effect, type (`onBefore`, `onSuccess`, `onError` and `onFinally`)
95
+ and handler. handler has signature depending on type. handler is a function that takes 2 parameters and should always
96
+ return value of the type that is currently specified in store. first parameter is current state of store, and the
97
+ second is an object and depends on subscription type. for `onBefore` it is only `{ args }` where `args` is an array of
98
+ arguments of the function that we passed to effect. for `onSuccess` it is `{ args, result }` where `result` is result
99
+ of `Promise` execution. for `onError` `{ args, error }` where `error` is `unknown`. and for `onFinally` it is
100
+ `{ args }`.
101
+ - `onBefore` - `(state, { args }) => state`
102
+ - `onSuccess` - `(state, { args, result }) => state`
103
+ - `onError` - `(state, { args, error }) => state`
104
+ - `onFinally` - `(state, { args }) => state`
105
+ - `get` - get current store value.
106
+ - `set` - set new value
107
+ - `subscribe` - receives a function as input that will be executed when the store changes and returns an unsubscribe
108
+ function.
109
+ - `enableOn` - receives a marker that, when triggered, will enable the store.
110
+ - `disableOn` - receives a marker that, when triggered, will disable the store.
111
+
112
+ ```typescript
113
+
114
+ const authIsPending = store<boolean>(false)
115
+ .on(loginEffect, 'onBefore', () => true)
116
+ .on(loginEffect, 'onFinally', to(false)); // instead of () => false
117
+
118
+ const authError = store<Error | null>(null)
119
+ .on(loginEffect, 'onError', (_, { error }) => {
120
+ // For example
121
+ if (error instanceof Error) {
122
+ return error;
123
+ } else {
124
+ return new Error(error);
125
+ }
126
+ });
127
+
128
+ const authData = store<UserData | null>(null)
129
+ .on(loginEffect, 'onSuccess', (_, { result }) => result)
130
+ .on(logoutEffect, 'onSuccess', () => null);
131
+
132
+ const postsForUserId = store<number>(0)
133
+ .enableOn(loginMarker)
134
+ .disableOn(logoutMarker)
135
+ .on(getPostsForUser, 'onBefore', (_, { args: [ id ] }) => id);
136
+
137
+ /**
138
+ *
139
+ * instead of
140
+ * const postsIsPending = store<boolean>(false)
141
+ * .on(getPostsForUser, 'onBefore', () => true)
142
+ * .on(getPostsForUser, 'onFinally', () => false)
143
+ * .on(createPostEffect, 'onBefore', () => true)
144
+ * .on(createPostEffect, 'onFinally', () => false);
145
+ *
146
+ */
147
+
148
+ const postsIsPending = pending([
149
+ getPostsForUser,
150
+ createPostEffect,
151
+ ]);
152
+
153
+ const posts = store<Array<Post>>([])
154
+ .enableOn(loginMarker)
155
+ .disableOn(logoutMarker)
156
+ .on(getPostsForUser, 'onSuccess', (state, { result, args: [ userId ] }) => {
157
+ if (postsForUserId.get() === userId) {
158
+ return state.concat(result);
159
+ } else {
160
+ return result;
161
+ }
162
+ })
163
+ .on(createPostEffect, 'onSuccess', (state, { result, args: [ userId ] }) => {
164
+ if (postsForUserId.get() === userId) {
165
+ return state.concat(result);
166
+ } else {
167
+ return state;
168
+ }
169
+ })
170
+ .on(logoutEffect, 'onSuccess', to([]));
171
+ ```
172
+
173
+ ### Types
174
+
175
+ instead of writing everything in `.on` - you can take out the handlers separately, setting the required type for them. `StoreEffectEventMap` is a generic and takes 2 parameters, and then the type is selected.
176
+
177
+ ```typescript
178
+ // Example:
12
179
 
13
- const sum = async (a, b) => a + b;
180
+ const getRandomId = async function () {
181
+ return { id: Math.random() };
182
+ };
14
183
 
15
- // Create effect
16
- const sumEffect = effect(sum);
184
+ const getRandomEffect = effect(getRandomId);
17
185
 
18
- // Create stores
19
- const allSums = store(0).on(sumEffect, 'onSuccess', (state, { result }) => state + result);
20
- const lastSum = store(0).on(sumEffect, 'onSuccess', (_, { result }) => result);
186
+ const handler: StoreHandlerMap<number, typeof getRandomId>['onSuccess'] = function (state, { result }) {
187
+ return result.id;
188
+ };
21
189
 
22
- // Create combine
23
- const bothSum = combine([ allSums, lastSum ], (...args) => args.reduce((acc, item) => acc + item.get(), 0));
190
+ const num = store<number>(0)
191
+ .on(getRandomEffect, 'onSuccess', handler);
24
192
  ```
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=function(s,o){s&&o()},A=function(s,o=!0){const c={on:(n,f,t)=>(f==="onBefore"?n.onBefore((...e)=>u(o,()=>s=t(s,{args:e}))):f==="onSuccess"?n.onSuccess((e,...r)=>u(o,()=>s=t(s,{result:e,args:r}))):f==="onError"?n.onError((e,...r)=>u(o,()=>s=t(s,{error:e,args:r}))):n.onFinally((...e)=>u(o,()=>s=t(s,{args:e}))),c),get(){return s},set(n){s=n},subscribe(n){return()=>{}},enableOn(n){return n.subscribe(()=>o=!1),c},disableOn(n){return n.subscribe(()=>()=>o=!0),c}};return c},h=function(){return{afterAll:[],beforeAll:[],other:[]}},E=function(s){const o=h(),c=h(),n=h(),f=h(),t=async function(...e){return o.beforeAll.forEach(r=>r(...e)),o.other.forEach(r=>r(...e)),o.afterAll.forEach(r=>r(...e)),s(...e).then(r=>(c.beforeAll.forEach(l=>l(r,...e)),c.other.forEach(l=>l(r,...e)),c.afterAll.forEach(l=>l(r,...e)),r)).catch(r=>{throw n.beforeAll.forEach(l=>l(r,...e)),n.other.forEach(l=>l(r,...e)),n.afterAll.forEach(l=>l(r,...e)),r}).finally(()=>{f.beforeAll.forEach(r=>r(...e)),f.other.forEach(r=>r(...e)),f.afterAll.forEach(r=>r(...e))})};return t.onBefore=(e,r)=>{switch(r){case"beforeAll":o.beforeAll.push(e);break;case"afterAll":o.afterAll.push(e);break;default:o.other.push(e)}},t.onSuccess=(e,r)=>{switch(r){case"beforeAll":c.beforeAll.push(e);break;case"afterAll":c.afterAll.push(e);break;default:c.other.push(e)}},t.onError=(e,r)=>{switch(r){case"beforeAll":n.beforeAll.push(e);break;case"afterAll":n.afterAll.push(e);break;default:n.other.push(e)}},t.onFinally=(e,r)=>{switch(r){case"beforeAll":f.beforeAll.push(e);break;case"afterAll":f.afterAll.push(e);break;default:f.other.push(e)}},t},i=function(s,o,c=!0){let n=o(...s);const f=[];s.forEach(e=>{e.subscribe(()=>{u(c,()=>{n=o(...s),f.forEach(r=>r(n))})})});const t={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 f.push(e),()=>{const r=f.indexOf(e);~r&&f.splice(r,1)}},enableOn(e){return e.subscribe(()=>c=!1),t},disableOn(e){return e.subscribe(()=>c=!0),t}};return t},a=function(s){const o=[],c={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),subscribe:n=>{o.push(n)}};return c},b=function(s){return()=>s},p=function(s){const o=A(!1);return s.forEach(c=>{o.on(c,"onBefore",b(!0)),o.on(c,"onFinally",b(!1))}),o};exports.combine=i;exports.effect=E;exports.enableCheck=u;exports.marker=a;exports.pending=p;exports.store=A;exports.to=b;
package/dist/index.d.ts CHANGED
@@ -1,29 +1,88 @@
1
- export type EffectFunction<Args extends any[], Result> = (...args: Args) => Promise<Result>;
2
- export type EffectEvent = 'onBefore' | 'onSuccess' | 'onError' | 'onFinally';
3
- export type Effect<Args extends any[], Result> = {
4
- (...args: Args): Promise<Result>;
5
- onSuccess: (callback: (result: Result, ...args: Args) => void) => void;
6
- onError: (callback: (error: unknown, ...args: Args) => void) => void;
7
- onFinally: (callback: (...args: Args) => void) => void;
8
- onBefore: (callback: (...args: Args) => void) => void;
9
- };
10
- export type Listener<State> = (state: State) => void;
11
- export type Payload<Result, Args> = {
12
- result?: Result;
13
- error?: unknown;
14
- args: Args;
15
- };
16
- export type Handler<State, Args, Result> = (state: State, payload: Payload<Result, Args>) => State;
17
- export declare function effect<Args extends any[], Result>(fn: EffectFunction<Args, Result>): Effect<Args, Result>;
18
- export type Store<State> = {
19
- get: () => State;
20
- set: (newState: State) => void;
21
- on: <Args extends any[], Result>(effect: Effect<Args, Result>, event: EffectEvent, handler: Handler<State, Args, Result>) => Store<State>;
22
- subscribe: (listener: Listener<State>) => () => void;
23
- };
24
- export declare function store<State>(initialState: State): Store<State>;
25
- export declare function combine<States extends any[], Result>(stores: {
26
- [K in keyof States]: Store<States[K]>;
27
- }, callback: (...stores: {
28
- [K in keyof States]: Store<States[K]>;
29
- }) => Result): Store<Result>;
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>(initialData: 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
- export function effect(fn) {
2
- let beforeListeners = [];
3
- let successListeners = [];
4
- let errorListeners = [];
5
- let finallyListeners = [];
6
- const wrappedEffect = async (...args) => {
7
- beforeListeners.forEach(listener => listener(...args));
8
- try {
9
- const result = await fn(...args);
10
- successListeners.forEach(listener => listener(result, ...args));
11
- return result;
12
- }
13
- catch (error) {
14
- errorListeners.forEach(listener => listener(error, ...args));
15
- throw error;
16
- }
17
- finally {
18
- finallyListeners.forEach(listener => listener(...args));
19
- }
20
- };
21
- wrappedEffect.onBefore = (callback) => {
22
- beforeListeners.push(callback);
23
- };
24
- wrappedEffect.onSuccess = (callback) => {
25
- successListeners.push(callback);
26
- };
27
- wrappedEffect.onError = (callback) => {
28
- errorListeners.push(callback);
29
- };
30
- wrappedEffect.onFinally = (callback) => {
31
- finallyListeners.push(callback);
32
- };
33
- return wrappedEffect;
34
- }
35
- export function store(initialState) {
36
- let state = initialState;
37
- let listeners = [];
38
- const get = () => state;
39
- const set = (newState) => {
40
- state = newState;
41
- listeners.forEach(listener => listener(state));
42
- };
43
- const on = (effect, event, handler) => {
44
- const callback = (payload) => storeApi.set(handler(state, payload));
45
- if (event === 'onBefore') {
46
- effect.onBefore((...args) => callback({ args }));
47
- }
48
- else if (event === 'onSuccess') {
49
- effect.onSuccess((result, ...args) => callback({
50
- result,
51
- args,
52
- }));
53
- }
54
- else if (event === 'onError') {
55
- effect.onError((error, ...args) => callback({ error, args }));
56
- }
57
- else if (event === 'onFinally') {
58
- effect.onFinally((...args) => callback({ args }));
59
- }
60
- return storeApi;
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 l = 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
+ (...e) => l(
7
+ o,
8
+ () => s = t(s, { args: e })
9
+ )
10
+ ) : f === "onSuccess" ? n.onSuccess(
11
+ (e, ...r) => l(
12
+ o,
13
+ () => s = t(s, {
14
+ result: e,
15
+ args: r
16
+ })
17
+ )
18
+ ) : f === "onError" ? n.onError(
19
+ (e, ...r) => l(
20
+ o,
21
+ () => s = t(s, {
22
+ error: e,
23
+ args: r
24
+ })
25
+ )
26
+ ) : n.onFinally(
27
+ (...e) => l(
28
+ o,
29
+ () => s = t(s, { args: e })
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 = !1), c;
44
+ },
45
+ disableOn(n) {
46
+ return n.subscribe(() => () => o = !0), c;
47
+ }
48
+ };
49
+ return c;
50
+ }, h = function() {
51
+ return {
52
+ afterAll: [],
53
+ beforeAll: [],
54
+ other: []
55
+ };
56
+ }, E = function(s) {
57
+ const o = h(), c = h(), n = h(), f = h(), t = async function(...e) {
58
+ return o.beforeAll.forEach((r) => r(...e)), o.other.forEach((r) => r(...e)), o.afterAll.forEach((r) => r(...e)), s(...e).then((r) => (c.beforeAll.forEach((u) => u(r, ...e)), c.other.forEach((u) => u(r, ...e)), c.afterAll.forEach((u) => u(r, ...e)), r)).catch((r) => {
59
+ throw n.beforeAll.forEach((u) => u(r, ...e)), n.other.forEach((u) => u(r, ...e)), n.afterAll.forEach((u) => u(r, ...e)), r;
60
+ }).finally(() => {
61
+ f.beforeAll.forEach((r) => r(...e)), f.other.forEach((r) => r(...e)), f.afterAll.forEach((r) => r(...e));
86
62
  });
87
- const listeners = [];
88
- const get = () => combinedState;
89
- const subscribe = (listener) => {
90
- listeners.push(listener);
91
- return () => {
92
- const index = listeners.indexOf(listener);
93
- if (index !== -1) {
94
- listeners.splice(index, 1);
95
- }
96
- };
97
- };
98
- const storeApi = {
99
- get,
100
- set: () => {
101
- throw new Error('Cannot call \'set\' on combined store');
102
- },
103
- on: () => {
104
- throw new Error('Cannot call \'on\' on combined store');
105
- },
106
- subscribe,
107
- };
108
- return storeApi;
109
- }
63
+ };
64
+ return t.onBefore = (e, r) => {
65
+ switch (r) {
66
+ case "beforeAll":
67
+ o.beforeAll.push(e);
68
+ break;
69
+ case "afterAll":
70
+ o.afterAll.push(e);
71
+ break;
72
+ default:
73
+ o.other.push(e);
74
+ }
75
+ }, t.onSuccess = (e, r) => {
76
+ switch (r) {
77
+ case "beforeAll":
78
+ c.beforeAll.push(e);
79
+ break;
80
+ case "afterAll":
81
+ c.afterAll.push(e);
82
+ break;
83
+ default:
84
+ c.other.push(e);
85
+ }
86
+ }, t.onError = (e, r) => {
87
+ switch (r) {
88
+ case "beforeAll":
89
+ n.beforeAll.push(e);
90
+ break;
91
+ case "afterAll":
92
+ n.afterAll.push(e);
93
+ break;
94
+ default:
95
+ n.other.push(e);
96
+ }
97
+ }, t.onFinally = (e, r) => {
98
+ switch (r) {
99
+ case "beforeAll":
100
+ f.beforeAll.push(e);
101
+ break;
102
+ case "afterAll":
103
+ f.afterAll.push(e);
104
+ break;
105
+ default:
106
+ f.other.push(e);
107
+ }
108
+ }, t;
109
+ }, a = function(s, o, c = !0) {
110
+ let n = o(...s);
111
+ const f = [];
112
+ s.forEach((e) => {
113
+ e.subscribe(() => {
114
+ l(c, () => {
115
+ n = o(...s), f.forEach((r) => r(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(e) {
130
+ return f.push(e), () => {
131
+ const r = f.indexOf(e);
132
+ ~r && f.splice(r, 1);
133
+ };
134
+ },
135
+ enableOn(e) {
136
+ return e.subscribe(() => c = !1), t;
137
+ },
138
+ disableOn(e) {
139
+ return e.subscribe(() => c = !0), t;
140
+ }
141
+ };
142
+ return t;
143
+ }, i = 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
+ }, b = function(s) {
152
+ return () => s;
153
+ }, p = function(s) {
154
+ const o = A(!1);
155
+ return s.forEach((c) => {
156
+ o.on(c, "onBefore", b(!0)), o.on(c, "onFinally", b(!1));
157
+ }), o;
158
+ };
159
+ export {
160
+ a as combine,
161
+ E as effect,
162
+ l as enableCheck,
163
+ i as marker,
164
+ p as pending,
165
+ A as store,
166
+ b as to
167
+ };
package/package.json CHANGED
@@ -1,25 +1,38 @@
1
- {
2
- "name": "@vanyamate/sec",
3
- "version": "0.1.4",
4
- "description": "SEC. Store, Effect, Combine. Tiny state manager",
5
- "scripts": {
6
- "build": "tsc"
7
- },
8
- "main": "./dist/index.js",
9
- "types": "./dist/index.d.ts",
10
- "type": "module",
11
- "repository": {
12
- "type": "git",
13
- "url": "git+https://github.com/VanyaMate/sec.git"
14
- },
15
- "keywords": [
16
- "state",
17
- "manager"
18
- ],
19
- "author": "Vanya Mate",
20
- "license": "ISC",
21
- "bugs": {
22
- "url": "https://github.com/VanyaMate/sec/issues"
23
- },
24
- "homepage": "https://github.com/VanyaMate/sec#readme"
25
- }
1
+ {
2
+ "name": "@vanyamate/sec",
3
+ "version": "0.2.0",
4
+ "description": "SEC. Store, Effect, Combine. Tiny state manager",
5
+ "scripts": {
6
+ "build": "vite build",
7
+ "test": "vitest"
8
+ },
9
+ "main": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "type": "module",
12
+ "exports": {
13
+ ".": {
14
+ "import": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "require": "./dist/index.cjs"
17
+ }
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/VanyaMate/sec.git"
22
+ },
23
+ "keywords": [
24
+ "state",
25
+ "manager"
26
+ ],
27
+ "author": "Vanya Mate",
28
+ "license": "ISC",
29
+ "bugs": {
30
+ "url": "https://github.com/VanyaMate/sec/issues"
31
+ },
32
+ "homepage": "https://github.com/VanyaMate/sec#readme",
33
+ "devDependencies": {
34
+ "vite": "^6.3.5",
35
+ "vite-plugin-dts": "^4.5.3",
36
+ "vitest": "^3.1.4"
37
+ }
38
+ }