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