@himanshu-sorathiya/react-kit 1.0.26 → 1.0.28
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 +1153 -43
- package/dist/index.js +5 -5
- package/dist/performance.d.ts +883 -30
- package/dist/performance.js +2 -2
- package/dist/performance2.js +358 -224
- package/dist/storage.d.ts +270 -13
- package/dist/storage2.js +150 -213
- package/package.json +7 -4
package/dist/storage2.js
CHANGED
|
@@ -1,30 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { useCallback as e, useEffect as t, useRef as n, useState as r, useSyncExternalStore as i } from "react";
|
|
2
|
+
//#region src/shared/storageShared/getStorage.ts
|
|
3
|
+
function a(e) {
|
|
4
|
+
return () => {
|
|
5
|
+
if (typeof window > "u") return null;
|
|
6
|
+
try {
|
|
7
|
+
return window[e];
|
|
8
|
+
} catch {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
//#endregion
|
|
3
14
|
//#region src/shared/storageShared/serializers.ts
|
|
4
|
-
var
|
|
15
|
+
var o = {
|
|
5
16
|
serialize: (e) => JSON.stringify(e),
|
|
6
17
|
deserialize: (e) => JSON.parse(e)
|
|
7
18
|
};
|
|
8
|
-
function
|
|
19
|
+
function s() {
|
|
9
20
|
return {
|
|
10
21
|
serialize: (e) => JSON.stringify(Array.from(e.entries())),
|
|
11
22
|
deserialize: (e) => new Map(JSON.parse(e))
|
|
12
23
|
};
|
|
13
24
|
}
|
|
14
|
-
function
|
|
25
|
+
function c() {
|
|
15
26
|
return {
|
|
16
27
|
serialize: (e) => JSON.stringify(Array.from(e.values())),
|
|
17
28
|
deserialize: (e) => new Set(JSON.parse(e))
|
|
18
29
|
};
|
|
19
30
|
}
|
|
20
|
-
var
|
|
31
|
+
var l = {
|
|
21
32
|
serialize: (e) => e.toISOString(),
|
|
22
33
|
deserialize: (e) => {
|
|
23
34
|
let t = new Date(e);
|
|
24
35
|
if (isNaN(t.getTime())) throw Error(`[react-kit] dateSerializer: invalid date string "${e}"`);
|
|
25
36
|
return t;
|
|
26
37
|
}
|
|
27
|
-
},
|
|
38
|
+
}, u = {
|
|
28
39
|
serialize: (e) => e.toString(),
|
|
29
40
|
deserialize: (e) => {
|
|
30
41
|
try {
|
|
@@ -33,229 +44,155 @@ var c = {
|
|
|
33
44
|
throw Error(`[react-kit] bigIntSerializer: cannot convert "${e}" to bigint`);
|
|
34
45
|
}
|
|
35
46
|
}
|
|
36
|
-
},
|
|
37
|
-
|
|
38
|
-
//#region src/storage/useLocalStorage/utils.ts
|
|
39
|
-
function d() {
|
|
40
|
-
if (typeof window > "u") return null;
|
|
47
|
+
}, d = globalThis.process?.env?.NODE_ENV !== "production";
|
|
48
|
+
function f(e, t, n) {
|
|
41
49
|
try {
|
|
42
|
-
return
|
|
50
|
+
return t.deserialize(e);
|
|
43
51
|
} catch {
|
|
44
|
-
return
|
|
52
|
+
return n;
|
|
45
53
|
}
|
|
46
54
|
}
|
|
47
|
-
function
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
55
|
+
function p(a, s, c, l = {}) {
|
|
56
|
+
let { serializer: u = o, initializeWithValue: p = !0, sameInstanceSync: m = !0, crossInstanceSync: h = !0, onError: g } = l, [_] = r(() => s), v = s !== _, [y] = r(() => Symbol()), b = n(u);
|
|
57
|
+
t(() => {
|
|
58
|
+
b.current = u;
|
|
59
|
+
}, [u]);
|
|
60
|
+
let x = n(c);
|
|
61
|
+
t(() => {
|
|
62
|
+
x.current = c;
|
|
63
|
+
}, [c]);
|
|
64
|
+
let S = n(g);
|
|
65
|
+
t(() => {
|
|
66
|
+
S.current = g;
|
|
67
|
+
}, [g]);
|
|
68
|
+
let [C, w] = r(null), T = e((e, t) => {
|
|
69
|
+
d && console.warn(`[${a.hookLabel}] ${e}:`, t.message), S.current?.(t), w(t);
|
|
70
|
+
}, [a.hookLabel]);
|
|
71
|
+
t(() => {
|
|
72
|
+
v && T("Ignoring key change", /* @__PURE__ */ Error(`Changing the storage key at runtime is not supported. Still using original key: "${_}". Received new key: "${s}".`));
|
|
73
|
+
}, [
|
|
74
|
+
s,
|
|
75
|
+
v,
|
|
76
|
+
_,
|
|
77
|
+
T
|
|
78
|
+
]);
|
|
79
|
+
let E = n({
|
|
80
|
+
raw: null,
|
|
81
|
+
parsed: c
|
|
82
|
+
}), D = n(null), O = e(() => {
|
|
83
|
+
let e = a.getStorage();
|
|
84
|
+
if (!e) return x.current;
|
|
85
|
+
let t = e.getItem(_);
|
|
86
|
+
if (E.current.raw === t) return E.current.parsed;
|
|
87
|
+
let n = t === null ? x.current : f(t, b.current, x.current);
|
|
88
|
+
return E.current = {
|
|
89
|
+
raw: t,
|
|
90
|
+
parsed: n
|
|
91
|
+
}, n;
|
|
92
|
+
}, [_]), k = e(() => x.current, []), A = i(e((e) => {
|
|
93
|
+
if (D.current = e, typeof window > "u") return () => {
|
|
94
|
+
D.current = null;
|
|
56
95
|
};
|
|
57
|
-
|
|
58
|
-
return {
|
|
59
|
-
|
|
60
|
-
|
|
96
|
+
let t = new AbortController();
|
|
97
|
+
return h && a.nativeStorageEventSupported && window.addEventListener("storage", (t) => {
|
|
98
|
+
t.storageArea === a.getStorage() && (t.key !== null && t.key !== _ || e());
|
|
99
|
+
}, { signal: t.signal }), m && window.addEventListener(`${a.customEventName}:${_}`, (t) => {
|
|
100
|
+
t.detail.instanceId !== y && e();
|
|
101
|
+
}, { signal: t.signal }), () => {
|
|
102
|
+
t.abort(), D.current = null;
|
|
61
103
|
};
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (!m) return;
|
|
80
|
-
let e = d();
|
|
81
|
-
if (!e) return s;
|
|
82
|
-
let { value: t } = f(e, o, s, l);
|
|
83
|
-
return t;
|
|
84
|
-
}), T = r(C);
|
|
85
|
-
n(() => {
|
|
86
|
-
T.current = C;
|
|
87
|
-
}, [C]);
|
|
88
|
-
let [E, D] = i(m && typeof window < "u"), [O, k] = i(null);
|
|
89
|
-
n(() => {
|
|
90
|
-
if (!y) return;
|
|
91
|
-
let e = /* @__PURE__ */ Error(`[react-kit:use-local-storage] Changing the storage key at runtime is not supported. Still using original key: "${v.current}". Received new key: "${o}".`);
|
|
92
|
-
console.warn(e.message), k(e);
|
|
93
|
-
}, [o, y]), n(() => {
|
|
94
|
-
if (m) return;
|
|
95
|
-
let e = d(), { value: t, error: n } = e === null ? {
|
|
96
|
-
value: s,
|
|
97
|
-
error: null
|
|
98
|
-
} : f(e, v.current, s, S.current);
|
|
99
|
-
n && (console.warn(`[react-kit:use-local-storage] Failed to read key "${v.current}" from localStorage:`, n.message), k(n)), w(t), D(!0);
|
|
100
|
-
}, [m]);
|
|
101
|
-
let A = t((e) => {
|
|
102
|
-
let t = d();
|
|
103
|
-
if (!t) return;
|
|
104
|
-
let n = T.current, r = typeof e == "function" ? e(n) : e;
|
|
105
|
-
try {
|
|
106
|
-
let e = S.current.serialize(r);
|
|
107
|
-
t.setItem(v.current, e), w(r), k(null), h && p(v.current, e, x.current);
|
|
104
|
+
}, [
|
|
105
|
+
_,
|
|
106
|
+
h,
|
|
107
|
+
m,
|
|
108
|
+
a,
|
|
109
|
+
y
|
|
110
|
+
]), O, k), [j, M] = r(!1);
|
|
111
|
+
t(() => {
|
|
112
|
+
M(!0);
|
|
113
|
+
}, []);
|
|
114
|
+
let N = p || j ? A : void 0, P = n(void 0);
|
|
115
|
+
return t(() => {
|
|
116
|
+
let e = a.getStorage();
|
|
117
|
+
if (!e) return;
|
|
118
|
+
let t = e.getItem(_);
|
|
119
|
+
if (t !== P.current && (P.current = t, t !== null)) try {
|
|
120
|
+
b.current.deserialize(t);
|
|
108
121
|
} catch (e) {
|
|
109
|
-
|
|
110
|
-
console.warn(`[react-kit:use-local-storage] Failed to write key "${v.current}" to localStorage:`, t.message), k(t);
|
|
122
|
+
T(`Failed to read key "${_}"`, e instanceof Error ? e : Error(String(e)));
|
|
111
123
|
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (e.newValue === null) {
|
|
119
|
-
w(s), k(null);
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
124
|
+
}), {
|
|
125
|
+
value: N,
|
|
126
|
+
setValue: e((e) => {
|
|
127
|
+
let t = a.getStorage();
|
|
128
|
+
if (!t) return;
|
|
129
|
+
let n = O(), r = typeof e == "function" ? e(n) : e;
|
|
122
130
|
try {
|
|
123
|
-
let
|
|
124
|
-
|
|
131
|
+
let e = b.current.serialize(r);
|
|
132
|
+
if (t.setItem(_, e), E.current = {
|
|
133
|
+
raw: e,
|
|
134
|
+
parsed: r
|
|
135
|
+
}, P.current = e, w(null), D.current?.(), m) {
|
|
136
|
+
let t = {
|
|
137
|
+
value: e,
|
|
138
|
+
instanceId: y
|
|
139
|
+
};
|
|
140
|
+
window.dispatchEvent(new CustomEvent(`${a.customEventName}:${_}`, { detail: t }));
|
|
141
|
+
}
|
|
125
142
|
} catch (e) {
|
|
126
|
-
|
|
127
|
-
console.warn(`[react-kit:use-local-storage] Failed to deserialize cross-tab update for key "${v.current}":`, t.message), w(s), k(t);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}), e(b, (e) => {
|
|
131
|
-
if (!h) return;
|
|
132
|
-
let t = e.detail;
|
|
133
|
-
if (t.instanceId !== x.current) {
|
|
134
|
-
if (t.value === null) {
|
|
135
|
-
w(s), k(null);
|
|
136
|
-
return;
|
|
143
|
+
T(`Failed to write key "${_}"`, e instanceof Error ? e : Error(String(e)));
|
|
137
144
|
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
145
|
+
}, [
|
|
146
|
+
a,
|
|
147
|
+
_,
|
|
148
|
+
m,
|
|
149
|
+
O,
|
|
150
|
+
T,
|
|
151
|
+
y
|
|
152
|
+
]),
|
|
153
|
+
removeValue: e(() => {
|
|
154
|
+
let e = a.getStorage();
|
|
155
|
+
if (e && (e.removeItem(_), E.current = {
|
|
156
|
+
raw: null,
|
|
157
|
+
parsed: x.current
|
|
158
|
+
}, P.current = null, w(null), D.current?.(), m)) {
|
|
159
|
+
let e = {
|
|
160
|
+
value: null,
|
|
161
|
+
instanceId: y
|
|
162
|
+
};
|
|
163
|
+
window.dispatchEvent(new CustomEvent(`${a.customEventName}:${_}`, { detail: e }));
|
|
144
164
|
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
165
|
+
}, [
|
|
166
|
+
a,
|
|
167
|
+
_,
|
|
168
|
+
m,
|
|
169
|
+
y
|
|
170
|
+
]),
|
|
171
|
+
isHydrated: j,
|
|
172
|
+
error: C
|
|
152
173
|
};
|
|
153
174
|
}
|
|
154
175
|
//#endregion
|
|
155
|
-
//#region src/storage/
|
|
156
|
-
var
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
return null;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
function _(e, t, n, r) {
|
|
168
|
-
try {
|
|
169
|
-
let i = e.getItem(t);
|
|
170
|
-
return i === null ? {
|
|
171
|
-
value: n,
|
|
172
|
-
error: null
|
|
173
|
-
} : {
|
|
174
|
-
value: r.deserialize(i),
|
|
175
|
-
error: null
|
|
176
|
-
};
|
|
177
|
-
} catch (e) {
|
|
178
|
-
return {
|
|
179
|
-
value: n,
|
|
180
|
-
error: e instanceof Error ? e : Error(String(e))
|
|
181
|
-
};
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
function v(e, t, n) {
|
|
185
|
-
let r = {
|
|
186
|
-
value: t,
|
|
187
|
-
instanceId: n
|
|
188
|
-
}, i = new CustomEvent(`${h}:${e}`, { detail: r });
|
|
189
|
-
window.dispatchEvent(i);
|
|
176
|
+
//#region src/storage/useLocalStorage/useLocalStorage.ts
|
|
177
|
+
var m = {
|
|
178
|
+
hookLabel: "react-kit:use-local-storage",
|
|
179
|
+
getStorage: a("localStorage"),
|
|
180
|
+
customEventName: "react-kit:use-local-storage",
|
|
181
|
+
nativeStorageEventSupported: !0
|
|
182
|
+
};
|
|
183
|
+
function h(e, t, n = {}) {
|
|
184
|
+
return p(m, e, t, n);
|
|
190
185
|
}
|
|
191
186
|
//#endregion
|
|
192
187
|
//#region src/storage/useSessionStorage/useSessionStorage.ts
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
if (!e) return s;
|
|
202
|
-
let { value: t } = _(e, o, s, l);
|
|
203
|
-
return t;
|
|
204
|
-
}), w = r(S);
|
|
205
|
-
n(() => {
|
|
206
|
-
w.current = S;
|
|
207
|
-
}, [S]);
|
|
208
|
-
let [T, E] = i(u && typeof window < "u"), [D, O] = i(null);
|
|
209
|
-
n(() => {
|
|
210
|
-
if (!m) return;
|
|
211
|
-
let e = /* @__PURE__ */ Error(`[react-kit:use-session-storage] Changing the storage key at runtime is not supported. Still using original key: "${p.current}". Received new key: "${o}".`);
|
|
212
|
-
console.warn(e.message), O(e);
|
|
213
|
-
}, [o, m]), n(() => {
|
|
214
|
-
if (u) return;
|
|
215
|
-
let e = g(), { value: t, error: n } = e === null ? {
|
|
216
|
-
value: s,
|
|
217
|
-
error: null
|
|
218
|
-
} : _(e, p.current, s, x.current);
|
|
219
|
-
n && (console.warn(`[react-kit:use-session-storage] Failed to read key "${p.current}" from sessionStorage:`, n.message), O(n)), C(t), E(!0);
|
|
220
|
-
}, [u]);
|
|
221
|
-
let k = t((e) => {
|
|
222
|
-
let t = g();
|
|
223
|
-
if (!t) return;
|
|
224
|
-
let n = w.current, r = typeof e == "function" ? e(n) : e;
|
|
225
|
-
try {
|
|
226
|
-
let e = x.current.serialize(r);
|
|
227
|
-
t.setItem(p.current, e), C(r), O(null), d && v(p.current, e, b.current);
|
|
228
|
-
} catch (e) {
|
|
229
|
-
let t = e instanceof Error ? e : Error(String(e));
|
|
230
|
-
console.warn(`[react-kit:use-session-storage] Failed to write key "${p.current}" to sessionStorage:`, t.message), O(t);
|
|
231
|
-
}
|
|
232
|
-
}, [d]), A = t(() => {
|
|
233
|
-
let e = g();
|
|
234
|
-
e && (e.removeItem(p.current), C(s), O(null), d && v(p.current, null, b.current));
|
|
235
|
-
}, [d]);
|
|
236
|
-
return e(y, (e) => {
|
|
237
|
-
if (!d) return;
|
|
238
|
-
let t = e.detail;
|
|
239
|
-
if (t.instanceId !== b.current) {
|
|
240
|
-
if (t.value === null) {
|
|
241
|
-
C(s), O(null);
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
try {
|
|
245
|
-
let e = x.current.deserialize(t.value);
|
|
246
|
-
C(e), O(null);
|
|
247
|
-
} catch (e) {
|
|
248
|
-
let t = e instanceof Error ? e : Error(String(e));
|
|
249
|
-
console.warn(`[react-kit:use-session-storage] Failed to deserialize same-tab update for key "${p.current}":`, t.message), C(s), O(t);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}, { target: typeof window > "u" ? null : window }), {
|
|
253
|
-
value: S,
|
|
254
|
-
setValue: k,
|
|
255
|
-
removeValue: A,
|
|
256
|
-
isHydrated: T,
|
|
257
|
-
error: D
|
|
258
|
-
};
|
|
188
|
+
var g = {
|
|
189
|
+
hookLabel: "react-kit:use-session-storage",
|
|
190
|
+
getStorage: a("sessionStorage"),
|
|
191
|
+
customEventName: "react-kit:use-session-storage",
|
|
192
|
+
nativeStorageEventSupported: !1
|
|
193
|
+
};
|
|
194
|
+
function _(e, t, n = {}) {
|
|
195
|
+
return p(g, e, t, n);
|
|
259
196
|
}
|
|
260
197
|
//#endregion
|
|
261
|
-
export { a,
|
|
198
|
+
export { o as a, l as i, h as n, s as o, u as r, c as s, _ as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@himanshu-sorathiya/react-kit",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.28",
|
|
4
4
|
"description": "An opinionated collection of react hooks, and reusable UI components.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"format": "prettier . --write"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"react": "^
|
|
47
|
-
"react-dom": "^
|
|
46
|
+
"react": "^19.2.0",
|
|
47
|
+
"react-dom": "^19.2.0"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@eslint/js": "^10.0.1",
|
|
@@ -85,5 +85,8 @@
|
|
|
85
85
|
"url": "git+https://github.com/Himanshu-Sorathiya-Simform/react-kit.git"
|
|
86
86
|
},
|
|
87
87
|
"license": "MIT",
|
|
88
|
-
"author": "Himanshu Sorathiya"
|
|
88
|
+
"author": "Himanshu Sorathiya",
|
|
89
|
+
"dependencies": {
|
|
90
|
+
"@himanshu-sorathiya/datetime": "^1.1.0"
|
|
91
|
+
}
|
|
89
92
|
}
|