@ludekarts/minirdx 0.3.0 → 0.5.0-beta.2
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.d.ts +5 -2
- package/dist/minirdx.index.cjs.js +1 -1
- package/dist/minirdx.index.es.js +150 -89
- package/dist/minirdx.react.cjs.js +1 -1
- package/dist/minirdx.react.es.js +18 -17
- package/dist/react.d.ts +2 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
export type Action<S> = (state: () => S, ...args: any[]) => S | Promise<S>;
|
|
2
|
+
type ActionEventName<A> = keyof A | "link";
|
|
3
|
+
type Middleware<S> = (state: () => S, actionName: string) => S | Promise<S>;
|
|
2
4
|
type OmitFirstParam<T extends (...args: any[]) => any> = T extends (state: any, ...args: infer P) => any ? P : never;
|
|
3
5
|
export interface Store<S, A extends Record<string, Action<S>>> {
|
|
4
6
|
state: () => S;
|
|
5
7
|
actions: {
|
|
6
8
|
[K in keyof A]: (...args: OmitFirstParam<A[K]>) => Promise<S>;
|
|
7
9
|
};
|
|
8
|
-
on: (action: keyof A | ((state: S, actionName:
|
|
10
|
+
on: (action: keyof A | ((state: S, actionName: ActionEventName<A>) => void), listener?: (state: S, actionName: ActionEventName<A>) => void) => () => void;
|
|
11
|
+
tap: (action: keyof A | Middleware<S>, middleware?: Middleware<S>) => () => void;
|
|
9
12
|
}
|
|
10
13
|
export declare function createStore<S, A extends Record<string, Action<S>>>(config: {
|
|
11
14
|
state: S;
|
|
@@ -14,7 +17,7 @@ export declare function createStore<S, A extends Record<string, Action<S>>>(conf
|
|
|
14
17
|
export declare function selector<S, Args extends any[]>(path: string, action: (...args: Args) => any): (state: () => S, ...args: OmitFirstParam<typeof action>) => S | Promise<S>;
|
|
15
18
|
type SelectorObject<V> = {
|
|
16
19
|
getter: (state: unknown) => V;
|
|
17
|
-
setter: (
|
|
20
|
+
setter: (state: unknown, value: V) => V;
|
|
18
21
|
};
|
|
19
22
|
export declare function createSelector<V>(selector: string): SelectorObject<V>;
|
|
20
23
|
export declare function link<T>(...args: any[]): Readonly<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const k=["on","tap","state","actions"];function P(t){let{state:e,actions:u}=t;const r=[],s=new Map,o=new Map;_(e,(n,i)=>{if(T(n)){const{setter:l}=A(`state.${i}`),f=c=>{l(y(),c),S()};n._rdx_link_(f)}});const a=Object.fromEntries(Object.entries(u).map(([n,i])=>{if(k.includes(n))throw new Error(`MiniRdxError: "${n}" is a reserved keyword`);if(typeof i!="function")throw new Error(`MiniRdxError: "${n}" should be a function`);return[n,(...l)=>new Promise((f,c)=>{p(n,i,l,f,c)})]}));function p(n,i,l,f,c){try{const d=L(n),w=h=>i(h,...l);g(d,n).then(()=>Promise.resolve(w(y))).then(h=>E(h,n,f)).catch(c)}catch(d){c(d)}}function g(n,i){return n.reduce((l,f)=>l.then(()=>Promise.resolve(f(y,i))).then(c=>{e=c}),Promise.resolve())}function E(n,i,l){var f;e=n,r.length&&r.forEach(c=>c(e,i)),(f=s.get(i))==null||f.forEach(c=>c(e,i)),l(e)}function y(){return e}function S(){const n=m(e)?{...e}:Array.isArray(e)?[...e]:e;e=n,r.length&&r.forEach(i=>i(n,"link"))}function x(n,i){var l;if(typeof n=="function"&&i===void 0)return r.push(n),()=>{const f=r.indexOf(n);f>=0&&r.splice(f,1)};if(typeof n=="string"&&i)return s.has(n)||s.set(n,[]),(l=s.get(n))==null||l.push(i),()=>{const f=s.get(n);if(!f)return;const c=f.indexOf(i);c>=0&&f.splice(c,1)};throw new Error("MiniRdxError: Invalid arguments. Try: state.on(action: string, listener: ActionListener)")}function I(n,i){var w;const c=typeof n=="function"&&i===void 0?"*":typeof n=="string"&&typeof i=="function"?n:null,d=typeof i=="function"?i:n;if(c===null||typeof d!="function")throw new Error("MiniRdxError: Invalid arguments. Try: state.tap(actionName, middleware)");return!o.has(c)&&o.set(c,[]),(w=o.get(c))==null||w.push(d),()=>{const h=o.get(c);if(!h)return;const O=h.indexOf(d);O>=0&&h.splice(O,1)}}function L(n){const i=o.get("*")||[],l=o.get(n)||[];return[...i,...l]}return{state:y,on:x,actions:a,tap:I}}function $(t,e){const{getter:u,setter:r}=A(t),s=o=>Array.isArray(o)?[...o]:m(o)?{...o}:o;return function(a,...p){const g=e(u(a()),...p);if(b(g))return Promise.resolve(g).then(y=>{const S=r(a(),y);return s(S)});const E=r(a(),g);return s(E)}}function A(t){const e=R(t);return{getter:u=>{let r=u;for(const s of e)r=r[s];return r},setter:(u,r)=>{let s=u;for(let o=0;o<e.length-1;o++)s=s[e[o]];return s[e[e.length-1]]=r,u}}}function R(t){if(!/^state(?:\.[A-Za-z_$][\w$]*|\[\d+\])+$/.test(t))throw new Error('MiniRDXError: Selector should be a string with dot notation starting with "state." e.g.: "state.user.cars[1].model" ');const e=t.slice(6),u=[];let r=0;for(;r<e.length;){const s=e[r];if(s==="."){r++;continue}if(s==="["){const a=e.indexOf("]",r+1),p=e.slice(r+1,a);u.push(Number(p)),r=a+1;continue}let o=r;for(;o<e.length&&/[\w$]/.test(e[o]);)o++;u.push(e.slice(r,o)),r=o}return u}function D(...t){return Object.freeze({_rdx_link_:e=>{const u=t.pop(),r=t,s=()=>{e(u(...r.map(o=>o.state())))};r.forEach(o=>o.on(()=>s())),s()}})}function b(t){return!!t&&(typeof t=="object"||typeof t=="function")&&typeof t.then=="function"}function _(t,e,u="",r=new WeakSet){if(!(typeof t!="object"||t===null)&&!r.has(t)){r.add(t);for(const s in t)if(M(t,s)){const o=t[s],a=/\d+/.test(s)?`[${s}]`:`.${s}`,p=u?`${u}${a}`:s;e(o,p),typeof o=="object"&&o!==null&&_(o,e,p,r)}}}function T(t){return t!==null&&typeof t=="object"&&M(t,"_rdx_link_")}function M(t,e){return Object.prototype.hasOwnProperty.call(t,e)}function m(t){return!!t&&typeof t!="symbol"&&typeof t!="string"&&typeof t!="number"&&typeof t!="boolean"&&typeof t!="function"&&!Array.isArray(t)}exports.createSelector=A;exports.createStore=P;exports.link=D;exports.selector=$;
|
package/dist/minirdx.index.es.js
CHANGED
|
@@ -1,130 +1,191 @@
|
|
|
1
|
-
const
|
|
2
|
-
function
|
|
3
|
-
let { state:
|
|
4
|
-
const r = [], s = /* @__PURE__ */ new Map();
|
|
5
|
-
|
|
6
|
-
if (
|
|
7
|
-
const { setter:
|
|
8
|
-
|
|
1
|
+
const k = ["on", "tap", "state", "actions"];
|
|
2
|
+
function D(t) {
|
|
3
|
+
let { state: e, actions: u } = t;
|
|
4
|
+
const r = [], s = /* @__PURE__ */ new Map(), o = /* @__PURE__ */ new Map();
|
|
5
|
+
S(e, (n, i) => {
|
|
6
|
+
if (R(n)) {
|
|
7
|
+
const { setter: l } = O(`state.${i}`), f = (c) => {
|
|
8
|
+
l(y(), c), A();
|
|
9
9
|
};
|
|
10
|
-
|
|
10
|
+
n._rdx_link_(f);
|
|
11
11
|
}
|
|
12
12
|
});
|
|
13
|
-
const
|
|
14
|
-
Object.entries(
|
|
15
|
-
if (
|
|
16
|
-
throw new Error(`MiniRdxError: "${
|
|
17
|
-
if (typeof
|
|
18
|
-
throw new Error(`MiniRdxError: "${
|
|
13
|
+
const a = Object.fromEntries(
|
|
14
|
+
Object.entries(u).map(([n, i]) => {
|
|
15
|
+
if (k.includes(n))
|
|
16
|
+
throw new Error(`MiniRdxError: "${n}" is a reserved keyword`);
|
|
17
|
+
if (typeof i != "function")
|
|
18
|
+
throw new Error(`MiniRdxError: "${n}" should be a function`);
|
|
19
19
|
return [
|
|
20
|
-
|
|
21
|
-
(...
|
|
22
|
-
p(
|
|
23
|
-
e,
|
|
24
|
-
(u) => o(u, ...f),
|
|
25
|
-
i
|
|
26
|
-
);
|
|
20
|
+
n,
|
|
21
|
+
(...l) => new Promise((f, c) => {
|
|
22
|
+
p(n, i, l, f, c);
|
|
27
23
|
})
|
|
28
24
|
];
|
|
29
25
|
})
|
|
30
26
|
);
|
|
31
|
-
function p(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
(
|
|
35
|
-
|
|
27
|
+
function p(n, i, l, f, c) {
|
|
28
|
+
try {
|
|
29
|
+
const d = m(n), g = (h) => i(h, ...l);
|
|
30
|
+
w(d, n).then(() => Promise.resolve(g(y))).then(
|
|
31
|
+
(h) => E(h, n, f)
|
|
32
|
+
).catch(c);
|
|
33
|
+
} catch (d) {
|
|
34
|
+
c(d);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function w(n, i) {
|
|
38
|
+
return n.reduce(
|
|
39
|
+
(l, f) => l.then(() => Promise.resolve(f(y, i))).then((c) => {
|
|
40
|
+
e = c;
|
|
41
|
+
}),
|
|
42
|
+
Promise.resolve()
|
|
43
|
+
);
|
|
36
44
|
}
|
|
37
|
-
function
|
|
38
|
-
var
|
|
39
|
-
|
|
45
|
+
function E(n, i, l) {
|
|
46
|
+
var f;
|
|
47
|
+
e = n, r.length && r.forEach((c) => c(e, i)), (f = s.get(i)) == null || f.forEach((c) => c(e, i)), l(e);
|
|
40
48
|
}
|
|
41
49
|
function y() {
|
|
42
|
-
return
|
|
50
|
+
return e;
|
|
43
51
|
}
|
|
44
|
-
function
|
|
45
|
-
const
|
|
46
|
-
r.length && r.forEach((
|
|
52
|
+
function A() {
|
|
53
|
+
const n = M(e) ? { ...e } : Array.isArray(e) ? [...e] : e;
|
|
54
|
+
e = n, r.length && r.forEach((i) => i(n, "link"));
|
|
47
55
|
}
|
|
48
|
-
function
|
|
49
|
-
var
|
|
50
|
-
if (typeof
|
|
51
|
-
return r.push(
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
+
function I(n, i) {
|
|
57
|
+
var l;
|
|
58
|
+
if (typeof n == "function" && i === void 0)
|
|
59
|
+
return r.push(n), () => {
|
|
60
|
+
const f = r.indexOf(n);
|
|
61
|
+
f >= 0 && r.splice(f, 1);
|
|
62
|
+
};
|
|
63
|
+
if (typeof n == "string" && i)
|
|
64
|
+
return s.has(n) || s.set(n, []), (l = s.get(n)) == null || l.push(i), () => {
|
|
65
|
+
const f = s.get(n);
|
|
66
|
+
if (!f) return;
|
|
67
|
+
const c = f.indexOf(i);
|
|
68
|
+
c >= 0 && f.splice(c, 1);
|
|
56
69
|
};
|
|
57
70
|
throw new Error(
|
|
58
71
|
"MiniRdxError: Invalid arguments. Try: state.on(action: string, listener: ActionListener)"
|
|
59
72
|
);
|
|
60
73
|
}
|
|
74
|
+
function L(n, i) {
|
|
75
|
+
var g;
|
|
76
|
+
const c = typeof n == "function" && i === void 0 ? "*" : typeof n == "string" && typeof i == "function" ? n : null, d = typeof i == "function" ? i : n;
|
|
77
|
+
if (c === null || typeof d != "function")
|
|
78
|
+
throw new Error(
|
|
79
|
+
"MiniRdxError: Invalid arguments. Try: state.tap(actionName, middleware)"
|
|
80
|
+
);
|
|
81
|
+
return !o.has(c) && o.set(c, []), (g = o.get(c)) == null || g.push(d), () => {
|
|
82
|
+
const h = o.get(c);
|
|
83
|
+
if (!h) return;
|
|
84
|
+
const _ = h.indexOf(d);
|
|
85
|
+
_ >= 0 && h.splice(_, 1);
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function m(n) {
|
|
89
|
+
const i = o.get("*") || [], l = o.get(n) || [];
|
|
90
|
+
return [...i, ...l];
|
|
91
|
+
}
|
|
61
92
|
return {
|
|
62
93
|
state: y,
|
|
63
|
-
|
|
64
|
-
|
|
94
|
+
on: I,
|
|
95
|
+
actions: a,
|
|
96
|
+
tap: L
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function T(t, e) {
|
|
100
|
+
const { getter: u, setter: r } = O(t), s = (o) => Array.isArray(o) ? [...o] : M(o) ? { ...o } : o;
|
|
101
|
+
return function(a, ...p) {
|
|
102
|
+
const w = e(u(a()), ...p);
|
|
103
|
+
if ($(w))
|
|
104
|
+
return Promise.resolve(w).then((y) => {
|
|
105
|
+
const A = r(a(), y);
|
|
106
|
+
return s(A);
|
|
107
|
+
});
|
|
108
|
+
const E = r(a(), w);
|
|
109
|
+
return s(E);
|
|
65
110
|
};
|
|
66
111
|
}
|
|
67
|
-
function
|
|
68
|
-
const
|
|
69
|
-
return
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
112
|
+
function O(t) {
|
|
113
|
+
const e = P(t);
|
|
114
|
+
return {
|
|
115
|
+
getter: (u) => {
|
|
116
|
+
let r = u;
|
|
117
|
+
for (const s of e)
|
|
118
|
+
r = r[s];
|
|
119
|
+
return r;
|
|
120
|
+
},
|
|
121
|
+
setter: (u, r) => {
|
|
122
|
+
let s = u;
|
|
123
|
+
for (let o = 0; o < e.length - 1; o++)
|
|
124
|
+
s = s[e[o]];
|
|
125
|
+
return s[e[e.length - 1]] = r, u;
|
|
77
126
|
}
|
|
78
127
|
};
|
|
79
128
|
}
|
|
80
|
-
function
|
|
81
|
-
if (
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
129
|
+
function P(t) {
|
|
130
|
+
if (!/^state(?:\.[A-Za-z_$][\w$]*|\[\d+\])+$/.test(t))
|
|
131
|
+
throw new Error(
|
|
132
|
+
'MiniRDXError: Selector should be a string with dot notation starting with "state." e.g.: "state.user.cars[1].model" '
|
|
133
|
+
);
|
|
134
|
+
const e = t.slice(6), u = [];
|
|
135
|
+
let r = 0;
|
|
136
|
+
for (; r < e.length; ) {
|
|
137
|
+
const s = e[r];
|
|
138
|
+
if (s === ".") {
|
|
139
|
+
r++;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (s === "[") {
|
|
143
|
+
const a = e.indexOf("]", r + 1), p = e.slice(r + 1, a);
|
|
144
|
+
u.push(Number(p)), r = a + 1;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
let o = r;
|
|
148
|
+
for (; o < e.length && /[\w$]/.test(e[o]); )
|
|
149
|
+
o++;
|
|
150
|
+
u.push(e.slice(r, o)), r = o;
|
|
151
|
+
}
|
|
152
|
+
return u;
|
|
93
153
|
}
|
|
94
|
-
function
|
|
154
|
+
function b(...t) {
|
|
95
155
|
return Object.freeze({
|
|
96
|
-
_rdx_link_: (
|
|
97
|
-
const
|
|
98
|
-
|
|
156
|
+
_rdx_link_: (e) => {
|
|
157
|
+
const u = t.pop(), r = t, s = () => {
|
|
158
|
+
e(u(...r.map((o) => o.state())));
|
|
99
159
|
};
|
|
100
|
-
r.forEach((
|
|
160
|
+
r.forEach((o) => o.on(() => s())), s();
|
|
101
161
|
}
|
|
102
162
|
});
|
|
103
163
|
}
|
|
104
|
-
function
|
|
164
|
+
function $(t) {
|
|
105
165
|
return !!t && (typeof t == "object" || typeof t == "function") && typeof t.then == "function";
|
|
106
166
|
}
|
|
107
|
-
function
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
const s = t[r], c = /\d+/.test(r) ? `[${r}]` : `.${r}`, p = a ? `${a}${c}` : r;
|
|
115
|
-
n(s, p), typeof s == "object" && s !== null && w(s, n, p);
|
|
167
|
+
function S(t, e, u = "", r = /* @__PURE__ */ new WeakSet()) {
|
|
168
|
+
if (!(typeof t != "object" || t === null) && !r.has(t)) {
|
|
169
|
+
r.add(t);
|
|
170
|
+
for (const s in t)
|
|
171
|
+
if (x(t, s)) {
|
|
172
|
+
const o = t[s], a = /\d+/.test(s) ? `[${s}]` : `.${s}`, p = u ? `${u}${a}` : s;
|
|
173
|
+
e(o, p), typeof o == "object" && o !== null && S(o, e, p, r);
|
|
116
174
|
}
|
|
117
175
|
}
|
|
118
176
|
}
|
|
119
|
-
function
|
|
120
|
-
return t !== null && typeof t == "object" && t
|
|
177
|
+
function R(t) {
|
|
178
|
+
return t !== null && typeof t == "object" && x(t, "_rdx_link_");
|
|
179
|
+
}
|
|
180
|
+
function x(t, e) {
|
|
181
|
+
return Object.prototype.hasOwnProperty.call(t, e);
|
|
121
182
|
}
|
|
122
|
-
function
|
|
183
|
+
function M(t) {
|
|
123
184
|
return !!t && typeof t != "symbol" && typeof t != "string" && typeof t != "number" && typeof t != "boolean" && typeof t != "function" && !Array.isArray(t);
|
|
124
185
|
}
|
|
125
186
|
export {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
187
|
+
O as createSelector,
|
|
188
|
+
D as createStore,
|
|
189
|
+
b as link,
|
|
190
|
+
T as selector
|
|
130
191
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const
|
|
1
|
+
"use strict";const i=require("react");function j(r){const{state:c,on:o,actions:a}=r;function f(t,s){const d=typeof t=="function"?t(c()):c(),[D,E]=i.useState(d);return i.useEffect(()=>{const b=typeof s=="function"?s:(n,e)=>Object.is(n,e);return o(n=>{const e=typeof t=="function"?t(n):n;E(u=>b(u,e)?u:e)})},[o,t,s]),D}function S(){return a}function p(){return o}return{useStore:f,useStoreActions:S,useStoreListeners:p}}module.exports=j;
|
package/dist/minirdx.react.es.js
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
|
-
import { useState as
|
|
2
|
-
function
|
|
3
|
-
const { state:
|
|
4
|
-
function
|
|
5
|
-
const [
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
function i() {
|
|
14
|
-
return u;
|
|
1
|
+
import { useState as E, useEffect as b } from "react";
|
|
2
|
+
function y(i) {
|
|
3
|
+
const { state: s, on: e, actions: f } = i;
|
|
4
|
+
function r(t, u) {
|
|
5
|
+
const S = typeof t == "function" ? t(s()) : s(), [d, m] = E(S);
|
|
6
|
+
return b(() => {
|
|
7
|
+
const D = typeof u == "function" ? u : (n, o) => Object.is(n, o);
|
|
8
|
+
return e((n) => {
|
|
9
|
+
const o = typeof t == "function" ? t(n) : n;
|
|
10
|
+
m((c) => D(c, o) ? c : o);
|
|
11
|
+
});
|
|
12
|
+
}, [e, t, u]), d;
|
|
15
13
|
}
|
|
16
14
|
function a() {
|
|
17
|
-
return
|
|
15
|
+
return f;
|
|
16
|
+
}
|
|
17
|
+
function p() {
|
|
18
|
+
return e;
|
|
18
19
|
}
|
|
19
|
-
return { useStore:
|
|
20
|
+
return { useStore: r, useStoreActions: a, useStoreListeners: p };
|
|
20
21
|
}
|
|
21
22
|
export {
|
|
22
|
-
|
|
23
|
+
y as default
|
|
23
24
|
};
|
package/dist/react.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Store, Action } from './index';
|
|
2
2
|
export default function createStoreHooks<S, A extends Record<string, Action<S>>>(store: Store<S, A>): {
|
|
3
|
-
useStore: <T = S>(selector?: (state: S) => T
|
|
3
|
+
useStore: <T = S>(selector?: (state: S) => T, equalityFn?: (prev: T, next: T) => boolean) => T;
|
|
4
4
|
useStoreActions: () => Store<S, A>["actions"];
|
|
5
|
-
useStoreListeners: () => (action: keyof A | ((state: S, actionName: keyof A) => void), listener?: ((state: S, actionName: keyof A) => void) | undefined) => () => void;
|
|
5
|
+
useStoreListeners: () => (action: keyof A | ((state: S, actionName: "link" | keyof A) => void), listener?: ((state: S, actionName: "link" | keyof A) => void) | undefined) => () => void;
|
|
6
6
|
};
|