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/dist/index.js CHANGED
@@ -1,309 +1,245 @@
1
- import { createWire as U } from "@forminator/react-wire";
2
- import { useRef as b, useEffect as O } from "react";
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
- const s = {};
5
- try {
6
- if (process) {
7
- process.env = Object.assign({}, process.env), Object.assign(process.env, s);
8
- return;
9
- }
10
- } catch {
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
- const y = {}, w = (s) => {
15
- y[s] = s;
16
- }, j = (s) => w(s), x = () => y, N = (s, e = null) => {
17
- const t = e || y;
18
- return s ? Object.keys(t).reduce((r, o) => ({
19
- ...r,
20
- [o]: `${s}.${t[o]}`
21
- }), {}) : t;
22
- }, g = {
23
- __IS_FAKE_LOCAL_STORAGE__: !0
24
- }, h = {
25
- getItem: (s) => g[s],
26
- setItem: (s, e) => {
27
- g[s] = e;
28
- },
29
- removeItem: (s) => {
30
- delete g[s];
31
- },
32
- // Make Object.keys() work properly for _resetAll method
33
- ...g
34
- };
35
- let S = !1, p = !1;
36
- typeof window < "u" && (S = !0, document.readyState === "loading" ? document.addEventListener("DOMContentLoaded", () => {
37
- p = !0;
38
- }) : p = !0);
39
- const m = () => S, _ = () => p, d = () => {
40
- if (!S) return !1;
41
- try {
42
- const s = "__rwp_test__";
43
- return window.localStorage.setItem(s, "test"), window.localStorage.removeItem(s), !0;
44
- } catch {
45
- return !1;
46
- }
47
- }, A = (s) => {
48
- const e = typeof s;
49
- return s === null ? !0 : Array.isArray(s) || e === "object" ? !1 : e !== "function";
50
- }, k = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
51
- __proto__: null,
52
- addKey: w,
53
- fakeLocalStorage: h,
54
- getHasHydrated: _,
55
- getIsClient: m,
56
- getKeys: x,
57
- getPrefixedKeys: N,
58
- isLocalStorageAvailable: d,
59
- isPrimitive: A,
60
- key: j
61
- }, Symbol.toStringTag, { value: "Module" }));
62
- class I {
63
- /**
64
- * Initializes the class
65
- * @param {String} namespace Namespace to prefix all keys with. Mostly used for the logging & reset functions
66
- * @param {Object} registry (Optional) Initialize the storage provider with an existing registry
67
- */
68
- constructor(e, t) {
69
- if (new.target === I)
70
- throw TypeError("StorageProvider is abstract. Extend this class to implement it");
71
- this.namespace = e || null, this.registry = t || /* istanbul ignore next */
72
- {};
73
- }
74
- /**
75
- * Sets the namespace for this storage provider, and migrates
76
- * all stored values to the new namespace
77
- * @param {String} namespace New namespace for this storage provider
78
- */
79
- /* istanbul ignore next */
80
- setNamespace(e) {
81
- }
82
- /**
83
- * Registers an item with it's initial value. This is used for logging, resetting, etc.
84
- * @param {String} key Storage item's key
85
- * @param {*} initialValue Storage item's initial value
86
- */
87
- register(e, t) {
88
- this.registry[e] = t;
89
- }
90
- /**
91
- * Reads an item from storage
92
- * @param {String} key Key for the item to retrieve
93
- */
94
- /* istanbul ignore next */
95
- getItem(e) {
96
- }
97
- /**
98
- * Stores a value
99
- * @param {String} key Item's storage key
100
- * @param {String} value Item's value to store
101
- */
102
- /* istanbul ignore next */
103
- setItem(e, t) {
104
- }
105
- /**
106
- * Removes an item from storage
107
- * @param {String} key Item's storage key
108
- * @param {Boolean} fromRegistry (Optional) If the item should also be removed from the registry
109
- */
110
- /* istanbul ignore next */
111
- removeItem(e, t = !1) {
112
- }
113
- /**
114
- * Gets all stored keys & values
115
- * If a `namespace` was set, only keys prefixed with the namespace will be returned
116
- */
117
- /* istanbul ignore next */
118
- getAll() {
119
- }
120
- /**
121
- *
122
- * @param {Boolean} useInitialValues If values should be replaced with their initial values. If false, keys are removed
123
- * @param {String[]} excludedKeys (Optional) List of keys to exclude
124
- * @param {Boolean} clearRegistry (Optional) If the registry should also be cleared
125
- */
126
- /* istanbul ignore next */
127
- _resetAll(e = !0, t = [], r = !1) {
128
- }
129
- /**
130
- * Resets all values to their initial values
131
- * If a `namespace` is set, only keys prefixed with the namespace will be reset
132
- * @param {String[]} excludedKeys (Optional) List of keys to exclude
133
- */
134
- /* istanbul ignore next */
135
- resetAll(e = []) {
136
- }
137
- /**
138
- * Removes all items from local storage.
139
- * If a `namespace` is set, only keys prefixed with the namespace will be removed
140
- * @param {String[]} excludedKeys (Optional) List of keys to exclude
141
- */
142
- /* istanbul ignore next */
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
- export {
298
- D as HydrationProvider,
299
- W as createPersistedWire,
300
- L as defaultOptions,
301
- P as getNamespace,
302
- $ as getOptions,
303
- V as getStorage,
304
- C as setNamespace,
305
- H as setOptions,
306
- K as upgradeStorage,
307
- J as useHydration,
308
- k as utils
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