gjendje 1.3.1 → 1.3.2
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/{chunk-UGISZQRH.js → chunk-JRTZTDT6.js} +91 -16
- package/dist/{chunk-TXLZGYYF.cjs → chunk-RB7FJ7WC.cjs} +91 -16
- package/dist/index.cjs +27 -27
- package/dist/index.js +2 -2
- package/dist/server.cjs +3 -3
- package/dist/server.js +1 -1
- package/package.json +1 -1
|
@@ -30,8 +30,18 @@ function flush() {
|
|
|
30
30
|
while (queue.length > 0) {
|
|
31
31
|
if (++iterations > MAX_FLUSH_ITERATIONS) {
|
|
32
32
|
console.error(
|
|
33
|
-
"[gjendje] Batch flush exceeded maximum iterations \u2014 possible infinite loop.
|
|
33
|
+
"[gjendje] Batch flush exceeded maximum iterations \u2014 possible infinite loop. Delivering remaining notifications once before stopping."
|
|
34
34
|
);
|
|
35
|
+
const remaining = queue;
|
|
36
|
+
queue = [];
|
|
37
|
+
for (let i = 0; i < remaining.length; i++) {
|
|
38
|
+
const fn = remaining[i];
|
|
39
|
+
try {
|
|
40
|
+
if (fn) fn();
|
|
41
|
+
} catch (err) {
|
|
42
|
+
console.error("[gjendje] Notification threw during best-effort delivery:", err);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
35
45
|
queue = [];
|
|
36
46
|
break;
|
|
37
47
|
}
|
|
@@ -73,6 +83,28 @@ function shallowEqual(a, b) {
|
|
|
73
83
|
return true;
|
|
74
84
|
}
|
|
75
85
|
if (Array.isArray(b)) return false;
|
|
86
|
+
if (a instanceof Set) {
|
|
87
|
+
if (!(b instanceof Set)) return false;
|
|
88
|
+
if (a.size !== b.size) return false;
|
|
89
|
+
for (const item of a) {
|
|
90
|
+
if (!b.has(item)) return false;
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
if (a instanceof Map) {
|
|
95
|
+
if (!(b instanceof Map)) return false;
|
|
96
|
+
if (a.size !== b.size) return false;
|
|
97
|
+
for (const [key, val] of a) {
|
|
98
|
+
if (!b.has(key) || !Object.is(val, b.get(key))) return false;
|
|
99
|
+
}
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
if (a instanceof Date) {
|
|
103
|
+
return b instanceof Date && a.getTime() === b.getTime();
|
|
104
|
+
}
|
|
105
|
+
if (a instanceof RegExp) {
|
|
106
|
+
return b instanceof RegExp && a.toString() === b.toString();
|
|
107
|
+
}
|
|
76
108
|
const objA = a;
|
|
77
109
|
const objB = b;
|
|
78
110
|
const keysA = Object.keys(objA);
|
|
@@ -219,9 +251,9 @@ function notifyWatchers(watchers, prev, next) {
|
|
|
219
251
|
// src/persist.ts
|
|
220
252
|
function isVersionedValue(value) {
|
|
221
253
|
if (!isRecord(value)) return false;
|
|
222
|
-
return "v" in value && "data" in value && Number.isSafeInteger(value.v);
|
|
254
|
+
return Object.keys(value).length === 2 && "v" in value && "data" in value && Number.isSafeInteger(value.v);
|
|
223
255
|
}
|
|
224
|
-
function readAndMigrate(raw, options, key, scope) {
|
|
256
|
+
function readAndMigrate(raw, options, key, scope, onFallback) {
|
|
225
257
|
const currentVersion = options.version ?? 1;
|
|
226
258
|
const defaultValue = options.default;
|
|
227
259
|
try {
|
|
@@ -253,6 +285,7 @@ function readAndMigrate(raw, options, key, scope) {
|
|
|
253
285
|
const validationErr = new ValidationError(key, scope, data);
|
|
254
286
|
safeCallConfig(config.onError, { key, scope, error: validationErr });
|
|
255
287
|
}
|
|
288
|
+
onFallback?.();
|
|
256
289
|
return defaultValue;
|
|
257
290
|
}
|
|
258
291
|
return data;
|
|
@@ -262,6 +295,7 @@ function readAndMigrate(raw, options, key, scope) {
|
|
|
262
295
|
const readErr = new StorageReadError(key, scope, err);
|
|
263
296
|
safeCallConfig(getConfig().onError, { key, scope, error: readErr });
|
|
264
297
|
}
|
|
298
|
+
onFallback?.();
|
|
265
299
|
return defaultValue;
|
|
266
300
|
}
|
|
267
301
|
}
|
|
@@ -299,12 +333,12 @@ function runMigrations(data, fromVersion, toVersion, migrations, key, scope) {
|
|
|
299
333
|
try {
|
|
300
334
|
current = migrateFn(current);
|
|
301
335
|
} catch (err) {
|
|
302
|
-
log("warn", `Migration from v${v} failed \u2014
|
|
336
|
+
log("warn", `Migration from v${v} failed \u2014 discarding partially migrated data.`);
|
|
337
|
+
const migrationErr = new MigrationError(key ?? "", scope ?? "memory", v, toVersion, err);
|
|
303
338
|
if (key && scope) {
|
|
304
|
-
const migrationErr = new MigrationError(key, scope, v, toVersion, err);
|
|
305
339
|
safeCallConfig(getConfig().onError, { key, scope, error: migrationErr });
|
|
306
340
|
}
|
|
307
|
-
|
|
341
|
+
throw migrationErr;
|
|
308
342
|
}
|
|
309
343
|
}
|
|
310
344
|
}
|
|
@@ -318,6 +352,25 @@ function createStorageAdapter(storage, key, options) {
|
|
|
318
352
|
let cachedRaw;
|
|
319
353
|
let cachedValue;
|
|
320
354
|
let cacheValid = false;
|
|
355
|
+
const backupKey = `${key}:__gjendje_backup`;
|
|
356
|
+
function backupRawData(raw) {
|
|
357
|
+
try {
|
|
358
|
+
if (storage.getItem(backupKey) === null) {
|
|
359
|
+
storage.setItem(backupKey, raw);
|
|
360
|
+
log(
|
|
361
|
+
"warn",
|
|
362
|
+
`Original data for key "${key}" backed up to "${backupKey}" after migration/validation failure.`
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
} catch (backupErr) {
|
|
366
|
+
const scope = options.scope ?? "local";
|
|
367
|
+
log(
|
|
368
|
+
"error",
|
|
369
|
+
`Failed to backup data for key "${key}" to "${backupKey}" \u2014 original data may be lost.`
|
|
370
|
+
);
|
|
371
|
+
reportError(key, scope, backupErr);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
321
374
|
function parse(raw) {
|
|
322
375
|
if (serialize) {
|
|
323
376
|
const value = serialize.parse(raw);
|
|
@@ -327,11 +380,12 @@ function createStorageAdapter(storage, key, options) {
|
|
|
327
380
|
safeCallConfig(config.onValidationFail, { key, scope, value });
|
|
328
381
|
const validationErr = new ValidationError(key, scope, value);
|
|
329
382
|
safeCallConfig(config.onError, { key, scope, error: validationErr });
|
|
383
|
+
backupRawData(raw);
|
|
330
384
|
return defaultValue;
|
|
331
385
|
}
|
|
332
386
|
return value;
|
|
333
387
|
}
|
|
334
|
-
return readAndMigrate(raw, options, key, options.scope);
|
|
388
|
+
return readAndMigrate(raw, options, key, options.scope, () => backupRawData(raw));
|
|
335
389
|
}
|
|
336
390
|
function read() {
|
|
337
391
|
if (cacheValid) return cachedValue;
|
|
@@ -385,6 +439,7 @@ function createStorageAdapter(storage, key, options) {
|
|
|
385
439
|
safeCallConfig(getConfig().onQuotaExceeded, { key, scope, error: writeErr });
|
|
386
440
|
}
|
|
387
441
|
reportError(key, scope, writeErr);
|
|
442
|
+
throw writeErr;
|
|
388
443
|
}
|
|
389
444
|
}
|
|
390
445
|
let lastNotifiedValue = defaultValue;
|
|
@@ -470,6 +525,7 @@ function createBucketAdapter(key, bucketOptions, options) {
|
|
|
470
525
|
let delegateUnsub;
|
|
471
526
|
const ready = (async () => {
|
|
472
527
|
if (!isBucketSupported()) return;
|
|
528
|
+
let hadUserWrite = false;
|
|
473
529
|
try {
|
|
474
530
|
const openOptions = {
|
|
475
531
|
persisted: bucketOptions.persisted ?? false,
|
|
@@ -503,7 +559,7 @@ function createBucketAdapter(key, bucketOptions, options) {
|
|
|
503
559
|
const storage = await bucket.localStorage();
|
|
504
560
|
if (isDestroyed) return;
|
|
505
561
|
const currentValue = delegate.get();
|
|
506
|
-
|
|
562
|
+
hadUserWrite = !shallowEqual(currentValue, defaultValue);
|
|
507
563
|
delegate.destroy?.();
|
|
508
564
|
delegate = createStorageAdapter(storage, key, options);
|
|
509
565
|
if (isDestroyed) {
|
|
@@ -525,10 +581,12 @@ function createBucketAdapter(key, bucketOptions, options) {
|
|
|
525
581
|
reportError(key, "bucket", err);
|
|
526
582
|
}
|
|
527
583
|
if (isDestroyed) return;
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
584
|
+
if (!hadUserWrite) {
|
|
585
|
+
const storedValue = delegate.get();
|
|
586
|
+
if (!shallowEqual(storedValue, defaultValue)) {
|
|
587
|
+
lastNotifiedValue = storedValue;
|
|
588
|
+
notify(notifyListeners);
|
|
589
|
+
}
|
|
532
590
|
}
|
|
533
591
|
delegateUnsub = delegate.subscribe((value) => {
|
|
534
592
|
lastNotifiedValue = value;
|
|
@@ -711,8 +769,12 @@ function createUrlAdapter(key, defaultValue, serializer, persist, urlReplace) {
|
|
|
711
769
|
}
|
|
712
770
|
cachedSearch = search ? `?${search}` : "";
|
|
713
771
|
cachedValue = persist ? mergeKeys(toStore, defaultValue, persist) : value;
|
|
714
|
-
} catch {
|
|
772
|
+
} catch (e) {
|
|
715
773
|
cachedSearch = void 0;
|
|
774
|
+
const writeErr = new StorageWriteError(key, "url", e);
|
|
775
|
+
log("error", writeErr.message);
|
|
776
|
+
reportError(key, "url", writeErr);
|
|
777
|
+
throw writeErr;
|
|
716
778
|
}
|
|
717
779
|
}
|
|
718
780
|
let lastNotifiedValue = defaultValue;
|
|
@@ -841,6 +903,7 @@ var StateImpl = class {
|
|
|
841
903
|
this._s = preallocatedState ?? {
|
|
842
904
|
lastValue: adapter.get(),
|
|
843
905
|
isDestroyed: false,
|
|
906
|
+
hasUserWrite: false,
|
|
844
907
|
interceptors: void 0,
|
|
845
908
|
changeHandlers: void 0,
|
|
846
909
|
settled: RESOLVED,
|
|
@@ -901,8 +964,14 @@ var StateImpl = class {
|
|
|
901
964
|
let next = typeof valueOrUpdater === "function" ? valueOrUpdater(prev) : valueOrUpdater;
|
|
902
965
|
next = this._applyInterceptors(next, prev);
|
|
903
966
|
if (this._options.isEqual?.(next, prev)) return;
|
|
967
|
+
try {
|
|
968
|
+
this._adapter.set(next);
|
|
969
|
+
} catch (err) {
|
|
970
|
+
if (err instanceof StorageWriteError) return;
|
|
971
|
+
throw err;
|
|
972
|
+
}
|
|
904
973
|
s.lastValue = next;
|
|
905
|
-
|
|
974
|
+
s.hasUserWrite = true;
|
|
906
975
|
s.settled = this._adapter.ready;
|
|
907
976
|
this._notifyChange(next, prev);
|
|
908
977
|
}
|
|
@@ -915,8 +984,14 @@ var StateImpl = class {
|
|
|
915
984
|
const prev = this._adapter.get();
|
|
916
985
|
const next = this._applyInterceptors(this._defaultValue, prev);
|
|
917
986
|
if (this._options.isEqual?.(next, prev)) return;
|
|
987
|
+
try {
|
|
988
|
+
this._adapter.set(next);
|
|
989
|
+
} catch (err) {
|
|
990
|
+
if (err instanceof StorageWriteError) return;
|
|
991
|
+
throw err;
|
|
992
|
+
}
|
|
918
993
|
s.lastValue = next;
|
|
919
|
-
|
|
994
|
+
s.hasUserWrite = true;
|
|
920
995
|
s.settled = this._adapter.ready;
|
|
921
996
|
safeCallConfig(this._config.onReset, { key: this.key, scope: this.scope, previousValue: prev });
|
|
922
997
|
this._notifyChange(next, prev);
|
|
@@ -1321,7 +1396,7 @@ function createBase(key, options) {
|
|
|
1321
1396
|
const instance = new StateImpl(key, scope, rKey, adapter, options, config);
|
|
1322
1397
|
if (isSsrMode && !isServer()) {
|
|
1323
1398
|
instance._s.hydrated = afterHydration(() => {
|
|
1324
|
-
if (instance.isDestroyed) return;
|
|
1399
|
+
if (instance.isDestroyed || instance._s.hasUserWrite) return;
|
|
1325
1400
|
const currentValue = instance.get();
|
|
1326
1401
|
if (!shallowEqual(currentValue, options.default)) return;
|
|
1327
1402
|
let realAdapter;
|
|
@@ -32,8 +32,18 @@ function flush() {
|
|
|
32
32
|
while (queue.length > 0) {
|
|
33
33
|
if (++iterations > MAX_FLUSH_ITERATIONS) {
|
|
34
34
|
console.error(
|
|
35
|
-
"[gjendje] Batch flush exceeded maximum iterations \u2014 possible infinite loop.
|
|
35
|
+
"[gjendje] Batch flush exceeded maximum iterations \u2014 possible infinite loop. Delivering remaining notifications once before stopping."
|
|
36
36
|
);
|
|
37
|
+
const remaining = queue;
|
|
38
|
+
queue = [];
|
|
39
|
+
for (let i = 0; i < remaining.length; i++) {
|
|
40
|
+
const fn = remaining[i];
|
|
41
|
+
try {
|
|
42
|
+
if (fn) fn();
|
|
43
|
+
} catch (err) {
|
|
44
|
+
console.error("[gjendje] Notification threw during best-effort delivery:", err);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
37
47
|
queue = [];
|
|
38
48
|
break;
|
|
39
49
|
}
|
|
@@ -75,6 +85,28 @@ function shallowEqual(a, b) {
|
|
|
75
85
|
return true;
|
|
76
86
|
}
|
|
77
87
|
if (Array.isArray(b)) return false;
|
|
88
|
+
if (a instanceof Set) {
|
|
89
|
+
if (!(b instanceof Set)) return false;
|
|
90
|
+
if (a.size !== b.size) return false;
|
|
91
|
+
for (const item of a) {
|
|
92
|
+
if (!b.has(item)) return false;
|
|
93
|
+
}
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
if (a instanceof Map) {
|
|
97
|
+
if (!(b instanceof Map)) return false;
|
|
98
|
+
if (a.size !== b.size) return false;
|
|
99
|
+
for (const [key, val] of a) {
|
|
100
|
+
if (!b.has(key) || !Object.is(val, b.get(key))) return false;
|
|
101
|
+
}
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
if (a instanceof Date) {
|
|
105
|
+
return b instanceof Date && a.getTime() === b.getTime();
|
|
106
|
+
}
|
|
107
|
+
if (a instanceof RegExp) {
|
|
108
|
+
return b instanceof RegExp && a.toString() === b.toString();
|
|
109
|
+
}
|
|
78
110
|
const objA = a;
|
|
79
111
|
const objB = b;
|
|
80
112
|
const keysA = Object.keys(objA);
|
|
@@ -221,9 +253,9 @@ function notifyWatchers(watchers, prev, next) {
|
|
|
221
253
|
// src/persist.ts
|
|
222
254
|
function isVersionedValue(value) {
|
|
223
255
|
if (!isRecord(value)) return false;
|
|
224
|
-
return "v" in value && "data" in value && Number.isSafeInteger(value.v);
|
|
256
|
+
return Object.keys(value).length === 2 && "v" in value && "data" in value && Number.isSafeInteger(value.v);
|
|
225
257
|
}
|
|
226
|
-
function readAndMigrate(raw, options, key, scope) {
|
|
258
|
+
function readAndMigrate(raw, options, key, scope, onFallback) {
|
|
227
259
|
const currentVersion = options.version ?? 1;
|
|
228
260
|
const defaultValue = options.default;
|
|
229
261
|
try {
|
|
@@ -255,6 +287,7 @@ function readAndMigrate(raw, options, key, scope) {
|
|
|
255
287
|
const validationErr = new ValidationError(key, scope, data);
|
|
256
288
|
chunk3YGK3425_cjs.safeCallConfig(config.onError, { key, scope, error: validationErr });
|
|
257
289
|
}
|
|
290
|
+
onFallback?.();
|
|
258
291
|
return defaultValue;
|
|
259
292
|
}
|
|
260
293
|
return data;
|
|
@@ -264,6 +297,7 @@ function readAndMigrate(raw, options, key, scope) {
|
|
|
264
297
|
const readErr = new StorageReadError(key, scope, err);
|
|
265
298
|
chunk3YGK3425_cjs.safeCallConfig(chunk3YGK3425_cjs.getConfig().onError, { key, scope, error: readErr });
|
|
266
299
|
}
|
|
300
|
+
onFallback?.();
|
|
267
301
|
return defaultValue;
|
|
268
302
|
}
|
|
269
303
|
}
|
|
@@ -301,12 +335,12 @@ function runMigrations(data, fromVersion, toVersion, migrations, key, scope) {
|
|
|
301
335
|
try {
|
|
302
336
|
current = migrateFn(current);
|
|
303
337
|
} catch (err) {
|
|
304
|
-
chunk3YGK3425_cjs.log("warn", `Migration from v${v} failed \u2014
|
|
338
|
+
chunk3YGK3425_cjs.log("warn", `Migration from v${v} failed \u2014 discarding partially migrated data.`);
|
|
339
|
+
const migrationErr = new MigrationError(key ?? "", scope ?? "memory", v, toVersion, err);
|
|
305
340
|
if (key && scope) {
|
|
306
|
-
const migrationErr = new MigrationError(key, scope, v, toVersion, err);
|
|
307
341
|
chunk3YGK3425_cjs.safeCallConfig(chunk3YGK3425_cjs.getConfig().onError, { key, scope, error: migrationErr });
|
|
308
342
|
}
|
|
309
|
-
|
|
343
|
+
throw migrationErr;
|
|
310
344
|
}
|
|
311
345
|
}
|
|
312
346
|
}
|
|
@@ -320,6 +354,25 @@ function createStorageAdapter(storage, key, options) {
|
|
|
320
354
|
let cachedRaw;
|
|
321
355
|
let cachedValue;
|
|
322
356
|
let cacheValid = false;
|
|
357
|
+
const backupKey = `${key}:__gjendje_backup`;
|
|
358
|
+
function backupRawData(raw) {
|
|
359
|
+
try {
|
|
360
|
+
if (storage.getItem(backupKey) === null) {
|
|
361
|
+
storage.setItem(backupKey, raw);
|
|
362
|
+
chunk3YGK3425_cjs.log(
|
|
363
|
+
"warn",
|
|
364
|
+
`Original data for key "${key}" backed up to "${backupKey}" after migration/validation failure.`
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
} catch (backupErr) {
|
|
368
|
+
const scope = options.scope ?? "local";
|
|
369
|
+
chunk3YGK3425_cjs.log(
|
|
370
|
+
"error",
|
|
371
|
+
`Failed to backup data for key "${key}" to "${backupKey}" \u2014 original data may be lost.`
|
|
372
|
+
);
|
|
373
|
+
chunk3YGK3425_cjs.reportError(key, scope, backupErr);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
323
376
|
function parse(raw) {
|
|
324
377
|
if (serialize) {
|
|
325
378
|
const value = serialize.parse(raw);
|
|
@@ -329,11 +382,12 @@ function createStorageAdapter(storage, key, options) {
|
|
|
329
382
|
chunk3YGK3425_cjs.safeCallConfig(config.onValidationFail, { key, scope, value });
|
|
330
383
|
const validationErr = new ValidationError(key, scope, value);
|
|
331
384
|
chunk3YGK3425_cjs.safeCallConfig(config.onError, { key, scope, error: validationErr });
|
|
385
|
+
backupRawData(raw);
|
|
332
386
|
return defaultValue;
|
|
333
387
|
}
|
|
334
388
|
return value;
|
|
335
389
|
}
|
|
336
|
-
return readAndMigrate(raw, options, key, options.scope);
|
|
390
|
+
return readAndMigrate(raw, options, key, options.scope, () => backupRawData(raw));
|
|
337
391
|
}
|
|
338
392
|
function read() {
|
|
339
393
|
if (cacheValid) return cachedValue;
|
|
@@ -387,6 +441,7 @@ function createStorageAdapter(storage, key, options) {
|
|
|
387
441
|
chunk3YGK3425_cjs.safeCallConfig(chunk3YGK3425_cjs.getConfig().onQuotaExceeded, { key, scope, error: writeErr });
|
|
388
442
|
}
|
|
389
443
|
chunk3YGK3425_cjs.reportError(key, scope, writeErr);
|
|
444
|
+
throw writeErr;
|
|
390
445
|
}
|
|
391
446
|
}
|
|
392
447
|
let lastNotifiedValue = defaultValue;
|
|
@@ -472,6 +527,7 @@ function createBucketAdapter(key, bucketOptions, options) {
|
|
|
472
527
|
let delegateUnsub;
|
|
473
528
|
const ready = (async () => {
|
|
474
529
|
if (!isBucketSupported()) return;
|
|
530
|
+
let hadUserWrite = false;
|
|
475
531
|
try {
|
|
476
532
|
const openOptions = {
|
|
477
533
|
persisted: bucketOptions.persisted ?? false,
|
|
@@ -505,7 +561,7 @@ function createBucketAdapter(key, bucketOptions, options) {
|
|
|
505
561
|
const storage = await bucket.localStorage();
|
|
506
562
|
if (isDestroyed) return;
|
|
507
563
|
const currentValue = delegate.get();
|
|
508
|
-
|
|
564
|
+
hadUserWrite = !shallowEqual(currentValue, defaultValue);
|
|
509
565
|
delegate.destroy?.();
|
|
510
566
|
delegate = createStorageAdapter(storage, key, options);
|
|
511
567
|
if (isDestroyed) {
|
|
@@ -527,10 +583,12 @@ function createBucketAdapter(key, bucketOptions, options) {
|
|
|
527
583
|
chunk3YGK3425_cjs.reportError(key, "bucket", err);
|
|
528
584
|
}
|
|
529
585
|
if (isDestroyed) return;
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
586
|
+
if (!hadUserWrite) {
|
|
587
|
+
const storedValue = delegate.get();
|
|
588
|
+
if (!shallowEqual(storedValue, defaultValue)) {
|
|
589
|
+
lastNotifiedValue = storedValue;
|
|
590
|
+
notify(notifyListeners);
|
|
591
|
+
}
|
|
534
592
|
}
|
|
535
593
|
delegateUnsub = delegate.subscribe((value) => {
|
|
536
594
|
lastNotifiedValue = value;
|
|
@@ -713,8 +771,12 @@ function createUrlAdapter(key, defaultValue, serializer, persist, urlReplace) {
|
|
|
713
771
|
}
|
|
714
772
|
cachedSearch = search ? `?${search}` : "";
|
|
715
773
|
cachedValue = persist ? mergeKeys(toStore, defaultValue, persist) : value;
|
|
716
|
-
} catch {
|
|
774
|
+
} catch (e) {
|
|
717
775
|
cachedSearch = void 0;
|
|
776
|
+
const writeErr = new StorageWriteError(key, "url", e);
|
|
777
|
+
chunk3YGK3425_cjs.log("error", writeErr.message);
|
|
778
|
+
chunk3YGK3425_cjs.reportError(key, "url", writeErr);
|
|
779
|
+
throw writeErr;
|
|
718
780
|
}
|
|
719
781
|
}
|
|
720
782
|
let lastNotifiedValue = defaultValue;
|
|
@@ -843,6 +905,7 @@ var StateImpl = class {
|
|
|
843
905
|
this._s = preallocatedState ?? {
|
|
844
906
|
lastValue: adapter.get(),
|
|
845
907
|
isDestroyed: false,
|
|
908
|
+
hasUserWrite: false,
|
|
846
909
|
interceptors: void 0,
|
|
847
910
|
changeHandlers: void 0,
|
|
848
911
|
settled: RESOLVED,
|
|
@@ -903,8 +966,14 @@ var StateImpl = class {
|
|
|
903
966
|
let next = typeof valueOrUpdater === "function" ? valueOrUpdater(prev) : valueOrUpdater;
|
|
904
967
|
next = this._applyInterceptors(next, prev);
|
|
905
968
|
if (this._options.isEqual?.(next, prev)) return;
|
|
969
|
+
try {
|
|
970
|
+
this._adapter.set(next);
|
|
971
|
+
} catch (err) {
|
|
972
|
+
if (err instanceof StorageWriteError) return;
|
|
973
|
+
throw err;
|
|
974
|
+
}
|
|
906
975
|
s.lastValue = next;
|
|
907
|
-
|
|
976
|
+
s.hasUserWrite = true;
|
|
908
977
|
s.settled = this._adapter.ready;
|
|
909
978
|
this._notifyChange(next, prev);
|
|
910
979
|
}
|
|
@@ -917,8 +986,14 @@ var StateImpl = class {
|
|
|
917
986
|
const prev = this._adapter.get();
|
|
918
987
|
const next = this._applyInterceptors(this._defaultValue, prev);
|
|
919
988
|
if (this._options.isEqual?.(next, prev)) return;
|
|
989
|
+
try {
|
|
990
|
+
this._adapter.set(next);
|
|
991
|
+
} catch (err) {
|
|
992
|
+
if (err instanceof StorageWriteError) return;
|
|
993
|
+
throw err;
|
|
994
|
+
}
|
|
920
995
|
s.lastValue = next;
|
|
921
|
-
|
|
996
|
+
s.hasUserWrite = true;
|
|
922
997
|
s.settled = this._adapter.ready;
|
|
923
998
|
chunk3YGK3425_cjs.safeCallConfig(this._config.onReset, { key: this.key, scope: this.scope, previousValue: prev });
|
|
924
999
|
this._notifyChange(next, prev);
|
|
@@ -1323,7 +1398,7 @@ function createBase(key, options) {
|
|
|
1323
1398
|
const instance = new StateImpl(key, scope, rKey, adapter, options, config);
|
|
1324
1399
|
if (isSsrMode && !isServer()) {
|
|
1325
1400
|
instance._s.hydrated = afterHydration(() => {
|
|
1326
|
-
if (instance.isDestroyed) return;
|
|
1401
|
+
if (instance.isDestroyed || instance._s.hasUserWrite) return;
|
|
1327
1402
|
const currentValue = instance.get();
|
|
1328
1403
|
if (!shallowEqual(currentValue, options.default)) return;
|
|
1329
1404
|
let realAdapter;
|
package/dist/index.cjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkRB7FJ7WC_cjs = require('./chunk-RB7FJ7WC.cjs');
|
|
4
4
|
var chunk3YGK3425_cjs = require('./chunk-3YGK3425.cjs');
|
|
5
5
|
|
|
6
6
|
// src/collection.ts
|
|
7
7
|
function collection(key, options) {
|
|
8
|
-
const base =
|
|
8
|
+
const base = chunkRB7FJ7WC_cjs.createBase(key, options);
|
|
9
9
|
let watchers;
|
|
10
10
|
let prevItems;
|
|
11
11
|
let unsubscribe;
|
|
@@ -35,8 +35,8 @@ function collection(key, options) {
|
|
|
35
35
|
const prev = prevItems[i];
|
|
36
36
|
const curr = next[i];
|
|
37
37
|
if (prev === curr) continue;
|
|
38
|
-
const p =
|
|
39
|
-
const n =
|
|
38
|
+
const p = chunkRB7FJ7WC_cjs.isRecord(prev) ? prev : void 0;
|
|
39
|
+
const n = chunkRB7FJ7WC_cjs.isRecord(curr) ? curr : void 0;
|
|
40
40
|
if (!p || !n) {
|
|
41
41
|
for (const [, listeners] of w) {
|
|
42
42
|
for (const listener of listeners) {
|
|
@@ -72,7 +72,7 @@ function collection(key, options) {
|
|
|
72
72
|
const baseDestroy = base.destroy;
|
|
73
73
|
const col = base;
|
|
74
74
|
col.watch = (watchKey, listener) => {
|
|
75
|
-
return
|
|
75
|
+
return chunkRB7FJ7WC_cjs.addWatcher(ensureWatchers(), watchKey, listener);
|
|
76
76
|
};
|
|
77
77
|
col.add = (...items) => {
|
|
78
78
|
base.set(base.get().concat(items));
|
|
@@ -153,7 +153,7 @@ function computed(deps, fn, options) {
|
|
|
153
153
|
let cached;
|
|
154
154
|
let isDirty = true;
|
|
155
155
|
let isDestroyed = false;
|
|
156
|
-
const lazyDestroyed =
|
|
156
|
+
const lazyDestroyed = chunkRB7FJ7WC_cjs.createLazyDestroyed();
|
|
157
157
|
const depValues = new Array(deps.length);
|
|
158
158
|
const depLen = deps.length;
|
|
159
159
|
function recompute() {
|
|
@@ -180,7 +180,7 @@ function computed(deps, fn, options) {
|
|
|
180
180
|
};
|
|
181
181
|
const markDirty = () => {
|
|
182
182
|
isDirty = true;
|
|
183
|
-
|
|
183
|
+
chunkRB7FJ7WC_cjs.notify(notifyListeners);
|
|
184
184
|
};
|
|
185
185
|
const unsubscribers = new Array(depLen);
|
|
186
186
|
for (let i = 0; i < depLen; i++) {
|
|
@@ -188,12 +188,12 @@ function computed(deps, fn, options) {
|
|
|
188
188
|
unsubscribers[i] = dep.subscribe(markDirty);
|
|
189
189
|
}
|
|
190
190
|
recompute();
|
|
191
|
-
let readyPromise =
|
|
192
|
-
let hydratedPromise =
|
|
193
|
-
let settledPromise =
|
|
191
|
+
let readyPromise = chunkRB7FJ7WC_cjs.RESOLVED;
|
|
192
|
+
let hydratedPromise = chunkRB7FJ7WC_cjs.RESOLVED;
|
|
193
|
+
let settledPromise = chunkRB7FJ7WC_cjs.RESOLVED;
|
|
194
194
|
let hasAsyncDep = false;
|
|
195
195
|
for (let i = 0; i < depLen; i++) {
|
|
196
|
-
if (deps[i].ready !==
|
|
196
|
+
if (deps[i].ready !== chunkRB7FJ7WC_cjs.RESOLVED) {
|
|
197
197
|
hasAsyncDep = true;
|
|
198
198
|
break;
|
|
199
199
|
}
|
|
@@ -225,7 +225,7 @@ function computed(deps, fn, options) {
|
|
|
225
225
|
return hydratedPromise;
|
|
226
226
|
},
|
|
227
227
|
get destroyed() {
|
|
228
|
-
if (isDestroyed) return
|
|
228
|
+
if (isDestroyed) return chunkRB7FJ7WC_cjs.RESOLVED;
|
|
229
229
|
return lazyDestroyed.promise;
|
|
230
230
|
},
|
|
231
231
|
get isDestroyed() {
|
|
@@ -401,7 +401,7 @@ function withWatch(instance) {
|
|
|
401
401
|
unsubscribe = instance.subscribe((next) => {
|
|
402
402
|
try {
|
|
403
403
|
if (watchers && watchers.size > 0) {
|
|
404
|
-
|
|
404
|
+
chunkRB7FJ7WC_cjs.notifyWatchers(watchers, prev, next);
|
|
405
405
|
}
|
|
406
406
|
} finally {
|
|
407
407
|
prev = next;
|
|
@@ -412,7 +412,7 @@ function withWatch(instance) {
|
|
|
412
412
|
result.watch = (watchKey, listener) => {
|
|
413
413
|
if (!watchers) watchers = /* @__PURE__ */ new Map();
|
|
414
414
|
ensureSubscription();
|
|
415
|
-
return
|
|
415
|
+
return chunkRB7FJ7WC_cjs.addWatcher(watchers, watchKey, listener);
|
|
416
416
|
};
|
|
417
417
|
result.destroy = () => {
|
|
418
418
|
watchers?.clear();
|
|
@@ -442,10 +442,10 @@ function previous(source, options) {
|
|
|
442
442
|
prev = current;
|
|
443
443
|
current = next;
|
|
444
444
|
if (old !== prev) {
|
|
445
|
-
|
|
445
|
+
chunkRB7FJ7WC_cjs.notify(notifyListeners);
|
|
446
446
|
}
|
|
447
447
|
});
|
|
448
|
-
const lazyDestroyed =
|
|
448
|
+
const lazyDestroyed = chunkRB7FJ7WC_cjs.createLazyDestroyed();
|
|
449
449
|
return {
|
|
450
450
|
key: instanceKey,
|
|
451
451
|
scope: "memory",
|
|
@@ -511,7 +511,7 @@ function extractEntry(entry) {
|
|
|
511
511
|
}
|
|
512
512
|
function scopeShortcut(scope, entry, options) {
|
|
513
513
|
const [key, defaultValue] = extractEntry(entry);
|
|
514
|
-
return
|
|
514
|
+
return chunkRB7FJ7WC_cjs.createBase(key, { ...options, default: defaultValue, scope });
|
|
515
515
|
}
|
|
516
516
|
function _state(keyOrEntry, optionsOrDefault, extraOptions) {
|
|
517
517
|
let key;
|
|
@@ -524,7 +524,7 @@ function _state(keyOrEntry, optionsOrDefault, extraOptions) {
|
|
|
524
524
|
key = keyOrEntry;
|
|
525
525
|
options = extraOptions ? { ...extraOptions, default: optionsOrDefault } : optionsOrDefault !== null && typeof optionsOrDefault === "object" && "default" in optionsOrDefault ? optionsOrDefault : { default: optionsOrDefault };
|
|
526
526
|
}
|
|
527
|
-
return
|
|
527
|
+
return chunkRB7FJ7WC_cjs.createBase(key, options);
|
|
528
528
|
}
|
|
529
529
|
_state.local = (e, o) => scopeShortcut("local", e, o);
|
|
530
530
|
_state.session = (e, o) => scopeShortcut("session", e, o);
|
|
@@ -535,39 +535,39 @@ var state = _state;
|
|
|
535
535
|
|
|
536
536
|
Object.defineProperty(exports, "GjendjeError", {
|
|
537
537
|
enumerable: true,
|
|
538
|
-
get: function () { return
|
|
538
|
+
get: function () { return chunkRB7FJ7WC_cjs.GjendjeError; }
|
|
539
539
|
});
|
|
540
540
|
Object.defineProperty(exports, "HydrationError", {
|
|
541
541
|
enumerable: true,
|
|
542
|
-
get: function () { return
|
|
542
|
+
get: function () { return chunkRB7FJ7WC_cjs.HydrationError; }
|
|
543
543
|
});
|
|
544
544
|
Object.defineProperty(exports, "MigrationError", {
|
|
545
545
|
enumerable: true,
|
|
546
|
-
get: function () { return
|
|
546
|
+
get: function () { return chunkRB7FJ7WC_cjs.MigrationError; }
|
|
547
547
|
});
|
|
548
548
|
Object.defineProperty(exports, "StorageReadError", {
|
|
549
549
|
enumerable: true,
|
|
550
|
-
get: function () { return
|
|
550
|
+
get: function () { return chunkRB7FJ7WC_cjs.StorageReadError; }
|
|
551
551
|
});
|
|
552
552
|
Object.defineProperty(exports, "StorageWriteError", {
|
|
553
553
|
enumerable: true,
|
|
554
|
-
get: function () { return
|
|
554
|
+
get: function () { return chunkRB7FJ7WC_cjs.StorageWriteError; }
|
|
555
555
|
});
|
|
556
556
|
Object.defineProperty(exports, "SyncError", {
|
|
557
557
|
enumerable: true,
|
|
558
|
-
get: function () { return
|
|
558
|
+
get: function () { return chunkRB7FJ7WC_cjs.SyncError; }
|
|
559
559
|
});
|
|
560
560
|
Object.defineProperty(exports, "ValidationError", {
|
|
561
561
|
enumerable: true,
|
|
562
|
-
get: function () { return
|
|
562
|
+
get: function () { return chunkRB7FJ7WC_cjs.ValidationError; }
|
|
563
563
|
});
|
|
564
564
|
Object.defineProperty(exports, "batch", {
|
|
565
565
|
enumerable: true,
|
|
566
|
-
get: function () { return
|
|
566
|
+
get: function () { return chunkRB7FJ7WC_cjs.batch; }
|
|
567
567
|
});
|
|
568
568
|
Object.defineProperty(exports, "shallowEqual", {
|
|
569
569
|
enumerable: true,
|
|
570
|
-
get: function () { return
|
|
570
|
+
get: function () { return chunkRB7FJ7WC_cjs.shallowEqual; }
|
|
571
571
|
});
|
|
572
572
|
Object.defineProperty(exports, "configure", {
|
|
573
573
|
enumerable: true,
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { createBase, addWatcher, createLazyDestroyed, RESOLVED, notify, isRecord, notifyWatchers } from './chunk-
|
|
2
|
-
export { GjendjeError, HydrationError, MigrationError, StorageReadError, StorageWriteError, SyncError, ValidationError, batch, shallowEqual } from './chunk-
|
|
1
|
+
import { createBase, addWatcher, createLazyDestroyed, RESOLVED, notify, isRecord, notifyWatchers } from './chunk-JRTZTDT6.js';
|
|
2
|
+
export { GjendjeError, HydrationError, MigrationError, StorageReadError, StorageWriteError, SyncError, ValidationError, batch, shallowEqual } from './chunk-JRTZTDT6.js';
|
|
3
3
|
import { getRegistry, createListeners, safeCall, reportError } from './chunk-YPT6TO4H.js';
|
|
4
4
|
export { configure, destroyAll, resetConfig } from './chunk-YPT6TO4H.js';
|
|
5
5
|
|
package/dist/server.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkRB7FJ7WC_cjs = require('./chunk-RB7FJ7WC.cjs');
|
|
4
4
|
var chunk3YGK3425_cjs = require('./chunk-3YGK3425.cjs');
|
|
5
5
|
var async_hooks = require('async_hooks');
|
|
6
6
|
|
|
@@ -32,7 +32,7 @@ function createServerAdapter(key, defaultValue) {
|
|
|
32
32
|
}
|
|
33
33
|
store.set(key, value);
|
|
34
34
|
lastNotifiedValue = value;
|
|
35
|
-
|
|
35
|
+
chunkRB7FJ7WC_cjs.notify(notifyListeners);
|
|
36
36
|
},
|
|
37
37
|
subscribe: listeners.subscribe,
|
|
38
38
|
destroy() {
|
|
@@ -40,7 +40,7 @@ function createServerAdapter(key, defaultValue) {
|
|
|
40
40
|
}
|
|
41
41
|
};
|
|
42
42
|
}
|
|
43
|
-
|
|
43
|
+
chunkRB7FJ7WC_cjs.registerServerAdapter(createServerAdapter);
|
|
44
44
|
|
|
45
45
|
exports.createServerAdapter = createServerAdapter;
|
|
46
46
|
exports.withServerSession = withServerSession;
|
package/dist/server.js
CHANGED