memorio 4.9.35 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +3 -3
- package/README.md +307 -359
- package/SECURITY.md +17 -1
- package/SUMMARY.md +59 -45
- package/adr/001-state-proxy-model.md +96 -0
- package/adr/002-observer-semantics.md +180 -0
- package/adr/003-deep-mutation-semantics.md +129 -0
- package/adr/004-array-mutation-semantics.md +128 -0
- package/adr/005-scheduler-contract.md +149 -0
- package/adr/006-context-isolation.md +92 -0
- package/adr/007-mutation-records.md +118 -0
- package/adr/008-transactions.md +106 -0
- package/adr/009-history-model.md +110 -0
- package/adr/README.md +46 -0
- package/adr/template.md +49 -0
- package/examples/basic.ts +115 -115
- package/examples/browser-vanilla.html +358 -358
- package/examples/cache.ts +72 -72
- package/examples/cross-platform-guards.ts +57 -57
- package/examples/history.ts +104 -0
- package/examples/idb.ts +109 -109
- package/examples/multi-tenant-context.ts +44 -44
- package/examples/node-server.ts +308 -308
- package/examples/observer.ts +60 -60
- package/examples/platform.ts +115 -115
- package/examples/react-app.tsx +362 -362
- package/examples/react-observer.tsx +63 -63
- package/examples/semantic-memory.ts +60 -60
- package/examples/session-advanced.ts +91 -91
- package/examples/sqlite-batched-writes.ts +57 -57
- package/examples/state-advanced.ts +89 -89
- package/examples/store-advanced.ts +117 -117
- package/examples/sync.ts +90 -0
- package/examples/typed-and-schema.ts +102 -100
- package/examples/useObserver.tsx +140 -141
- package/global.cjs +4594 -0
- package/global.d.ts +8 -0
- package/global.js +4532 -0
- package/index.cjs +700 -678
- package/index.d.ts +1 -0
- package/index.js +680 -677
- package/llms.txt +72 -4
- package/markdown/AUDIT-REPORT.md +135 -0
- package/markdown/CACHE.md +100 -0
- package/markdown/CHANGELOG.md +243 -0
- package/markdown/DEVTOOLS.md +129 -0
- package/markdown/DISPATCH.md +177 -0
- package/markdown/HISTORY.md +199 -0
- package/markdown/IDB.md +178 -0
- package/markdown/IMPORT.md +153 -0
- package/markdown/INSPECT.md +123 -0
- package/markdown/LOGGER.md +154 -0
- package/markdown/MEMORY-ATTACHMENT.md +96 -0
- package/markdown/MEMORY.md +162 -0
- package/markdown/OBSERVER.md +209 -0
- package/markdown/PLATFORM.md +271 -0
- package/markdown/PROJECT.md +311 -0
- package/markdown/SCHEMA.md +176 -0
- package/markdown/SECURITY.md +330 -0
- package/markdown/SESSION.md +165 -0
- package/markdown/SQLITE.md +190 -0
- package/markdown/STATE.md +160 -0
- package/markdown/STORE.md +171 -0
- package/markdown/SYNC.md +319 -0
- package/markdown/TYPED.md +165 -0
- package/markdown/USEOBSERVER.md +257 -0
- package/modules/redux.cjs +320 -167
- package/modules/redux.cjs.map +1 -1
- package/modules/redux.js +320 -167
- package/modules/redux.js.map +1 -1
- package/package.json +13 -3
- package/types/env.d.ts +19 -9
- package/types/exports.d.ts +20 -0
- package/types/history.d.ts +13 -1
- package/types/memorio.d.ts +17 -5
- package/types/mutation.d.ts +75 -0
package/modules/redux.cjs
CHANGED
|
@@ -178,7 +178,10 @@ var DEV;
|
|
|
178
178
|
var init_env = __esm({
|
|
179
179
|
"core/env.ts"() {
|
|
180
180
|
init_cjs_shims();
|
|
181
|
-
DEV =
|
|
181
|
+
DEV = false ;
|
|
182
|
+
if (typeof globalThis !== "undefined") {
|
|
183
|
+
globalThis["__DEV__"] = DEV;
|
|
184
|
+
}
|
|
182
185
|
}
|
|
183
186
|
});
|
|
184
187
|
|
|
@@ -808,9 +811,25 @@ var dispatch = {
|
|
|
808
811
|
const wrapped = /* @__PURE__ */ __name((e) => {
|
|
809
812
|
const target = e;
|
|
810
813
|
if (globalThis.queueMicrotask) {
|
|
811
|
-
globalThis.queueMicrotask(() =>
|
|
814
|
+
globalThis.queueMicrotask(() => {
|
|
815
|
+
try {
|
|
816
|
+
cb(target);
|
|
817
|
+
} catch (err) {
|
|
818
|
+
if (typeof console !== "undefined") {
|
|
819
|
+
console.error("[memorio dispatch] observer error:", err);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
});
|
|
812
823
|
} else {
|
|
813
|
-
Promise.resolve().then(() =>
|
|
824
|
+
Promise.resolve().then(() => {
|
|
825
|
+
try {
|
|
826
|
+
cb(target);
|
|
827
|
+
} catch (err) {
|
|
828
|
+
if (typeof console !== "undefined") {
|
|
829
|
+
console.error("[memorio dispatch] observer error:", err);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
});
|
|
814
833
|
}
|
|
815
834
|
}, "wrapped");
|
|
816
835
|
globalThis.addEventListener(name, wrapped);
|
|
@@ -1217,10 +1236,211 @@ if (globalThis[STORE_INSTANCE_KEY]) {
|
|
|
1217
1236
|
|
|
1218
1237
|
// functions/state/index.ts
|
|
1219
1238
|
init_env();
|
|
1239
|
+
|
|
1240
|
+
// core/mutation/engine.ts
|
|
1241
|
+
init_cjs_shims();
|
|
1242
|
+
init_internal();
|
|
1243
|
+
|
|
1244
|
+
// core/hlc.ts
|
|
1245
|
+
init_cjs_shims();
|
|
1246
|
+
var NODE_ID_KEY = "__memorio_hlc_node";
|
|
1247
|
+
function getNodeId() {
|
|
1248
|
+
if (typeof globalThis === "undefined") return "node";
|
|
1249
|
+
try {
|
|
1250
|
+
if (typeof sessionStorage !== "undefined" && sessionStorage) {
|
|
1251
|
+
let id = sessionStorage.getItem(NODE_ID_KEY);
|
|
1252
|
+
if (!id) {
|
|
1253
|
+
id = generateNodeId();
|
|
1254
|
+
sessionStorage.setItem(NODE_ID_KEY, id);
|
|
1255
|
+
}
|
|
1256
|
+
return id;
|
|
1257
|
+
}
|
|
1258
|
+
} catch {
|
|
1259
|
+
}
|
|
1260
|
+
if (!globalThis.__memorioHlcNodeId) {
|
|
1261
|
+
globalThis.__memorioHlcNodeId = generateNodeId();
|
|
1262
|
+
}
|
|
1263
|
+
return globalThis.__memorioHlcNodeId;
|
|
1264
|
+
}
|
|
1265
|
+
__name(getNodeId, "getNodeId");
|
|
1266
|
+
function generateNodeId() {
|
|
1267
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
1268
|
+
return crypto.randomUUID().slice(0, 8);
|
|
1269
|
+
}
|
|
1270
|
+
return Math.random().toString(36).slice(2, 10);
|
|
1271
|
+
}
|
|
1272
|
+
__name(generateNodeId, "generateNodeId");
|
|
1273
|
+
var HLC_KEY = "__memorio_hlc_local";
|
|
1274
|
+
var local = globalThis[HLC_KEY] || {
|
|
1275
|
+
physical: 0,
|
|
1276
|
+
logical: 0,
|
|
1277
|
+
node: getNodeId()
|
|
1278
|
+
};
|
|
1279
|
+
if (!globalThis[HLC_KEY]) {
|
|
1280
|
+
globalThis[HLC_KEY] = local;
|
|
1281
|
+
}
|
|
1282
|
+
function tick(_incoming) {
|
|
1283
|
+
const now2 = Date.now();
|
|
1284
|
+
const base = local.physical;
|
|
1285
|
+
let physical;
|
|
1286
|
+
let logical;
|
|
1287
|
+
if (now2 > base) {
|
|
1288
|
+
physical = now2;
|
|
1289
|
+
logical = 0;
|
|
1290
|
+
} else if (now2 < base) {
|
|
1291
|
+
physical = base;
|
|
1292
|
+
logical = local.logical + 1;
|
|
1293
|
+
} else {
|
|
1294
|
+
physical = base;
|
|
1295
|
+
logical = local.logical + 1;
|
|
1296
|
+
}
|
|
1297
|
+
local.physical = physical;
|
|
1298
|
+
local.logical = logical;
|
|
1299
|
+
return { physical, logical, node: local.node };
|
|
1300
|
+
}
|
|
1301
|
+
__name(tick, "tick");
|
|
1302
|
+
function receive(remote) {
|
|
1303
|
+
const now2 = Date.now();
|
|
1304
|
+
if (remote.physical > local.physical) {
|
|
1305
|
+
local.physical = remote.physical > now2 ? remote.physical : now2;
|
|
1306
|
+
local.logical = 1;
|
|
1307
|
+
} else if (remote.physical === local.physical) {
|
|
1308
|
+
local.logical = Math.max(local.logical, remote.logical) + 1;
|
|
1309
|
+
}
|
|
1310
|
+
local.node = getNodeId();
|
|
1311
|
+
return { ...local };
|
|
1312
|
+
}
|
|
1313
|
+
__name(receive, "receive");
|
|
1314
|
+
function compare(a, b) {
|
|
1315
|
+
if (a.physical !== b.physical) {
|
|
1316
|
+
return a.physical < b.physical ? -1 : 1;
|
|
1317
|
+
}
|
|
1318
|
+
if (a.logical !== b.logical) {
|
|
1319
|
+
return a.logical < b.logical ? -1 : 1;
|
|
1320
|
+
}
|
|
1321
|
+
if (a.node !== b.node) {
|
|
1322
|
+
return a.node < b.node ? -1 : 1;
|
|
1323
|
+
}
|
|
1324
|
+
return 0;
|
|
1325
|
+
}
|
|
1326
|
+
__name(compare, "compare");
|
|
1327
|
+
function happenedBefore(a, b) {
|
|
1328
|
+
return compare(a, b) < 0;
|
|
1329
|
+
}
|
|
1330
|
+
__name(happenedBefore, "happenedBefore");
|
|
1331
|
+
function serialize(hlc2) {
|
|
1332
|
+
return `hlc:${hlc2.physical}:${hlc2.logical}:${hlc2.node}`;
|
|
1333
|
+
}
|
|
1334
|
+
__name(serialize, "serialize");
|
|
1335
|
+
function parse(serialized) {
|
|
1336
|
+
if (!serialized.startsWith("hlc:")) return null;
|
|
1337
|
+
const parts = serialized.split(":");
|
|
1338
|
+
if (parts.length !== 4) return null;
|
|
1339
|
+
const physical = parseInt(parts[1], 10);
|
|
1340
|
+
const logical = parseInt(parts[2], 10);
|
|
1341
|
+
const node = parts[3];
|
|
1342
|
+
if (isNaN(physical) || isNaN(logical)) return null;
|
|
1343
|
+
return { physical, logical, node };
|
|
1344
|
+
}
|
|
1345
|
+
__name(parse, "parse");
|
|
1346
|
+
var hlc = {
|
|
1347
|
+
tick,
|
|
1348
|
+
receive,
|
|
1349
|
+
compare,
|
|
1350
|
+
happenedBefore,
|
|
1351
|
+
serialize,
|
|
1352
|
+
parse,
|
|
1353
|
+
getNodeId
|
|
1354
|
+
};
|
|
1355
|
+
|
|
1356
|
+
// core/mutation/id.ts
|
|
1357
|
+
init_cjs_shims();
|
|
1358
|
+
function generateMutationId() {
|
|
1359
|
+
const now2 = hlc.tick();
|
|
1360
|
+
return "m_" + encodeHLC(now2);
|
|
1361
|
+
}
|
|
1362
|
+
__name(generateMutationId, "generateMutationId");
|
|
1363
|
+
function encodeHLC(hlcValue) {
|
|
1364
|
+
const timePart = hlcValue.physical.toString(36);
|
|
1365
|
+
const logicalPart = hlcValue.logical.toString(36).padStart(2, "0");
|
|
1366
|
+
const nodePart = hlcValue.node.slice(0, 4);
|
|
1367
|
+
return `${timePart}${logicalPart}${nodePart}`;
|
|
1368
|
+
}
|
|
1369
|
+
__name(encodeHLC, "encodeHLC");
|
|
1370
|
+
|
|
1371
|
+
// core/mutation/patch.ts
|
|
1372
|
+
init_cjs_shims();
|
|
1373
|
+
|
|
1374
|
+
// core/mutation/transaction.ts
|
|
1375
|
+
init_cjs_shims();
|
|
1376
|
+
init_internal();
|
|
1377
|
+
var TX_REGISTRY_KEY = "__memorio_transaction_registry__";
|
|
1378
|
+
var registry = globalThis[TX_REGISTRY_KEY] || /* @__PURE__ */ new Map();
|
|
1379
|
+
if (!globalThis[TX_REGISTRY_KEY]) {
|
|
1380
|
+
globalThis[TX_REGISTRY_KEY] = registry;
|
|
1381
|
+
}
|
|
1382
|
+
var TX_STACK_KEY = "__memorio_transaction_stack__";
|
|
1383
|
+
var txStack = globalThis[TX_STACK_KEY] || [];
|
|
1384
|
+
if (!globalThis[TX_STACK_KEY]) {
|
|
1385
|
+
globalThis[TX_STACK_KEY] = txStack;
|
|
1386
|
+
}
|
|
1387
|
+
function getActiveTxId() {
|
|
1388
|
+
return txStack.length > 0 ? txStack[txStack.length - 1] : null;
|
|
1389
|
+
}
|
|
1390
|
+
__name(getActiveTxId, "getActiveTxId");
|
|
1391
|
+
function tagForTransaction() {
|
|
1392
|
+
const txId = getActiveTxId();
|
|
1393
|
+
if (!txId) return void 0;
|
|
1394
|
+
const ctx = registry.get(txId);
|
|
1395
|
+
if (ctx && ctx.active) {
|
|
1396
|
+
return txId;
|
|
1397
|
+
}
|
|
1398
|
+
return void 0;
|
|
1399
|
+
}
|
|
1400
|
+
__name(tagForTransaction, "tagForTransaction");
|
|
1401
|
+
function registerMutationInTransaction(mutationId) {
|
|
1402
|
+
const txId = getActiveTxId();
|
|
1403
|
+
if (!txId) return;
|
|
1404
|
+
const ctx = registry.get(txId);
|
|
1405
|
+
if (ctx && ctx.active) {
|
|
1406
|
+
ctx.mutations.push(mutationId);
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
__name(registerMutationInTransaction, "registerMutationInTransaction");
|
|
1410
|
+
|
|
1411
|
+
// core/mutation/engine.ts
|
|
1412
|
+
function createMutation(path, operation, before, after, options) {
|
|
1413
|
+
const now2 = hlc.tick();
|
|
1414
|
+
const mutation = {
|
|
1415
|
+
id: generateMutationId(),
|
|
1416
|
+
path,
|
|
1417
|
+
operation,
|
|
1418
|
+
after,
|
|
1419
|
+
before,
|
|
1420
|
+
timestamp: now2.physical,
|
|
1421
|
+
hlc: hlc.serialize(now2),
|
|
1422
|
+
context: options?.context || internal_default.currentContext || void 0,
|
|
1423
|
+
source: options?.source,
|
|
1424
|
+
transactionId: options?.transactionId || (operation !== "length" ? tagForTransaction() : void 0),
|
|
1425
|
+
// Legacy fields for backward compatibility
|
|
1426
|
+
action: operation === "set" ? "set" : "delete",
|
|
1427
|
+
newValue: after,
|
|
1428
|
+
previousValue: before
|
|
1429
|
+
};
|
|
1430
|
+
if (mutation.transactionId) {
|
|
1431
|
+
registerMutationInTransaction(mutation.id);
|
|
1432
|
+
}
|
|
1433
|
+
return mutation;
|
|
1434
|
+
}
|
|
1435
|
+
__name(createMutation, "createMutation");
|
|
1436
|
+
|
|
1437
|
+
// functions/state/index.ts
|
|
1220
1438
|
var TARGET = /* @__PURE__ */ Symbol("memorio.target");
|
|
1221
1439
|
var wrapperCache = /* @__PURE__ */ new WeakMap();
|
|
1440
|
+
var arrayRootDedup = /* @__PURE__ */ new Set();
|
|
1222
1441
|
var deepRaw = /* @__PURE__ */ __name((val, seen = null) => {
|
|
1223
1442
|
if (val === null || typeof val !== "object") return val;
|
|
1443
|
+
if (!seen) seen = /* @__PURE__ */ new WeakSet();
|
|
1224
1444
|
if (val[TARGET] !== void 0) return deepRaw(val[TARGET], seen);
|
|
1225
1445
|
if (seen) {
|
|
1226
1446
|
if (seen.has(val)) return val;
|
|
@@ -1283,8 +1503,8 @@ var buildProxy = /* @__PURE__ */ __name((obj, callback, tree = []) => {
|
|
|
1283
1503
|
if (prop === "removeAll") {
|
|
1284
1504
|
return () => {
|
|
1285
1505
|
try {
|
|
1286
|
-
for (const key
|
|
1287
|
-
if (typeof target[key] !== "function" && !["list", "remove", "removeAll", "lock", "unlock"].includes(key)) {
|
|
1506
|
+
for (const key of Reflect.ownKeys(target)) {
|
|
1507
|
+
if (typeof target[key] !== "function" && !["list", "remove", "removeAll", "lock", "unlock", "persist"].includes(key)) {
|
|
1288
1508
|
delete target[key];
|
|
1289
1509
|
}
|
|
1290
1510
|
}
|
|
@@ -1358,6 +1578,16 @@ var buildProxy = /* @__PURE__ */ __name((obj, callback, tree = []) => {
|
|
|
1358
1578
|
}
|
|
1359
1579
|
);
|
|
1360
1580
|
event("state." + path);
|
|
1581
|
+
if (Array.isArray(target) && tree.length > 0) {
|
|
1582
|
+
const rootName = "state." + tree.join(".");
|
|
1583
|
+
if (!arrayRootDedup.has(rootName)) {
|
|
1584
|
+
arrayRootDedup.add(rootName);
|
|
1585
|
+
event(rootName);
|
|
1586
|
+
if (typeof globalThis.queueMicrotask === "function") {
|
|
1587
|
+
globalThis.queueMicrotask(() => arrayRootDedup.clear());
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1361
1591
|
Reflect.set(target, key, rawValue);
|
|
1362
1592
|
internal_default.bumpStateVersion();
|
|
1363
1593
|
if (target[key] && typeof target[key] === "object") {
|
|
@@ -1414,6 +1644,17 @@ var buildProxy = /* @__PURE__ */ __name((obj, callback, tree = []) => {
|
|
|
1414
1644
|
const path = objPath(prop, tree);
|
|
1415
1645
|
const previousValue = Reflect.get(target, prop);
|
|
1416
1646
|
callback({ action: "delete", path, target, previousValue });
|
|
1647
|
+
event("state." + path);
|
|
1648
|
+
if (Array.isArray(target) && tree.length > 0) {
|
|
1649
|
+
const rootName = "state." + tree.join(".");
|
|
1650
|
+
if (!arrayRootDedup.has(rootName)) {
|
|
1651
|
+
arrayRootDedup.add(rootName);
|
|
1652
|
+
event(rootName);
|
|
1653
|
+
if (typeof globalThis.queueMicrotask === "function") {
|
|
1654
|
+
globalThis.queueMicrotask(() => arrayRootDedup.clear());
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1417
1658
|
return Reflect.deleteProperty(target, prop);
|
|
1418
1659
|
} catch (error) {
|
|
1419
1660
|
console.error("Error in deleteProperty trap:", error);
|
|
@@ -1435,57 +1676,83 @@ if (globalThis[STATE_INSTANCE_KEY]) {
|
|
|
1435
1676
|
state2 = globalThis[STATE_INSTANCE_KEY];
|
|
1436
1677
|
} else {
|
|
1437
1678
|
state2 = buildProxy({}, (mutation) => {
|
|
1438
|
-
if (internal_default.historyEnabled) {
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1679
|
+
if (internal_default.historyEnabled && internal_default._recording !== false) {
|
|
1680
|
+
const operation = mutation.action === "delete" ? "delete" : "set";
|
|
1681
|
+
const record = createMutation(
|
|
1682
|
+
mutation.path,
|
|
1683
|
+
operation,
|
|
1684
|
+
mutation.previousValue,
|
|
1685
|
+
mutation.newValue,
|
|
1686
|
+
{
|
|
1687
|
+
context: mutation.context,
|
|
1688
|
+
source: mutation.source
|
|
1689
|
+
}
|
|
1690
|
+
);
|
|
1691
|
+
internal_default.recordMutation(record);
|
|
1446
1692
|
}
|
|
1447
1693
|
});
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1694
|
+
const _rawTarget = state2[TARGET];
|
|
1695
|
+
try {
|
|
1696
|
+
Object.defineProperty(_rawTarget, "lock", {
|
|
1697
|
+
value: /* @__PURE__ */ __name(() => {
|
|
1698
|
+
internal_default.locked = true;
|
|
1699
|
+
message_default("State is now locked. All modifications are blocked.");
|
|
1700
|
+
}, "value"),
|
|
1701
|
+
writable: true,
|
|
1702
|
+
configurable: true,
|
|
1703
|
+
enumerable: false
|
|
1704
|
+
});
|
|
1705
|
+
Object.defineProperty(_rawTarget, "unlock", {
|
|
1706
|
+
value: /* @__PURE__ */ __name(() => {
|
|
1707
|
+
internal_default.locked = false;
|
|
1708
|
+
message_default("State is now unlocked. Modifications are allowed.");
|
|
1709
|
+
}, "value"),
|
|
1710
|
+
writable: true,
|
|
1711
|
+
configurable: true,
|
|
1712
|
+
enumerable: false
|
|
1713
|
+
});
|
|
1714
|
+
} catch (e) {
|
|
1715
|
+
if (DEV) console.warn("[memorio state] lock/unlock defineProperty failed:", e);
|
|
1716
|
+
}
|
|
1717
|
+
try {
|
|
1718
|
+
Object.defineProperty(_rawTarget, "persist", {
|
|
1719
|
+
value: /* @__PURE__ */ __name((path) => {
|
|
1720
|
+
if (typeof path !== "string" || !path.length) {
|
|
1721
|
+
if (DEV) console.warn('[memorio state] state.persist() requires a non-empty path string (e.g. "state.user")');
|
|
1722
|
+
return () => {
|
|
1723
|
+
};
|
|
1475
1724
|
}
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1725
|
+
const normalized = path.startsWith("state.") ? path : "state." + path;
|
|
1726
|
+
const key = normalized.replace(/^state\./, "");
|
|
1727
|
+
const segments = key.split(".");
|
|
1728
|
+
const initial = store.get(key);
|
|
1729
|
+
if (initial !== void 0 && initial !== null) {
|
|
1730
|
+
let acc = state2;
|
|
1731
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
1732
|
+
const seg = segments[i];
|
|
1733
|
+
if (acc[seg] === void 0 || acc[seg] === null) acc[seg] = {};
|
|
1734
|
+
acc = acc[seg];
|
|
1735
|
+
}
|
|
1736
|
+
if (acc) acc[segments[segments.length - 1]] = deepRaw(initial);
|
|
1737
|
+
}
|
|
1738
|
+
const handler = /* @__PURE__ */ __name(() => {
|
|
1739
|
+
let current = state2;
|
|
1740
|
+
for (const seg of segments) {
|
|
1741
|
+
current = current === void 0 || current === null ? void 0 : current[seg];
|
|
1742
|
+
}
|
|
1743
|
+
store.set(key, deepRaw(current));
|
|
1744
|
+
}, "handler");
|
|
1745
|
+
const off = dispatch.listen(normalized, handler);
|
|
1746
|
+
return typeof off === "function" ? off : () => {
|
|
1747
|
+
};
|
|
1748
|
+
}, "value"),
|
|
1749
|
+
writable: true,
|
|
1750
|
+
configurable: true,
|
|
1751
|
+
enumerable: false
|
|
1752
|
+
});
|
|
1753
|
+
} catch (e) {
|
|
1754
|
+
if (DEV) console.warn("[memorio state] persist defineProperty failed:", e);
|
|
1755
|
+
}
|
|
1489
1756
|
globalThis[STATE_INSTANCE_KEY] = state2;
|
|
1490
1757
|
}
|
|
1491
1758
|
|
|
@@ -1747,120 +2014,6 @@ if (globalThis[SESSION_INSTANCE_KEY]) {
|
|
|
1747
2014
|
|
|
1748
2015
|
// functions/memory/index.ts
|
|
1749
2016
|
init_idb();
|
|
1750
|
-
|
|
1751
|
-
// core/hlc.ts
|
|
1752
|
-
init_cjs_shims();
|
|
1753
|
-
var NODE_ID_KEY = "__memorio_hlc_node";
|
|
1754
|
-
function getNodeId() {
|
|
1755
|
-
if (typeof globalThis === "undefined") return "node";
|
|
1756
|
-
try {
|
|
1757
|
-
if (typeof sessionStorage !== "undefined" && sessionStorage) {
|
|
1758
|
-
let id = sessionStorage.getItem(NODE_ID_KEY);
|
|
1759
|
-
if (!id) {
|
|
1760
|
-
id = generateNodeId();
|
|
1761
|
-
sessionStorage.setItem(NODE_ID_KEY, id);
|
|
1762
|
-
}
|
|
1763
|
-
return id;
|
|
1764
|
-
}
|
|
1765
|
-
} catch {
|
|
1766
|
-
}
|
|
1767
|
-
if (!globalThis.__memorioHlcNodeId) {
|
|
1768
|
-
globalThis.__memorioHlcNodeId = generateNodeId();
|
|
1769
|
-
}
|
|
1770
|
-
return globalThis.__memorioHlcNodeId;
|
|
1771
|
-
}
|
|
1772
|
-
__name(getNodeId, "getNodeId");
|
|
1773
|
-
function generateNodeId() {
|
|
1774
|
-
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
1775
|
-
return crypto.randomUUID().slice(0, 8);
|
|
1776
|
-
}
|
|
1777
|
-
return Math.random().toString(36).slice(2, 10);
|
|
1778
|
-
}
|
|
1779
|
-
__name(generateNodeId, "generateNodeId");
|
|
1780
|
-
var HLC_KEY = "__memorio_hlc_local";
|
|
1781
|
-
var local = globalThis[HLC_KEY] || {
|
|
1782
|
-
physical: 0,
|
|
1783
|
-
logical: 0,
|
|
1784
|
-
node: getNodeId()
|
|
1785
|
-
};
|
|
1786
|
-
if (!globalThis[HLC_KEY]) {
|
|
1787
|
-
globalThis[HLC_KEY] = local;
|
|
1788
|
-
}
|
|
1789
|
-
function tick(_incoming) {
|
|
1790
|
-
const now2 = Date.now();
|
|
1791
|
-
const base = local.physical;
|
|
1792
|
-
let physical;
|
|
1793
|
-
let logical;
|
|
1794
|
-
if (now2 > base) {
|
|
1795
|
-
physical = now2;
|
|
1796
|
-
logical = 0;
|
|
1797
|
-
} else if (now2 < base) {
|
|
1798
|
-
physical = base;
|
|
1799
|
-
logical = local.logical + 1;
|
|
1800
|
-
} else {
|
|
1801
|
-
physical = base;
|
|
1802
|
-
logical = local.logical + 1;
|
|
1803
|
-
}
|
|
1804
|
-
local.physical = physical;
|
|
1805
|
-
local.logical = logical;
|
|
1806
|
-
return { physical, logical, node: local.node };
|
|
1807
|
-
}
|
|
1808
|
-
__name(tick, "tick");
|
|
1809
|
-
function receive(remote) {
|
|
1810
|
-
const now2 = Date.now();
|
|
1811
|
-
if (remote.physical > local.physical) {
|
|
1812
|
-
local.physical = remote.physical > now2 ? remote.physical : now2;
|
|
1813
|
-
local.logical = 1;
|
|
1814
|
-
} else if (remote.physical === local.physical) {
|
|
1815
|
-
local.logical = Math.max(local.logical, remote.logical) + 1;
|
|
1816
|
-
}
|
|
1817
|
-
local.node = getNodeId();
|
|
1818
|
-
return { ...local };
|
|
1819
|
-
}
|
|
1820
|
-
__name(receive, "receive");
|
|
1821
|
-
function compare(a, b) {
|
|
1822
|
-
if (a.physical !== b.physical) {
|
|
1823
|
-
return a.physical < b.physical ? -1 : 1;
|
|
1824
|
-
}
|
|
1825
|
-
if (a.logical !== b.logical) {
|
|
1826
|
-
return a.logical < b.logical ? -1 : 1;
|
|
1827
|
-
}
|
|
1828
|
-
if (a.node !== b.node) {
|
|
1829
|
-
return a.node < b.node ? -1 : 1;
|
|
1830
|
-
}
|
|
1831
|
-
return 0;
|
|
1832
|
-
}
|
|
1833
|
-
__name(compare, "compare");
|
|
1834
|
-
function happenedBefore(a, b) {
|
|
1835
|
-
return compare(a, b) < 0;
|
|
1836
|
-
}
|
|
1837
|
-
__name(happenedBefore, "happenedBefore");
|
|
1838
|
-
function serialize(hlc2) {
|
|
1839
|
-
return `hlc:${hlc2.physical}:${hlc2.logical}:${hlc2.node}`;
|
|
1840
|
-
}
|
|
1841
|
-
__name(serialize, "serialize");
|
|
1842
|
-
function parse(serialized) {
|
|
1843
|
-
if (!serialized.startsWith("hlc:")) return null;
|
|
1844
|
-
const parts = serialized.split(":");
|
|
1845
|
-
if (parts.length !== 4) return null;
|
|
1846
|
-
const physical = parseInt(parts[1], 10);
|
|
1847
|
-
const logical = parseInt(parts[2], 10);
|
|
1848
|
-
const node = parts[3];
|
|
1849
|
-
if (isNaN(physical) || isNaN(logical)) return null;
|
|
1850
|
-
return { physical, logical, node };
|
|
1851
|
-
}
|
|
1852
|
-
__name(parse, "parse");
|
|
1853
|
-
var hlc = {
|
|
1854
|
-
tick,
|
|
1855
|
-
receive,
|
|
1856
|
-
compare,
|
|
1857
|
-
happenedBefore,
|
|
1858
|
-
serialize,
|
|
1859
|
-
parse,
|
|
1860
|
-
getNodeId
|
|
1861
|
-
};
|
|
1862
|
-
|
|
1863
|
-
// functions/memory/index.ts
|
|
1864
2017
|
init_env();
|
|
1865
2018
|
|
|
1866
2019
|
// functions/memory/journal.ts
|