react-wire-persisted 2.0.1 → 3.0.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/README.md +121 -28
- package/client.js +1 -1
- package/client.mjs +6 -5
- package/dist/index.d.ts +197 -0
- package/dist/index.js +241 -305
- package/dist/react-wire-persisted.js +241 -305
- package/dist/react-wire-persisted.js.map +1 -0
- package/dist/react-wire-persisted.umd.cjs +2 -1
- package/dist/react-wire-persisted.umd.cjs.map +1 -0
- package/nextjs.js +4 -27
- package/package.json +91 -75
- package/src/components/{HydrationProvider.jsx → HydrationProvider.tsx} +13 -3
- package/src/components/index.tsx +1 -0
- package/src/global.d.ts +16 -0
- package/src/hooks/{useHydration.js → useHydration.ts} +19 -26
- package/src/index.ts +9 -0
- package/src/providers/LocalStorageProvider.ts +145 -0
- package/src/providers/MemoryStorageProvider.ts +14 -0
- package/src/providers/{StorageProvider.js → RWPStorageProvider.ts} +42 -39
- package/src/react-wire-persisted.ts +257 -0
- package/src/types.ts +19 -0
- package/src/utils/fakeLocalStorage.ts +17 -0
- package/src/utils/{index.js → index.ts} +5 -7
- package/src/utils/{isomorphic.js → isomorphic.ts} +24 -11
- package/src/utils/keys.ts +49 -0
- package/src/components/HydrationProvider.js +0 -45
- package/src/components/index.js +0 -1
- package/src/index.js +0 -6
- package/src/providers/LocalStorageProvider.js +0 -194
- package/src/providers/MemoryStorageProvider.js +0 -20
- package/src/react-wire-persisted.js +0 -136
- package/src/utils/fakeLocalStorage.js +0 -16
- package/src/utils/keys.js +0 -49
package/dist/index.js
CHANGED
|
@@ -1,309 +1,245 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useEffect as e, useRef as t } from "react";
|
|
2
|
+
import { createWire as n } from "@forminator/react-wire";
|
|
3
|
+
//#region \0rolldown/runtime.js
|
|
4
|
+
var r = Object.defineProperty, i = (e, t) => {
|
|
5
|
+
let n = {};
|
|
6
|
+
for (var i in e) r(n, i, {
|
|
7
|
+
get: e[i],
|
|
8
|
+
enumerable: !0
|
|
9
|
+
});
|
|
10
|
+
return t || r(n, Symbol.toStringTag, { value: "Module" }), n;
|
|
11
|
+
};
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region \0rollup-plugin-inject-process-env
|
|
3
14
|
(function() {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
globalThis.process = { env: s };
|
|
15
|
+
let e = {};
|
|
16
|
+
try {
|
|
17
|
+
if (process) {
|
|
18
|
+
process.env = Object.assign({}, process.env), Object.assign(process.env, e);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
} catch {}
|
|
22
|
+
globalThis.process = { env: e };
|
|
13
23
|
})();
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
removeAll(e = []) {
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
class T extends I {
|
|
147
|
-
constructor(e = null, t = {}) {
|
|
148
|
-
super(e, t), this.storage = this.getStorage(), this._isUsingFakeStorage = !d();
|
|
149
|
-
}
|
|
150
|
-
getStorage() {
|
|
151
|
-
return d() ? window.localStorage : h;
|
|
152
|
-
}
|
|
153
|
-
setNamespace(e) {
|
|
154
|
-
if (!this.namespace) {
|
|
155
|
-
this.namespace = e;
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
if (this.namespace === e)
|
|
159
|
-
return;
|
|
160
|
-
const t = JSON.parse(JSON.stringify(this.getAll()));
|
|
161
|
-
this.removeAll();
|
|
162
|
-
for (const [r, o] of Object.entries(t)) {
|
|
163
|
-
const n = r.replace(this.namespace, e);
|
|
164
|
-
this.setItem(n, o);
|
|
165
|
-
}
|
|
166
|
-
this.namespace = e;
|
|
167
|
-
}
|
|
168
|
-
getItem(e) {
|
|
169
|
-
const t = this.storage.getItem(e);
|
|
170
|
-
if (t == null)
|
|
171
|
-
return null;
|
|
172
|
-
try {
|
|
173
|
-
return JSON.parse(t);
|
|
174
|
-
} catch {
|
|
175
|
-
return t;
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
setItem(e, t) {
|
|
179
|
-
let r = t;
|
|
180
|
-
return r != null && (r = A(t) ? t : JSON.stringify(t)), this.storage.setItem(e, r);
|
|
181
|
-
}
|
|
182
|
-
removeItem(e, t = !1) {
|
|
183
|
-
return t && delete this.registry[e], this.storage.removeItem(e);
|
|
184
|
-
}
|
|
185
|
-
getAll() {
|
|
186
|
-
const e = `${this.namespace}.`;
|
|
187
|
-
return Object.keys(this.storage).reduce((t, r) => ((!this.namespace || r.startsWith(e)) && (t[r] = this.storage.getItem(r)), t), {});
|
|
188
|
-
}
|
|
189
|
-
_resetAll(e = !0, t = [], r = !1) {
|
|
190
|
-
const o = `${this.namespace}.`;
|
|
191
|
-
Object.keys(this.storage).forEach((n) => {
|
|
192
|
-
const a = this.namespace ? n.startsWith(o) : !0, l = t?.includes(n) || !1;
|
|
193
|
-
!a || l || (e ? Object.prototype.hasOwnProperty.call(this.registry, n) ? this.storage.setItem(n, this.registry[n]) : this.storage.removeItem(n) : (this.storage.removeItem(n), r && delete this.registry[n]));
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
resetAll(e = [], t = !1) {
|
|
197
|
-
this._resetAll(!0, e || [], t);
|
|
198
|
-
}
|
|
199
|
-
removeAll(e = [], t = !1) {
|
|
200
|
-
this._resetAll(!1, e || [], t);
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* Attempt to upgrade from fake storage to real localStorage
|
|
204
|
-
* This is useful for hydration scenarios
|
|
205
|
-
*/
|
|
206
|
-
upgradeToRealStorage() {
|
|
207
|
-
if (!this._isUsingFakeStorage || !d())
|
|
208
|
-
return !1;
|
|
209
|
-
const e = { ...this.storage };
|
|
210
|
-
return this.storage = window.localStorage, this._isUsingFakeStorage = !1, Object.keys(e).forEach((t) => {
|
|
211
|
-
if (t !== "__IS_FAKE_LOCAL_STORAGE__" && e[t] != null)
|
|
212
|
-
try {
|
|
213
|
-
this.storage.setItem(t, e[t]);
|
|
214
|
-
} catch {
|
|
215
|
-
return this.storage = h, this._isUsingFakeStorage = !0, !1;
|
|
216
|
-
}
|
|
217
|
-
}), !0;
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Check if currently using fake storage
|
|
221
|
-
*/
|
|
222
|
-
isUsingFakeStorage() {
|
|
223
|
-
return this._isUsingFakeStorage;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
const L = {
|
|
227
|
-
logging: {
|
|
228
|
-
enabled: !1
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
let v = T, i = new v(), u = { ...L }, f = [];
|
|
232
|
-
const P = () => i.namespace, V = () => i, $ = () => u, C = (s) => {
|
|
233
|
-
i.setNamespace(s), i = new v(s || P());
|
|
234
|
-
}, H = (s) => {
|
|
235
|
-
if (u = {
|
|
236
|
-
...u,
|
|
237
|
-
...s
|
|
238
|
-
}, u.logging.enabled)
|
|
239
|
-
for (console.info("Flushing", f.length, "pending logs"); f.length; )
|
|
240
|
-
console.log(...f.shift());
|
|
241
|
-
}, K = () => {
|
|
242
|
-
if (!m())
|
|
243
|
-
return !1;
|
|
244
|
-
const s = i.upgradeToRealStorage();
|
|
245
|
-
return s && E("react-wire-persisted: Upgraded to real localStorage after hydration"), s;
|
|
246
|
-
}, E = (...s) => {
|
|
247
|
-
u.logging.enabled ? console.log(...s) : f.push(s);
|
|
248
|
-
}, W = (s, e = null) => {
|
|
249
|
-
if (!s && typeof s != "number") throw new Error(
|
|
250
|
-
`createPersistedWire: Key cannot be a falsey value (${s}}`
|
|
251
|
-
);
|
|
252
|
-
i.register(s, e);
|
|
253
|
-
const t = U(e), r = () => t.getValue(), o = (c) => (i.setItem(s, c), t.setValue(c)), n = (c) => {
|
|
254
|
-
t.subscribe(c);
|
|
255
|
-
}, a = i.getItem(s), l = a === null ? e : a;
|
|
256
|
-
return E("react-wire-persisted: create", s, {
|
|
257
|
-
value: e,
|
|
258
|
-
storedValue: a,
|
|
259
|
-
initialValue: l
|
|
260
|
-
}), l !== e && o(l), {
|
|
261
|
-
...t,
|
|
262
|
-
getValue: r,
|
|
263
|
-
setValue: o,
|
|
264
|
-
subscribe: n
|
|
265
|
-
};
|
|
266
|
-
}, J = (s = {}) => {
|
|
267
|
-
const {
|
|
268
|
-
autoUpgrade: e = !0,
|
|
269
|
-
onUpgrade: t
|
|
270
|
-
} = s, r = b(!1);
|
|
271
|
-
return O(() => {
|
|
272
|
-
if (!e || r.current || !m())
|
|
273
|
-
return;
|
|
274
|
-
const o = () => {
|
|
275
|
-
_() && !r.current && K() && (r.current = !0, t?.());
|
|
276
|
-
};
|
|
277
|
-
o();
|
|
278
|
-
const n = setTimeout(o, 0);
|
|
279
|
-
return () => clearTimeout(n);
|
|
280
|
-
}, [e, t]), {
|
|
281
|
-
hasUpgraded: r.current
|
|
282
|
-
};
|
|
283
|
-
};
|
|
284
|
-
function D({ children: s, onUpgrade: e, autoUpgrade: t = !0 }) {
|
|
285
|
-
const r = b(!1);
|
|
286
|
-
return O(() => {
|
|
287
|
-
if (!t || r.current || !m())
|
|
288
|
-
return;
|
|
289
|
-
const o = () => {
|
|
290
|
-
_() && !r.current && K() && (r.current = !0, e?.());
|
|
291
|
-
};
|
|
292
|
-
o();
|
|
293
|
-
const n = setTimeout(o, 0);
|
|
294
|
-
return () => clearTimeout(n);
|
|
295
|
-
}, [t, e]), s;
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/utils/fakeLocalStorage.ts
|
|
26
|
+
var a = { __IS_FAKE_LOCAL_STORAGE__: "true" }, o = {
|
|
27
|
+
getItem: (e) => a[e],
|
|
28
|
+
setItem: (e, t) => {
|
|
29
|
+
a[e] = t;
|
|
30
|
+
},
|
|
31
|
+
removeItem: (e) => {
|
|
32
|
+
delete a[e];
|
|
33
|
+
},
|
|
34
|
+
...a
|
|
35
|
+
}, s = !1, c = !1, l = !1;
|
|
36
|
+
typeof window < "u" && (s = !0, document.readyState === "loading" ? document.addEventListener("DOMContentLoaded", () => {
|
|
37
|
+
c = !0;
|
|
38
|
+
}) : c = !0);
|
|
39
|
+
var u = () => s, d = () => c, f = () => l, p = () => {
|
|
40
|
+
l = !0;
|
|
41
|
+
}, m = () => {
|
|
42
|
+
if (!s) return !1;
|
|
43
|
+
try {
|
|
44
|
+
let e = "__rwp_test__";
|
|
45
|
+
return window.localStorage.setItem(e, "test"), window.localStorage.removeItem(e), !0;
|
|
46
|
+
} catch {
|
|
47
|
+
return !1;
|
|
48
|
+
}
|
|
49
|
+
}, h = {}, g = (e) => {
|
|
50
|
+
h[e] = e;
|
|
51
|
+
}, _ = (e) => g(e), v = () => h, y = (e, t = null) => {
|
|
52
|
+
let n = t || h;
|
|
53
|
+
return e ? Object.keys(n).reduce((t, r) => (t[r] = `${e}.${n[r]}`, t), {}) : n;
|
|
54
|
+
}, b = /* @__PURE__ */ i({
|
|
55
|
+
addKey: () => g,
|
|
56
|
+
fakeLocalStorage: () => o,
|
|
57
|
+
getHasHydrated: () => d,
|
|
58
|
+
getHasHydratedStorage: () => f,
|
|
59
|
+
getIsClient: () => u,
|
|
60
|
+
getKeys: () => v,
|
|
61
|
+
getPrefixedKeys: () => y,
|
|
62
|
+
isLocalStorageAvailable: () => m,
|
|
63
|
+
isPrimitive: () => x,
|
|
64
|
+
key: () => _,
|
|
65
|
+
markStorageAsHydrated: () => p
|
|
66
|
+
}), x = (e) => {
|
|
67
|
+
let t = typeof e;
|
|
68
|
+
return e === null ? !0 : Array.isArray(e) || t === "object" ? !1 : t !== "function";
|
|
69
|
+
}, S = class e {
|
|
70
|
+
constructor(t, n) {
|
|
71
|
+
if (new.target === e) throw TypeError("StorageProvider is abstract. Extend this class to implement it");
|
|
72
|
+
this.namespace = t || null, this.registry = n || ( /* istanbul ignore next */ {});
|
|
73
|
+
}
|
|
74
|
+
register(e, t) {
|
|
75
|
+
this.registry[e] = t;
|
|
76
|
+
}
|
|
77
|
+
upgradeToRealStorage() {
|
|
78
|
+
return !1;
|
|
79
|
+
}
|
|
80
|
+
isUsingFakeStorage() {
|
|
81
|
+
return !1;
|
|
82
|
+
}
|
|
83
|
+
}, C = class extends S {
|
|
84
|
+
constructor(e, t = {}) {
|
|
85
|
+
super(e, t), this.storage = o, this._isUsingFakeStorage = !0;
|
|
86
|
+
}
|
|
87
|
+
getStorage() {
|
|
88
|
+
return m() ? window.localStorage : o;
|
|
89
|
+
}
|
|
90
|
+
setNamespace(e) {
|
|
91
|
+
if (!this.namespace) {
|
|
92
|
+
this.namespace = e;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (this.namespace === e) return;
|
|
96
|
+
let t = JSON.parse(JSON.stringify(this.getAll()));
|
|
97
|
+
this.removeAll();
|
|
98
|
+
for (let [n, r] of Object.entries(t)) {
|
|
99
|
+
let t = n.replace(this.namespace, e);
|
|
100
|
+
this.setItem(t, r);
|
|
101
|
+
}
|
|
102
|
+
this.namespace = e;
|
|
103
|
+
}
|
|
104
|
+
getItem(e) {
|
|
105
|
+
let t = this.storage.getItem(e);
|
|
106
|
+
if (t == null) return null;
|
|
107
|
+
try {
|
|
108
|
+
return JSON.parse(t);
|
|
109
|
+
} catch {
|
|
110
|
+
return t;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
setItem(e, t) {
|
|
114
|
+
return t == null ? this.removeItem(e) : this.storage.setItem(e, JSON.stringify(t));
|
|
115
|
+
}
|
|
116
|
+
removeItem(e, t = !1) {
|
|
117
|
+
return t && delete this.registry[e], this.storage.removeItem(e);
|
|
118
|
+
}
|
|
119
|
+
getAll() {
|
|
120
|
+
let e = `${this.namespace}.`;
|
|
121
|
+
return Object.keys(this.storage).reduce((t, n) => ((!this.namespace || n.startsWith(e)) && (t[n] = this.storage.getItem(n)), t), {});
|
|
122
|
+
}
|
|
123
|
+
_resetAll(e = !0, t = [], n = !1) {
|
|
124
|
+
let r = `${this.namespace}.`;
|
|
125
|
+
Object.keys(this.storage).forEach((i) => {
|
|
126
|
+
let a = this.namespace ? i.startsWith(r) : !0, o = t?.includes(i) || !1;
|
|
127
|
+
!a || o || (e ? Object.hasOwn(this.registry, i) ? this.registry[i] === void 0 || this.registry[i] === null ? this.storage.removeItem(i) : this.storage.setItem(i, JSON.stringify(this.registry[i])) : this.storage.removeItem(i) : (this.storage.removeItem(i), n && delete this.registry[i]));
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
resetAll(e = [], t = !1) {
|
|
131
|
+
this._resetAll(!0, e || [], t);
|
|
132
|
+
}
|
|
133
|
+
removeAll(e = [], t = !1) {
|
|
134
|
+
this._resetAll(!1, e || [], t);
|
|
135
|
+
}
|
|
136
|
+
upgradeToRealStorage() {
|
|
137
|
+
return !this._isUsingFakeStorage || !m() ? !1 : (this.storage = window.localStorage, this._isUsingFakeStorage = !1, !0);
|
|
138
|
+
}
|
|
139
|
+
isUsingFakeStorage() {
|
|
140
|
+
return this._isUsingFakeStorage;
|
|
141
|
+
}
|
|
142
|
+
}, w = Math.random().toString(36).substring(7), T = (...e) => {
|
|
143
|
+
typeof globalThis < "u" && globalThis.__RWP_LOGGING_ENABLED__ !== !1 && console.log(...e);
|
|
144
|
+
}, E = {
|
|
145
|
+
logging: { enabled: !1 },
|
|
146
|
+
storageProvider: C
|
|
147
|
+
}, D = { ...E }, O, k = [];
|
|
148
|
+
typeof globalThis < "u" && globalThis.__RWP_LOGGING_ENABLED__ === void 0 && (globalThis.__RWP_LOGGING_ENABLED__ = E.logging.enabled), T("[RWP] Module initialized, instance ID:", w), T("[RWP] About to check global storage, instanceId:", w);
|
|
149
|
+
try {
|
|
150
|
+
globalThis.__RWP_STORAGE__ ? T("[RWP] Using existing global storage in instance:", w) : (T("[RWP] Creating global storage in instance:", w), globalThis.__RWP_STORAGE__ = new C("__internal_rwp_storage__")), O = globalThis.__RWP_STORAGE__, T("[RWP] InternalStorage assigned successfully");
|
|
151
|
+
} catch (e) {
|
|
152
|
+
globalThis.__RWP_LOGGING_ENABLED__ && console.error("[RWP] Error setting up global storage:", e), O = new C("__internal_rwp_storage__");
|
|
296
153
|
}
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
154
|
+
typeof globalThis < "u" && (globalThis.__RWP_REGISTERED_WIRES__ ? T("[RWP] Using existing global registeredWires in instance:", w) : (T("[RWP] Creating global registeredWires in instance:", w), globalThis.__RWP_REGISTERED_WIRES__ = /* @__PURE__ */ new Map()));
|
|
155
|
+
var A = globalThis.__RWP_REGISTERED_WIRES__ || /* @__PURE__ */ new Map();
|
|
156
|
+
T("[RWP] registeredWires Map reference in instance:", w, "size:", A.size);
|
|
157
|
+
var j = () => O.namespace, M = () => O, N = (e) => {
|
|
158
|
+
T("[RWP] setNamespace() called with:", e, "registered wires before:", A.size);
|
|
159
|
+
let t = e || j();
|
|
160
|
+
if (!t) throw Error("react-wire-persisted: Cannot set namespace to null or undefined");
|
|
161
|
+
O.setNamespace(e), O = new C(t), T("[RWP] setNamespace() done, registered wires after:", A.size);
|
|
162
|
+
}, P = () => D, F = (e) => {
|
|
163
|
+
/* istanbul ignore next */
|
|
164
|
+
if (D = {
|
|
165
|
+
...D,
|
|
166
|
+
...e
|
|
167
|
+
}, typeof globalThis < "u" && (globalThis.__RWP_LOGGING_ENABLED__ = D.logging.enabled), D.logging.enabled) for (console.info("Flushing", k.length, "pending logs"); k.length;)
|
|
168
|
+
/* istanbul ignore next */
|
|
169
|
+
console.log(...k.shift() || []);
|
|
170
|
+
}, I = () => {
|
|
171
|
+
T("[RWP] refreshAllWires() called in instance:", w, "registered wires:", A.size), R("react-wire-persisted: refreshAllWires() called, registered wires:", A.size), A.forEach((e, t) => {
|
|
172
|
+
let n = O.getItem(t), r = e.getValue();
|
|
173
|
+
T("[RWP] Checking wire", t, {
|
|
174
|
+
storedValue: n,
|
|
175
|
+
currentValue: r,
|
|
176
|
+
willUpdate: n !== null && n !== r
|
|
177
|
+
}), R("react-wire-persisted: Checking wire", t, {
|
|
178
|
+
storedValue: n,
|
|
179
|
+
currentValue: r,
|
|
180
|
+
willUpdate: n !== null && n !== r
|
|
181
|
+
}), n !== null && n !== r && (T("[RWP] Refreshing wire", t, "with stored value", n), R("react-wire-persisted: Refreshing wire", t, "with stored value", n), e.setValue(n));
|
|
182
|
+
});
|
|
183
|
+
}, L = () => {
|
|
184
|
+
if (T("[RWP] upgradeStorage() called in instance:", w, {
|
|
185
|
+
isClient: u(),
|
|
186
|
+
isUsingFakeStorage: O.isUsingFakeStorage()
|
|
187
|
+
}), R("react-wire-persisted: upgradeStorage() called", {
|
|
188
|
+
isClient: u(),
|
|
189
|
+
isUsingFakeStorage: O.isUsingFakeStorage()
|
|
190
|
+
}), !u()) return !1;
|
|
191
|
+
let e = O.upgradeToRealStorage();
|
|
192
|
+
return T("[RWP] upgradeToRealStorage() returned", e), R("react-wire-persisted: upgradeToRealStorage() returned", e), e && (p(), T("[RWP] Upgraded to real localStorage, calling refreshAllWires()"), R("react-wire-persisted: Upgraded to real localStorage after hydration"), I()), e;
|
|
193
|
+
}, R = (...e) => {
|
|
194
|
+
/* istanbul ignore next */
|
|
195
|
+
D.logging.enabled ? console.log(...e) : k.push(e);
|
|
196
|
+
}, z = (e, t = null) => {
|
|
197
|
+
if (T("[RWP] createPersistedWire() called in instance:", w, "key:", e, "value:", t), !e) throw Error(`createPersistedWire: Key cannot be a falsey value (${e}}`);
|
|
198
|
+
O.register(e, t);
|
|
199
|
+
let r = n(t), i = () => r.getValue(), a = (t) => (T("[RWP] setValue called in instance:", w, "key:", e, "isUsingFakeStorage:", O.isUsingFakeStorage()), O.setItem(e, t), r.setValue(t)), o = (e) => r.subscribe(e), s = t, c = f() || !O.isUsingFakeStorage();
|
|
200
|
+
if (c && u()) {
|
|
201
|
+
let t = O.getItem(e);
|
|
202
|
+
t !== null && (s = t);
|
|
203
|
+
}
|
|
204
|
+
return R("react-wire-persisted: create", e, {
|
|
205
|
+
value: t,
|
|
206
|
+
initialValue: s,
|
|
207
|
+
hasHydratedStorage: f(),
|
|
208
|
+
isUsingFakeStorage: O.isUsingFakeStorage(),
|
|
209
|
+
canReadStorage: c
|
|
210
|
+
}), s !== t && s !== void 0 && a(s), A.set(e, {
|
|
211
|
+
getValue: i,
|
|
212
|
+
setValue: a,
|
|
213
|
+
subscribe: o
|
|
214
|
+
}), T("[RWP] Wire registered, total wires:", A.size, "keys:", Array.from(A.keys())), {
|
|
215
|
+
...r,
|
|
216
|
+
getValue: i,
|
|
217
|
+
setValue: a,
|
|
218
|
+
subscribe: o
|
|
219
|
+
};
|
|
220
|
+
}, B = (n = {}) => {
|
|
221
|
+
let { autoUpgrade: r = !0, onUpgrade: i } = n, a = t(!1);
|
|
222
|
+
return e(() => {
|
|
223
|
+
if (!r || a.current || !u()) return;
|
|
224
|
+
let e = () => {
|
|
225
|
+
d() && !a.current && L() && (a.current = !0, i?.());
|
|
226
|
+
};
|
|
227
|
+
e();
|
|
228
|
+
let t = setTimeout(e, 0);
|
|
229
|
+
return () => clearTimeout(t);
|
|
230
|
+
}, [r, i]), { hasUpgraded: a.current };
|
|
231
|
+
}, V = ({ children: e, onUpgrade: t, autoUpgrade: n = !0 }) => (B({
|
|
232
|
+
onUpgrade: t,
|
|
233
|
+
autoUpgrade: n
|
|
234
|
+
}), e), H = class extends C {
|
|
235
|
+
constructor(e, t = {}) {
|
|
236
|
+
super(e, t);
|
|
237
|
+
}
|
|
238
|
+
getStorage() {
|
|
239
|
+
return o;
|
|
240
|
+
}
|
|
309
241
|
};
|
|
242
|
+
//#endregion
|
|
243
|
+
export { V as HydrationProvider, C as LocalStorageProvider, H as MemoryStorageProvider, S as RWPStorageProvider, z as createPersistedWire, E as defaultOptions, j as getNamespace, P as getOptions, M as getStorage, N as setNamespace, F as setOptions, L as upgradeStorage, B as useHydration, b as utils };
|
|
244
|
+
|
|
245
|
+
//# sourceMappingURL=react-wire-persisted.js.map
|