react-mnemonic 1.3.0 → 1.4.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 +11 -4
- package/dist/bootstrap.cjs +864 -0
- package/dist/bootstrap.cjs.map +1 -0
- package/dist/bootstrap.d.cts +100 -0
- package/dist/bootstrap.d.ts +100 -0
- package/dist/bootstrap.js +857 -0
- package/dist/bootstrap.js.map +1 -0
- package/dist/core.cjs +921 -880
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.cts +3 -3
- package/dist/core.d.ts +3 -3
- package/dist/core.js +922 -881
- package/dist/core.js.map +1 -1
- package/dist/index.cjs +907 -866
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +908 -867
- package/dist/index.js.map +1 -1
- package/dist/{key-Bad1hAKN.d.cts → key-B0_N_rl6.d.cts} +1 -1
- package/dist/{key-QIDPiubI.d.ts → key-yGSxLOVj.d.ts} +1 -1
- package/dist/optional.d.cts +2 -2
- package/dist/optional.d.ts +2 -2
- package/dist/schema.cjs +907 -866
- package/dist/schema.cjs.map +1 -1
- package/dist/schema.d.cts +4 -4
- package/dist/schema.d.ts +4 -4
- package/dist/schema.js +908 -867
- package/dist/schema.js.map +1 -1
- package/dist/{types-DcdXbmVl.d.cts → types-RML7bepB.d.cts} +72 -1
- package/dist/{types-DcdXbmVl.d.ts → types-RML7bepB.d.ts} +72 -1
- package/package.json +10 -1
package/dist/core.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createContext, useMemo, useCallback,
|
|
1
|
+
import { createContext, useMemo, useCallback, useRef, useEffect, useContext, createElement, useState, useSyncExternalStore } from 'react';
|
|
2
2
|
import { jsx } from 'react/jsx-runtime';
|
|
3
3
|
|
|
4
4
|
// src/Mnemonic/provider.tsx
|
|
@@ -412,789 +412,357 @@ var SchemaError = class extends Error {
|
|
|
412
412
|
}
|
|
413
413
|
};
|
|
414
414
|
|
|
415
|
-
// src/Mnemonic/
|
|
416
|
-
function
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
const runtimeProcess = getGlobalProcess();
|
|
421
|
-
if (runtimeProcess?.env?.NODE_ENV !== void 0) {
|
|
422
|
-
return runtimeProcess.env.NODE_ENV;
|
|
415
|
+
// src/Mnemonic/persistence-shared.ts
|
|
416
|
+
function objectHasOwn2(value, property) {
|
|
417
|
+
const hasOwn = Object.hasOwn;
|
|
418
|
+
if (typeof hasOwn === "function") {
|
|
419
|
+
return hasOwn(value, property);
|
|
423
420
|
}
|
|
424
|
-
return void 0;
|
|
425
|
-
}
|
|
426
|
-
function getNativeBrowserStorages() {
|
|
427
|
-
const globalWindow = globalThis.window;
|
|
428
|
-
if (!globalWindow) return [];
|
|
429
|
-
const storages = [];
|
|
430
|
-
const addStorage = (getter) => {
|
|
431
|
-
try {
|
|
432
|
-
storages.push(getter());
|
|
433
|
-
} catch {
|
|
434
|
-
}
|
|
435
|
-
};
|
|
436
|
-
addStorage(() => globalWindow.localStorage);
|
|
437
|
-
addStorage(() => globalWindow.sessionStorage);
|
|
438
|
-
return storages;
|
|
421
|
+
return Object.getOwnPropertyDescriptor(value, property) !== void 0;
|
|
439
422
|
}
|
|
440
|
-
|
|
441
|
-
// src/Mnemonic/use-shared.ts
|
|
442
|
-
var SSR_SNAPSHOT_TOKEN = /* @__PURE__ */ Symbol("mnemonic:ssr-snapshot");
|
|
443
|
-
var diagnosticContractRegistry = /* @__PURE__ */ new WeakMap();
|
|
444
|
-
var diagnosticWarningRegistry = /* @__PURE__ */ new WeakMap();
|
|
445
|
-
var diagnosticObjectIds = /* @__PURE__ */ new WeakMap();
|
|
446
|
-
var nextDiagnosticObjectId = 1;
|
|
447
423
|
function serializeEnvelope(version, payload) {
|
|
448
424
|
return JSON.stringify({
|
|
449
425
|
version,
|
|
450
426
|
payload
|
|
451
427
|
});
|
|
452
428
|
}
|
|
453
|
-
function
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
429
|
+
function parseEnvelope(key, rawText) {
|
|
430
|
+
try {
|
|
431
|
+
const parsed = JSON.parse(rawText);
|
|
432
|
+
if (typeof parsed !== "object" || parsed == null || !Number.isInteger(parsed.version) || parsed.version < 0 || !objectHasOwn2(parsed, "payload")) {
|
|
433
|
+
throw new SchemaError("INVALID_ENVELOPE", `Invalid envelope for key "${key}"`);
|
|
434
|
+
}
|
|
435
|
+
return parsed;
|
|
436
|
+
} catch (error) {
|
|
437
|
+
if (error instanceof SchemaError) {
|
|
438
|
+
throw error;
|
|
439
|
+
}
|
|
440
|
+
throw new SchemaError("INVALID_ENVELOPE", `Invalid envelope for key "${key}"`, error);
|
|
457
441
|
}
|
|
458
|
-
if (rewriteRaw !== void 0) result.rewriteRaw = rewriteRaw;
|
|
459
|
-
return result;
|
|
460
442
|
}
|
|
461
|
-
function
|
|
462
|
-
|
|
443
|
+
function decodeStringPayload(key, payload, codec) {
|
|
444
|
+
try {
|
|
445
|
+
return codec.decode(payload);
|
|
446
|
+
} catch (error) {
|
|
447
|
+
throw error instanceof CodecError ? error : new CodecError(`Codec decode failed for key "${key}"`, error);
|
|
448
|
+
}
|
|
463
449
|
}
|
|
464
|
-
function
|
|
465
|
-
|
|
466
|
-
if (
|
|
467
|
-
|
|
468
|
-
diagnosticWarningRegistry.set(api, warnings);
|
|
450
|
+
function validateAgainstSchema(key, value, jsonSchema) {
|
|
451
|
+
const errors = validateJsonSchema(value, jsonSchema);
|
|
452
|
+
if (errors.length === 0) {
|
|
453
|
+
return;
|
|
469
454
|
}
|
|
470
|
-
|
|
455
|
+
const message = errors.map((entry) => `${entry.path || "/"}: ${entry.message}`).join("; ");
|
|
456
|
+
throw new SchemaError("TYPE_MISMATCH", `Schema validation failed for key "${key}": ${message}`);
|
|
471
457
|
}
|
|
472
|
-
function
|
|
473
|
-
|
|
474
|
-
if (warnings.has(id)) return;
|
|
475
|
-
warnings.add(id);
|
|
476
|
-
console.warn(message);
|
|
458
|
+
function getLatestSchema(schemaRegistry, key) {
|
|
459
|
+
return schemaRegistry?.getLatestSchema(key);
|
|
477
460
|
}
|
|
478
|
-
function
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
461
|
+
function getSchemaForVersion(schemaRegistry, key, version) {
|
|
462
|
+
return schemaRegistry?.getSchema(key, version);
|
|
463
|
+
}
|
|
464
|
+
function getMigrationPath(schemaRegistry, key, fromVersion, toVersion) {
|
|
465
|
+
return schemaRegistry?.getMigrationPath(key, fromVersion, toVersion) ?? null;
|
|
466
|
+
}
|
|
467
|
+
function resolveTargetWriteSchema({
|
|
468
|
+
key,
|
|
469
|
+
explicitVersion,
|
|
470
|
+
schemaMode,
|
|
471
|
+
schemaRegistry
|
|
472
|
+
}) {
|
|
473
|
+
const latestSchema = getLatestSchema(schemaRegistry, key);
|
|
474
|
+
if (explicitVersion === void 0) {
|
|
475
|
+
return latestSchema;
|
|
483
476
|
}
|
|
484
|
-
|
|
485
|
-
if (
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
477
|
+
const explicitSchema = getSchemaForVersion(schemaRegistry, key, explicitVersion);
|
|
478
|
+
if (explicitSchema) {
|
|
479
|
+
return explicitSchema;
|
|
480
|
+
}
|
|
481
|
+
return schemaMode === "strict" ? void 0 : latestSchema;
|
|
482
|
+
}
|
|
483
|
+
function encodePersistedValueForWrite({
|
|
484
|
+
key,
|
|
485
|
+
nextValue,
|
|
486
|
+
codec,
|
|
487
|
+
explicitVersion,
|
|
488
|
+
schemaMode,
|
|
489
|
+
schemaRegistry
|
|
490
|
+
}) {
|
|
491
|
+
const targetSchema = resolveTargetWriteSchema({
|
|
492
|
+
key,
|
|
493
|
+
explicitVersion,
|
|
494
|
+
schemaMode,
|
|
495
|
+
schemaRegistry
|
|
496
|
+
});
|
|
497
|
+
if (!targetSchema) {
|
|
498
|
+
if (explicitVersion !== void 0 && schemaMode === "strict") {
|
|
499
|
+
throw new SchemaError("WRITE_SCHEMA_REQUIRED", `Write requires schema for key "${key}" in strict mode`);
|
|
500
|
+
}
|
|
501
|
+
return serializeEnvelope(0, codec.encode(nextValue));
|
|
502
|
+
}
|
|
503
|
+
let valueToStore = nextValue;
|
|
504
|
+
const writeMigration = schemaRegistry?.getWriteMigration?.(key, targetSchema.version);
|
|
505
|
+
if (writeMigration) {
|
|
506
|
+
try {
|
|
507
|
+
valueToStore = writeMigration.migrate(valueToStore);
|
|
508
|
+
} catch (error) {
|
|
509
|
+
throw error instanceof SchemaError ? error : new SchemaError("MIGRATION_FAILED", `Write-time migration failed for key "${key}"`, error);
|
|
493
510
|
}
|
|
494
|
-
return tag;
|
|
495
511
|
}
|
|
512
|
+
validateAgainstSchema(key, valueToStore, targetSchema.schema);
|
|
513
|
+
return serializeEnvelope(targetSchema.version, valueToStore);
|
|
496
514
|
}
|
|
497
|
-
|
|
498
|
-
|
|
515
|
+
|
|
516
|
+
// src/Mnemonic/optional-bridge-adapter.ts
|
|
517
|
+
function resolveOptionalDefaultValue(defaultValue) {
|
|
518
|
+
return typeof defaultValue === "function" ? defaultValue() : defaultValue;
|
|
499
519
|
}
|
|
500
|
-
function
|
|
501
|
-
|
|
502
|
-
if (typeof hasOwn === "function") {
|
|
503
|
-
return hasOwn(value, property);
|
|
504
|
-
}
|
|
505
|
-
return Object.getOwnPropertyDescriptor(value, property) !== void 0;
|
|
520
|
+
function getSchemaCapabilities(schemaRegistry) {
|
|
521
|
+
return schemaRegistry !== void 0;
|
|
506
522
|
}
|
|
507
|
-
function
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
diagnosticObjectIds.set(value, id);
|
|
512
|
-
return id;
|
|
523
|
+
function buildFallbackResult(options) {
|
|
524
|
+
return {
|
|
525
|
+
value: resolveOptionalDefaultValue(options.defaultValue)
|
|
526
|
+
};
|
|
513
527
|
}
|
|
514
|
-
function
|
|
515
|
-
|
|
516
|
-
key,
|
|
517
|
-
defaultValue,
|
|
518
|
-
codecOpt,
|
|
519
|
-
schemaVersion,
|
|
520
|
-
reconcile,
|
|
521
|
-
listenCrossTab,
|
|
522
|
-
ssrOptions
|
|
523
|
-
}) {
|
|
524
|
-
const codecSignature = codecOpt == null || !isObjectLike(codecOpt) ? "default-json-codec" : `codec:${stableDiagnosticValue(codecOpt.encode)}:${stableDiagnosticValue(codecOpt.decode)}`;
|
|
525
|
-
const reconcileSignature = reconcile == null || !isObjectLike(reconcile) ? "no-reconcile" : `reconcile:${stableDiagnosticValue(reconcile)}`;
|
|
526
|
-
return JSON.stringify({
|
|
528
|
+
function encodeValueForWrite(key, nextValue, options, schemaMode, schemaRegistry) {
|
|
529
|
+
return encodePersistedValueForWrite({
|
|
527
530
|
key,
|
|
528
|
-
|
|
529
|
-
codec:
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
ssrHydration: ssrOptions?.hydration ?? null,
|
|
534
|
-
hasServerValue: ssrOptions?.serverValue !== void 0,
|
|
535
|
-
providerHydration: api.ssrHydration ?? null
|
|
531
|
+
nextValue,
|
|
532
|
+
codec: options.codec ?? JSONCodec,
|
|
533
|
+
explicitVersion: options.schema?.version,
|
|
534
|
+
schemaMode,
|
|
535
|
+
schemaRegistry
|
|
536
536
|
});
|
|
537
537
|
}
|
|
538
|
-
function
|
|
539
|
-
if (typeof
|
|
540
|
-
return
|
|
538
|
+
function decodeCodecManagedEnvelope(key, envelope, options) {
|
|
539
|
+
if (typeof envelope.payload !== "string") {
|
|
540
|
+
return {
|
|
541
|
+
value: envelope.payload
|
|
542
|
+
};
|
|
541
543
|
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
+
return {
|
|
545
|
+
value: decodeStringPayload(key, envelope.payload, options.codec ?? JSONCodec)
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
function decodeAutoschemaEnvelope(key, envelope, options, schemaRegistry) {
|
|
549
|
+
if (!schemaRegistry?.registerSchema) {
|
|
550
|
+
throw new SchemaError(
|
|
551
|
+
"MODE_CONFIGURATION_INVALID",
|
|
552
|
+
`Autoschema mode requires schema registry registration for key "${key}"`
|
|
553
|
+
);
|
|
544
554
|
}
|
|
555
|
+
const decoded = typeof envelope.payload === "string" ? decodeStringPayload(key, envelope.payload, options.codec ?? JSONCodec) : envelope.payload;
|
|
556
|
+
const pendingSchema = {
|
|
557
|
+
key,
|
|
558
|
+
version: 1,
|
|
559
|
+
schema: inferJsonSchema(decoded)
|
|
560
|
+
};
|
|
545
561
|
return {
|
|
546
|
-
|
|
547
|
-
|
|
562
|
+
value: decoded,
|
|
563
|
+
rewriteRaw: serializeEnvelope(pendingSchema.version, decoded),
|
|
564
|
+
pendingSchema
|
|
548
565
|
};
|
|
549
566
|
}
|
|
550
|
-
function
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
const
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
() => developmentRuntime ? buildContractFingerprint({
|
|
570
|
-
api,
|
|
571
|
-
key,
|
|
572
|
-
defaultValue,
|
|
573
|
-
codecOpt,
|
|
574
|
-
...{} ,
|
|
575
|
-
reconcile,
|
|
576
|
-
listenCrossTab,
|
|
577
|
-
ssrOptions
|
|
578
|
-
}) : null,
|
|
579
|
-
[
|
|
580
|
-
developmentRuntime,
|
|
581
|
-
api,
|
|
582
|
-
key,
|
|
583
|
-
defaultValue,
|
|
584
|
-
codecOpt,
|
|
585
|
-
schemaVersion,
|
|
586
|
-
reconcile,
|
|
587
|
-
listenCrossTab,
|
|
588
|
-
ssrOptions?.hydration,
|
|
589
|
-
ssrOptions?.serverValue
|
|
590
|
-
]
|
|
591
|
-
);
|
|
592
|
-
const getFallback = useCallback(
|
|
593
|
-
(error) => typeof defaultValue === "function" ? defaultValue(error) : defaultValue,
|
|
594
|
-
[defaultValue]
|
|
595
|
-
);
|
|
596
|
-
const getServerValue = useCallback(() => {
|
|
597
|
-
const serverValue = ssrOptions?.serverValue;
|
|
598
|
-
if (serverValue === void 0) {
|
|
599
|
-
return getFallback();
|
|
600
|
-
}
|
|
601
|
-
return typeof serverValue === "function" ? serverValue() : serverValue;
|
|
602
|
-
}, [getFallback, ssrOptions?.serverValue]);
|
|
603
|
-
const parseEnvelope2 = useCallback(
|
|
604
|
-
(rawText) => {
|
|
605
|
-
try {
|
|
606
|
-
const parsed = JSON.parse(rawText);
|
|
607
|
-
if (typeof parsed !== "object" || parsed == null || !Number.isInteger(parsed.version) || parsed.version < 0 || !objectHasOwn2(parsed, "payload")) {
|
|
608
|
-
return {
|
|
609
|
-
ok: false,
|
|
610
|
-
error: new SchemaError("INVALID_ENVELOPE", `Invalid envelope for key "${key}"`)
|
|
611
|
-
};
|
|
612
|
-
}
|
|
613
|
-
return { ok: true, envelope: parsed };
|
|
614
|
-
} catch (err) {
|
|
615
|
-
return {
|
|
616
|
-
ok: false,
|
|
617
|
-
error: new SchemaError("INVALID_ENVELOPE", `Invalid envelope for key "${key}"`, err)
|
|
618
|
-
};
|
|
619
|
-
}
|
|
620
|
-
},
|
|
621
|
-
[key]
|
|
622
|
-
);
|
|
623
|
-
const decodeStringPayload2 = useCallback(
|
|
624
|
-
(payload, activeCodec) => {
|
|
625
|
-
try {
|
|
626
|
-
return activeCodec.decode(payload);
|
|
627
|
-
} catch (err) {
|
|
628
|
-
throw err instanceof CodecError ? err : new CodecError(`Codec decode failed for key "${key}"`, err);
|
|
629
|
-
}
|
|
630
|
-
},
|
|
631
|
-
[key]
|
|
632
|
-
);
|
|
633
|
-
const buildFallbackResult2 = useCallback(
|
|
634
|
-
(error, extra) => {
|
|
635
|
-
return withReadMetadata(getFallback(error), void 0, extra);
|
|
636
|
-
},
|
|
637
|
-
[getFallback]
|
|
638
|
-
);
|
|
567
|
+
function decodeSchemaManagedEnvelope(key, envelope, schemaForVersion, latestSchema, schemaRegistry) {
|
|
568
|
+
let current = envelope.payload;
|
|
569
|
+
validateAgainstSchema(key, current, schemaForVersion.schema);
|
|
570
|
+
if (!latestSchema || envelope.version >= latestSchema.version) {
|
|
571
|
+
return {
|
|
572
|
+
value: current
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
const path = getMigrationPath(schemaRegistry, key, envelope.version, latestSchema.version);
|
|
576
|
+
if (!path) {
|
|
577
|
+
throw new SchemaError(
|
|
578
|
+
"MIGRATION_PATH_NOT_FOUND",
|
|
579
|
+
`No migration path for key "${key}" from v${envelope.version} to v${latestSchema.version}`
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
for (const step of path) {
|
|
583
|
+
current = step.migrate(current);
|
|
584
|
+
}
|
|
585
|
+
validateAgainstSchema(key, current, latestSchema.schema);
|
|
639
586
|
return {
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
codec,
|
|
643
|
-
codecOpt,
|
|
644
|
-
schema,
|
|
645
|
-
reconcile,
|
|
646
|
-
onMount,
|
|
647
|
-
onChange,
|
|
648
|
-
listenCrossTab,
|
|
649
|
-
getFallback,
|
|
650
|
-
getServerValue,
|
|
651
|
-
parseEnvelope: parseEnvelope2,
|
|
652
|
-
decodeStringPayload: decodeStringPayload2,
|
|
653
|
-
buildFallbackResult: buildFallbackResult2,
|
|
654
|
-
developmentRuntime,
|
|
655
|
-
contractFingerprint,
|
|
656
|
-
hasMounted,
|
|
657
|
-
setHasMounted,
|
|
658
|
-
hydrationMode,
|
|
659
|
-
ssrOptions
|
|
587
|
+
value: current,
|
|
588
|
+
rewriteRaw: serializeEnvelope(latestSchema.version, current)
|
|
660
589
|
};
|
|
661
590
|
}
|
|
662
|
-
function
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
591
|
+
function decodePersistedValue(key, raw, options, api, schemaRegistry) {
|
|
592
|
+
if (raw == null) {
|
|
593
|
+
return buildFallbackResult(options);
|
|
594
|
+
}
|
|
595
|
+
const envelope = parseEnvelope(key, raw);
|
|
596
|
+
const latestSchema = getLatestSchema(schemaRegistry, key);
|
|
597
|
+
const schemaForVersion = getSchemaForVersion(schemaRegistry, key, envelope.version);
|
|
598
|
+
if (api.schemaMode === "strict" && !schemaForVersion) {
|
|
599
|
+
throw new SchemaError("SCHEMA_NOT_FOUND", `No schema for key "${key}" v${envelope.version}`);
|
|
600
|
+
}
|
|
601
|
+
if (api.schemaMode === "autoschema" && !schemaForVersion) {
|
|
602
|
+
return decodeAutoschemaEnvelope(key, envelope, options, schemaRegistry);
|
|
603
|
+
}
|
|
604
|
+
if (!schemaForVersion) {
|
|
605
|
+
return decodeCodecManagedEnvelope(key, envelope, options);
|
|
606
|
+
}
|
|
607
|
+
return decodeSchemaManagedEnvelope(key, envelope, schemaForVersion, latestSchema, schemaRegistry);
|
|
608
|
+
}
|
|
609
|
+
function createMnemonicOptionalBridge({
|
|
610
|
+
api,
|
|
611
|
+
schemaRegistry
|
|
666
612
|
}) {
|
|
667
|
-
return
|
|
668
|
-
(
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
const context = {
|
|
681
|
-
key,
|
|
682
|
-
persistedVersion
|
|
683
|
-
};
|
|
684
|
-
if (latestVersion !== void 0) {
|
|
685
|
-
context.latestVersion = latestVersion;
|
|
613
|
+
return {
|
|
614
|
+
namespace: api.prefix.endsWith(".") ? api.prefix.slice(0, -1) : api.prefix,
|
|
615
|
+
capabilities: {
|
|
616
|
+
persistence: true,
|
|
617
|
+
schema: getSchemaCapabilities(schemaRegistry)
|
|
618
|
+
},
|
|
619
|
+
subscribeRaw: (key, listener) => api.subscribeRaw(key, listener),
|
|
620
|
+
getRawSnapshot: (key) => api.getRawSnapshot(key),
|
|
621
|
+
decodeSnapshot: (key, raw, options) => {
|
|
622
|
+
try {
|
|
623
|
+
return decodePersistedValue(key, raw, options, api, schemaRegistry);
|
|
624
|
+
} catch {
|
|
625
|
+
return buildFallbackResult(options);
|
|
686
626
|
}
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
return serializeForPersist(value);
|
|
690
|
-
} catch {
|
|
691
|
-
return rewriteRaw;
|
|
692
|
-
}
|
|
693
|
-
})();
|
|
627
|
+
},
|
|
628
|
+
setValue: (key, nextValue, options) => {
|
|
694
629
|
try {
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
630
|
+
api.setRaw(key, encodeValueForWrite(key, nextValue, options, api.schemaMode, schemaRegistry));
|
|
631
|
+
} catch (error) {
|
|
632
|
+
if (error instanceof SchemaError) {
|
|
633
|
+
console.error(`[Mnemonic] Schema error for key "${key}" (${error.code}):`, error.message);
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
if (error instanceof CodecError) {
|
|
637
|
+
console.error(`[Mnemonic] Codec error for key "${key}":`, error.message);
|
|
638
|
+
return;
|
|
639
|
+
}
|
|
640
|
+
throw error;
|
|
703
641
|
}
|
|
704
642
|
},
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
listenCrossTab,
|
|
717
|
-
getFallback,
|
|
718
|
-
getServerValue,
|
|
719
|
-
developmentRuntime,
|
|
720
|
-
contractFingerprint,
|
|
721
|
-
hasMounted,
|
|
722
|
-
setHasMounted,
|
|
723
|
-
hydrationMode,
|
|
724
|
-
ssrOptions
|
|
725
|
-
} = shared;
|
|
726
|
-
const { decodeForRead, encodeForWrite, active = true, additionalDevWarnings, onDecodedEffect } = config;
|
|
727
|
-
const getServerRawSnapshot = useCallback(
|
|
728
|
-
() => ssrOptions?.serverValue === void 0 ? null : SSR_SNAPSHOT_TOKEN,
|
|
729
|
-
[ssrOptions?.serverValue]
|
|
730
|
-
);
|
|
731
|
-
const deferStorageRead = hydrationMode === "client-only" && !hasMounted;
|
|
732
|
-
const subscribe = useCallback(
|
|
733
|
-
(listener) => {
|
|
734
|
-
if (!active) {
|
|
735
|
-
return () => void 0;
|
|
643
|
+
removeValue: (key) => {
|
|
644
|
+
api.removeRaw(key);
|
|
645
|
+
},
|
|
646
|
+
commitSnapshot: (key, raw, snapshot) => {
|
|
647
|
+
if (snapshot.pendingSchema && schemaRegistry?.registerSchema) {
|
|
648
|
+
if (!schemaRegistry.getSchema(snapshot.pendingSchema.key, snapshot.pendingSchema.version)) {
|
|
649
|
+
try {
|
|
650
|
+
schemaRegistry.registerSchema(snapshot.pendingSchema);
|
|
651
|
+
} catch {
|
|
652
|
+
}
|
|
653
|
+
}
|
|
736
654
|
}
|
|
737
|
-
if (
|
|
738
|
-
|
|
655
|
+
if (snapshot.rewriteRaw !== void 0 && snapshot.rewriteRaw !== raw) {
|
|
656
|
+
api.setRaw(key, snapshot.rewriteRaw);
|
|
739
657
|
}
|
|
740
|
-
return api.subscribeRaw(key, listener);
|
|
741
|
-
},
|
|
742
|
-
[active, api, deferStorageRead, key]
|
|
743
|
-
);
|
|
744
|
-
const raw = useSyncExternalStore(
|
|
745
|
-
subscribe,
|
|
746
|
-
() => active && !deferStorageRead ? api.getRawSnapshot(key) : getServerRawSnapshot(),
|
|
747
|
-
getServerRawSnapshot
|
|
748
|
-
);
|
|
749
|
-
const decoded = useMemo(() => {
|
|
750
|
-
if (raw === SSR_SNAPSHOT_TOKEN) {
|
|
751
|
-
return withReadMetadata(getServerValue());
|
|
752
658
|
}
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
var MnemonicOptionalBridgeContext = createContext(null);
|
|
662
|
+
|
|
663
|
+
// src/Mnemonic/optional-bridge-provider.tsx
|
|
664
|
+
function MnemonicOptionalBridgeProvider({
|
|
665
|
+
bridge,
|
|
666
|
+
children
|
|
667
|
+
}) {
|
|
668
|
+
return createElement(MnemonicOptionalBridgeContext.Provider, { value: bridge }, children);
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
// src/Mnemonic/runtime.ts
|
|
672
|
+
function getGlobalProcess() {
|
|
673
|
+
return globalThis.process;
|
|
674
|
+
}
|
|
675
|
+
function getRuntimeNodeEnv() {
|
|
676
|
+
const runtimeProcess = getGlobalProcess();
|
|
677
|
+
if (runtimeProcess?.env?.NODE_ENV !== void 0) {
|
|
678
|
+
return runtimeProcess.env.NODE_ENV;
|
|
679
|
+
}
|
|
680
|
+
return void 0;
|
|
681
|
+
}
|
|
682
|
+
function getDefaultBrowserStorage() {
|
|
683
|
+
const globalWindow = globalThis.window;
|
|
684
|
+
if (globalWindow === void 0) return void 0;
|
|
685
|
+
try {
|
|
686
|
+
return globalWindow.localStorage;
|
|
687
|
+
} catch {
|
|
688
|
+
return void 0;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
function getNativeBrowserStorages() {
|
|
692
|
+
const globalWindow = globalThis.window;
|
|
693
|
+
if (!globalWindow) return [];
|
|
694
|
+
const storages = [];
|
|
695
|
+
const addStorage = (getter) => {
|
|
696
|
+
try {
|
|
697
|
+
storages.push(getter());
|
|
698
|
+
} catch {
|
|
766
699
|
}
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
codecOpt,
|
|
772
|
-
schema,
|
|
773
|
-
warnOnce: (id, message) => warnOnce(api, id, message)
|
|
774
|
-
});
|
|
775
|
-
let keyContracts = diagnosticContractRegistry.get(api);
|
|
776
|
-
if (!keyContracts) {
|
|
777
|
-
keyContracts = /* @__PURE__ */ new Map();
|
|
778
|
-
diagnosticContractRegistry.set(api, keyContracts);
|
|
779
|
-
}
|
|
780
|
-
if (contractFingerprint === null) {
|
|
781
|
-
return;
|
|
782
|
-
}
|
|
783
|
-
const previousContract = keyContracts.get(key);
|
|
784
|
-
if (previousContract === void 0) {
|
|
785
|
-
keyContracts.set(key, contractFingerprint);
|
|
786
|
-
return;
|
|
787
|
-
}
|
|
788
|
-
if (previousContract === contractFingerprint) {
|
|
789
|
-
return;
|
|
790
|
-
}
|
|
791
|
-
warnOnce(
|
|
792
|
-
api,
|
|
793
|
-
`contract-conflict:${key}`,
|
|
794
|
-
`[Mnemonic] Conflicting useMnemonicKey contracts detected for key "${key}" in namespace "${api.prefix.slice(0, -1)}". Reuse a shared descriptor with defineMnemonicKey(...) or align defaultValue/codec/schema/reconcile options so every consumer describes the same persisted contract.`
|
|
795
|
-
);
|
|
796
|
-
}, [
|
|
797
|
-
active,
|
|
798
|
-
additionalDevWarnings,
|
|
799
|
-
api,
|
|
800
|
-
key,
|
|
801
|
-
developmentRuntime,
|
|
802
|
-
contractFingerprint,
|
|
803
|
-
listenCrossTab,
|
|
804
|
-
codecOpt,
|
|
805
|
-
schema,
|
|
806
|
-
api.crossTabSyncMode
|
|
807
|
-
]);
|
|
808
|
-
useEffect(() => {
|
|
809
|
-
if (hasMounted) return;
|
|
810
|
-
setHasMounted(true);
|
|
811
|
-
}, [hasMounted, setHasMounted]);
|
|
812
|
-
useEffect(() => {
|
|
813
|
-
if (!active) return;
|
|
814
|
-
if (decoded.rewriteRaw && decoded.rewriteRaw !== raw) {
|
|
815
|
-
api.setRaw(key, decoded.rewriteRaw);
|
|
816
|
-
}
|
|
817
|
-
}, [active, api, decoded.rewriteRaw, key, raw]);
|
|
818
|
-
useEffect(() => {
|
|
819
|
-
if (!active) return;
|
|
820
|
-
onDecodedEffect?.(decoded);
|
|
821
|
-
}, [active, decoded, onDecodedEffect]);
|
|
822
|
-
const prevRef = useRef(value);
|
|
823
|
-
const mounted = useRef(false);
|
|
824
|
-
useEffect(() => {
|
|
825
|
-
if (!active) return;
|
|
826
|
-
if (mounted.current) return;
|
|
827
|
-
mounted.current = true;
|
|
828
|
-
onMount?.(value);
|
|
829
|
-
prevRef.current = value;
|
|
830
|
-
}, [active]);
|
|
831
|
-
useEffect(() => {
|
|
832
|
-
if (!active) return;
|
|
833
|
-
const prev = prevRef.current;
|
|
834
|
-
if (Object.is(prev, value)) return;
|
|
835
|
-
prevRef.current = value;
|
|
836
|
-
onChange?.(value, prev);
|
|
837
|
-
}, [active, value, onChange]);
|
|
838
|
-
useEffect(() => {
|
|
839
|
-
if (!active) return;
|
|
840
|
-
if (!listenCrossTab) return;
|
|
841
|
-
const globalWindow = globalThis.window;
|
|
842
|
-
if (globalWindow === void 0) return;
|
|
843
|
-
const storageKey = api.prefix + key;
|
|
844
|
-
const handler = (e) => {
|
|
845
|
-
if (e.key === null) {
|
|
846
|
-
api.removeRaw(key);
|
|
847
|
-
return;
|
|
848
|
-
}
|
|
849
|
-
if (e.key !== storageKey) return;
|
|
850
|
-
if (e.newValue == null) {
|
|
851
|
-
api.removeRaw(key);
|
|
852
|
-
return;
|
|
853
|
-
}
|
|
854
|
-
api.setRaw(key, e.newValue);
|
|
855
|
-
};
|
|
856
|
-
globalWindow.addEventListener("storage", handler);
|
|
857
|
-
return () => globalWindow.removeEventListener("storage", handler);
|
|
858
|
-
}, [active, listenCrossTab, api, key]);
|
|
859
|
-
const set = useMemo(() => {
|
|
860
|
-
if (!active) {
|
|
861
|
-
return () => void 0;
|
|
862
|
-
}
|
|
863
|
-
return (next) => {
|
|
864
|
-
const nextVal = typeof next === "function" ? next(decodeForRead(api.getRawSnapshot(key)).value) : next;
|
|
865
|
-
try {
|
|
866
|
-
const encoded = encodeForWrite(nextVal);
|
|
867
|
-
api.setRaw(key, encoded);
|
|
868
|
-
} catch (err) {
|
|
869
|
-
if (err instanceof SchemaError) {
|
|
870
|
-
console.error(`[Mnemonic] Schema error for key "${key}" (${err.code}):`, err.message);
|
|
871
|
-
return;
|
|
872
|
-
}
|
|
873
|
-
if (err instanceof CodecError) {
|
|
874
|
-
console.error(`[Mnemonic] Codec encode error for key "${key}":`, err.message);
|
|
875
|
-
return;
|
|
876
|
-
}
|
|
877
|
-
console.error(`[Mnemonic] Failed to persist key "${key}":`, err);
|
|
878
|
-
}
|
|
879
|
-
};
|
|
880
|
-
}, [active, api, key, decodeForRead, encodeForWrite]);
|
|
881
|
-
const reset = useMemo(() => {
|
|
882
|
-
if (!active) {
|
|
883
|
-
return () => void 0;
|
|
884
|
-
}
|
|
885
|
-
return () => {
|
|
886
|
-
const v = getFallback();
|
|
887
|
-
try {
|
|
888
|
-
const encoded = encodeForWrite(v);
|
|
889
|
-
api.setRaw(key, encoded);
|
|
890
|
-
} catch (err) {
|
|
891
|
-
if (err instanceof SchemaError) {
|
|
892
|
-
console.error(`[Mnemonic] Schema error for key "${key}" (${err.code}):`, err.message);
|
|
893
|
-
return;
|
|
894
|
-
}
|
|
895
|
-
if (err instanceof CodecError) {
|
|
896
|
-
console.error(`[Mnemonic] Codec encode error for key "${key}":`, err.message);
|
|
897
|
-
}
|
|
898
|
-
return;
|
|
899
|
-
}
|
|
900
|
-
};
|
|
901
|
-
}, [active, api, key, getFallback, encodeForWrite]);
|
|
902
|
-
const remove = useMemo(() => {
|
|
903
|
-
if (!active) {
|
|
904
|
-
return () => void 0;
|
|
905
|
-
}
|
|
906
|
-
return () => api.removeRaw(key);
|
|
907
|
-
}, [active, api, key]);
|
|
908
|
-
return useMemo(
|
|
909
|
-
() => ({
|
|
910
|
-
value,
|
|
911
|
-
set,
|
|
912
|
-
reset,
|
|
913
|
-
remove
|
|
914
|
-
}),
|
|
915
|
-
[value, set, reset, remove]
|
|
916
|
-
);
|
|
917
|
-
}
|
|
918
|
-
|
|
919
|
-
// src/Mnemonic/optional-bridge-adapter.ts
|
|
920
|
-
function resolveOptionalDefaultValue(defaultValue) {
|
|
921
|
-
return typeof defaultValue === "function" ? defaultValue() : defaultValue;
|
|
700
|
+
};
|
|
701
|
+
addStorage(() => globalWindow.localStorage);
|
|
702
|
+
addStorage(() => globalWindow.sessionStorage);
|
|
703
|
+
return storages;
|
|
922
704
|
}
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
705
|
+
var MnemonicContext = createContext(null);
|
|
706
|
+
var warnedNestedProviderStores = /* @__PURE__ */ new WeakSet();
|
|
707
|
+
function useMnemonic() {
|
|
708
|
+
const context = useMnemonicOptional();
|
|
709
|
+
if (!context) {
|
|
710
|
+
throw new Error("useMnemonic must be used within a MnemonicProvider");
|
|
927
711
|
}
|
|
928
|
-
return
|
|
712
|
+
return context;
|
|
929
713
|
}
|
|
930
|
-
function
|
|
931
|
-
|
|
932
|
-
const parsed = JSON.parse(rawText);
|
|
933
|
-
if (typeof parsed !== "object" || parsed == null || !Number.isInteger(parsed.version) || parsed.version < 0 || !objectHasOwn3(parsed, "payload")) {
|
|
934
|
-
throw new SchemaError("INVALID_ENVELOPE", `Invalid envelope for key "${key}"`);
|
|
935
|
-
}
|
|
936
|
-
return parsed;
|
|
937
|
-
} catch (error) {
|
|
938
|
-
if (error instanceof SchemaError) {
|
|
939
|
-
throw error;
|
|
940
|
-
}
|
|
941
|
-
throw new SchemaError("INVALID_ENVELOPE", `Invalid envelope for key "${key}"`, error);
|
|
942
|
-
}
|
|
714
|
+
function useMnemonicOptional() {
|
|
715
|
+
return useContext(MnemonicContext);
|
|
943
716
|
}
|
|
944
|
-
function
|
|
945
|
-
|
|
717
|
+
function detectEnumerableStorage(storage) {
|
|
718
|
+
if (!storage) return false;
|
|
946
719
|
try {
|
|
947
|
-
return
|
|
948
|
-
} catch
|
|
949
|
-
|
|
720
|
+
return typeof storage.length === "number" && typeof storage.key === "function";
|
|
721
|
+
} catch {
|
|
722
|
+
return false;
|
|
950
723
|
}
|
|
951
724
|
}
|
|
952
|
-
function
|
|
953
|
-
const
|
|
954
|
-
if (
|
|
955
|
-
return;
|
|
725
|
+
function isProductionRuntime() {
|
|
726
|
+
const env = getRuntimeNodeEnv();
|
|
727
|
+
if (env === void 0) {
|
|
728
|
+
return true;
|
|
956
729
|
}
|
|
957
|
-
|
|
958
|
-
throw new SchemaError("TYPE_MISMATCH", `Schema validation failed for key "${key}": ${message}`);
|
|
730
|
+
return env === "production";
|
|
959
731
|
}
|
|
960
|
-
function
|
|
961
|
-
|
|
732
|
+
function weakRefConstructor() {
|
|
733
|
+
const ctor = globalThis.WeakRef;
|
|
734
|
+
return typeof ctor === "function" ? ctor : null;
|
|
962
735
|
}
|
|
963
|
-
function
|
|
964
|
-
return
|
|
965
|
-
value: resolveOptionalDefaultValue(options.defaultValue)
|
|
966
|
-
};
|
|
736
|
+
function hasFinalizationRegistry() {
|
|
737
|
+
return typeof globalThis.FinalizationRegistry === "function";
|
|
967
738
|
}
|
|
968
|
-
function
|
|
969
|
-
|
|
739
|
+
function isPromiseLike(value) {
|
|
740
|
+
if (value == null) return false;
|
|
741
|
+
if (typeof value !== "object" && typeof value !== "function") return false;
|
|
742
|
+
return typeof value.then === "function";
|
|
970
743
|
}
|
|
971
|
-
function
|
|
972
|
-
|
|
744
|
+
function getCrossTabSyncMode(requestedStorage, activeStorage) {
|
|
745
|
+
const isExplicitNativeBrowserStorage = activeStorage !== void 0 && requestedStorage !== void 0 && getNativeBrowserStorages().includes(activeStorage);
|
|
746
|
+
if (requestedStorage === void 0 && activeStorage !== void 0 || isExplicitNativeBrowserStorage) {
|
|
747
|
+
return "browser-storage-event";
|
|
748
|
+
}
|
|
749
|
+
if (typeof activeStorage?.onExternalChange === "function") {
|
|
750
|
+
return "custom-external-change";
|
|
751
|
+
}
|
|
752
|
+
return "none";
|
|
973
753
|
}
|
|
974
|
-
function
|
|
975
|
-
return
|
|
754
|
+
function getDevToolsWindow() {
|
|
755
|
+
return globalThis.window;
|
|
976
756
|
}
|
|
977
|
-
function
|
|
978
|
-
const
|
|
979
|
-
const
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
return serializeEnvelope(0, codec.encode(nextValue));
|
|
984
|
-
}
|
|
985
|
-
let valueToStore = nextValue;
|
|
986
|
-
const writeMigration = schemaRegistry?.getWriteMigration?.(key, targetSchema.version);
|
|
987
|
-
if (writeMigration) {
|
|
757
|
+
function sanitizeDevToolsRoot(root) {
|
|
758
|
+
const reserved = /* @__PURE__ */ new Set(["providers", "resolve", "list", "capabilities", "__meta"]);
|
|
759
|
+
for (const key of Object.keys(root)) {
|
|
760
|
+
if (reserved.has(key)) continue;
|
|
761
|
+
const descriptor = Object.getOwnPropertyDescriptor(root, key);
|
|
762
|
+
if (descriptor && !descriptor.configurable) continue;
|
|
988
763
|
try {
|
|
989
|
-
|
|
990
|
-
} catch
|
|
991
|
-
throw error instanceof SchemaError ? error : new SchemaError("MIGRATION_FAILED", `Write-time migration failed for key "${key}"`, error);
|
|
992
|
-
}
|
|
993
|
-
}
|
|
994
|
-
validateAgainstSchema(key, valueToStore, targetSchema.schema);
|
|
995
|
-
return serializeEnvelope(targetSchema.version, valueToStore);
|
|
996
|
-
}
|
|
997
|
-
function decodeCodecManagedEnvelope(key, envelope, options) {
|
|
998
|
-
if (typeof envelope.payload !== "string") {
|
|
999
|
-
return {
|
|
1000
|
-
value: envelope.payload
|
|
1001
|
-
};
|
|
1002
|
-
}
|
|
1003
|
-
return {
|
|
1004
|
-
value: decodeStringPayload(key, envelope.payload, options)
|
|
1005
|
-
};
|
|
1006
|
-
}
|
|
1007
|
-
function decodeAutoschemaEnvelope(key, envelope, options, schemaRegistry) {
|
|
1008
|
-
if (!schemaRegistry?.registerSchema) {
|
|
1009
|
-
throw new SchemaError(
|
|
1010
|
-
"MODE_CONFIGURATION_INVALID",
|
|
1011
|
-
`Autoschema mode requires schema registry registration for key "${key}"`
|
|
1012
|
-
);
|
|
1013
|
-
}
|
|
1014
|
-
const decoded = typeof envelope.payload === "string" ? decodeStringPayload(key, envelope.payload, options) : envelope.payload;
|
|
1015
|
-
const pendingSchema = {
|
|
1016
|
-
key,
|
|
1017
|
-
version: 1,
|
|
1018
|
-
schema: inferJsonSchema(decoded)
|
|
1019
|
-
};
|
|
1020
|
-
return {
|
|
1021
|
-
value: decoded,
|
|
1022
|
-
rewriteRaw: serializeEnvelope(pendingSchema.version, decoded),
|
|
1023
|
-
pendingSchema
|
|
1024
|
-
};
|
|
1025
|
-
}
|
|
1026
|
-
function decodeSchemaManagedEnvelope(key, envelope, schemaForVersion, latestSchema, schemaRegistry) {
|
|
1027
|
-
let current = envelope.payload;
|
|
1028
|
-
validateAgainstSchema(key, current, schemaForVersion.schema);
|
|
1029
|
-
if (!latestSchema || envelope.version >= latestSchema.version) {
|
|
1030
|
-
return {
|
|
1031
|
-
value: current
|
|
1032
|
-
};
|
|
1033
|
-
}
|
|
1034
|
-
const path = getMigrationPath(schemaRegistry, key, envelope.version, latestSchema.version);
|
|
1035
|
-
if (!path) {
|
|
1036
|
-
throw new SchemaError(
|
|
1037
|
-
"MIGRATION_PATH_NOT_FOUND",
|
|
1038
|
-
`No migration path for key "${key}" from v${envelope.version} to v${latestSchema.version}`
|
|
1039
|
-
);
|
|
1040
|
-
}
|
|
1041
|
-
for (const step of path) {
|
|
1042
|
-
current = step.migrate(current);
|
|
1043
|
-
}
|
|
1044
|
-
validateAgainstSchema(key, current, latestSchema.schema);
|
|
1045
|
-
return {
|
|
1046
|
-
value: current,
|
|
1047
|
-
rewriteRaw: serializeEnvelope(latestSchema.version, current)
|
|
1048
|
-
};
|
|
1049
|
-
}
|
|
1050
|
-
function decodePersistedValue(key, raw, options, api, schemaRegistry) {
|
|
1051
|
-
if (raw == null) {
|
|
1052
|
-
return buildFallbackResult(options);
|
|
1053
|
-
}
|
|
1054
|
-
const envelope = parseEnvelope(key, raw);
|
|
1055
|
-
const latestSchema = getLatestSchema(schemaRegistry, key);
|
|
1056
|
-
const schemaForVersion = getSchemaForVersion(schemaRegistry, key, envelope.version);
|
|
1057
|
-
if (api.schemaMode === "strict" && !schemaForVersion) {
|
|
1058
|
-
throw new SchemaError("SCHEMA_NOT_FOUND", `No schema for key "${key}" v${envelope.version}`);
|
|
1059
|
-
}
|
|
1060
|
-
if (api.schemaMode === "autoschema" && !schemaForVersion) {
|
|
1061
|
-
return decodeAutoschemaEnvelope(key, envelope, options, schemaRegistry);
|
|
1062
|
-
}
|
|
1063
|
-
if (!schemaForVersion) {
|
|
1064
|
-
return decodeCodecManagedEnvelope(key, envelope, options);
|
|
1065
|
-
}
|
|
1066
|
-
return decodeSchemaManagedEnvelope(key, envelope, schemaForVersion, latestSchema, schemaRegistry);
|
|
1067
|
-
}
|
|
1068
|
-
function createMnemonicOptionalBridge({
|
|
1069
|
-
api,
|
|
1070
|
-
schemaRegistry
|
|
1071
|
-
}) {
|
|
1072
|
-
return {
|
|
1073
|
-
namespace: api.prefix.endsWith(".") ? api.prefix.slice(0, -1) : api.prefix,
|
|
1074
|
-
capabilities: {
|
|
1075
|
-
persistence: true,
|
|
1076
|
-
schema: getSchemaCapabilities(schemaRegistry)
|
|
1077
|
-
},
|
|
1078
|
-
subscribeRaw: (key, listener) => api.subscribeRaw(key, listener),
|
|
1079
|
-
getRawSnapshot: (key) => api.getRawSnapshot(key),
|
|
1080
|
-
decodeSnapshot: (key, raw, options) => {
|
|
1081
|
-
try {
|
|
1082
|
-
return decodePersistedValue(key, raw, options, api, schemaRegistry);
|
|
1083
|
-
} catch {
|
|
1084
|
-
return buildFallbackResult(options);
|
|
1085
|
-
}
|
|
1086
|
-
},
|
|
1087
|
-
setValue: (key, nextValue, options) => {
|
|
1088
|
-
try {
|
|
1089
|
-
api.setRaw(key, encodeValueForWrite(key, nextValue, options, schemaRegistry));
|
|
1090
|
-
} catch (error) {
|
|
1091
|
-
if (error instanceof SchemaError) {
|
|
1092
|
-
console.error(`[Mnemonic] Schema error for key "${key}" (${error.code}):`, error.message);
|
|
1093
|
-
return;
|
|
1094
|
-
}
|
|
1095
|
-
if (error instanceof CodecError) {
|
|
1096
|
-
console.error(`[Mnemonic] Codec error for key "${key}":`, error.message);
|
|
1097
|
-
return;
|
|
1098
|
-
}
|
|
1099
|
-
throw error;
|
|
1100
|
-
}
|
|
1101
|
-
},
|
|
1102
|
-
removeValue: (key) => {
|
|
1103
|
-
api.removeRaw(key);
|
|
1104
|
-
},
|
|
1105
|
-
commitSnapshot: (key, raw, snapshot) => {
|
|
1106
|
-
if (snapshot.pendingSchema && schemaRegistry?.registerSchema) {
|
|
1107
|
-
if (!schemaRegistry.getSchema(snapshot.pendingSchema.key, snapshot.pendingSchema.version)) {
|
|
1108
|
-
try {
|
|
1109
|
-
schemaRegistry.registerSchema(snapshot.pendingSchema);
|
|
1110
|
-
} catch {
|
|
1111
|
-
}
|
|
1112
|
-
}
|
|
1113
|
-
}
|
|
1114
|
-
if (snapshot.rewriteRaw !== void 0 && snapshot.rewriteRaw !== raw) {
|
|
1115
|
-
api.setRaw(key, snapshot.rewriteRaw);
|
|
1116
|
-
}
|
|
1117
|
-
}
|
|
1118
|
-
};
|
|
1119
|
-
}
|
|
1120
|
-
var MnemonicOptionalBridgeContext = createContext(null);
|
|
1121
|
-
|
|
1122
|
-
// src/Mnemonic/optional-bridge-provider.tsx
|
|
1123
|
-
function MnemonicOptionalBridgeProvider({
|
|
1124
|
-
bridge,
|
|
1125
|
-
children
|
|
1126
|
-
}) {
|
|
1127
|
-
return createElement(MnemonicOptionalBridgeContext.Provider, { value: bridge }, children);
|
|
1128
|
-
}
|
|
1129
|
-
var MnemonicContext = createContext(null);
|
|
1130
|
-
function useMnemonic() {
|
|
1131
|
-
const context = useMnemonicOptional();
|
|
1132
|
-
if (!context) {
|
|
1133
|
-
throw new Error("useMnemonic must be used within a MnemonicProvider");
|
|
1134
|
-
}
|
|
1135
|
-
return context;
|
|
1136
|
-
}
|
|
1137
|
-
function useMnemonicOptional() {
|
|
1138
|
-
return useContext(MnemonicContext);
|
|
1139
|
-
}
|
|
1140
|
-
function defaultBrowserStorage() {
|
|
1141
|
-
const globalWindow = globalThis.window;
|
|
1142
|
-
if (globalWindow === void 0) return void 0;
|
|
1143
|
-
try {
|
|
1144
|
-
return globalWindow.localStorage;
|
|
1145
|
-
} catch {
|
|
1146
|
-
return void 0;
|
|
1147
|
-
}
|
|
1148
|
-
}
|
|
1149
|
-
function detectEnumerableStorage(storage) {
|
|
1150
|
-
if (!storage) return false;
|
|
1151
|
-
try {
|
|
1152
|
-
return typeof storage.length === "number" && typeof storage.key === "function";
|
|
1153
|
-
} catch {
|
|
1154
|
-
return false;
|
|
1155
|
-
}
|
|
1156
|
-
}
|
|
1157
|
-
function isProductionRuntime() {
|
|
1158
|
-
const env = getRuntimeNodeEnv();
|
|
1159
|
-
if (env === void 0) {
|
|
1160
|
-
return true;
|
|
1161
|
-
}
|
|
1162
|
-
return env === "production";
|
|
1163
|
-
}
|
|
1164
|
-
function weakRefConstructor() {
|
|
1165
|
-
const ctor = globalThis.WeakRef;
|
|
1166
|
-
return typeof ctor === "function" ? ctor : null;
|
|
1167
|
-
}
|
|
1168
|
-
function hasFinalizationRegistry() {
|
|
1169
|
-
return typeof globalThis.FinalizationRegistry === "function";
|
|
1170
|
-
}
|
|
1171
|
-
function isPromiseLike(value) {
|
|
1172
|
-
if (value == null) return false;
|
|
1173
|
-
if (typeof value !== "object" && typeof value !== "function") return false;
|
|
1174
|
-
return typeof value.then === "function";
|
|
1175
|
-
}
|
|
1176
|
-
function getCrossTabSyncMode(requestedStorage, activeStorage) {
|
|
1177
|
-
const isExplicitNativeBrowserStorage = activeStorage !== void 0 && requestedStorage !== void 0 && getNativeBrowserStorages().includes(activeStorage);
|
|
1178
|
-
if (requestedStorage === void 0 && activeStorage !== void 0 || isExplicitNativeBrowserStorage) {
|
|
1179
|
-
return "browser-storage-event";
|
|
1180
|
-
}
|
|
1181
|
-
if (typeof activeStorage?.onExternalChange === "function") {
|
|
1182
|
-
return "custom-external-change";
|
|
1183
|
-
}
|
|
1184
|
-
return "none";
|
|
1185
|
-
}
|
|
1186
|
-
function getDevToolsWindow() {
|
|
1187
|
-
return globalThis.window;
|
|
1188
|
-
}
|
|
1189
|
-
function sanitizeDevToolsRoot(root) {
|
|
1190
|
-
const reserved = /* @__PURE__ */ new Set(["providers", "resolve", "list", "capabilities", "__meta"]);
|
|
1191
|
-
for (const key of Object.keys(root)) {
|
|
1192
|
-
if (reserved.has(key)) continue;
|
|
1193
|
-
const descriptor = Object.getOwnPropertyDescriptor(root, key);
|
|
1194
|
-
if (descriptor && !descriptor.configurable) continue;
|
|
1195
|
-
try {
|
|
1196
|
-
delete root[key];
|
|
1197
|
-
} catch {
|
|
764
|
+
delete root[key];
|
|
765
|
+
} catch {
|
|
1198
766
|
}
|
|
1199
767
|
}
|
|
1200
768
|
}
|
|
@@ -1530,7 +1098,8 @@ function MnemonicProvider({
|
|
|
1530
1098
|
enableDevTools = false,
|
|
1531
1099
|
schemaMode = "default",
|
|
1532
1100
|
schemaRegistry,
|
|
1533
|
-
ssr
|
|
1101
|
+
ssr,
|
|
1102
|
+
bootstrap
|
|
1534
1103
|
}) {
|
|
1535
1104
|
if (schemaMode === "strict" && !schemaRegistry) {
|
|
1536
1105
|
throw new Error("MnemonicProvider strict mode requires schemaRegistry");
|
|
@@ -1538,14 +1107,32 @@ function MnemonicProvider({
|
|
|
1538
1107
|
if (schemaMode === "autoschema" && typeof schemaRegistry?.registerSchema !== "function") {
|
|
1539
1108
|
throw new Error("MnemonicProvider autoschema mode requires schemaRegistry.registerSchema");
|
|
1540
1109
|
}
|
|
1110
|
+
const prefix = `${namespace}.`;
|
|
1111
|
+
const parentStore = useMnemonicOptional();
|
|
1112
|
+
const bootstrapRawSeed = useRef(bootstrap?.raw).current;
|
|
1113
|
+
useEffect(() => {
|
|
1114
|
+
if (isProductionRuntime()) return;
|
|
1115
|
+
if (parentStore?.prefix !== prefix) return;
|
|
1116
|
+
if (warnedNestedProviderStores.has(parentStore)) return;
|
|
1117
|
+
warnedNestedProviderStores.add(parentStore);
|
|
1118
|
+
console.warn(
|
|
1119
|
+
`[Mnemonic] Nested MnemonicProvider detected for namespace "${namespace}". The nearest provider wins, so the inner provider creates a separate store and cache even though the namespace matches. Prefer a single provider per namespace, or use distinct namespaces for intentionally separate scopes.`
|
|
1120
|
+
);
|
|
1121
|
+
}, [namespace, parentStore, prefix]);
|
|
1541
1122
|
const store = useMemo(() => {
|
|
1542
|
-
const
|
|
1543
|
-
const st = storage ?? defaultBrowserStorage();
|
|
1123
|
+
const st = storage ?? getDefaultBrowserStorage();
|
|
1544
1124
|
const ssrHydration = ssr?.hydration ?? "immediate";
|
|
1545
1125
|
const devToolsRoot = ensureDevToolsRoot(enableDevTools);
|
|
1546
1126
|
const canEnumerateKeys = detectEnumerableStorage(st);
|
|
1547
1127
|
const crossTabSyncMode = getCrossTabSyncMode(storage, st);
|
|
1548
1128
|
const cache = /* @__PURE__ */ new Map();
|
|
1129
|
+
if (bootstrapRawSeed) {
|
|
1130
|
+
for (const [key, raw] of Object.entries(bootstrapRawSeed)) {
|
|
1131
|
+
if (raw != null) {
|
|
1132
|
+
cache.set(key, raw);
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1549
1136
|
const listeners = /* @__PURE__ */ new Map();
|
|
1550
1137
|
let quotaErrorLogged = false;
|
|
1551
1138
|
let accessErrorLogged = false;
|
|
@@ -1570,168 +1157,622 @@ function MnemonicProvider({
|
|
|
1570
1157
|
);
|
|
1571
1158
|
accessErrorLogged = true;
|
|
1572
1159
|
}
|
|
1573
|
-
};
|
|
1574
|
-
const handleAsyncStorageContractViolation = (method, thenable) => {
|
|
1575
|
-
asyncContractViolationDetected = true;
|
|
1576
|
-
void Promise.resolve(thenable).catch(() => void 0);
|
|
1577
|
-
if (accessErrorLogged) return;
|
|
1578
|
-
console.error(
|
|
1579
|
-
`[Mnemonic] StorageLike.${method} returned a Promise. StorageLike must remain synchronous for react-mnemonic v1. Wrap async persistence behind a synchronous cache facade instead.`
|
|
1580
|
-
);
|
|
1581
|
-
accessErrorLogged = true;
|
|
1582
|
-
};
|
|
1583
|
-
const readThrough = (key) => {
|
|
1584
|
-
if (cache.has(key)) return cache.get(key) ?? null;
|
|
1585
|
-
if (!st || asyncContractViolationDetected) {
|
|
1586
|
-
cache.set(key, null);
|
|
1587
|
-
return null;
|
|
1160
|
+
};
|
|
1161
|
+
const handleAsyncStorageContractViolation = (method, thenable) => {
|
|
1162
|
+
asyncContractViolationDetected = true;
|
|
1163
|
+
void Promise.resolve(thenable).catch(() => void 0);
|
|
1164
|
+
if (accessErrorLogged) return;
|
|
1165
|
+
console.error(
|
|
1166
|
+
`[Mnemonic] StorageLike.${method} returned a Promise. StorageLike must remain synchronous for react-mnemonic v1. Wrap async persistence behind a synchronous cache facade instead.`
|
|
1167
|
+
);
|
|
1168
|
+
accessErrorLogged = true;
|
|
1169
|
+
};
|
|
1170
|
+
const readThrough = (key) => {
|
|
1171
|
+
if (cache.has(key)) return cache.get(key) ?? null;
|
|
1172
|
+
if (!st || asyncContractViolationDetected) {
|
|
1173
|
+
cache.set(key, null);
|
|
1174
|
+
return null;
|
|
1175
|
+
}
|
|
1176
|
+
const raw = readStorageRaw(st, fullKey(key), storageAccessCallbacks);
|
|
1177
|
+
cache.set(key, raw);
|
|
1178
|
+
return raw;
|
|
1179
|
+
};
|
|
1180
|
+
const writeRaw = (key, raw) => {
|
|
1181
|
+
cache.set(key, raw);
|
|
1182
|
+
if (st && !asyncContractViolationDetected) {
|
|
1183
|
+
try {
|
|
1184
|
+
const result = st.setItem(fullKey(key), raw);
|
|
1185
|
+
if (isPromiseLike(result)) {
|
|
1186
|
+
handleAsyncStorageContractViolation("setItem", result);
|
|
1187
|
+
} else {
|
|
1188
|
+
quotaErrorLogged = false;
|
|
1189
|
+
accessErrorLogged = false;
|
|
1190
|
+
}
|
|
1191
|
+
} catch (err) {
|
|
1192
|
+
if (!quotaErrorLogged && err instanceof DOMException && err.name === "QuotaExceededError") {
|
|
1193
|
+
console.error(
|
|
1194
|
+
`[Mnemonic] Storage quota exceeded writing key "${key}". Data is cached in memory but will not persist.`
|
|
1195
|
+
);
|
|
1196
|
+
quotaErrorLogged = true;
|
|
1197
|
+
}
|
|
1198
|
+
logAccessError(err);
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
emit(key);
|
|
1202
|
+
bumpDevToolsVersion(devToolsRoot, namespace, `set:${key}`);
|
|
1203
|
+
};
|
|
1204
|
+
const removeRaw = (key) => {
|
|
1205
|
+
cache.set(key, null);
|
|
1206
|
+
if (st && !asyncContractViolationDetected) {
|
|
1207
|
+
try {
|
|
1208
|
+
const result = st.removeItem(fullKey(key));
|
|
1209
|
+
if (isPromiseLike(result)) {
|
|
1210
|
+
handleAsyncStorageContractViolation("removeItem", result);
|
|
1211
|
+
} else {
|
|
1212
|
+
accessErrorLogged = false;
|
|
1213
|
+
}
|
|
1214
|
+
} catch (err) {
|
|
1215
|
+
logAccessError(err);
|
|
1216
|
+
}
|
|
1217
|
+
}
|
|
1218
|
+
emit(key);
|
|
1219
|
+
bumpDevToolsVersion(devToolsRoot, namespace, `remove:${key}`);
|
|
1220
|
+
};
|
|
1221
|
+
const subscribeRaw = (key, listener) => {
|
|
1222
|
+
let set = listeners.get(key);
|
|
1223
|
+
if (!set) {
|
|
1224
|
+
set = /* @__PURE__ */ new Set();
|
|
1225
|
+
listeners.set(key, set);
|
|
1226
|
+
}
|
|
1227
|
+
set.add(listener);
|
|
1228
|
+
readThrough(key);
|
|
1229
|
+
return () => {
|
|
1230
|
+
const s = listeners.get(key);
|
|
1231
|
+
if (!s) return;
|
|
1232
|
+
s.delete(listener);
|
|
1233
|
+
if (s.size === 0) listeners.delete(key);
|
|
1234
|
+
};
|
|
1235
|
+
};
|
|
1236
|
+
const getRawSnapshot = (key) => readThrough(key);
|
|
1237
|
+
const keys = () => {
|
|
1238
|
+
if (asyncContractViolationDetected) {
|
|
1239
|
+
return Array.from(cache.entries()).filter(([, value]) => value != null).map(([key]) => key);
|
|
1240
|
+
}
|
|
1241
|
+
if (!canEnumerateKeys) return [];
|
|
1242
|
+
return enumerateNamespaceKeys(st, prefix, storageAccessCallbacks);
|
|
1243
|
+
};
|
|
1244
|
+
const dump = () => {
|
|
1245
|
+
const out = {};
|
|
1246
|
+
for (const k of keys()) {
|
|
1247
|
+
const raw = readThrough(k);
|
|
1248
|
+
if (raw != null) out[k] = raw;
|
|
1249
|
+
}
|
|
1250
|
+
return out;
|
|
1251
|
+
};
|
|
1252
|
+
const reloadFromStorage = createReloadFromStorage({
|
|
1253
|
+
storage: st,
|
|
1254
|
+
hasAsyncContractViolation: () => asyncContractViolationDetected,
|
|
1255
|
+
prefix,
|
|
1256
|
+
listeners,
|
|
1257
|
+
cache,
|
|
1258
|
+
emit,
|
|
1259
|
+
callbacks: storageAccessCallbacks,
|
|
1260
|
+
devToolsRoot,
|
|
1261
|
+
namespace
|
|
1262
|
+
});
|
|
1263
|
+
const store2 = {
|
|
1264
|
+
prefix,
|
|
1265
|
+
canEnumerateKeys,
|
|
1266
|
+
subscribeRaw,
|
|
1267
|
+
getRawSnapshot,
|
|
1268
|
+
setRaw: writeRaw,
|
|
1269
|
+
removeRaw,
|
|
1270
|
+
keys,
|
|
1271
|
+
dump,
|
|
1272
|
+
reloadFromStorage,
|
|
1273
|
+
schemaMode,
|
|
1274
|
+
ssrHydration,
|
|
1275
|
+
crossTabSyncMode,
|
|
1276
|
+
...schemaRegistry ? { schemaRegistry } : {}
|
|
1277
|
+
};
|
|
1278
|
+
if (devToolsRoot) {
|
|
1279
|
+
registerDevToolsProvider({
|
|
1280
|
+
devToolsRoot,
|
|
1281
|
+
namespace,
|
|
1282
|
+
store: store2,
|
|
1283
|
+
dump,
|
|
1284
|
+
keys,
|
|
1285
|
+
readThrough,
|
|
1286
|
+
writeRaw,
|
|
1287
|
+
removeRaw
|
|
1288
|
+
});
|
|
1289
|
+
}
|
|
1290
|
+
return store2;
|
|
1291
|
+
}, [namespace, storage, enableDevTools, schemaMode, schemaRegistry, ssr?.hydration, bootstrapRawSeed]);
|
|
1292
|
+
const optionalBridge = useMemo(
|
|
1293
|
+
() => createMnemonicOptionalBridge({
|
|
1294
|
+
api: store,
|
|
1295
|
+
...schemaRegistry ? { schemaRegistry } : {}
|
|
1296
|
+
}),
|
|
1297
|
+
[schemaRegistry, store]
|
|
1298
|
+
);
|
|
1299
|
+
useEffect(() => {
|
|
1300
|
+
if (!storage?.onExternalChange) return;
|
|
1301
|
+
return storage.onExternalChange((changedKeys) => store.reloadFromStorage(changedKeys));
|
|
1302
|
+
}, [storage, store]);
|
|
1303
|
+
return /* @__PURE__ */ jsx(MnemonicContext.Provider, { value: store, children: /* @__PURE__ */ jsx(MnemonicOptionalBridgeProvider, { bridge: optionalBridge, children }) });
|
|
1304
|
+
}
|
|
1305
|
+
function throwCoreProviderSchemaImportError(propName) {
|
|
1306
|
+
throw new Error(
|
|
1307
|
+
`[Mnemonic] MnemonicProvider from react-mnemonic/core does not support ${propName}. Import MnemonicProvider from "react-mnemonic/schema" or "react-mnemonic" for schema validation, autoschema, and migration support.`
|
|
1308
|
+
);
|
|
1309
|
+
}
|
|
1310
|
+
function assertNoSchemaProps(props) {
|
|
1311
|
+
if (props.schemaMode !== void 0) {
|
|
1312
|
+
throwCoreProviderSchemaImportError("schemaMode");
|
|
1313
|
+
}
|
|
1314
|
+
if (props.schemaRegistry !== void 0) {
|
|
1315
|
+
throwCoreProviderSchemaImportError("schemaRegistry");
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
function MnemonicProvider2(props) {
|
|
1319
|
+
assertNoSchemaProps(props);
|
|
1320
|
+
return /* @__PURE__ */ jsx(MnemonicProvider, { ...props });
|
|
1321
|
+
}
|
|
1322
|
+
var SSR_SNAPSHOT_TOKEN = /* @__PURE__ */ Symbol("mnemonic:ssr-snapshot");
|
|
1323
|
+
var diagnosticContractRegistry = /* @__PURE__ */ new WeakMap();
|
|
1324
|
+
var diagnosticWarningRegistry = /* @__PURE__ */ new WeakMap();
|
|
1325
|
+
var diagnosticObjectIds = /* @__PURE__ */ new WeakMap();
|
|
1326
|
+
var nextDiagnosticObjectId = 1;
|
|
1327
|
+
function withReadMetadata(value, rewriteRaw, extra) {
|
|
1328
|
+
const result = { value };
|
|
1329
|
+
if (extra !== void 0) {
|
|
1330
|
+
Object.assign(result, extra);
|
|
1331
|
+
}
|
|
1332
|
+
if (rewriteRaw !== void 0) result.rewriteRaw = rewriteRaw;
|
|
1333
|
+
return result;
|
|
1334
|
+
}
|
|
1335
|
+
function isDevelopmentRuntime() {
|
|
1336
|
+
return getRuntimeNodeEnv() === "development";
|
|
1337
|
+
}
|
|
1338
|
+
function getDiagnosticWarnings(api) {
|
|
1339
|
+
let warnings = diagnosticWarningRegistry.get(api);
|
|
1340
|
+
if (!warnings) {
|
|
1341
|
+
warnings = /* @__PURE__ */ new Set();
|
|
1342
|
+
diagnosticWarningRegistry.set(api, warnings);
|
|
1343
|
+
}
|
|
1344
|
+
return warnings;
|
|
1345
|
+
}
|
|
1346
|
+
function warnOnce(api, id, message) {
|
|
1347
|
+
const warnings = getDiagnosticWarnings(api);
|
|
1348
|
+
if (warnings.has(id)) return;
|
|
1349
|
+
warnings.add(id);
|
|
1350
|
+
console.warn(message);
|
|
1351
|
+
}
|
|
1352
|
+
function stableDiagnosticValue(value) {
|
|
1353
|
+
if (typeof value === "function") {
|
|
1354
|
+
const source = Function.prototype.toString.call(value).split(/\s+/).join(" ").trim();
|
|
1355
|
+
const name = value.name || "anonymous";
|
|
1356
|
+
return `[factory:${name}/${value.length}:${source}]`;
|
|
1357
|
+
}
|
|
1358
|
+
if (typeof value === "bigint") return `${value.toString()}n`;
|
|
1359
|
+
if (typeof value === "symbol") return value.toString();
|
|
1360
|
+
if (value === void 0) return "undefined";
|
|
1361
|
+
try {
|
|
1362
|
+
return JSON.stringify(value);
|
|
1363
|
+
} catch {
|
|
1364
|
+
const tag = Object.prototype.toString.call(value);
|
|
1365
|
+
if (value !== null && (typeof value === "object" || typeof value === "function")) {
|
|
1366
|
+
return `${tag}#${getDiagnosticObjectId(value)}`;
|
|
1367
|
+
}
|
|
1368
|
+
return tag;
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
function isObjectLike(value) {
|
|
1372
|
+
return value !== null && (typeof value === "object" || typeof value === "function");
|
|
1373
|
+
}
|
|
1374
|
+
function getDiagnosticObjectId(value) {
|
|
1375
|
+
const existing = diagnosticObjectIds.get(value);
|
|
1376
|
+
if (existing !== void 0) return existing;
|
|
1377
|
+
const id = nextDiagnosticObjectId++;
|
|
1378
|
+
diagnosticObjectIds.set(value, id);
|
|
1379
|
+
return id;
|
|
1380
|
+
}
|
|
1381
|
+
function buildContractFingerprint({
|
|
1382
|
+
api,
|
|
1383
|
+
key,
|
|
1384
|
+
defaultValue,
|
|
1385
|
+
codecOpt,
|
|
1386
|
+
schemaVersion,
|
|
1387
|
+
reconcile,
|
|
1388
|
+
listenCrossTab,
|
|
1389
|
+
ssrOptions
|
|
1390
|
+
}) {
|
|
1391
|
+
const codecSignature = codecOpt == null || !isObjectLike(codecOpt) ? "default-json-codec" : `codec:${stableDiagnosticValue(codecOpt.encode)}:${stableDiagnosticValue(codecOpt.decode)}`;
|
|
1392
|
+
const reconcileSignature = reconcile == null || !isObjectLike(reconcile) ? "no-reconcile" : `reconcile:${stableDiagnosticValue(reconcile)}`;
|
|
1393
|
+
return JSON.stringify({
|
|
1394
|
+
key,
|
|
1395
|
+
defaultValue: stableDiagnosticValue(defaultValue),
|
|
1396
|
+
codec: codecSignature,
|
|
1397
|
+
schemaVersion: schemaVersion ?? null,
|
|
1398
|
+
listenCrossTab: Boolean(listenCrossTab),
|
|
1399
|
+
reconcile: reconcileSignature,
|
|
1400
|
+
ssrHydration: ssrOptions?.hydration ?? null,
|
|
1401
|
+
hasServerValue: ssrOptions?.serverValue !== void 0,
|
|
1402
|
+
providerHydration: api.ssrHydration ?? null
|
|
1403
|
+
});
|
|
1404
|
+
}
|
|
1405
|
+
function resolveMnemonicKeyArgs(keyOrDescriptor, options) {
|
|
1406
|
+
if (typeof keyOrDescriptor !== "string") {
|
|
1407
|
+
return keyOrDescriptor;
|
|
1408
|
+
}
|
|
1409
|
+
if (!options) {
|
|
1410
|
+
throw new Error("useMnemonicKey requires options when called with a string key");
|
|
1411
|
+
}
|
|
1412
|
+
return {
|
|
1413
|
+
key: keyOrDescriptor,
|
|
1414
|
+
options
|
|
1415
|
+
};
|
|
1416
|
+
}
|
|
1417
|
+
function useMnemonicKeySharedFromApi(api, keyOrDescriptor, options, schemaVersion) {
|
|
1418
|
+
const descriptor = resolveMnemonicKeyArgs(keyOrDescriptor, options);
|
|
1419
|
+
const key = descriptor.key;
|
|
1420
|
+
const resolvedOptions = descriptor.options;
|
|
1421
|
+
const {
|
|
1422
|
+
defaultValue,
|
|
1423
|
+
onMount,
|
|
1424
|
+
onChange,
|
|
1425
|
+
listenCrossTab,
|
|
1426
|
+
codec: codecOpt,
|
|
1427
|
+
schema,
|
|
1428
|
+
reconcile,
|
|
1429
|
+
ssr: ssrOptions
|
|
1430
|
+
} = resolvedOptions;
|
|
1431
|
+
const codec = codecOpt ?? JSONCodec;
|
|
1432
|
+
const hydrationMode = ssrOptions?.hydration ?? api.ssrHydration;
|
|
1433
|
+
const [hasMounted, setHasMounted] = useState(hydrationMode !== "client-only");
|
|
1434
|
+
const developmentRuntime = isDevelopmentRuntime();
|
|
1435
|
+
const contractFingerprint = useMemo(
|
|
1436
|
+
() => developmentRuntime ? buildContractFingerprint({
|
|
1437
|
+
api,
|
|
1438
|
+
key,
|
|
1439
|
+
defaultValue,
|
|
1440
|
+
codecOpt,
|
|
1441
|
+
...{} ,
|
|
1442
|
+
reconcile,
|
|
1443
|
+
listenCrossTab,
|
|
1444
|
+
ssrOptions
|
|
1445
|
+
}) : null,
|
|
1446
|
+
[
|
|
1447
|
+
developmentRuntime,
|
|
1448
|
+
api,
|
|
1449
|
+
key,
|
|
1450
|
+
defaultValue,
|
|
1451
|
+
codecOpt,
|
|
1452
|
+
schemaVersion,
|
|
1453
|
+
reconcile,
|
|
1454
|
+
listenCrossTab,
|
|
1455
|
+
ssrOptions?.hydration,
|
|
1456
|
+
ssrOptions?.serverValue
|
|
1457
|
+
]
|
|
1458
|
+
);
|
|
1459
|
+
const getFallback = useCallback(
|
|
1460
|
+
(error) => typeof defaultValue === "function" ? defaultValue(error) : defaultValue,
|
|
1461
|
+
[defaultValue]
|
|
1462
|
+
);
|
|
1463
|
+
const getServerValue = useCallback(() => {
|
|
1464
|
+
const serverValue = ssrOptions?.serverValue;
|
|
1465
|
+
if (serverValue === void 0) {
|
|
1466
|
+
return getFallback();
|
|
1467
|
+
}
|
|
1468
|
+
return typeof serverValue === "function" ? serverValue() : serverValue;
|
|
1469
|
+
}, [getFallback, ssrOptions?.serverValue]);
|
|
1470
|
+
const parseEnvelope2 = useCallback(
|
|
1471
|
+
(rawText) => {
|
|
1472
|
+
try {
|
|
1473
|
+
return { ok: true, envelope: parseEnvelope(key, rawText) };
|
|
1474
|
+
} catch (error) {
|
|
1475
|
+
return {
|
|
1476
|
+
ok: false,
|
|
1477
|
+
error: error instanceof SchemaError ? error : new SchemaError("INVALID_ENVELOPE", `Invalid envelope for key "${key}"`, error)
|
|
1478
|
+
};
|
|
1479
|
+
}
|
|
1480
|
+
},
|
|
1481
|
+
[key]
|
|
1482
|
+
);
|
|
1483
|
+
const decodeStringPayload2 = useCallback(
|
|
1484
|
+
(payload, activeCodec) => {
|
|
1485
|
+
return decodeStringPayload(key, payload, activeCodec);
|
|
1486
|
+
},
|
|
1487
|
+
[key]
|
|
1488
|
+
);
|
|
1489
|
+
const buildFallbackResult2 = useCallback(
|
|
1490
|
+
(error, extra) => {
|
|
1491
|
+
return withReadMetadata(getFallback(error), void 0, extra);
|
|
1492
|
+
},
|
|
1493
|
+
[getFallback]
|
|
1494
|
+
);
|
|
1495
|
+
return {
|
|
1496
|
+
api,
|
|
1497
|
+
key,
|
|
1498
|
+
codec,
|
|
1499
|
+
codecOpt,
|
|
1500
|
+
schema,
|
|
1501
|
+
reconcile,
|
|
1502
|
+
onMount,
|
|
1503
|
+
onChange,
|
|
1504
|
+
listenCrossTab,
|
|
1505
|
+
getFallback,
|
|
1506
|
+
getServerValue,
|
|
1507
|
+
parseEnvelope: parseEnvelope2,
|
|
1508
|
+
decodeStringPayload: decodeStringPayload2,
|
|
1509
|
+
buildFallbackResult: buildFallbackResult2,
|
|
1510
|
+
developmentRuntime,
|
|
1511
|
+
contractFingerprint,
|
|
1512
|
+
hasMounted,
|
|
1513
|
+
setHasMounted,
|
|
1514
|
+
hydrationMode,
|
|
1515
|
+
ssrOptions
|
|
1516
|
+
};
|
|
1517
|
+
}
|
|
1518
|
+
function useApplyReconcile({
|
|
1519
|
+
key,
|
|
1520
|
+
reconcile,
|
|
1521
|
+
buildFallbackResult: buildFallbackResult2
|
|
1522
|
+
}) {
|
|
1523
|
+
return useCallback(
|
|
1524
|
+
({
|
|
1525
|
+
value,
|
|
1526
|
+
rewriteRaw,
|
|
1527
|
+
extra,
|
|
1528
|
+
persistedVersion,
|
|
1529
|
+
latestVersion,
|
|
1530
|
+
serializeForPersist,
|
|
1531
|
+
deriveExtra
|
|
1532
|
+
}) => {
|
|
1533
|
+
if (!reconcile) {
|
|
1534
|
+
return withReadMetadata(value, rewriteRaw, extra);
|
|
1535
|
+
}
|
|
1536
|
+
const context = {
|
|
1537
|
+
key,
|
|
1538
|
+
persistedVersion
|
|
1539
|
+
};
|
|
1540
|
+
if (latestVersion !== void 0) {
|
|
1541
|
+
context.latestVersion = latestVersion;
|
|
1542
|
+
}
|
|
1543
|
+
const baselineSerialized = (() => {
|
|
1544
|
+
try {
|
|
1545
|
+
return serializeForPersist(value);
|
|
1546
|
+
} catch {
|
|
1547
|
+
return rewriteRaw;
|
|
1548
|
+
}
|
|
1549
|
+
})();
|
|
1550
|
+
try {
|
|
1551
|
+
const reconciled = reconcile(value, context);
|
|
1552
|
+
const nextExtra = deriveExtra ? deriveExtra(reconciled, extra) : extra;
|
|
1553
|
+
const nextSerialized = serializeForPersist(reconciled);
|
|
1554
|
+
const nextRewriteRaw = baselineSerialized === void 0 || nextSerialized !== baselineSerialized ? nextSerialized : rewriteRaw;
|
|
1555
|
+
return withReadMetadata(reconciled, nextRewriteRaw, nextExtra);
|
|
1556
|
+
} catch (err) {
|
|
1557
|
+
const typedErr = err instanceof SchemaError ? err : new SchemaError("RECONCILE_FAILED", `Reconciliation failed for key "${key}"`, err);
|
|
1558
|
+
return buildFallbackResult2(typedErr, extra);
|
|
1559
|
+
}
|
|
1560
|
+
},
|
|
1561
|
+
[buildFallbackResult2, key, reconcile]
|
|
1562
|
+
);
|
|
1563
|
+
}
|
|
1564
|
+
function useMnemonicKeyState(shared, config) {
|
|
1565
|
+
const {
|
|
1566
|
+
api,
|
|
1567
|
+
key,
|
|
1568
|
+
codecOpt,
|
|
1569
|
+
schema,
|
|
1570
|
+
onMount,
|
|
1571
|
+
onChange,
|
|
1572
|
+
listenCrossTab,
|
|
1573
|
+
getFallback,
|
|
1574
|
+
getServerValue,
|
|
1575
|
+
developmentRuntime,
|
|
1576
|
+
contractFingerprint,
|
|
1577
|
+
hasMounted,
|
|
1578
|
+
setHasMounted,
|
|
1579
|
+
hydrationMode,
|
|
1580
|
+
ssrOptions
|
|
1581
|
+
} = shared;
|
|
1582
|
+
const { decodeForRead, encodeForWrite, active = true, additionalDevWarnings, onDecodedEffect } = config;
|
|
1583
|
+
const getServerRawSnapshot = useCallback(
|
|
1584
|
+
() => ssrOptions?.serverValue === void 0 ? null : SSR_SNAPSHOT_TOKEN,
|
|
1585
|
+
[ssrOptions?.serverValue]
|
|
1586
|
+
);
|
|
1587
|
+
const deferStorageRead = hydrationMode === "client-only" && !hasMounted;
|
|
1588
|
+
const subscribe = useCallback(
|
|
1589
|
+
(listener) => {
|
|
1590
|
+
if (!active) {
|
|
1591
|
+
return () => void 0;
|
|
1592
|
+
}
|
|
1593
|
+
if (deferStorageRead) {
|
|
1594
|
+
return () => void 0;
|
|
1595
|
+
}
|
|
1596
|
+
return api.subscribeRaw(key, listener);
|
|
1597
|
+
},
|
|
1598
|
+
[active, api, deferStorageRead, key]
|
|
1599
|
+
);
|
|
1600
|
+
const raw = useSyncExternalStore(
|
|
1601
|
+
subscribe,
|
|
1602
|
+
() => active && !deferStorageRead ? api.getRawSnapshot(key) : getServerRawSnapshot(),
|
|
1603
|
+
getServerRawSnapshot
|
|
1604
|
+
);
|
|
1605
|
+
const decoded = useMemo(() => {
|
|
1606
|
+
if (raw === SSR_SNAPSHOT_TOKEN) {
|
|
1607
|
+
return withReadMetadata(getServerValue());
|
|
1608
|
+
}
|
|
1609
|
+
return decodeForRead(raw);
|
|
1610
|
+
}, [decodeForRead, getServerValue, raw]);
|
|
1611
|
+
const value = decoded.value;
|
|
1612
|
+
useEffect(() => {
|
|
1613
|
+
if (!active) return;
|
|
1614
|
+
if (!developmentRuntime) return;
|
|
1615
|
+
const globalWindow = globalThis.window;
|
|
1616
|
+
if (listenCrossTab && (api.crossTabSyncMode ?? "none") === "none" && globalWindow !== void 0) {
|
|
1617
|
+
warnOnce(
|
|
1618
|
+
api,
|
|
1619
|
+
`listenCrossTab:${key}`,
|
|
1620
|
+
`[Mnemonic] useMnemonicKey("${key}") enabled listenCrossTab, but the active storage backend may not be able to notify external changes. If you're using a custom Storage-like wrapper around localStorage, ensure it forwards browser "storage" events or implements storage.onExternalChange(...); otherwise, use localStorage or implement storage.onExternalChange(...) on your custom backend.`
|
|
1621
|
+
);
|
|
1622
|
+
}
|
|
1623
|
+
additionalDevWarnings?.({
|
|
1624
|
+
api,
|
|
1625
|
+
key,
|
|
1626
|
+
listenCrossTab,
|
|
1627
|
+
codecOpt,
|
|
1628
|
+
schema,
|
|
1629
|
+
warnOnce: (id, message) => warnOnce(api, id, message)
|
|
1630
|
+
});
|
|
1631
|
+
let keyContracts = diagnosticContractRegistry.get(api);
|
|
1632
|
+
if (!keyContracts) {
|
|
1633
|
+
keyContracts = /* @__PURE__ */ new Map();
|
|
1634
|
+
diagnosticContractRegistry.set(api, keyContracts);
|
|
1635
|
+
}
|
|
1636
|
+
if (contractFingerprint === null) {
|
|
1637
|
+
return;
|
|
1638
|
+
}
|
|
1639
|
+
const previousContract = keyContracts.get(key);
|
|
1640
|
+
if (previousContract === void 0) {
|
|
1641
|
+
keyContracts.set(key, contractFingerprint);
|
|
1642
|
+
return;
|
|
1643
|
+
}
|
|
1644
|
+
if (previousContract === contractFingerprint) {
|
|
1645
|
+
return;
|
|
1646
|
+
}
|
|
1647
|
+
warnOnce(
|
|
1648
|
+
api,
|
|
1649
|
+
`contract-conflict:${key}`,
|
|
1650
|
+
`[Mnemonic] Conflicting useMnemonicKey contracts detected for key "${key}" in namespace "${api.prefix.slice(0, -1)}". Reuse a shared descriptor with defineMnemonicKey(...) or align defaultValue/codec/schema/reconcile options so every consumer describes the same persisted contract.`
|
|
1651
|
+
);
|
|
1652
|
+
}, [
|
|
1653
|
+
active,
|
|
1654
|
+
additionalDevWarnings,
|
|
1655
|
+
api,
|
|
1656
|
+
key,
|
|
1657
|
+
developmentRuntime,
|
|
1658
|
+
contractFingerprint,
|
|
1659
|
+
listenCrossTab,
|
|
1660
|
+
codecOpt,
|
|
1661
|
+
schema,
|
|
1662
|
+
api.crossTabSyncMode
|
|
1663
|
+
]);
|
|
1664
|
+
useEffect(() => {
|
|
1665
|
+
if (hasMounted) return;
|
|
1666
|
+
setHasMounted(true);
|
|
1667
|
+
}, [hasMounted, setHasMounted]);
|
|
1668
|
+
useEffect(() => {
|
|
1669
|
+
if (!active) return;
|
|
1670
|
+
if (decoded.rewriteRaw && decoded.rewriteRaw !== raw) {
|
|
1671
|
+
api.setRaw(key, decoded.rewriteRaw);
|
|
1672
|
+
}
|
|
1673
|
+
}, [active, api, decoded.rewriteRaw, key, raw]);
|
|
1674
|
+
useEffect(() => {
|
|
1675
|
+
if (!active) return;
|
|
1676
|
+
onDecodedEffect?.(decoded);
|
|
1677
|
+
}, [active, decoded, onDecodedEffect]);
|
|
1678
|
+
const prevRef = useRef(value);
|
|
1679
|
+
const mounted = useRef(false);
|
|
1680
|
+
useEffect(() => {
|
|
1681
|
+
if (!active) return;
|
|
1682
|
+
if (mounted.current) return;
|
|
1683
|
+
mounted.current = true;
|
|
1684
|
+
onMount?.(value);
|
|
1685
|
+
prevRef.current = value;
|
|
1686
|
+
}, [active]);
|
|
1687
|
+
useEffect(() => {
|
|
1688
|
+
if (!active) return;
|
|
1689
|
+
const prev = prevRef.current;
|
|
1690
|
+
if (Object.is(prev, value)) return;
|
|
1691
|
+
prevRef.current = value;
|
|
1692
|
+
onChange?.(value, prev);
|
|
1693
|
+
}, [active, value, onChange]);
|
|
1694
|
+
useEffect(() => {
|
|
1695
|
+
if (!active) return;
|
|
1696
|
+
if (!listenCrossTab) return;
|
|
1697
|
+
const globalWindow = globalThis.window;
|
|
1698
|
+
if (globalWindow === void 0) return;
|
|
1699
|
+
const storageKey = api.prefix + key;
|
|
1700
|
+
const handler = (e) => {
|
|
1701
|
+
if (e.key === null) {
|
|
1702
|
+
api.removeRaw(key);
|
|
1703
|
+
return;
|
|
1704
|
+
}
|
|
1705
|
+
if (e.key !== storageKey) return;
|
|
1706
|
+
if (e.newValue == null) {
|
|
1707
|
+
api.removeRaw(key);
|
|
1708
|
+
return;
|
|
1588
1709
|
}
|
|
1589
|
-
|
|
1590
|
-
cache.set(key, raw);
|
|
1591
|
-
return raw;
|
|
1710
|
+
api.setRaw(key, e.newValue);
|
|
1592
1711
|
};
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
quotaErrorLogged = true;
|
|
1610
|
-
}
|
|
1611
|
-
logAccessError(err);
|
|
1712
|
+
globalWindow.addEventListener("storage", handler);
|
|
1713
|
+
return () => globalWindow.removeEventListener("storage", handler);
|
|
1714
|
+
}, [active, listenCrossTab, api, key]);
|
|
1715
|
+
const set = useMemo(() => {
|
|
1716
|
+
if (!active) {
|
|
1717
|
+
return () => void 0;
|
|
1718
|
+
}
|
|
1719
|
+
return (next) => {
|
|
1720
|
+
const nextVal = typeof next === "function" ? next(decodeForRead(api.getRawSnapshot(key)).value) : next;
|
|
1721
|
+
try {
|
|
1722
|
+
const encoded = encodeForWrite(nextVal);
|
|
1723
|
+
api.setRaw(key, encoded);
|
|
1724
|
+
} catch (err) {
|
|
1725
|
+
if (err instanceof SchemaError) {
|
|
1726
|
+
console.error(`[Mnemonic] Schema error for key "${key}" (${err.code}):`, err.message);
|
|
1727
|
+
return;
|
|
1612
1728
|
}
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
};
|
|
1617
|
-
const removeRaw = (key) => {
|
|
1618
|
-
cache.set(key, null);
|
|
1619
|
-
if (st && !asyncContractViolationDetected) {
|
|
1620
|
-
try {
|
|
1621
|
-
const result = st.removeItem(fullKey(key));
|
|
1622
|
-
if (isPromiseLike(result)) {
|
|
1623
|
-
handleAsyncStorageContractViolation("removeItem", result);
|
|
1624
|
-
} else {
|
|
1625
|
-
accessErrorLogged = false;
|
|
1626
|
-
}
|
|
1627
|
-
} catch (err) {
|
|
1628
|
-
logAccessError(err);
|
|
1729
|
+
if (err instanceof CodecError) {
|
|
1730
|
+
console.error(`[Mnemonic] Codec encode error for key "${key}":`, err.message);
|
|
1731
|
+
return;
|
|
1629
1732
|
}
|
|
1733
|
+
console.error(`[Mnemonic] Failed to persist key "${key}":`, err);
|
|
1630
1734
|
}
|
|
1631
|
-
emit(key);
|
|
1632
|
-
bumpDevToolsVersion(devToolsRoot, namespace, `remove:${key}`);
|
|
1633
|
-
};
|
|
1634
|
-
const subscribeRaw = (key, listener) => {
|
|
1635
|
-
let set = listeners.get(key);
|
|
1636
|
-
if (!set) {
|
|
1637
|
-
set = /* @__PURE__ */ new Set();
|
|
1638
|
-
listeners.set(key, set);
|
|
1639
|
-
}
|
|
1640
|
-
set.add(listener);
|
|
1641
|
-
readThrough(key);
|
|
1642
|
-
return () => {
|
|
1643
|
-
const s = listeners.get(key);
|
|
1644
|
-
if (!s) return;
|
|
1645
|
-
s.delete(listener);
|
|
1646
|
-
if (s.size === 0) listeners.delete(key);
|
|
1647
|
-
};
|
|
1648
|
-
};
|
|
1649
|
-
const getRawSnapshot = (key) => readThrough(key);
|
|
1650
|
-
const keys = () => {
|
|
1651
|
-
if (asyncContractViolationDetected) {
|
|
1652
|
-
return Array.from(cache.entries()).filter(([, value]) => value != null).map(([key]) => key);
|
|
1653
|
-
}
|
|
1654
|
-
if (!canEnumerateKeys) return [];
|
|
1655
|
-
return enumerateNamespaceKeys(st, prefix, storageAccessCallbacks);
|
|
1656
1735
|
};
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1736
|
+
}, [active, api, key, decodeForRead, encodeForWrite]);
|
|
1737
|
+
const reset = useMemo(() => {
|
|
1738
|
+
if (!active) {
|
|
1739
|
+
return () => void 0;
|
|
1740
|
+
}
|
|
1741
|
+
return () => {
|
|
1742
|
+
const v = getFallback();
|
|
1743
|
+
try {
|
|
1744
|
+
const encoded = encodeForWrite(v);
|
|
1745
|
+
api.setRaw(key, encoded);
|
|
1746
|
+
} catch (err) {
|
|
1747
|
+
if (err instanceof SchemaError) {
|
|
1748
|
+
console.error(`[Mnemonic] Schema error for key "${key}" (${err.code}):`, err.message);
|
|
1749
|
+
return;
|
|
1750
|
+
}
|
|
1751
|
+
if (err instanceof CodecError) {
|
|
1752
|
+
console.error(`[Mnemonic] Codec encode error for key "${key}":`, err.message);
|
|
1753
|
+
}
|
|
1754
|
+
return;
|
|
1662
1755
|
}
|
|
1663
|
-
return out;
|
|
1664
|
-
};
|
|
1665
|
-
const reloadFromStorage = createReloadFromStorage({
|
|
1666
|
-
storage: st,
|
|
1667
|
-
hasAsyncContractViolation: () => asyncContractViolationDetected,
|
|
1668
|
-
prefix,
|
|
1669
|
-
listeners,
|
|
1670
|
-
cache,
|
|
1671
|
-
emit,
|
|
1672
|
-
callbacks: storageAccessCallbacks,
|
|
1673
|
-
devToolsRoot,
|
|
1674
|
-
namespace
|
|
1675
|
-
});
|
|
1676
|
-
const store2 = {
|
|
1677
|
-
prefix,
|
|
1678
|
-
canEnumerateKeys,
|
|
1679
|
-
subscribeRaw,
|
|
1680
|
-
getRawSnapshot,
|
|
1681
|
-
setRaw: writeRaw,
|
|
1682
|
-
removeRaw,
|
|
1683
|
-
keys,
|
|
1684
|
-
dump,
|
|
1685
|
-
reloadFromStorage,
|
|
1686
|
-
schemaMode,
|
|
1687
|
-
ssrHydration,
|
|
1688
|
-
crossTabSyncMode,
|
|
1689
|
-
...schemaRegistry ? { schemaRegistry } : {}
|
|
1690
1756
|
};
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
store: store2,
|
|
1696
|
-
dump,
|
|
1697
|
-
keys,
|
|
1698
|
-
readThrough,
|
|
1699
|
-
writeRaw,
|
|
1700
|
-
removeRaw
|
|
1701
|
-
});
|
|
1757
|
+
}, [active, api, key, getFallback, encodeForWrite]);
|
|
1758
|
+
const remove = useMemo(() => {
|
|
1759
|
+
if (!active) {
|
|
1760
|
+
return () => void 0;
|
|
1702
1761
|
}
|
|
1703
|
-
return
|
|
1704
|
-
}, [
|
|
1705
|
-
|
|
1706
|
-
() =>
|
|
1707
|
-
|
|
1708
|
-
|
|
1762
|
+
return () => api.removeRaw(key);
|
|
1763
|
+
}, [active, api, key]);
|
|
1764
|
+
return useMemo(
|
|
1765
|
+
() => ({
|
|
1766
|
+
value,
|
|
1767
|
+
set,
|
|
1768
|
+
reset,
|
|
1769
|
+
remove
|
|
1709
1770
|
}),
|
|
1710
|
-
[
|
|
1711
|
-
);
|
|
1712
|
-
useEffect(() => {
|
|
1713
|
-
if (!storage?.onExternalChange) return;
|
|
1714
|
-
return storage.onExternalChange((changedKeys) => store.reloadFromStorage(changedKeys));
|
|
1715
|
-
}, [storage, store]);
|
|
1716
|
-
return /* @__PURE__ */ jsx(MnemonicContext.Provider, { value: store, children: /* @__PURE__ */ jsx(MnemonicOptionalBridgeProvider, { bridge: optionalBridge, children }) });
|
|
1717
|
-
}
|
|
1718
|
-
function throwCoreProviderSchemaImportError(propName) {
|
|
1719
|
-
throw new Error(
|
|
1720
|
-
`[Mnemonic] MnemonicProvider from react-mnemonic/core does not support ${propName}. Import MnemonicProvider from "react-mnemonic/schema" or "react-mnemonic" for schema validation, autoschema, and migration support.`
|
|
1771
|
+
[value, set, reset, remove]
|
|
1721
1772
|
);
|
|
1722
1773
|
}
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
throwCoreProviderSchemaImportError("schemaMode");
|
|
1726
|
-
}
|
|
1727
|
-
if (props.schemaRegistry !== void 0) {
|
|
1728
|
-
throwCoreProviderSchemaImportError("schemaRegistry");
|
|
1729
|
-
}
|
|
1730
|
-
}
|
|
1731
|
-
function MnemonicProvider2(props) {
|
|
1732
|
-
assertNoSchemaProps(props);
|
|
1733
|
-
return /* @__PURE__ */ jsx(MnemonicProvider, { ...props });
|
|
1734
|
-
}
|
|
1774
|
+
|
|
1775
|
+
// src/Mnemonic/use-core.ts
|
|
1735
1776
|
function throwCoreSchemaImportError(key) {
|
|
1736
1777
|
throw new Error(
|
|
1737
1778
|
`[Mnemonic] useMnemonicKey("${key}") requested schema features from react-mnemonic/core. Import useMnemonicKey from "react-mnemonic/schema" or "react-mnemonic" for schema validation, autoschema, and migration support.`
|