@himanshu-sorathiya/react-kit 1.0.24 → 1.0.26
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/events.d.ts +369 -11
- package/dist/events2.js +34 -57
- package/dist/index.d.ts +409 -13
- package/dist/index.js +3 -2
- package/dist/storage.d.ts +42 -0
- package/dist/storage.js +2 -0
- package/dist/storage2.js +261 -0
- package/dist/useEventListener.js +32 -24
- package/package.json +6 -2
package/dist/storage2.js
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { t as e } from "./useEventListener.js";
|
|
2
|
+
import { useCallback as t, useEffect as n, useRef as r, useState as i } from "react";
|
|
3
|
+
//#region src/shared/storageShared/serializers.ts
|
|
4
|
+
var a = {
|
|
5
|
+
serialize: (e) => JSON.stringify(e),
|
|
6
|
+
deserialize: (e) => JSON.parse(e)
|
|
7
|
+
};
|
|
8
|
+
function o() {
|
|
9
|
+
return {
|
|
10
|
+
serialize: (e) => JSON.stringify(Array.from(e.entries())),
|
|
11
|
+
deserialize: (e) => new Map(JSON.parse(e))
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function s() {
|
|
15
|
+
return {
|
|
16
|
+
serialize: (e) => JSON.stringify(Array.from(e.values())),
|
|
17
|
+
deserialize: (e) => new Set(JSON.parse(e))
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
var c = {
|
|
21
|
+
serialize: (e) => e.toISOString(),
|
|
22
|
+
deserialize: (e) => {
|
|
23
|
+
let t = new Date(e);
|
|
24
|
+
if (isNaN(t.getTime())) throw Error(`[react-kit] dateSerializer: invalid date string "${e}"`);
|
|
25
|
+
return t;
|
|
26
|
+
}
|
|
27
|
+
}, l = {
|
|
28
|
+
serialize: (e) => e.toString(),
|
|
29
|
+
deserialize: (e) => {
|
|
30
|
+
try {
|
|
31
|
+
return BigInt(e);
|
|
32
|
+
} catch {
|
|
33
|
+
throw Error(`[react-kit] bigIntSerializer: cannot convert "${e}" to bigint`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}, u = "react-kit:use-local-storage";
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/storage/useLocalStorage/utils.ts
|
|
39
|
+
function d() {
|
|
40
|
+
if (typeof window > "u") return null;
|
|
41
|
+
try {
|
|
42
|
+
return window.localStorage;
|
|
43
|
+
} catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function f(e, t, n, r) {
|
|
48
|
+
try {
|
|
49
|
+
let i = e.getItem(t);
|
|
50
|
+
return i === null ? {
|
|
51
|
+
value: n,
|
|
52
|
+
error: null
|
|
53
|
+
} : {
|
|
54
|
+
value: r.deserialize(i),
|
|
55
|
+
error: null
|
|
56
|
+
};
|
|
57
|
+
} catch (e) {
|
|
58
|
+
return {
|
|
59
|
+
value: n,
|
|
60
|
+
error: e instanceof Error ? e : Error(String(e))
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function p(e, t, n) {
|
|
65
|
+
let r = {
|
|
66
|
+
value: t,
|
|
67
|
+
instanceId: n
|
|
68
|
+
}, i = new CustomEvent(`${u}:${e}`, { detail: r });
|
|
69
|
+
window.dispatchEvent(i);
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/storage/useLocalStorage/useLocalStorage.ts
|
|
73
|
+
function m(o, s, c = {}) {
|
|
74
|
+
let { serializer: l = a, initializeWithValue: m = !0, sameInstanceSync: h = !0, crossInstanceSync: g = !0 } = c, [_] = i(() => o), v = r(_), y = o !== _, b = `${u}:${_}`, x = r(Symbol()), S = r(l);
|
|
75
|
+
n(() => {
|
|
76
|
+
S.current = l;
|
|
77
|
+
}, [l]);
|
|
78
|
+
let [C, w] = i(() => {
|
|
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);
|
|
108
|
+
} catch (e) {
|
|
109
|
+
let t = e instanceof Error ? e : Error(String(e));
|
|
110
|
+
console.warn(`[react-kit:use-local-storage] Failed to write key "${v.current}" to localStorage:`, t.message), k(t);
|
|
111
|
+
}
|
|
112
|
+
}, [h]), j = t(() => {
|
|
113
|
+
let e = d();
|
|
114
|
+
e && (e.removeItem(v.current), w(s), k(null), h && p(v.current, null, x.current));
|
|
115
|
+
}, [h]);
|
|
116
|
+
return e("storage", (e) => {
|
|
117
|
+
if (g && e.storageArea === d() && e.key === v.current) {
|
|
118
|
+
if (e.newValue === null) {
|
|
119
|
+
w(s), k(null);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
try {
|
|
123
|
+
let t = S.current.deserialize(e.newValue);
|
|
124
|
+
w(t), k(null);
|
|
125
|
+
} catch (e) {
|
|
126
|
+
let t = e instanceof Error ? e : Error(String(e));
|
|
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;
|
|
137
|
+
}
|
|
138
|
+
try {
|
|
139
|
+
let e = S.current.deserialize(t.value);
|
|
140
|
+
w(e), k(null);
|
|
141
|
+
} catch (e) {
|
|
142
|
+
let t = e instanceof Error ? e : Error(String(e));
|
|
143
|
+
console.warn(`[react-kit:use-local-storage] Failed to deserialize same-tab update for key "${v.current}":`, t.message), w(s), k(t);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}, { target: typeof window > "u" ? null : window }), {
|
|
147
|
+
value: C,
|
|
148
|
+
setValue: A,
|
|
149
|
+
removeValue: j,
|
|
150
|
+
isHydrated: E,
|
|
151
|
+
error: O
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
//#endregion
|
|
155
|
+
//#region src/storage/useSessionStorage/constants.ts
|
|
156
|
+
var h = "react-kit:use-session-storage";
|
|
157
|
+
//#endregion
|
|
158
|
+
//#region src/storage/useSessionStorage/utils.ts
|
|
159
|
+
function g() {
|
|
160
|
+
if (typeof window > "u") return null;
|
|
161
|
+
try {
|
|
162
|
+
return window.sessionStorage;
|
|
163
|
+
} catch {
|
|
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);
|
|
190
|
+
}
|
|
191
|
+
//#endregion
|
|
192
|
+
//#region src/storage/useSessionStorage/useSessionStorage.ts
|
|
193
|
+
function y(o, s, c = {}) {
|
|
194
|
+
let { serializer: l = a, initializeWithValue: u = !0, sameInstanceSync: d = !0 } = c, [f] = i(() => o), p = r(f), m = o !== f, y = `${h}:${f}`, b = r(Symbol()), x = r(l);
|
|
195
|
+
n(() => {
|
|
196
|
+
x.current = l;
|
|
197
|
+
}, [l]);
|
|
198
|
+
let [S, C] = i(() => {
|
|
199
|
+
if (!u) return;
|
|
200
|
+
let e = g();
|
|
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
|
+
};
|
|
259
|
+
}
|
|
260
|
+
//#endregion
|
|
261
|
+
export { a, c as i, m as n, o, l as r, s, y as t };
|
package/dist/useEventListener.js
CHANGED
|
@@ -1,32 +1,40 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useCallback as e, useEffect as t, useEffectEvent as n, useRef as r } from "react";
|
|
2
2
|
//#region src/events/useEventListener/useEventListener.ts
|
|
3
|
-
var
|
|
4
|
-
function
|
|
5
|
-
let { target:
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
let e =
|
|
10
|
-
|
|
11
|
-
let
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
3
|
+
var i = globalThis.process?.env?.NODE_ENV !== "production";
|
|
4
|
+
function a(a, o, s = {}) {
|
|
5
|
+
let { target: c, capture: l = !1, passive: u = !1, once: d = !1, signal: f } = s, p = c === void 0 ? typeof window > "u" ? null : window : c, m = n(o), h = Array.isArray(a) ? a.join(",") : a, g = r(null), _ = e(() => {
|
|
6
|
+
g.current?.abort(), g.current = null;
|
|
7
|
+
}, []);
|
|
8
|
+
return t(() => {
|
|
9
|
+
let e = Array.isArray(a) ? a : [a];
|
|
10
|
+
i && e.length === 0 && console.warn("[useEventListener] Called with an empty eventName array — no listeners were attached.");
|
|
11
|
+
let t = p && "current" in p ? p.current : p;
|
|
12
|
+
if (!t) return;
|
|
13
|
+
if (!t.addEventListener) {
|
|
14
|
+
i && console.warn("[useEventListener] The resolved target does not implement addEventListener — no listener was attached.", t);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
let n = new AbortController();
|
|
18
|
+
g.current = n;
|
|
19
|
+
let r = f ? AbortSignal.any([n.signal, f]) : n.signal, o = (e) => m(e);
|
|
20
|
+
return e.forEach((e) => {
|
|
21
|
+
t.addEventListener(e, o, {
|
|
22
|
+
capture: l,
|
|
23
|
+
passive: u,
|
|
24
|
+
once: d,
|
|
25
|
+
signal: r
|
|
17
26
|
});
|
|
18
27
|
}), () => {
|
|
19
|
-
n.
|
|
20
|
-
e.removeEventListener(t, r, { capture: s });
|
|
21
|
-
});
|
|
28
|
+
n.abort(), g.current === n && (g.current = null);
|
|
22
29
|
};
|
|
23
30
|
}, [
|
|
24
|
-
|
|
31
|
+
h,
|
|
32
|
+
p,
|
|
33
|
+
l,
|
|
25
34
|
u,
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
]);
|
|
35
|
+
d,
|
|
36
|
+
f
|
|
37
|
+
]), _;
|
|
30
38
|
}
|
|
31
39
|
//#endregion
|
|
32
|
-
export {
|
|
40
|
+
export { a 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.26",
|
|
4
4
|
"description": "An opinionated collection of react hooks, and reusable UI components.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -29,11 +29,15 @@
|
|
|
29
29
|
"types": "./dist/events.d.ts",
|
|
30
30
|
"import": "./dist/events.js"
|
|
31
31
|
},
|
|
32
|
+
"./storage": {
|
|
33
|
+
"types": "./dist/storage.d.ts",
|
|
34
|
+
"import": "./dist/storage.js"
|
|
35
|
+
},
|
|
32
36
|
"./styles.css": "./dist/index.css"
|
|
33
37
|
},
|
|
34
38
|
"scripts": {
|
|
35
39
|
"dev": "vite",
|
|
36
|
-
"build": "vite build && dts-bundle-generator --project tsconfig.app.json -o dist/index.d.ts src/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/ui.d.ts src/ui/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/state.d.ts src/state/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/performance.d.ts src/performance/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/events.d.ts src/events/index.ts",
|
|
40
|
+
"build": "vite build && dts-bundle-generator --project tsconfig.app.json -o dist/index.d.ts src/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/ui.d.ts src/ui/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/state.d.ts src/state/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/performance.d.ts src/performance/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/events.d.ts src/events/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/storage.d.ts src/storage/index.ts",
|
|
37
41
|
"lint": "eslint .",
|
|
38
42
|
"preview": "vite preview",
|
|
39
43
|
"format": "prettier . --write"
|