memorio 4.9.31 → 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 +327 -330
- 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 +706 -649
- package/index.d.ts +1 -0
- package/index.js +686 -648
- 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 +561 -374
- package/modules/redux.cjs.map +1 -1
- package/modules/redux.js +561 -374
- 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);
|
|
@@ -943,246 +962,6 @@ var schema = {
|
|
|
943
962
|
get: getSchema
|
|
944
963
|
};
|
|
945
964
|
|
|
946
|
-
// functions/state/index.ts
|
|
947
|
-
init_env();
|
|
948
|
-
var TARGET = /* @__PURE__ */ Symbol("memorio.target");
|
|
949
|
-
var wrapperCache = /* @__PURE__ */ new WeakMap();
|
|
950
|
-
var deepRaw = /* @__PURE__ */ __name((val, seen = null) => {
|
|
951
|
-
if (val === null || typeof val !== "object") return val;
|
|
952
|
-
if (val[TARGET] !== void 0) return deepRaw(val[TARGET], seen);
|
|
953
|
-
if (seen) {
|
|
954
|
-
if (seen.has(val)) return val;
|
|
955
|
-
seen.add(val);
|
|
956
|
-
}
|
|
957
|
-
const ctor = val.constructor?.name;
|
|
958
|
-
if (ctor !== "Object" && ctor !== "Array") return val;
|
|
959
|
-
const copy = Array.isArray(val) ? [] : {};
|
|
960
|
-
for (const k of Reflect.ownKeys(val)) {
|
|
961
|
-
copy[k] = deepRaw(val[k], seen);
|
|
962
|
-
}
|
|
963
|
-
return copy;
|
|
964
|
-
}, "deepRaw");
|
|
965
|
-
var objPath = /* @__PURE__ */ __name((prop, object, separator = ".") => {
|
|
966
|
-
return object.concat(prop).join(separator);
|
|
967
|
-
}, "objPath");
|
|
968
|
-
var buildProxy = /* @__PURE__ */ __name((obj, callback, tree = []) => {
|
|
969
|
-
const event = /* @__PURE__ */ __name((name) => {
|
|
970
|
-
try {
|
|
971
|
-
dispatch.set(name, { detail: { name } });
|
|
972
|
-
} catch (err) {
|
|
973
|
-
if (DEV) console.warn("[memorio state] event dispatch failed:", err);
|
|
974
|
-
}
|
|
975
|
-
return;
|
|
976
|
-
}, "event");
|
|
977
|
-
return new Proxy(
|
|
978
|
-
obj,
|
|
979
|
-
{
|
|
980
|
-
get(target, prop) {
|
|
981
|
-
if (prop === "__path") {
|
|
982
|
-
return tree.length > 0 ? "state." + tree.join(".") : "state";
|
|
983
|
-
}
|
|
984
|
-
if (prop === TARGET) {
|
|
985
|
-
return target;
|
|
986
|
-
}
|
|
987
|
-
if (target[TARGET] !== void 0) {
|
|
988
|
-
return target;
|
|
989
|
-
}
|
|
990
|
-
if (prop === "list") {
|
|
991
|
-
message_default('Some state could be hidden when you use "state.list". Use "state" to see the complete list ');
|
|
992
|
-
return deepRaw(target, /* @__PURE__ */ new WeakSet());
|
|
993
|
-
}
|
|
994
|
-
if (typeof prop === "string") {
|
|
995
|
-
const path = tree.length > 0 ? "state." + tree.concat(prop).join(".") : "state." + prop;
|
|
996
|
-
internal_default.lastAccessedPath = path;
|
|
997
|
-
if (internal_default.tracking) {
|
|
998
|
-
internal_default.trackedPaths.add(path);
|
|
999
|
-
}
|
|
1000
|
-
}
|
|
1001
|
-
if (prop === "_lastPath") {
|
|
1002
|
-
return internal_default.lastAccessedPath;
|
|
1003
|
-
}
|
|
1004
|
-
if (prop === "remove") {
|
|
1005
|
-
return (key) => {
|
|
1006
|
-
delete target[key];
|
|
1007
|
-
return true;
|
|
1008
|
-
};
|
|
1009
|
-
}
|
|
1010
|
-
if (prop === "removeAll") {
|
|
1011
|
-
return () => {
|
|
1012
|
-
try {
|
|
1013
|
-
for (const key in target) {
|
|
1014
|
-
if (typeof target[key] !== "function" && !["list", "remove", "removeAll", "lock", "unlock"].includes(key)) {
|
|
1015
|
-
delete target[key];
|
|
1016
|
-
}
|
|
1017
|
-
}
|
|
1018
|
-
} catch (error) {
|
|
1019
|
-
console.error(error);
|
|
1020
|
-
}
|
|
1021
|
-
return;
|
|
1022
|
-
};
|
|
1023
|
-
}
|
|
1024
|
-
if (Object.isFrozen(target[prop])) return target[prop];
|
|
1025
|
-
if (prop === "lock" || prop === "unlock") {
|
|
1026
|
-
const value = Reflect.get(target, prop);
|
|
1027
|
-
if (value) return value;
|
|
1028
|
-
}
|
|
1029
|
-
try {
|
|
1030
|
-
const value = Reflect.get(target, prop);
|
|
1031
|
-
if (value && typeof value === "object" && ["Array", "Object"].includes(value.constructor.name)) {
|
|
1032
|
-
let wrapper = wrapperCache.get(value);
|
|
1033
|
-
if (!wrapper) {
|
|
1034
|
-
wrapper = buildProxy(value, callback, tree.concat(prop));
|
|
1035
|
-
wrapperCache.set(value, wrapper);
|
|
1036
|
-
}
|
|
1037
|
-
return wrapper;
|
|
1038
|
-
}
|
|
1039
|
-
return value;
|
|
1040
|
-
} catch (error) {
|
|
1041
|
-
console.error("Error: ", error);
|
|
1042
|
-
return void 0;
|
|
1043
|
-
}
|
|
1044
|
-
},
|
|
1045
|
-
set(target, key, value) {
|
|
1046
|
-
if (internal_default.locked) {
|
|
1047
|
-
console.error("Error: state is locked. Use state.unlock() to enable modifications.");
|
|
1048
|
-
return false;
|
|
1049
|
-
}
|
|
1050
|
-
if (protect.includes(key)) {
|
|
1051
|
-
console.error("key " + key + " is protected");
|
|
1052
|
-
return false;
|
|
1053
|
-
}
|
|
1054
|
-
if (target[key] && typeof target[key] === "object" && Object.isFrozen(target[key])) {
|
|
1055
|
-
console.error(`Error: state '${key}' is locked`);
|
|
1056
|
-
return false;
|
|
1057
|
-
}
|
|
1058
|
-
if (target[key] && typeof target[key] === "object" && target[key].__locked) {
|
|
1059
|
-
console.error(`Error: state '${key}' is locked. Use state.${key}.unlock() to enable modifications.`);
|
|
1060
|
-
return false;
|
|
1061
|
-
}
|
|
1062
|
-
try {
|
|
1063
|
-
const path = objPath(key, tree);
|
|
1064
|
-
const rawValue = deepRaw(value);
|
|
1065
|
-
const _schemaResult = schema.validate(path, rawValue);
|
|
1066
|
-
if (!_schemaResult.valid) {
|
|
1067
|
-
if (DEV && internal_default.debug) {
|
|
1068
|
-
console.error(
|
|
1069
|
-
`[memorio schema] Validation failed for '${path}':`,
|
|
1070
|
-
_schemaResult.errors
|
|
1071
|
-
);
|
|
1072
|
-
}
|
|
1073
|
-
message_default(
|
|
1074
|
-
`[memorio schema] Validation failed for '${path}': ` + (_schemaResult.errors?.join(", ") || "unknown error")
|
|
1075
|
-
);
|
|
1076
|
-
return false;
|
|
1077
|
-
}
|
|
1078
|
-
callback(
|
|
1079
|
-
{
|
|
1080
|
-
action: "set",
|
|
1081
|
-
path,
|
|
1082
|
-
target,
|
|
1083
|
-
newValue: rawValue,
|
|
1084
|
-
previousValue: Reflect.get(target, key)
|
|
1085
|
-
}
|
|
1086
|
-
);
|
|
1087
|
-
event("state." + path);
|
|
1088
|
-
Reflect.set(target, key, rawValue);
|
|
1089
|
-
internal_default.bumpStateVersion();
|
|
1090
|
-
if (target[key] && typeof target[key] === "object") {
|
|
1091
|
-
const lockValue = target[key];
|
|
1092
|
-
if (lockValue && typeof lockValue === "object") {
|
|
1093
|
-
try {
|
|
1094
|
-
Object.defineProperty(lockValue, "__locked", {
|
|
1095
|
-
writable: true,
|
|
1096
|
-
enumerable: false,
|
|
1097
|
-
configurable: false,
|
|
1098
|
-
value: false
|
|
1099
|
-
});
|
|
1100
|
-
Object.defineProperty(lockValue, "lock", {
|
|
1101
|
-
writable: false,
|
|
1102
|
-
enumerable: false,
|
|
1103
|
-
configurable: false,
|
|
1104
|
-
value: /* @__PURE__ */ __name(() => {
|
|
1105
|
-
Object.defineProperty(lockValue, "__locked", {
|
|
1106
|
-
writable: true,
|
|
1107
|
-
enumerable: false,
|
|
1108
|
-
configurable: false,
|
|
1109
|
-
value: true
|
|
1110
|
-
});
|
|
1111
|
-
message_default(`State '${key}' is now locked.`);
|
|
1112
|
-
}, "value")
|
|
1113
|
-
});
|
|
1114
|
-
Object.defineProperty(lockValue, "unlock", {
|
|
1115
|
-
writable: false,
|
|
1116
|
-
enumerable: false,
|
|
1117
|
-
configurable: false,
|
|
1118
|
-
value: /* @__PURE__ */ __name(() => {
|
|
1119
|
-
Object.defineProperty(lockValue, "__locked", {
|
|
1120
|
-
writable: true,
|
|
1121
|
-
enumerable: false,
|
|
1122
|
-
configurable: false,
|
|
1123
|
-
value: false
|
|
1124
|
-
});
|
|
1125
|
-
message_default(`State '${key}' is now unlocked.`);
|
|
1126
|
-
}, "value")
|
|
1127
|
-
});
|
|
1128
|
-
} catch (err) {
|
|
1129
|
-
if (DEV) console.warn("[memorio state] lock/unlock defineProperty failed:", err);
|
|
1130
|
-
}
|
|
1131
|
-
}
|
|
1132
|
-
}
|
|
1133
|
-
return true;
|
|
1134
|
-
} catch (error) {
|
|
1135
|
-
console.error("Error in set trap:", error);
|
|
1136
|
-
return false;
|
|
1137
|
-
}
|
|
1138
|
-
},
|
|
1139
|
-
deleteProperty(target, prop) {
|
|
1140
|
-
try {
|
|
1141
|
-
const path = objPath(prop, tree);
|
|
1142
|
-
const previousValue = Reflect.get(target, prop);
|
|
1143
|
-
callback({ action: "delete", path, target, previousValue });
|
|
1144
|
-
return Reflect.deleteProperty(target, prop);
|
|
1145
|
-
} catch (error) {
|
|
1146
|
-
console.error("Error in deleteProperty trap:", error);
|
|
1147
|
-
return false;
|
|
1148
|
-
}
|
|
1149
|
-
},
|
|
1150
|
-
ownKeys(target) {
|
|
1151
|
-
return Reflect.ownKeys(target);
|
|
1152
|
-
},
|
|
1153
|
-
getOwnPropertyDescriptor(target, prop) {
|
|
1154
|
-
return Reflect.getOwnPropertyDescriptor(target, prop);
|
|
1155
|
-
}
|
|
1156
|
-
}
|
|
1157
|
-
);
|
|
1158
|
-
}, "buildProxy");
|
|
1159
|
-
var STATE_INSTANCE_KEY = "__memorio_state_instance__";
|
|
1160
|
-
var state2;
|
|
1161
|
-
if (globalThis[STATE_INSTANCE_KEY]) {
|
|
1162
|
-
state2 = globalThis[STATE_INSTANCE_KEY];
|
|
1163
|
-
} else {
|
|
1164
|
-
state2 = buildProxy({}, (mutation) => {
|
|
1165
|
-
if (internal_default.historyEnabled) {
|
|
1166
|
-
internal_default.recordMutation({
|
|
1167
|
-
path: mutation.path,
|
|
1168
|
-
action: mutation.action,
|
|
1169
|
-
newValue: mutation.newValue,
|
|
1170
|
-
previousValue: mutation.previousValue,
|
|
1171
|
-
timestamp: Date.now()
|
|
1172
|
-
});
|
|
1173
|
-
}
|
|
1174
|
-
});
|
|
1175
|
-
state2.lock = () => {
|
|
1176
|
-
internal_default.locked = true;
|
|
1177
|
-
message_default("State is now locked. All modifications are blocked.");
|
|
1178
|
-
};
|
|
1179
|
-
state2.unlock = () => {
|
|
1180
|
-
internal_default.locked = false;
|
|
1181
|
-
message_default("State is now unlocked. Modifications are allowed.");
|
|
1182
|
-
};
|
|
1183
|
-
globalThis[STATE_INSTANCE_KEY] = state2;
|
|
1184
|
-
}
|
|
1185
|
-
|
|
1186
965
|
// functions/store/index.ts
|
|
1187
966
|
init_cjs_shims();
|
|
1188
967
|
init_platform();
|
|
@@ -1455,25 +1234,547 @@ if (globalThis[STORE_INSTANCE_KEY]) {
|
|
|
1455
1234
|
globalThis[STORE_INSTANCE_KEY] = store;
|
|
1456
1235
|
}
|
|
1457
1236
|
|
|
1458
|
-
// functions/
|
|
1237
|
+
// functions/state/index.ts
|
|
1238
|
+
init_env();
|
|
1239
|
+
|
|
1240
|
+
// core/mutation/engine.ts
|
|
1459
1241
|
init_cjs_shims();
|
|
1460
|
-
init_platform();
|
|
1461
1242
|
init_internal();
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
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
|
|
1438
|
+
var TARGET = /* @__PURE__ */ Symbol("memorio.target");
|
|
1439
|
+
var wrapperCache = /* @__PURE__ */ new WeakMap();
|
|
1440
|
+
var arrayRootDedup = /* @__PURE__ */ new Set();
|
|
1441
|
+
var deepRaw = /* @__PURE__ */ __name((val, seen = null) => {
|
|
1442
|
+
if (val === null || typeof val !== "object") return val;
|
|
1443
|
+
if (!seen) seen = /* @__PURE__ */ new WeakSet();
|
|
1444
|
+
if (val[TARGET] !== void 0) return deepRaw(val[TARGET], seen);
|
|
1445
|
+
if (seen) {
|
|
1446
|
+
if (seen.has(val)) return val;
|
|
1447
|
+
seen.add(val);
|
|
1448
|
+
}
|
|
1449
|
+
const ctor = val.constructor?.name;
|
|
1450
|
+
if (ctor !== "Object" && ctor !== "Array") return val;
|
|
1451
|
+
const copy = Array.isArray(val) ? [] : {};
|
|
1452
|
+
for (const k of Reflect.ownKeys(val)) {
|
|
1453
|
+
if (!Object.prototype.propertyIsEnumerable.call(val, k)) continue;
|
|
1454
|
+
copy[k] = deepRaw(val[k], seen);
|
|
1455
|
+
}
|
|
1456
|
+
return copy;
|
|
1457
|
+
}, "deepRaw");
|
|
1458
|
+
var objPath = /* @__PURE__ */ __name((prop, object, separator = ".") => {
|
|
1459
|
+
return object.concat(prop).join(separator);
|
|
1460
|
+
}, "objPath");
|
|
1461
|
+
var buildProxy = /* @__PURE__ */ __name((obj, callback, tree = []) => {
|
|
1462
|
+
const event = /* @__PURE__ */ __name((name) => {
|
|
1463
|
+
try {
|
|
1464
|
+
dispatch.set(name, { detail: { name } });
|
|
1465
|
+
} catch (err) {
|
|
1466
|
+
if (DEV) console.warn("[memorio state] event dispatch failed:", err);
|
|
1467
|
+
}
|
|
1468
|
+
return;
|
|
1469
|
+
}, "event");
|
|
1470
|
+
return new Proxy(
|
|
1471
|
+
obj,
|
|
1472
|
+
{
|
|
1473
|
+
get(target, prop) {
|
|
1474
|
+
if (prop === "__path") {
|
|
1475
|
+
return tree.length > 0 ? "state." + tree.join(".") : "state";
|
|
1476
|
+
}
|
|
1477
|
+
if (prop === TARGET) {
|
|
1478
|
+
return target;
|
|
1479
|
+
}
|
|
1480
|
+
if (target[TARGET] !== void 0) {
|
|
1481
|
+
return target;
|
|
1482
|
+
}
|
|
1483
|
+
if (prop === "list") {
|
|
1484
|
+
message_default('Some state could be hidden when you use "state.list". Use "state" to see the complete list ');
|
|
1485
|
+
return deepRaw(target, /* @__PURE__ */ new WeakSet());
|
|
1486
|
+
}
|
|
1487
|
+
if (typeof prop === "string") {
|
|
1488
|
+
const path = tree.length > 0 ? "state." + tree.concat(prop).join(".") : "state." + prop;
|
|
1489
|
+
internal_default.lastAccessedPath = path;
|
|
1490
|
+
if (internal_default.tracking) {
|
|
1491
|
+
internal_default.trackedPaths.add(path);
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
if (prop === "_lastPath") {
|
|
1495
|
+
return internal_default.lastAccessedPath;
|
|
1496
|
+
}
|
|
1497
|
+
if (prop === "remove") {
|
|
1498
|
+
return (key) => {
|
|
1499
|
+
delete target[key];
|
|
1500
|
+
return true;
|
|
1501
|
+
};
|
|
1502
|
+
}
|
|
1503
|
+
if (prop === "removeAll") {
|
|
1504
|
+
return () => {
|
|
1505
|
+
try {
|
|
1506
|
+
for (const key of Reflect.ownKeys(target)) {
|
|
1507
|
+
if (typeof target[key] !== "function" && !["list", "remove", "removeAll", "lock", "unlock", "persist"].includes(key)) {
|
|
1508
|
+
delete target[key];
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
} catch (error) {
|
|
1512
|
+
console.error(error);
|
|
1513
|
+
}
|
|
1514
|
+
return;
|
|
1515
|
+
};
|
|
1516
|
+
}
|
|
1517
|
+
if (Object.isFrozen(target[prop])) return target[prop];
|
|
1518
|
+
if (prop === "lock" || prop === "unlock") {
|
|
1519
|
+
const value = Reflect.get(target, prop);
|
|
1520
|
+
if (value) return value;
|
|
1521
|
+
}
|
|
1522
|
+
try {
|
|
1523
|
+
const value = Reflect.get(target, prop);
|
|
1524
|
+
if (value && typeof value === "object" && ["Array", "Object"].includes(value.constructor.name)) {
|
|
1525
|
+
let wrapper = wrapperCache.get(value);
|
|
1526
|
+
if (!wrapper) {
|
|
1527
|
+
wrapper = buildProxy(value, callback, tree.concat(prop));
|
|
1528
|
+
wrapperCache.set(value, wrapper);
|
|
1529
|
+
}
|
|
1530
|
+
return wrapper;
|
|
1531
|
+
}
|
|
1532
|
+
return value;
|
|
1533
|
+
} catch (error) {
|
|
1534
|
+
console.error("Error: ", error);
|
|
1535
|
+
return void 0;
|
|
1536
|
+
}
|
|
1537
|
+
},
|
|
1538
|
+
set(target, key, value) {
|
|
1539
|
+
if (internal_default.locked) {
|
|
1540
|
+
console.error("Error: state is locked. Use state.unlock() to enable modifications.");
|
|
1541
|
+
return false;
|
|
1542
|
+
}
|
|
1543
|
+
if (protect.includes(key)) {
|
|
1544
|
+
console.error("key " + key + " is protected");
|
|
1545
|
+
return false;
|
|
1546
|
+
}
|
|
1547
|
+
if (target[key] && typeof target[key] === "object" && Object.isFrozen(target[key])) {
|
|
1548
|
+
console.error(`Error: state '${key}' is locked`);
|
|
1549
|
+
return false;
|
|
1550
|
+
}
|
|
1551
|
+
if (target[key] && typeof target[key] === "object" && target[key].__locked) {
|
|
1552
|
+
console.error(`Error: state '${key}' is locked. Use state.${key}.unlock() to enable modifications.`);
|
|
1553
|
+
return false;
|
|
1554
|
+
}
|
|
1555
|
+
try {
|
|
1556
|
+
const path = objPath(key, tree);
|
|
1557
|
+
const rawValue = deepRaw(value);
|
|
1558
|
+
const _schemaResult = schema.validate(path, rawValue);
|
|
1559
|
+
if (!_schemaResult.valid) {
|
|
1560
|
+
if (DEV && internal_default.debug) {
|
|
1561
|
+
console.error(
|
|
1562
|
+
`[memorio schema] Validation failed for '${path}':`,
|
|
1563
|
+
_schemaResult.errors
|
|
1564
|
+
);
|
|
1565
|
+
}
|
|
1566
|
+
message_default(
|
|
1567
|
+
`[memorio schema] Validation failed for '${path}': ` + (_schemaResult.errors?.join(", ") || "unknown error")
|
|
1568
|
+
);
|
|
1569
|
+
return false;
|
|
1570
|
+
}
|
|
1571
|
+
callback(
|
|
1572
|
+
{
|
|
1573
|
+
action: "set",
|
|
1574
|
+
path,
|
|
1575
|
+
target,
|
|
1576
|
+
newValue: rawValue,
|
|
1577
|
+
previousValue: Reflect.get(target, key)
|
|
1578
|
+
}
|
|
1579
|
+
);
|
|
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
|
+
}
|
|
1591
|
+
Reflect.set(target, key, rawValue);
|
|
1592
|
+
internal_default.bumpStateVersion();
|
|
1593
|
+
if (target[key] && typeof target[key] === "object") {
|
|
1594
|
+
const lockValue = target[key];
|
|
1595
|
+
if (lockValue && typeof lockValue === "object") {
|
|
1596
|
+
try {
|
|
1597
|
+
Object.defineProperty(lockValue, "__locked", {
|
|
1598
|
+
writable: true,
|
|
1599
|
+
enumerable: false,
|
|
1600
|
+
configurable: false,
|
|
1601
|
+
value: false
|
|
1602
|
+
});
|
|
1603
|
+
Object.defineProperty(lockValue, "lock", {
|
|
1604
|
+
writable: false,
|
|
1605
|
+
enumerable: false,
|
|
1606
|
+
configurable: false,
|
|
1607
|
+
value: /* @__PURE__ */ __name(() => {
|
|
1608
|
+
Object.defineProperty(lockValue, "__locked", {
|
|
1609
|
+
writable: true,
|
|
1610
|
+
enumerable: false,
|
|
1611
|
+
configurable: false,
|
|
1612
|
+
value: true
|
|
1613
|
+
});
|
|
1614
|
+
message_default(`State '${key}' is now locked.`);
|
|
1615
|
+
}, "value")
|
|
1616
|
+
});
|
|
1617
|
+
Object.defineProperty(lockValue, "unlock", {
|
|
1618
|
+
writable: false,
|
|
1619
|
+
enumerable: false,
|
|
1620
|
+
configurable: false,
|
|
1621
|
+
value: /* @__PURE__ */ __name(() => {
|
|
1622
|
+
Object.defineProperty(lockValue, "__locked", {
|
|
1623
|
+
writable: true,
|
|
1624
|
+
enumerable: false,
|
|
1625
|
+
configurable: false,
|
|
1626
|
+
value: false
|
|
1627
|
+
});
|
|
1628
|
+
message_default(`State '${key}' is now unlocked.`);
|
|
1629
|
+
}, "value")
|
|
1630
|
+
});
|
|
1631
|
+
} catch (err) {
|
|
1632
|
+
if (DEV) console.warn("[memorio state] lock/unlock defineProperty failed:", err);
|
|
1633
|
+
}
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
return true;
|
|
1637
|
+
} catch (error) {
|
|
1638
|
+
console.error("Error in set trap:", error);
|
|
1639
|
+
return false;
|
|
1640
|
+
}
|
|
1641
|
+
},
|
|
1642
|
+
deleteProperty(target, prop) {
|
|
1643
|
+
try {
|
|
1644
|
+
const path = objPath(prop, tree);
|
|
1645
|
+
const previousValue = Reflect.get(target, prop);
|
|
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
|
+
}
|
|
1658
|
+
return Reflect.deleteProperty(target, prop);
|
|
1659
|
+
} catch (error) {
|
|
1660
|
+
console.error("Error in deleteProperty trap:", error);
|
|
1661
|
+
return false;
|
|
1662
|
+
}
|
|
1663
|
+
},
|
|
1664
|
+
ownKeys(target) {
|
|
1665
|
+
return Reflect.ownKeys(target);
|
|
1666
|
+
},
|
|
1667
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
1668
|
+
return Reflect.getOwnPropertyDescriptor(target, prop);
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1671
|
+
);
|
|
1672
|
+
}, "buildProxy");
|
|
1673
|
+
var STATE_INSTANCE_KEY = "__memorio_state_instance__";
|
|
1674
|
+
var state2;
|
|
1675
|
+
if (globalThis[STATE_INSTANCE_KEY]) {
|
|
1676
|
+
state2 = globalThis[STATE_INSTANCE_KEY];
|
|
1677
|
+
} else {
|
|
1678
|
+
state2 = buildProxy({}, (mutation) => {
|
|
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);
|
|
1692
|
+
}
|
|
1693
|
+
});
|
|
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
|
+
};
|
|
1724
|
+
}
|
|
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
|
+
}
|
|
1756
|
+
globalThis[STATE_INSTANCE_KEY] = state2;
|
|
1757
|
+
}
|
|
1758
|
+
|
|
1759
|
+
// functions/session/index.ts
|
|
1760
|
+
init_cjs_shims();
|
|
1761
|
+
init_platform();
|
|
1762
|
+
init_internal();
|
|
1763
|
+
init_message();
|
|
1764
|
+
var _sessionStorage = getSessionStorage();
|
|
1765
|
+
var _fallbackStorageByContext2 = /* @__PURE__ */ new Map();
|
|
1766
|
+
function _getFallbackStorage2() {
|
|
1767
|
+
const contextName = internal_default.currentContext || "";
|
|
1768
|
+
const key = contextName || "default";
|
|
1769
|
+
if (!_fallbackStorageByContext2.has(key)) {
|
|
1770
|
+
_fallbackStorageByContext2.set(key, /* @__PURE__ */ new Map());
|
|
1771
|
+
}
|
|
1772
|
+
return _fallbackStorageByContext2.get(key);
|
|
1773
|
+
}
|
|
1774
|
+
__name(_getFallbackStorage2, "_getFallbackStorage");
|
|
1775
|
+
var _hasStorage2 = _sessionStorage !== null;
|
|
1776
|
+
function _getPrefix2() {
|
|
1777
|
+
const contextName = internal_default.currentContext || "";
|
|
1477
1778
|
return contextName ? `${contextName}-` : `memorio_session_`;
|
|
1478
1779
|
}
|
|
1479
1780
|
__name(_getPrefix2, "_getPrefix");
|
|
@@ -1713,120 +2014,6 @@ if (globalThis[SESSION_INSTANCE_KEY]) {
|
|
|
1713
2014
|
|
|
1714
2015
|
// functions/memory/index.ts
|
|
1715
2016
|
init_idb();
|
|
1716
|
-
|
|
1717
|
-
// core/hlc.ts
|
|
1718
|
-
init_cjs_shims();
|
|
1719
|
-
var NODE_ID_KEY = "__memorio_hlc_node";
|
|
1720
|
-
function getNodeId() {
|
|
1721
|
-
if (typeof globalThis === "undefined") return "node";
|
|
1722
|
-
try {
|
|
1723
|
-
if (typeof sessionStorage !== "undefined" && sessionStorage) {
|
|
1724
|
-
let id = sessionStorage.getItem(NODE_ID_KEY);
|
|
1725
|
-
if (!id) {
|
|
1726
|
-
id = generateNodeId();
|
|
1727
|
-
sessionStorage.setItem(NODE_ID_KEY, id);
|
|
1728
|
-
}
|
|
1729
|
-
return id;
|
|
1730
|
-
}
|
|
1731
|
-
} catch {
|
|
1732
|
-
}
|
|
1733
|
-
if (!globalThis.__memorioHlcNodeId) {
|
|
1734
|
-
globalThis.__memorioHlcNodeId = generateNodeId();
|
|
1735
|
-
}
|
|
1736
|
-
return globalThis.__memorioHlcNodeId;
|
|
1737
|
-
}
|
|
1738
|
-
__name(getNodeId, "getNodeId");
|
|
1739
|
-
function generateNodeId() {
|
|
1740
|
-
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
1741
|
-
return crypto.randomUUID().slice(0, 8);
|
|
1742
|
-
}
|
|
1743
|
-
return Math.random().toString(36).slice(2, 10);
|
|
1744
|
-
}
|
|
1745
|
-
__name(generateNodeId, "generateNodeId");
|
|
1746
|
-
var HLC_KEY = "__memorio_hlc_local";
|
|
1747
|
-
var local = globalThis[HLC_KEY] || {
|
|
1748
|
-
physical: 0,
|
|
1749
|
-
logical: 0,
|
|
1750
|
-
node: getNodeId()
|
|
1751
|
-
};
|
|
1752
|
-
if (!globalThis[HLC_KEY]) {
|
|
1753
|
-
globalThis[HLC_KEY] = local;
|
|
1754
|
-
}
|
|
1755
|
-
function tick(_incoming) {
|
|
1756
|
-
const now2 = Date.now();
|
|
1757
|
-
const base = local.physical;
|
|
1758
|
-
let physical;
|
|
1759
|
-
let logical;
|
|
1760
|
-
if (now2 > base) {
|
|
1761
|
-
physical = now2;
|
|
1762
|
-
logical = 0;
|
|
1763
|
-
} else if (now2 < base) {
|
|
1764
|
-
physical = base;
|
|
1765
|
-
logical = local.logical + 1;
|
|
1766
|
-
} else {
|
|
1767
|
-
physical = base;
|
|
1768
|
-
logical = local.logical + 1;
|
|
1769
|
-
}
|
|
1770
|
-
local.physical = physical;
|
|
1771
|
-
local.logical = logical;
|
|
1772
|
-
return { physical, logical, node: local.node };
|
|
1773
|
-
}
|
|
1774
|
-
__name(tick, "tick");
|
|
1775
|
-
function receive(remote) {
|
|
1776
|
-
const now2 = Date.now();
|
|
1777
|
-
if (remote.physical > local.physical) {
|
|
1778
|
-
local.physical = remote.physical > now2 ? remote.physical : now2;
|
|
1779
|
-
local.logical = 1;
|
|
1780
|
-
} else if (remote.physical === local.physical) {
|
|
1781
|
-
local.logical = Math.max(local.logical, remote.logical) + 1;
|
|
1782
|
-
}
|
|
1783
|
-
local.node = getNodeId();
|
|
1784
|
-
return { ...local };
|
|
1785
|
-
}
|
|
1786
|
-
__name(receive, "receive");
|
|
1787
|
-
function compare(a, b) {
|
|
1788
|
-
if (a.physical !== b.physical) {
|
|
1789
|
-
return a.physical < b.physical ? -1 : 1;
|
|
1790
|
-
}
|
|
1791
|
-
if (a.logical !== b.logical) {
|
|
1792
|
-
return a.logical < b.logical ? -1 : 1;
|
|
1793
|
-
}
|
|
1794
|
-
if (a.node !== b.node) {
|
|
1795
|
-
return a.node < b.node ? -1 : 1;
|
|
1796
|
-
}
|
|
1797
|
-
return 0;
|
|
1798
|
-
}
|
|
1799
|
-
__name(compare, "compare");
|
|
1800
|
-
function happenedBefore(a, b) {
|
|
1801
|
-
return compare(a, b) < 0;
|
|
1802
|
-
}
|
|
1803
|
-
__name(happenedBefore, "happenedBefore");
|
|
1804
|
-
function serialize(hlc2) {
|
|
1805
|
-
return `hlc:${hlc2.physical}:${hlc2.logical}:${hlc2.node}`;
|
|
1806
|
-
}
|
|
1807
|
-
__name(serialize, "serialize");
|
|
1808
|
-
function parse(serialized) {
|
|
1809
|
-
if (!serialized.startsWith("hlc:")) return null;
|
|
1810
|
-
const parts = serialized.split(":");
|
|
1811
|
-
if (parts.length !== 4) return null;
|
|
1812
|
-
const physical = parseInt(parts[1], 10);
|
|
1813
|
-
const logical = parseInt(parts[2], 10);
|
|
1814
|
-
const node = parts[3];
|
|
1815
|
-
if (isNaN(physical) || isNaN(logical)) return null;
|
|
1816
|
-
return { physical, logical, node };
|
|
1817
|
-
}
|
|
1818
|
-
__name(parse, "parse");
|
|
1819
|
-
var hlc = {
|
|
1820
|
-
tick,
|
|
1821
|
-
receive,
|
|
1822
|
-
compare,
|
|
1823
|
-
happenedBefore,
|
|
1824
|
-
serialize,
|
|
1825
|
-
parse,
|
|
1826
|
-
getNodeId
|
|
1827
|
-
};
|
|
1828
|
-
|
|
1829
|
-
// functions/memory/index.ts
|
|
1830
2017
|
init_env();
|
|
1831
2018
|
|
|
1832
2019
|
// functions/memory/journal.ts
|