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.js
CHANGED
|
@@ -177,7 +177,10 @@ var DEV;
|
|
|
177
177
|
var init_env = __esm({
|
|
178
178
|
"core/env.ts"() {
|
|
179
179
|
init_esm_shims();
|
|
180
|
-
DEV =
|
|
180
|
+
DEV = false ;
|
|
181
|
+
if (typeof globalThis !== "undefined") {
|
|
182
|
+
globalThis["__DEV__"] = DEV;
|
|
183
|
+
}
|
|
181
184
|
}
|
|
182
185
|
});
|
|
183
186
|
|
|
@@ -807,9 +810,25 @@ var dispatch = {
|
|
|
807
810
|
const wrapped = /* @__PURE__ */ __name((e) => {
|
|
808
811
|
const target = e;
|
|
809
812
|
if (globalThis.queueMicrotask) {
|
|
810
|
-
globalThis.queueMicrotask(() =>
|
|
813
|
+
globalThis.queueMicrotask(() => {
|
|
814
|
+
try {
|
|
815
|
+
cb(target);
|
|
816
|
+
} catch (err) {
|
|
817
|
+
if (typeof console !== "undefined") {
|
|
818
|
+
console.error("[memorio dispatch] observer error:", err);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
});
|
|
811
822
|
} else {
|
|
812
|
-
Promise.resolve().then(() =>
|
|
823
|
+
Promise.resolve().then(() => {
|
|
824
|
+
try {
|
|
825
|
+
cb(target);
|
|
826
|
+
} catch (err) {
|
|
827
|
+
if (typeof console !== "undefined") {
|
|
828
|
+
console.error("[memorio dispatch] observer error:", err);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
});
|
|
813
832
|
}
|
|
814
833
|
}, "wrapped");
|
|
815
834
|
globalThis.addEventListener(name, wrapped);
|
|
@@ -1216,10 +1235,211 @@ if (globalThis[STORE_INSTANCE_KEY]) {
|
|
|
1216
1235
|
|
|
1217
1236
|
// functions/state/index.ts
|
|
1218
1237
|
init_env();
|
|
1238
|
+
|
|
1239
|
+
// core/mutation/engine.ts
|
|
1240
|
+
init_esm_shims();
|
|
1241
|
+
init_internal();
|
|
1242
|
+
|
|
1243
|
+
// core/hlc.ts
|
|
1244
|
+
init_esm_shims();
|
|
1245
|
+
var NODE_ID_KEY = "__memorio_hlc_node";
|
|
1246
|
+
function getNodeId() {
|
|
1247
|
+
if (typeof globalThis === "undefined") return "node";
|
|
1248
|
+
try {
|
|
1249
|
+
if (typeof sessionStorage !== "undefined" && sessionStorage) {
|
|
1250
|
+
let id = sessionStorage.getItem(NODE_ID_KEY);
|
|
1251
|
+
if (!id) {
|
|
1252
|
+
id = generateNodeId();
|
|
1253
|
+
sessionStorage.setItem(NODE_ID_KEY, id);
|
|
1254
|
+
}
|
|
1255
|
+
return id;
|
|
1256
|
+
}
|
|
1257
|
+
} catch {
|
|
1258
|
+
}
|
|
1259
|
+
if (!globalThis.__memorioHlcNodeId) {
|
|
1260
|
+
globalThis.__memorioHlcNodeId = generateNodeId();
|
|
1261
|
+
}
|
|
1262
|
+
return globalThis.__memorioHlcNodeId;
|
|
1263
|
+
}
|
|
1264
|
+
__name(getNodeId, "getNodeId");
|
|
1265
|
+
function generateNodeId() {
|
|
1266
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
1267
|
+
return crypto.randomUUID().slice(0, 8);
|
|
1268
|
+
}
|
|
1269
|
+
return Math.random().toString(36).slice(2, 10);
|
|
1270
|
+
}
|
|
1271
|
+
__name(generateNodeId, "generateNodeId");
|
|
1272
|
+
var HLC_KEY = "__memorio_hlc_local";
|
|
1273
|
+
var local = globalThis[HLC_KEY] || {
|
|
1274
|
+
physical: 0,
|
|
1275
|
+
logical: 0,
|
|
1276
|
+
node: getNodeId()
|
|
1277
|
+
};
|
|
1278
|
+
if (!globalThis[HLC_KEY]) {
|
|
1279
|
+
globalThis[HLC_KEY] = local;
|
|
1280
|
+
}
|
|
1281
|
+
function tick(_incoming) {
|
|
1282
|
+
const now2 = Date.now();
|
|
1283
|
+
const base = local.physical;
|
|
1284
|
+
let physical;
|
|
1285
|
+
let logical;
|
|
1286
|
+
if (now2 > base) {
|
|
1287
|
+
physical = now2;
|
|
1288
|
+
logical = 0;
|
|
1289
|
+
} else if (now2 < base) {
|
|
1290
|
+
physical = base;
|
|
1291
|
+
logical = local.logical + 1;
|
|
1292
|
+
} else {
|
|
1293
|
+
physical = base;
|
|
1294
|
+
logical = local.logical + 1;
|
|
1295
|
+
}
|
|
1296
|
+
local.physical = physical;
|
|
1297
|
+
local.logical = logical;
|
|
1298
|
+
return { physical, logical, node: local.node };
|
|
1299
|
+
}
|
|
1300
|
+
__name(tick, "tick");
|
|
1301
|
+
function receive(remote) {
|
|
1302
|
+
const now2 = Date.now();
|
|
1303
|
+
if (remote.physical > local.physical) {
|
|
1304
|
+
local.physical = remote.physical > now2 ? remote.physical : now2;
|
|
1305
|
+
local.logical = 1;
|
|
1306
|
+
} else if (remote.physical === local.physical) {
|
|
1307
|
+
local.logical = Math.max(local.logical, remote.logical) + 1;
|
|
1308
|
+
}
|
|
1309
|
+
local.node = getNodeId();
|
|
1310
|
+
return { ...local };
|
|
1311
|
+
}
|
|
1312
|
+
__name(receive, "receive");
|
|
1313
|
+
function compare(a, b) {
|
|
1314
|
+
if (a.physical !== b.physical) {
|
|
1315
|
+
return a.physical < b.physical ? -1 : 1;
|
|
1316
|
+
}
|
|
1317
|
+
if (a.logical !== b.logical) {
|
|
1318
|
+
return a.logical < b.logical ? -1 : 1;
|
|
1319
|
+
}
|
|
1320
|
+
if (a.node !== b.node) {
|
|
1321
|
+
return a.node < b.node ? -1 : 1;
|
|
1322
|
+
}
|
|
1323
|
+
return 0;
|
|
1324
|
+
}
|
|
1325
|
+
__name(compare, "compare");
|
|
1326
|
+
function happenedBefore(a, b) {
|
|
1327
|
+
return compare(a, b) < 0;
|
|
1328
|
+
}
|
|
1329
|
+
__name(happenedBefore, "happenedBefore");
|
|
1330
|
+
function serialize(hlc2) {
|
|
1331
|
+
return `hlc:${hlc2.physical}:${hlc2.logical}:${hlc2.node}`;
|
|
1332
|
+
}
|
|
1333
|
+
__name(serialize, "serialize");
|
|
1334
|
+
function parse(serialized) {
|
|
1335
|
+
if (!serialized.startsWith("hlc:")) return null;
|
|
1336
|
+
const parts = serialized.split(":");
|
|
1337
|
+
if (parts.length !== 4) return null;
|
|
1338
|
+
const physical = parseInt(parts[1], 10);
|
|
1339
|
+
const logical = parseInt(parts[2], 10);
|
|
1340
|
+
const node = parts[3];
|
|
1341
|
+
if (isNaN(physical) || isNaN(logical)) return null;
|
|
1342
|
+
return { physical, logical, node };
|
|
1343
|
+
}
|
|
1344
|
+
__name(parse, "parse");
|
|
1345
|
+
var hlc = {
|
|
1346
|
+
tick,
|
|
1347
|
+
receive,
|
|
1348
|
+
compare,
|
|
1349
|
+
happenedBefore,
|
|
1350
|
+
serialize,
|
|
1351
|
+
parse,
|
|
1352
|
+
getNodeId
|
|
1353
|
+
};
|
|
1354
|
+
|
|
1355
|
+
// core/mutation/id.ts
|
|
1356
|
+
init_esm_shims();
|
|
1357
|
+
function generateMutationId() {
|
|
1358
|
+
const now2 = hlc.tick();
|
|
1359
|
+
return "m_" + encodeHLC(now2);
|
|
1360
|
+
}
|
|
1361
|
+
__name(generateMutationId, "generateMutationId");
|
|
1362
|
+
function encodeHLC(hlcValue) {
|
|
1363
|
+
const timePart = hlcValue.physical.toString(36);
|
|
1364
|
+
const logicalPart = hlcValue.logical.toString(36).padStart(2, "0");
|
|
1365
|
+
const nodePart = hlcValue.node.slice(0, 4);
|
|
1366
|
+
return `${timePart}${logicalPart}${nodePart}`;
|
|
1367
|
+
}
|
|
1368
|
+
__name(encodeHLC, "encodeHLC");
|
|
1369
|
+
|
|
1370
|
+
// core/mutation/patch.ts
|
|
1371
|
+
init_esm_shims();
|
|
1372
|
+
|
|
1373
|
+
// core/mutation/transaction.ts
|
|
1374
|
+
init_esm_shims();
|
|
1375
|
+
init_internal();
|
|
1376
|
+
var TX_REGISTRY_KEY = "__memorio_transaction_registry__";
|
|
1377
|
+
var registry = globalThis[TX_REGISTRY_KEY] || /* @__PURE__ */ new Map();
|
|
1378
|
+
if (!globalThis[TX_REGISTRY_KEY]) {
|
|
1379
|
+
globalThis[TX_REGISTRY_KEY] = registry;
|
|
1380
|
+
}
|
|
1381
|
+
var TX_STACK_KEY = "__memorio_transaction_stack__";
|
|
1382
|
+
var txStack = globalThis[TX_STACK_KEY] || [];
|
|
1383
|
+
if (!globalThis[TX_STACK_KEY]) {
|
|
1384
|
+
globalThis[TX_STACK_KEY] = txStack;
|
|
1385
|
+
}
|
|
1386
|
+
function getActiveTxId() {
|
|
1387
|
+
return txStack.length > 0 ? txStack[txStack.length - 1] : null;
|
|
1388
|
+
}
|
|
1389
|
+
__name(getActiveTxId, "getActiveTxId");
|
|
1390
|
+
function tagForTransaction() {
|
|
1391
|
+
const txId = getActiveTxId();
|
|
1392
|
+
if (!txId) return void 0;
|
|
1393
|
+
const ctx = registry.get(txId);
|
|
1394
|
+
if (ctx && ctx.active) {
|
|
1395
|
+
return txId;
|
|
1396
|
+
}
|
|
1397
|
+
return void 0;
|
|
1398
|
+
}
|
|
1399
|
+
__name(tagForTransaction, "tagForTransaction");
|
|
1400
|
+
function registerMutationInTransaction(mutationId) {
|
|
1401
|
+
const txId = getActiveTxId();
|
|
1402
|
+
if (!txId) return;
|
|
1403
|
+
const ctx = registry.get(txId);
|
|
1404
|
+
if (ctx && ctx.active) {
|
|
1405
|
+
ctx.mutations.push(mutationId);
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
__name(registerMutationInTransaction, "registerMutationInTransaction");
|
|
1409
|
+
|
|
1410
|
+
// core/mutation/engine.ts
|
|
1411
|
+
function createMutation(path2, operation, before, after, options) {
|
|
1412
|
+
const now2 = hlc.tick();
|
|
1413
|
+
const mutation = {
|
|
1414
|
+
id: generateMutationId(),
|
|
1415
|
+
path: path2,
|
|
1416
|
+
operation,
|
|
1417
|
+
after,
|
|
1418
|
+
before,
|
|
1419
|
+
timestamp: now2.physical,
|
|
1420
|
+
hlc: hlc.serialize(now2),
|
|
1421
|
+
context: options?.context || internal_default.currentContext || void 0,
|
|
1422
|
+
source: options?.source,
|
|
1423
|
+
transactionId: options?.transactionId || (operation !== "length" ? tagForTransaction() : void 0),
|
|
1424
|
+
// Legacy fields for backward compatibility
|
|
1425
|
+
action: operation === "set" ? "set" : "delete",
|
|
1426
|
+
newValue: after,
|
|
1427
|
+
previousValue: before
|
|
1428
|
+
};
|
|
1429
|
+
if (mutation.transactionId) {
|
|
1430
|
+
registerMutationInTransaction(mutation.id);
|
|
1431
|
+
}
|
|
1432
|
+
return mutation;
|
|
1433
|
+
}
|
|
1434
|
+
__name(createMutation, "createMutation");
|
|
1435
|
+
|
|
1436
|
+
// functions/state/index.ts
|
|
1219
1437
|
var TARGET = /* @__PURE__ */ Symbol("memorio.target");
|
|
1220
1438
|
var wrapperCache = /* @__PURE__ */ new WeakMap();
|
|
1439
|
+
var arrayRootDedup = /* @__PURE__ */ new Set();
|
|
1221
1440
|
var deepRaw = /* @__PURE__ */ __name((val, seen = null) => {
|
|
1222
1441
|
if (val === null || typeof val !== "object") return val;
|
|
1442
|
+
if (!seen) seen = /* @__PURE__ */ new WeakSet();
|
|
1223
1443
|
if (val[TARGET] !== void 0) return deepRaw(val[TARGET], seen);
|
|
1224
1444
|
if (seen) {
|
|
1225
1445
|
if (seen.has(val)) return val;
|
|
@@ -1282,8 +1502,8 @@ var buildProxy = /* @__PURE__ */ __name((obj, callback, tree = []) => {
|
|
|
1282
1502
|
if (prop === "removeAll") {
|
|
1283
1503
|
return () => {
|
|
1284
1504
|
try {
|
|
1285
|
-
for (const key
|
|
1286
|
-
if (typeof target[key] !== "function" && !["list", "remove", "removeAll", "lock", "unlock"].includes(key)) {
|
|
1505
|
+
for (const key of Reflect.ownKeys(target)) {
|
|
1506
|
+
if (typeof target[key] !== "function" && !["list", "remove", "removeAll", "lock", "unlock", "persist"].includes(key)) {
|
|
1287
1507
|
delete target[key];
|
|
1288
1508
|
}
|
|
1289
1509
|
}
|
|
@@ -1357,6 +1577,16 @@ var buildProxy = /* @__PURE__ */ __name((obj, callback, tree = []) => {
|
|
|
1357
1577
|
}
|
|
1358
1578
|
);
|
|
1359
1579
|
event("state." + path2);
|
|
1580
|
+
if (Array.isArray(target) && tree.length > 0) {
|
|
1581
|
+
const rootName = "state." + tree.join(".");
|
|
1582
|
+
if (!arrayRootDedup.has(rootName)) {
|
|
1583
|
+
arrayRootDedup.add(rootName);
|
|
1584
|
+
event(rootName);
|
|
1585
|
+
if (typeof globalThis.queueMicrotask === "function") {
|
|
1586
|
+
globalThis.queueMicrotask(() => arrayRootDedup.clear());
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1360
1590
|
Reflect.set(target, key, rawValue);
|
|
1361
1591
|
internal_default.bumpStateVersion();
|
|
1362
1592
|
if (target[key] && typeof target[key] === "object") {
|
|
@@ -1413,6 +1643,17 @@ var buildProxy = /* @__PURE__ */ __name((obj, callback, tree = []) => {
|
|
|
1413
1643
|
const path2 = objPath(prop, tree);
|
|
1414
1644
|
const previousValue = Reflect.get(target, prop);
|
|
1415
1645
|
callback({ action: "delete", path: path2, target, previousValue });
|
|
1646
|
+
event("state." + path2);
|
|
1647
|
+
if (Array.isArray(target) && tree.length > 0) {
|
|
1648
|
+
const rootName = "state." + tree.join(".");
|
|
1649
|
+
if (!arrayRootDedup.has(rootName)) {
|
|
1650
|
+
arrayRootDedup.add(rootName);
|
|
1651
|
+
event(rootName);
|
|
1652
|
+
if (typeof globalThis.queueMicrotask === "function") {
|
|
1653
|
+
globalThis.queueMicrotask(() => arrayRootDedup.clear());
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1416
1657
|
return Reflect.deleteProperty(target, prop);
|
|
1417
1658
|
} catch (error) {
|
|
1418
1659
|
console.error("Error in deleteProperty trap:", error);
|
|
@@ -1434,57 +1675,83 @@ if (globalThis[STATE_INSTANCE_KEY]) {
|
|
|
1434
1675
|
state2 = globalThis[STATE_INSTANCE_KEY];
|
|
1435
1676
|
} else {
|
|
1436
1677
|
state2 = buildProxy({}, (mutation) => {
|
|
1437
|
-
if (internal_default.historyEnabled) {
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1678
|
+
if (internal_default.historyEnabled && internal_default._recording !== false) {
|
|
1679
|
+
const operation = mutation.action === "delete" ? "delete" : "set";
|
|
1680
|
+
const record = createMutation(
|
|
1681
|
+
mutation.path,
|
|
1682
|
+
operation,
|
|
1683
|
+
mutation.previousValue,
|
|
1684
|
+
mutation.newValue,
|
|
1685
|
+
{
|
|
1686
|
+
context: mutation.context,
|
|
1687
|
+
source: mutation.source
|
|
1688
|
+
}
|
|
1689
|
+
);
|
|
1690
|
+
internal_default.recordMutation(record);
|
|
1445
1691
|
}
|
|
1446
1692
|
});
|
|
1447
|
-
|
|
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
|
-
|
|
1693
|
+
const _rawTarget = state2[TARGET];
|
|
1694
|
+
try {
|
|
1695
|
+
Object.defineProperty(_rawTarget, "lock", {
|
|
1696
|
+
value: /* @__PURE__ */ __name(() => {
|
|
1697
|
+
internal_default.locked = true;
|
|
1698
|
+
message_default("State is now locked. All modifications are blocked.");
|
|
1699
|
+
}, "value"),
|
|
1700
|
+
writable: true,
|
|
1701
|
+
configurable: true,
|
|
1702
|
+
enumerable: false
|
|
1703
|
+
});
|
|
1704
|
+
Object.defineProperty(_rawTarget, "unlock", {
|
|
1705
|
+
value: /* @__PURE__ */ __name(() => {
|
|
1706
|
+
internal_default.locked = false;
|
|
1707
|
+
message_default("State is now unlocked. Modifications are allowed.");
|
|
1708
|
+
}, "value"),
|
|
1709
|
+
writable: true,
|
|
1710
|
+
configurable: true,
|
|
1711
|
+
enumerable: false
|
|
1712
|
+
});
|
|
1713
|
+
} catch (e) {
|
|
1714
|
+
if (DEV) console.warn("[memorio state] lock/unlock defineProperty failed:", e);
|
|
1715
|
+
}
|
|
1716
|
+
try {
|
|
1717
|
+
Object.defineProperty(_rawTarget, "persist", {
|
|
1718
|
+
value: /* @__PURE__ */ __name((path2) => {
|
|
1719
|
+
if (typeof path2 !== "string" || !path2.length) {
|
|
1720
|
+
if (DEV) console.warn('[memorio state] state.persist() requires a non-empty path string (e.g. "state.user")');
|
|
1721
|
+
return () => {
|
|
1722
|
+
};
|
|
1474
1723
|
}
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1724
|
+
const normalized = path2.startsWith("state.") ? path2 : "state." + path2;
|
|
1725
|
+
const key = normalized.replace(/^state\./, "");
|
|
1726
|
+
const segments = key.split(".");
|
|
1727
|
+
const initial = store.get(key);
|
|
1728
|
+
if (initial !== void 0 && initial !== null) {
|
|
1729
|
+
let acc = state2;
|
|
1730
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
1731
|
+
const seg = segments[i];
|
|
1732
|
+
if (acc[seg] === void 0 || acc[seg] === null) acc[seg] = {};
|
|
1733
|
+
acc = acc[seg];
|
|
1734
|
+
}
|
|
1735
|
+
if (acc) acc[segments[segments.length - 1]] = deepRaw(initial);
|
|
1736
|
+
}
|
|
1737
|
+
const handler = /* @__PURE__ */ __name(() => {
|
|
1738
|
+
let current = state2;
|
|
1739
|
+
for (const seg of segments) {
|
|
1740
|
+
current = current === void 0 || current === null ? void 0 : current[seg];
|
|
1741
|
+
}
|
|
1742
|
+
store.set(key, deepRaw(current));
|
|
1743
|
+
}, "handler");
|
|
1744
|
+
const off = dispatch.listen(normalized, handler);
|
|
1745
|
+
return typeof off === "function" ? off : () => {
|
|
1746
|
+
};
|
|
1747
|
+
}, "value"),
|
|
1748
|
+
writable: true,
|
|
1749
|
+
configurable: true,
|
|
1750
|
+
enumerable: false
|
|
1751
|
+
});
|
|
1752
|
+
} catch (e) {
|
|
1753
|
+
if (DEV) console.warn("[memorio state] persist defineProperty failed:", e);
|
|
1754
|
+
}
|
|
1488
1755
|
globalThis[STATE_INSTANCE_KEY] = state2;
|
|
1489
1756
|
}
|
|
1490
1757
|
|
|
@@ -1746,120 +2013,6 @@ if (globalThis[SESSION_INSTANCE_KEY]) {
|
|
|
1746
2013
|
|
|
1747
2014
|
// functions/memory/index.ts
|
|
1748
2015
|
init_idb();
|
|
1749
|
-
|
|
1750
|
-
// core/hlc.ts
|
|
1751
|
-
init_esm_shims();
|
|
1752
|
-
var NODE_ID_KEY = "__memorio_hlc_node";
|
|
1753
|
-
function getNodeId() {
|
|
1754
|
-
if (typeof globalThis === "undefined") return "node";
|
|
1755
|
-
try {
|
|
1756
|
-
if (typeof sessionStorage !== "undefined" && sessionStorage) {
|
|
1757
|
-
let id = sessionStorage.getItem(NODE_ID_KEY);
|
|
1758
|
-
if (!id) {
|
|
1759
|
-
id = generateNodeId();
|
|
1760
|
-
sessionStorage.setItem(NODE_ID_KEY, id);
|
|
1761
|
-
}
|
|
1762
|
-
return id;
|
|
1763
|
-
}
|
|
1764
|
-
} catch {
|
|
1765
|
-
}
|
|
1766
|
-
if (!globalThis.__memorioHlcNodeId) {
|
|
1767
|
-
globalThis.__memorioHlcNodeId = generateNodeId();
|
|
1768
|
-
}
|
|
1769
|
-
return globalThis.__memorioHlcNodeId;
|
|
1770
|
-
}
|
|
1771
|
-
__name(getNodeId, "getNodeId");
|
|
1772
|
-
function generateNodeId() {
|
|
1773
|
-
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
1774
|
-
return crypto.randomUUID().slice(0, 8);
|
|
1775
|
-
}
|
|
1776
|
-
return Math.random().toString(36).slice(2, 10);
|
|
1777
|
-
}
|
|
1778
|
-
__name(generateNodeId, "generateNodeId");
|
|
1779
|
-
var HLC_KEY = "__memorio_hlc_local";
|
|
1780
|
-
var local = globalThis[HLC_KEY] || {
|
|
1781
|
-
physical: 0,
|
|
1782
|
-
logical: 0,
|
|
1783
|
-
node: getNodeId()
|
|
1784
|
-
};
|
|
1785
|
-
if (!globalThis[HLC_KEY]) {
|
|
1786
|
-
globalThis[HLC_KEY] = local;
|
|
1787
|
-
}
|
|
1788
|
-
function tick(_incoming) {
|
|
1789
|
-
const now2 = Date.now();
|
|
1790
|
-
const base = local.physical;
|
|
1791
|
-
let physical;
|
|
1792
|
-
let logical;
|
|
1793
|
-
if (now2 > base) {
|
|
1794
|
-
physical = now2;
|
|
1795
|
-
logical = 0;
|
|
1796
|
-
} else if (now2 < base) {
|
|
1797
|
-
physical = base;
|
|
1798
|
-
logical = local.logical + 1;
|
|
1799
|
-
} else {
|
|
1800
|
-
physical = base;
|
|
1801
|
-
logical = local.logical + 1;
|
|
1802
|
-
}
|
|
1803
|
-
local.physical = physical;
|
|
1804
|
-
local.logical = logical;
|
|
1805
|
-
return { physical, logical, node: local.node };
|
|
1806
|
-
}
|
|
1807
|
-
__name(tick, "tick");
|
|
1808
|
-
function receive(remote) {
|
|
1809
|
-
const now2 = Date.now();
|
|
1810
|
-
if (remote.physical > local.physical) {
|
|
1811
|
-
local.physical = remote.physical > now2 ? remote.physical : now2;
|
|
1812
|
-
local.logical = 1;
|
|
1813
|
-
} else if (remote.physical === local.physical) {
|
|
1814
|
-
local.logical = Math.max(local.logical, remote.logical) + 1;
|
|
1815
|
-
}
|
|
1816
|
-
local.node = getNodeId();
|
|
1817
|
-
return { ...local };
|
|
1818
|
-
}
|
|
1819
|
-
__name(receive, "receive");
|
|
1820
|
-
function compare(a, b) {
|
|
1821
|
-
if (a.physical !== b.physical) {
|
|
1822
|
-
return a.physical < b.physical ? -1 : 1;
|
|
1823
|
-
}
|
|
1824
|
-
if (a.logical !== b.logical) {
|
|
1825
|
-
return a.logical < b.logical ? -1 : 1;
|
|
1826
|
-
}
|
|
1827
|
-
if (a.node !== b.node) {
|
|
1828
|
-
return a.node < b.node ? -1 : 1;
|
|
1829
|
-
}
|
|
1830
|
-
return 0;
|
|
1831
|
-
}
|
|
1832
|
-
__name(compare, "compare");
|
|
1833
|
-
function happenedBefore(a, b) {
|
|
1834
|
-
return compare(a, b) < 0;
|
|
1835
|
-
}
|
|
1836
|
-
__name(happenedBefore, "happenedBefore");
|
|
1837
|
-
function serialize(hlc2) {
|
|
1838
|
-
return `hlc:${hlc2.physical}:${hlc2.logical}:${hlc2.node}`;
|
|
1839
|
-
}
|
|
1840
|
-
__name(serialize, "serialize");
|
|
1841
|
-
function parse(serialized) {
|
|
1842
|
-
if (!serialized.startsWith("hlc:")) return null;
|
|
1843
|
-
const parts = serialized.split(":");
|
|
1844
|
-
if (parts.length !== 4) return null;
|
|
1845
|
-
const physical = parseInt(parts[1], 10);
|
|
1846
|
-
const logical = parseInt(parts[2], 10);
|
|
1847
|
-
const node = parts[3];
|
|
1848
|
-
if (isNaN(physical) || isNaN(logical)) return null;
|
|
1849
|
-
return { physical, logical, node };
|
|
1850
|
-
}
|
|
1851
|
-
__name(parse, "parse");
|
|
1852
|
-
var hlc = {
|
|
1853
|
-
tick,
|
|
1854
|
-
receive,
|
|
1855
|
-
compare,
|
|
1856
|
-
happenedBefore,
|
|
1857
|
-
serialize,
|
|
1858
|
-
parse,
|
|
1859
|
-
getNodeId
|
|
1860
|
-
};
|
|
1861
|
-
|
|
1862
|
-
// functions/memory/index.ts
|
|
1863
2016
|
init_env();
|
|
1864
2017
|
|
|
1865
2018
|
// functions/memory/journal.ts
|