@automerge/automerge-repo-solid-primitives 2.5.6 → 2.6.0-alpha.1
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/dist/index.js +9 -78
- package/dist/index.js.map +1 -1
- package/dist/useDocHandle.d.ts +1 -2
- package/package.json +3 -3
- package/src/makeDocumentProjection.ts +8 -2
- package/src/useDocHandle.ts +5 -39
- package/test/useDocHandle.test.tsx +25 -0
- package/test/useDocument.test.tsx +94 -0
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { onCleanup, createMemo, createContext, useContext, createResource, createEffect, createSignal } from 'solid-js';
|
|
2
|
-
import { createStore,
|
|
2
|
+
import { createStore, reconcile, produce } from 'solid-js/store';
|
|
3
3
|
|
|
4
4
|
// Properties of the document root object
|
|
5
5
|
const STATE = Symbol.for("_am_meta"); // symbol used to hide application metadata on automerge objects
|
|
@@ -1232,51 +1232,6 @@ function getObjectId(doc, prop) {
|
|
|
1232
1232
|
return _obj(doc);
|
|
1233
1233
|
}
|
|
1234
1234
|
}
|
|
1235
|
-
/**
|
|
1236
|
-
* Create a set of patches representing the change from one set of heads to another
|
|
1237
|
-
*
|
|
1238
|
-
* If either of the heads are missing from the document the returned set of patches will be empty
|
|
1239
|
-
*/
|
|
1240
|
-
function diff(doc, before, after) {
|
|
1241
|
-
checkHeads(before, "before");
|
|
1242
|
-
checkHeads(after, "after");
|
|
1243
|
-
const state = _state(doc);
|
|
1244
|
-
if (state.mostRecentPatch &&
|
|
1245
|
-
equals(state.mostRecentPatch.before, before) &&
|
|
1246
|
-
equals(state.mostRecentPatch.after, after)) {
|
|
1247
|
-
return state.mostRecentPatch.patches;
|
|
1248
|
-
}
|
|
1249
|
-
return state.handle.diff(before, after);
|
|
1250
|
-
}
|
|
1251
|
-
function checkHeads(heads, fieldname) {
|
|
1252
|
-
if (!Array.isArray(heads)) {
|
|
1253
|
-
throw new Error(`${fieldname} must be an array`);
|
|
1254
|
-
}
|
|
1255
|
-
}
|
|
1256
|
-
/** @hidden */
|
|
1257
|
-
// FIXME : no tests
|
|
1258
|
-
// FIXME can we just use deep equals now?
|
|
1259
|
-
function equals(val1, val2) {
|
|
1260
|
-
if (!isObject(val1) || !isObject(val2))
|
|
1261
|
-
return val1 === val2;
|
|
1262
|
-
const keys1 = Object.keys(val1).sort(), keys2 = Object.keys(val2).sort();
|
|
1263
|
-
if (keys1.length !== keys2.length)
|
|
1264
|
-
return false;
|
|
1265
|
-
for (let i = 0; i < keys1.length; i++) {
|
|
1266
|
-
if (keys1[i] !== keys2[i])
|
|
1267
|
-
return false;
|
|
1268
|
-
if (!equals(val1[keys1[i]], val2[keys2[i]]))
|
|
1269
|
-
return false;
|
|
1270
|
-
}
|
|
1271
|
-
return true;
|
|
1272
|
-
}
|
|
1273
|
-
/**
|
|
1274
|
-
* Get the hashes of the heads of this document
|
|
1275
|
-
*/
|
|
1276
|
-
function getHeads(doc) {
|
|
1277
|
-
const state = _state(doc);
|
|
1278
|
-
return state.heads || state.handle.getHeads();
|
|
1279
|
-
}
|
|
1280
1235
|
function isAutomerge(doc) {
|
|
1281
1236
|
if (typeof doc == "object" && doc !== null) {
|
|
1282
1237
|
return getObjectId(doc) === "_root" && !!Reflect.get(doc, STATE);
|
|
@@ -1285,9 +1240,6 @@ function isAutomerge(doc) {
|
|
|
1285
1240
|
return false;
|
|
1286
1241
|
}
|
|
1287
1242
|
}
|
|
1288
|
-
function isObject(obj) {
|
|
1289
|
-
return typeof obj === "object" && obj !== null;
|
|
1290
|
-
}
|
|
1291
1243
|
/**
|
|
1292
1244
|
* Modify a string
|
|
1293
1245
|
*
|
|
@@ -1361,7 +1313,7 @@ function autoproduce(payload) {
|
|
|
1361
1313
|
const cache = /* @__PURE__ */ new WeakMap();
|
|
1362
1314
|
function initial(handle) {
|
|
1363
1315
|
const template = {};
|
|
1364
|
-
applyPatches(template,
|
|
1316
|
+
applyPatches(template, handle.diff([], handle.heads()));
|
|
1365
1317
|
return template;
|
|
1366
1318
|
}
|
|
1367
1319
|
function makeDocumentProjection(handle) {
|
|
@@ -1388,6 +1340,10 @@ function makeDocumentProjection(handle) {
|
|
|
1388
1340
|
}
|
|
1389
1341
|
});
|
|
1390
1342
|
function patch(payload) {
|
|
1343
|
+
if (payload.scopeReplaced) {
|
|
1344
|
+
set(reconcile(payload.doc ?? {}));
|
|
1345
|
+
return;
|
|
1346
|
+
}
|
|
1391
1347
|
set(produce(autoproduce(payload)));
|
|
1392
1348
|
}
|
|
1393
1349
|
function ondelete() {
|
|
@@ -1410,8 +1366,6 @@ const RepoContext = createContext(
|
|
|
1410
1366
|
null
|
|
1411
1367
|
);
|
|
1412
1368
|
|
|
1413
|
-
const readyStates = ["ready", "deleted", "unavailable"];
|
|
1414
|
-
const badStates = ["deleted", "unavailable"];
|
|
1415
1369
|
function useDocHandle(url, options) {
|
|
1416
1370
|
const contextRepo = useContext(RepoContext);
|
|
1417
1371
|
if (!options?.repo && !contextRepo) {
|
|
@@ -1422,35 +1376,12 @@ function useDocHandle(url, options) {
|
|
|
1422
1376
|
if (options?.["~skipInitialValue"]) return void 0;
|
|
1423
1377
|
const unwrappedURL = typeof url == "function" ? url() : url;
|
|
1424
1378
|
if (!unwrappedURL) return void 0;
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
const existingHandle = repo.handles[documentId];
|
|
1428
|
-
if (existingHandle?.isReady()) {
|
|
1429
|
-
return existingHandle;
|
|
1430
|
-
}
|
|
1431
|
-
} catch (error) {
|
|
1432
|
-
console.error("Error parsing URL:", error);
|
|
1433
|
-
}
|
|
1379
|
+
const state = repo.findWithProgress(unwrappedURL).peek();
|
|
1380
|
+
return state.state === "ready" ? state.handle : void 0;
|
|
1434
1381
|
}
|
|
1435
1382
|
const [handle, { mutate }] = createResource(
|
|
1436
1383
|
url,
|
|
1437
|
-
|
|
1438
|
-
const handle2 = await repo.find(url2, {
|
|
1439
|
-
allowableStates: readyStates
|
|
1440
|
-
});
|
|
1441
|
-
const reject = (state) => Promise.reject(new Error(`document not available: [${state}]`));
|
|
1442
|
-
if (handle2.isReady()) {
|
|
1443
|
-
return handle2;
|
|
1444
|
-
} else if (handle2.inState(badStates)) {
|
|
1445
|
-
return reject(handle2.state);
|
|
1446
|
-
}
|
|
1447
|
-
return handle2.whenReady(readyStates).then(() => {
|
|
1448
|
-
if (handle2.isReady()) {
|
|
1449
|
-
return handle2;
|
|
1450
|
-
}
|
|
1451
|
-
return reject(handle2.state);
|
|
1452
|
-
});
|
|
1453
|
-
},
|
|
1384
|
+
(url2) => repo.findWithProgress(url2).whenReady(),
|
|
1454
1385
|
{
|
|
1455
1386
|
initialValue: getExistingHandle()
|
|
1456
1387
|
}
|