@ludekarts/minirdx 0.3.0 → 0.5.0-beta.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.
@@ -1 +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;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const $=["on","state","actions"];function _(t){let{state:n,actions:a}=t;const r=[],s=new Map;E(n,(e,o)=>{if(b(e)){const{setter:u}=g(`state.${o}`),i=c=>{u(l(),c),y()};e._rdx_link_(i)}});const f=Object.fromEntries(Object.entries(a).map(([e,o])=>{if($.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,(...u)=>new Promise((i,c)=>{p(e,w=>o(w,...u),i,c)})]}));function p(e,o,u,i){try{const c=o(l);S(c)?c.then(w=>d(w,e,u)).catch(i):d(c,e,u)}catch(c){i(c)}}function d(e,o,u){var i;n=e,r.length&&r.forEach(c=>c(n,o)),(i=s.get(o))==null||i.forEach(c=>c(n,o)),u(n)}function l(){return n}function y(){const e=x(n)?{...n}:Array.isArray(n)?[...n]:n;r.length&&r.forEach(o=>o(e,"link"))}function h(e,o){var u;if(typeof e=="function"&&o===void 0)return r.push(e),()=>{const i=r.indexOf(e);i>=0&&r.splice(i,1)};if(typeof e=="string"&&o)return s.has(e)||s.set(e,[]),(u=s.get(e))==null||u.push(o),()=>{const i=s.get(e);if(!i)return;const c=i.indexOf(o);c>=0&&i.splice(c,1)};throw new Error("MiniRdxError: Invalid arguments. Try: state.on(action: string, listener: ActionListener)")}return{state:l,actions:f,on:h}}function m(t,n){const{getter:a,setter:r}=g(t),s=f=>Array.isArray(f)?[...f]:x(f)?{...f}:f;return function(p,...d){const l=n(a(p()),...d);if(S(l))return Promise.resolve(l).then(h=>{const e=r(p(),h);return s(e)});const y=r(p(),l);return s(y)}}function g(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 A(...t){return Object.freeze({_rdx_link_:n=>{const a=t.pop(),r=t,s=()=>{n(a(...r.map(f=>f.state())))};r.forEach(f=>f.on(()=>s())),s()}})}function S(t){return!!t&&(typeof t=="object"||typeof t=="function")&&typeof t.then=="function"}function E(t,n,a=""){if(!(typeof t!="object"||t===null)){for(const r in t)if(O(t,r)){const s=t[r],f=/\d+/.test(r)?`[${r}]`:`.${r}`,p=a?`${a}${f}`:r;n(s,p),typeof s=="object"&&s!==null&&E(s,n,p)}}}function b(t){return t!==null&&typeof t=="object"&&O(t,"_rdx_link_")}function O(t,n){return Object.prototype.hasOwnProperty.call(t,n)}function x(t){return!!t&&typeof t!="symbol"&&typeof t!="string"&&typeof t!="number"&&typeof t!="boolean"&&typeof t!="function"&&!Array.isArray(t)}exports.createSelector=g;exports.createStore=_;exports.link=A;exports.selector=m;
@@ -1,83 +1,93 @@
1
- const E = ["on", "state", "actions"];
2
- function _(t) {
3
- let { state: n, actions: a } = t;
1
+ const $ = ["on", "state", "actions"];
2
+ function A(t) {
3
+ let { state: e, actions: a } = t;
4
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();
5
+ x(e, (n, o) => {
6
+ if (_(n)) {
7
+ const { setter: u } = g(`state.${o}`), i = (c) => {
8
+ u(d(), c), l();
9
9
  };
10
- e._rdx_link_(i);
10
+ n._rdx_link_(i);
11
11
  }
12
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`);
13
+ const f = Object.fromEntries(
14
+ Object.entries(a).map(([n, o]) => {
15
+ if ($.includes(n))
16
+ throw new Error(`MiniRdxError: "${n}" is a reserved keyword`);
17
17
  if (typeof o != "function")
18
- throw new Error(`MiniRdxError: "${e}" should be a function`);
18
+ throw new Error(`MiniRdxError: "${n}" should be a function`);
19
19
  return [
20
- e,
21
- (...f) => new Promise((i) => {
20
+ n,
21
+ (...u) => new Promise((i, c) => {
22
22
  p(
23
- e,
24
- (u) => o(u, ...f),
25
- i
23
+ n,
24
+ (w) => o(w, ...u),
25
+ i,
26
+ c
26
27
  );
27
28
  })
28
29
  ];
29
30
  })
30
31
  );
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);
32
+ function p(n, o, u, i) {
33
+ try {
34
+ const c = o(d);
35
+ E(c) ? c.then(
36
+ (w) => y(w, n, u)
37
+ ).catch(i) : y(c, n, u);
38
+ } catch (c) {
39
+ i(c);
40
+ }
36
41
  }
37
- function l(e, o, f) {
42
+ function y(n, o, u) {
38
43
  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);
44
+ e = n, r.length && r.forEach((c) => c(e, o)), (i = s.get(o)) == null || i.forEach((c) => c(e, o)), u(e);
40
45
  }
41
- function y() {
42
- return n;
46
+ function d() {
47
+ return e;
43
48
  }
44
- function h() {
45
- const e = x(n) ? { ...n } : Array.isArray(n) ? [...n] : n;
46
- r.length && r.forEach((o) => o(e, "link"));
49
+ function l() {
50
+ const n = S(e) ? { ...e } : Array.isArray(e) ? [...e] : e;
51
+ r.length && r.forEach((o) => o(n, "link"));
47
52
  }
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);
53
+ function h(n, o) {
54
+ var u;
55
+ if (typeof n == "function" && o === void 0)
56
+ return r.push(n), () => {
57
+ const i = r.indexOf(n);
58
+ i >= 0 && r.splice(i, 1);
59
+ };
60
+ if (typeof n == "string" && o)
61
+ return s.has(n) || s.set(n, []), (u = s.get(n)) == null || u.push(o), () => {
62
+ const i = s.get(n);
63
+ if (!i) return;
64
+ const c = i.indexOf(o);
65
+ c >= 0 && i.splice(c, 1);
56
66
  };
57
67
  throw new Error(
58
68
  "MiniRdxError: Invalid arguments. Try: state.on(action: string, listener: ActionListener)"
59
69
  );
60
70
  }
61
71
  return {
62
- state: y,
63
- actions: c,
64
- on: g
72
+ state: d,
73
+ actions: f,
74
+ on: h
65
75
  };
66
76
  }
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
- }
77
+ function m(t, e) {
78
+ const { getter: a, setter: r } = g(t), s = (f) => Array.isArray(f) ? [...f] : S(f) ? { ...f } : f;
79
+ return function(p, ...y) {
80
+ const d = e(a(p()), ...y);
81
+ if (E(d))
82
+ return Promise.resolve(d).then((h) => {
83
+ const n = r(p(), h);
84
+ return s(n);
85
+ });
86
+ const l = r(p(), d);
87
+ return s(l);
78
88
  };
79
89
  }
80
- function d(t) {
90
+ function g(t) {
81
91
  if (/^state\.[\w\[\]\d\.]+$/.test(t))
82
92
  return {
83
93
  getter: new Function("state", `return ${t}`),
@@ -91,40 +101,40 @@ function d(t) {
91
101
  'MiniRDXError: Selector should be a string with dot notation starting with "state." e.g.: "state.user.cars[1].model" '
92
102
  );
93
103
  }
94
- function A(...t) {
104
+ function L(...t) {
95
105
  return Object.freeze({
96
- _rdx_link_: (n) => {
106
+ _rdx_link_: (e) => {
97
107
  const a = t.pop(), r = t, s = () => {
98
- n(a(...r.map((c) => c.state())));
108
+ e(a(...r.map((f) => f.state())));
99
109
  };
100
- r.forEach((c) => c.on(() => s())), s();
110
+ r.forEach((f) => f.on(() => s())), s();
101
111
  }
102
112
  });
103
113
  }
104
- function S(t) {
114
+ function E(t) {
105
115
  return !!t && (typeof t == "object" || typeof t == "function") && typeof t.then == "function";
106
116
  }
107
- function O(t) {
108
- return t.constructor.name === "AsyncFunction";
109
- }
110
- function w(t, n, a = "") {
117
+ function x(t, e, a = "") {
111
118
  if (!(typeof t != "object" || t === null)) {
112
119
  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);
120
+ if (O(t, r)) {
121
+ const s = t[r], f = /\d+/.test(r) ? `[${r}]` : `.${r}`, p = a ? `${a}${f}` : r;
122
+ e(s, p), typeof s == "object" && s !== null && x(s, e, p);
116
123
  }
117
124
  }
118
125
  }
119
- function $(t) {
120
- return t !== null && typeof t == "object" && t.hasOwnProperty("_rdx_link_");
126
+ function _(t) {
127
+ return t !== null && typeof t == "object" && O(t, "_rdx_link_");
128
+ }
129
+ function O(t, e) {
130
+ return Object.prototype.hasOwnProperty.call(t, e);
121
131
  }
122
- function x(t) {
132
+ function S(t) {
123
133
  return !!t && typeof t != "symbol" && typeof t != "string" && typeof t != "number" && typeof t != "boolean" && typeof t != "function" && !Array.isArray(t);
124
134
  }
125
135
  export {
126
- d as createSelector,
127
- _ as createStore,
128
- A as link,
136
+ g as createSelector,
137
+ A as createStore,
138
+ L as link,
129
139
  m as selector
130
140
  };
@@ -1 +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;
1
+ "use strict";const c=require("react");function y(r){const{state:e,on:n,actions:i}=r;function a(t){const[p,d]=c.useState(typeof t=="function"?t(e()):e());return c.useEffect(()=>n(u=>{const o=typeof t=="function"?t(u):u;d(s=>Object.is(s,o)?s:o)}),[n,t]),p}function f(){return i}function S(){return n}return{useStore:a,useStoreActions:f,useStoreListeners:S}}module.exports=y;
@@ -1,23 +1,23 @@
1
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(
2
+ function j(c) {
3
+ const { state: e, on: n, actions: i } = c;
4
+ function r(t) {
5
+ const [S, p] = d(
6
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];
7
+ );
8
+ return m(() => n((o) => {
9
+ const u = typeof t == "function" ? t(o) : o;
10
+ p((s) => Object.is(s, u) ? s : u);
11
+ }), [n, t]), S;
12
12
  }
13
- function i() {
14
- return u;
13
+ function f() {
14
+ return i;
15
15
  }
16
16
  function a() {
17
- return o;
17
+ return n;
18
18
  }
19
- return { useStore: f, useStoreActions: i, useStoreListeners: a };
19
+ return { useStore: r, useStoreActions: f, useStoreListeners: a };
20
20
  }
21
21
  export {
22
- k as default
22
+ j as default
23
23
  };
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) => [T, Store<S, A>["actions"]];
3
+ useStore: <T = S>(selector?: (state: S) => T) => T;
4
4
  useStoreActions: () => Store<S, A>["actions"];
5
5
  useStoreListeners: () => (action: keyof A | ((state: S, actionName: keyof A) => void), listener?: ((state: S, actionName: keyof A) => void) | undefined) => () => void;
6
6
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ludekarts/minirdx",
3
- "version": "0.3.0",
3
+ "version": "0.5.0-beta.1",
4
4
  "description": "Minimal state management library",
5
5
  "scripts": {
6
6
  "dev": "vite build --watch",