@ludekarts/minirdx 0.3.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/dist/index.d.ts +23 -0
- package/dist/minirdx.index.cjs.js +1 -0
- package/dist/minirdx.index.es.js +130 -0
- package/dist/minirdx.react.cjs.js +1 -0
- package/dist/minirdx.react.es.js +23 -0
- package/dist/react.d.ts +6 -0
- package/package.json +42 -0
- package/readme.md +5 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type Action<S> = (state: () => S, ...args: any[]) => S | Promise<S>;
|
|
2
|
+
type OmitFirstParam<T extends (...args: any[]) => any> = T extends (state: any, ...args: infer P) => any ? P : never;
|
|
3
|
+
export interface Store<S, A extends Record<string, Action<S>>> {
|
|
4
|
+
state: () => S;
|
|
5
|
+
actions: {
|
|
6
|
+
[K in keyof A]: (...args: OmitFirstParam<A[K]>) => Promise<S>;
|
|
7
|
+
};
|
|
8
|
+
on: (action: keyof A | ((state: S, actionName: keyof A) => void), listener?: (state: S, actionName: keyof A) => void) => () => void;
|
|
9
|
+
}
|
|
10
|
+
export declare function createStore<S, A extends Record<string, Action<S>>>(config: {
|
|
11
|
+
state: S;
|
|
12
|
+
actions: A;
|
|
13
|
+
}): Store<S, A>;
|
|
14
|
+
export declare function selector<S, Args extends any[]>(path: string, action: (...args: Args) => any): (state: () => S, ...args: OmitFirstParam<typeof action>) => S | Promise<S>;
|
|
15
|
+
type SelectorObject<V> = {
|
|
16
|
+
getter: (state: unknown) => V;
|
|
17
|
+
setter: (_state: unknown, value: V) => V;
|
|
18
|
+
};
|
|
19
|
+
export declare function createSelector<V>(selector: string): SelectorObject<V>;
|
|
20
|
+
export declare function link<T>(...args: any[]): Readonly<{
|
|
21
|
+
_rdx_link_: (updateLinkValue: (value: T) => void) => void;
|
|
22
|
+
}>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=["on","state","actions"];function E(t){let{state:n,actions:a}=t;const r=[],s=new Map;w(n,(e,o)=>{if(x(e)){const{setter:f}=d(`state.${o}`),i=u=>{f(y(),u),h()};e._rdx_link_(i)}});const c=Object.fromEntries(Object.entries(a).map(([e,o])=>{if(S.includes(e))throw new Error(`MiniRdxError: "${e}" is a reserved keyword`);if(typeof o!="function")throw new Error(`MiniRdxError: "${e}" should be a function`);return[e,(...f)=>new Promise(i=>{p(e,u=>o(u,...f),i)})]}));function p(e,o,f){const i=o(y);m(i)?i.then(u=>l(u,e,f)):l(i,e,f)}function l(e,o,f){var i;n=e,r.length&&r.forEach(u=>u(n,o)),(i=s.get(o))==null||i.forEach(u=>u(n,o)),f(n)}function y(){return n}function h(){const e=A(n)?{...n}:Array.isArray(n)?[...n]:n;r.length&&r.forEach(o=>o(e,"link"))}function g(e,o){var f;if(typeof e=="function"&&o===void 0)return r.push(e),()=>r.splice(r.indexOf(e),1);if(typeof e=="string"&&o)return s.has(e)||s.set(e,[]),(f=s.get(e))==null||f.push(o),()=>{var i,u;(u=s.get(e))==null||u.splice((i=s.get(e))==null?void 0:i.indexOf(o),1)};throw new Error("MiniRdxError: Invalid arguments. Try: state.on(action: string, listener: ActionListener)")}return{state:y,actions:c,on:g}}function O(t,n){const{getter:a,setter:r}=d(t);return function(c,...p){if(_(n))return Promise.resolve(n(a(c()),...p)).then(l=>({...r(c(),l)}));{const l=n(a(c()),...p);return{...r(c(),l)}}}}function d(t){if(/^state\.[\w\[\]\d\.]+$/.test(t))return{getter:new Function("state",`return ${t}`),setter:new Function("state","value",`{${t} = value; return state;}`)};throw new Error('MiniRDXError: Selector should be a string with dot notation starting with "state." e.g.: "state.user.cars[1].model" ')}function $(...t){return Object.freeze({_rdx_link_:n=>{const a=t.pop(),r=t,s=()=>{n(a(...r.map(c=>c.state())))};r.forEach(c=>c.on(()=>s())),s()}})}function m(t){return!!t&&(typeof t=="object"||typeof t=="function")&&typeof t.then=="function"}function _(t){return t.constructor.name==="AsyncFunction"}function w(t,n,a=""){if(!(typeof t!="object"||t===null)){for(const r in t)if(t.hasOwnProperty(r)){const s=t[r],c=/\d+/.test(r)?`[${r}]`:`.${r}`,p=a?`${a}${c}`:r;n(s,p),typeof s=="object"&&s!==null&&w(s,n,p)}}}function x(t){return t!==null&&typeof t=="object"&&t.hasOwnProperty("_rdx_link_")}function A(t){return!!t&&typeof t!="symbol"&&typeof t!="string"&&typeof t!="number"&&typeof t!="boolean"&&typeof t!="function"&&!Array.isArray(t)}exports.createSelector=d;exports.createStore=E;exports.link=$;exports.selector=O;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
const E = ["on", "state", "actions"];
|
|
2
|
+
function _(t) {
|
|
3
|
+
let { state: n, actions: a } = t;
|
|
4
|
+
const r = [], s = /* @__PURE__ */ new Map();
|
|
5
|
+
w(n, (e, o) => {
|
|
6
|
+
if ($(e)) {
|
|
7
|
+
const { setter: f } = d(`state.${o}`), i = (u) => {
|
|
8
|
+
f(y(), u), h();
|
|
9
|
+
};
|
|
10
|
+
e._rdx_link_(i);
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
const c = Object.fromEntries(
|
|
14
|
+
Object.entries(a).map(([e, o]) => {
|
|
15
|
+
if (E.includes(e))
|
|
16
|
+
throw new Error(`MiniRdxError: "${e}" is a reserved keyword`);
|
|
17
|
+
if (typeof o != "function")
|
|
18
|
+
throw new Error(`MiniRdxError: "${e}" should be a function`);
|
|
19
|
+
return [
|
|
20
|
+
e,
|
|
21
|
+
(...f) => new Promise((i) => {
|
|
22
|
+
p(
|
|
23
|
+
e,
|
|
24
|
+
(u) => o(u, ...f),
|
|
25
|
+
i
|
|
26
|
+
);
|
|
27
|
+
})
|
|
28
|
+
];
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
function p(e, o, f) {
|
|
32
|
+
const i = o(y);
|
|
33
|
+
S(i) ? i.then(
|
|
34
|
+
(u) => l(u, e, f)
|
|
35
|
+
) : l(i, e, f);
|
|
36
|
+
}
|
|
37
|
+
function l(e, o, f) {
|
|
38
|
+
var i;
|
|
39
|
+
n = e, r.length && r.forEach((u) => u(n, o)), (i = s.get(o)) == null || i.forEach((u) => u(n, o)), f(n);
|
|
40
|
+
}
|
|
41
|
+
function y() {
|
|
42
|
+
return n;
|
|
43
|
+
}
|
|
44
|
+
function h() {
|
|
45
|
+
const e = x(n) ? { ...n } : Array.isArray(n) ? [...n] : n;
|
|
46
|
+
r.length && r.forEach((o) => o(e, "link"));
|
|
47
|
+
}
|
|
48
|
+
function g(e, o) {
|
|
49
|
+
var f;
|
|
50
|
+
if (typeof e == "function" && o === void 0)
|
|
51
|
+
return r.push(e), () => r.splice(r.indexOf(e), 1);
|
|
52
|
+
if (typeof e == "string" && o)
|
|
53
|
+
return s.has(e) || s.set(e, []), (f = s.get(e)) == null || f.push(o), () => {
|
|
54
|
+
var i, u;
|
|
55
|
+
(u = s.get(e)) == null || u.splice((i = s.get(e)) == null ? void 0 : i.indexOf(o), 1);
|
|
56
|
+
};
|
|
57
|
+
throw new Error(
|
|
58
|
+
"MiniRdxError: Invalid arguments. Try: state.on(action: string, listener: ActionListener)"
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
state: y,
|
|
63
|
+
actions: c,
|
|
64
|
+
on: g
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function m(t, n) {
|
|
68
|
+
const { getter: a, setter: r } = d(t);
|
|
69
|
+
return function(c, ...p) {
|
|
70
|
+
if (O(n))
|
|
71
|
+
return Promise.resolve(
|
|
72
|
+
n(a(c()), ...p)
|
|
73
|
+
).then((l) => ({ ...r(c(), l) }));
|
|
74
|
+
{
|
|
75
|
+
const l = n(a(c()), ...p);
|
|
76
|
+
return { ...r(c(), l) };
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function d(t) {
|
|
81
|
+
if (/^state\.[\w\[\]\d\.]+$/.test(t))
|
|
82
|
+
return {
|
|
83
|
+
getter: new Function("state", `return ${t}`),
|
|
84
|
+
setter: new Function(
|
|
85
|
+
"state",
|
|
86
|
+
"value",
|
|
87
|
+
`{${t} = value; return state;}`
|
|
88
|
+
)
|
|
89
|
+
};
|
|
90
|
+
throw new Error(
|
|
91
|
+
'MiniRDXError: Selector should be a string with dot notation starting with "state." e.g.: "state.user.cars[1].model" '
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
function A(...t) {
|
|
95
|
+
return Object.freeze({
|
|
96
|
+
_rdx_link_: (n) => {
|
|
97
|
+
const a = t.pop(), r = t, s = () => {
|
|
98
|
+
n(a(...r.map((c) => c.state())));
|
|
99
|
+
};
|
|
100
|
+
r.forEach((c) => c.on(() => s())), s();
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
function S(t) {
|
|
105
|
+
return !!t && (typeof t == "object" || typeof t == "function") && typeof t.then == "function";
|
|
106
|
+
}
|
|
107
|
+
function O(t) {
|
|
108
|
+
return t.constructor.name === "AsyncFunction";
|
|
109
|
+
}
|
|
110
|
+
function w(t, n, a = "") {
|
|
111
|
+
if (!(typeof t != "object" || t === null)) {
|
|
112
|
+
for (const r in t)
|
|
113
|
+
if (t.hasOwnProperty(r)) {
|
|
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);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function $(t) {
|
|
120
|
+
return t !== null && typeof t == "object" && t.hasOwnProperty("_rdx_link_");
|
|
121
|
+
}
|
|
122
|
+
function x(t) {
|
|
123
|
+
return !!t && typeof t != "symbol" && typeof t != "string" && typeof t != "number" && typeof t != "boolean" && typeof t != "function" && !Array.isArray(t);
|
|
124
|
+
}
|
|
125
|
+
export {
|
|
126
|
+
d as createSelector,
|
|
127
|
+
_ as createStore,
|
|
128
|
+
A as link,
|
|
129
|
+
m as selector
|
|
130
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const r=require("react");function y(i){const{state:e,on:o,actions:u}=i;function f(t){const[n,p]=r.useState(typeof t=="function"?t(e()):e()),d=s=>{const c=typeof t=="function"?t(s):s;c!==n&&p(c)};return r.useEffect(()=>o(d),[n]),[n,u]}function a(){return u}function S(){return o}return{useStore:f,useStoreActions:a,useStoreListeners:S}}module.exports=y;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useState as d, useEffect as m } from "react";
|
|
2
|
+
function k(c) {
|
|
3
|
+
const { state: e, on: o, actions: u } = c;
|
|
4
|
+
function f(t) {
|
|
5
|
+
const [n, S] = d(
|
|
6
|
+
typeof t == "function" ? t(e()) : e()
|
|
7
|
+
), p = (r) => {
|
|
8
|
+
const s = typeof t == "function" ? t(r) : r;
|
|
9
|
+
s !== n && S(s);
|
|
10
|
+
};
|
|
11
|
+
return m(() => o(p), [n]), [n, u];
|
|
12
|
+
}
|
|
13
|
+
function i() {
|
|
14
|
+
return u;
|
|
15
|
+
}
|
|
16
|
+
function a() {
|
|
17
|
+
return o;
|
|
18
|
+
}
|
|
19
|
+
return { useStore: f, useStoreActions: i, useStoreListeners: a };
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
k as default
|
|
23
|
+
};
|
package/dist/react.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Store, Action } from './index';
|
|
2
|
+
export default function createStoreHooks<S, A extends Record<string, Action<S>>>(store: Store<S, A>): {
|
|
3
|
+
useStore: <T = S>(selector?: (state: S) => T) => [T, Store<S, A>["actions"]];
|
|
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;
|
|
6
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ludekarts/minirdx",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Minimal state management library",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"dev": "vite build --watch",
|
|
7
|
+
"build": "tsc && vite build",
|
|
8
|
+
"preview": "vite preview"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "./dist/minirdx.index.cjs.js",
|
|
15
|
+
"module": "./dist/minirdx.index.es.js",
|
|
16
|
+
"types": "./dist/minirdx.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/minirdx.index.es.js",
|
|
21
|
+
"require": "./dist/minirdx.index.cjs.js"
|
|
22
|
+
},
|
|
23
|
+
"./react": {
|
|
24
|
+
"types": "./dist/react.d.ts",
|
|
25
|
+
"import": "./dist/minirdx.react.es.js",
|
|
26
|
+
"require": "./dist/minirdx.react.cjs.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"keywords": [],
|
|
30
|
+
"author": "Wojciech Ludwin << @ludekarts >>",
|
|
31
|
+
"license": "ISC",
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/react": "^19.0.10",
|
|
34
|
+
"react": "^19.0.0",
|
|
35
|
+
"typescript": "^5.5.3",
|
|
36
|
+
"vite": "^5.4.1",
|
|
37
|
+
"vite-plugin-dts": "^4.2.1"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"react": "^19.0.0"
|
|
41
|
+
}
|
|
42
|
+
}
|