@cf-sync/client 0.3.1 → 0.4.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/dist/index.js
CHANGED
|
@@ -11,6 +11,36 @@ function request(req) {
|
|
|
11
11
|
req.onerror = () => reject(req.error ?? /* @__PURE__ */ new Error("IndexedDB request failed"));
|
|
12
12
|
});
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Row writes per batch. Each put structured-clones its value synchronously
|
|
16
|
+
* on the calling thread, so a bootstrap's worth of puts issued at once is
|
|
17
|
+
* one long main-thread task; batches let the page breathe between them.
|
|
18
|
+
*/
|
|
19
|
+
const WRITE_BATCH = 1e3;
|
|
20
|
+
/**
|
|
21
|
+
* Issues the row ops in batches, each queued from the previous batch's last
|
|
22
|
+
* request callback — the transaction stays active inside request callbacks,
|
|
23
|
+
* so every op still commits in the one transaction.
|
|
24
|
+
*/
|
|
25
|
+
function writeRows(rowStore, ops) {
|
|
26
|
+
return new Promise((resolve, reject) => {
|
|
27
|
+
let i = 0;
|
|
28
|
+
const next = () => {
|
|
29
|
+
let last = null;
|
|
30
|
+
for (const end = Math.min(i + WRITE_BATCH, ops.length); i < end; i++) {
|
|
31
|
+
const op = ops[i];
|
|
32
|
+
last = op.op === "put" ? rowStore.put(op.value, [op.tbl, op.id]) : rowStore.delete([op.tbl, op.id]);
|
|
33
|
+
}
|
|
34
|
+
if (!last || i >= ops.length) {
|
|
35
|
+
resolve();
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
last.onsuccess = next;
|
|
39
|
+
last.onerror = () => reject(last.error ?? /* @__PURE__ */ new Error("IndexedDB request failed"));
|
|
40
|
+
};
|
|
41
|
+
next();
|
|
42
|
+
});
|
|
43
|
+
}
|
|
14
44
|
function txnDone(txn) {
|
|
15
45
|
return new Promise((resolve, reject) => {
|
|
16
46
|
txn.oncomplete = () => resolve();
|
|
@@ -118,8 +148,7 @@ var IndexedDBSyncStore = class {
|
|
|
118
148
|
if (!(!update.clear && stored !== void 0 && stored.cursor.backendId === update.cursor.backendId && stored.cursor.version >= update.cursor.version)) {
|
|
119
149
|
const rowStore = txn.objectStore(ROWS);
|
|
120
150
|
if (update.clear) rowStore.clear();
|
|
121
|
-
|
|
122
|
-
else rowStore.delete([op.tbl, op.id]);
|
|
151
|
+
await writeRows(rowStore, update.ops);
|
|
123
152
|
metaStore.put({
|
|
124
153
|
schemaVersion: update.schemaVersion,
|
|
125
154
|
cursor: update.cursor
|
|
@@ -1814,11 +1843,11 @@ const runIntentTransaction = (intent, flush, persist) => {
|
|
|
1814
1843
|
});
|
|
1815
1844
|
tx.mutate(flush);
|
|
1816
1845
|
if (tx.mutations.length === 0) return persist();
|
|
1817
|
-
return tx.
|
|
1846
|
+
return tx.when("settled").then(() => void 0);
|
|
1818
1847
|
};
|
|
1819
1848
|
/** TanStack enriches rows with virtual props; keep them out of mutator reads and overlays. */
|
|
1820
1849
|
function stripVirtualProps(row) {
|
|
1821
|
-
const { $
|
|
1850
|
+
const { $hasPendingWrites, $origin, $key, $collectionId, ...data } = row;
|
|
1822
1851
|
return data;
|
|
1823
1852
|
}
|
|
1824
1853
|
function createHookGate() {
|
|
@@ -1924,6 +1953,7 @@ function workspaceCollectionOptions(cfg) {
|
|
|
1924
1953
|
getKey,
|
|
1925
1954
|
schema: tableSchema,
|
|
1926
1955
|
startSync: cfg.startSync,
|
|
1956
|
+
...cfg.startSync ? { gcTime: 0 } : {},
|
|
1927
1957
|
sync: {
|
|
1928
1958
|
rowUpdateMode: "full",
|
|
1929
1959
|
sync: (params) => {
|
|
@@ -2014,7 +2044,7 @@ function createCollections(client, options) {
|
|
|
2014
2044
|
startSync: options?.startSync ?? true
|
|
2015
2045
|
}));
|
|
2016
2046
|
client.registerApplier(table, collectionApplier(collection), runIntentTransaction);
|
|
2017
|
-
client.onDestroy(() => collection.cleanup());
|
|
2047
|
+
client.onDestroy(() => collection.cleanup({ dependents: true }));
|
|
2018
2048
|
collections[table] = collection;
|
|
2019
2049
|
}
|
|
2020
2050
|
return collections;
|
|
@@ -54,7 +54,7 @@ for (const { id } of tx.list('checklists', { where: { studyId, assignedTo: userI
|
|
|
54
54
|
}
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
On the server the filter runs in SQL, and each table and field you filter on gets an index the first time a list uses it, so a filtered read does not walk the table. The client's optimistic run still walks its collection, so a mutator that filters once per item in a large batch should build its own lookup up front.
|
|
58
58
|
|
|
59
59
|
The definitions can also be written directly inside `defineApp({ mutators: { ... } })` — inference is identical either way. `defineMutators` is only *required* when declaring an [`authContext`](/guide/auth#reading-the-verdict-in-mutators), its third argument.
|
|
60
60
|
|
|
@@ -32,7 +32,7 @@ The unit shape is the point for workspace-per-project apps: switching projects i
|
|
|
32
32
|
|
|
33
33
|
One fully typed collection per table in the client's schema — the one-line alternative to a `createCollection(workspaceCollectionOptions(…))` block per table. Rows are keyed by their `id` field (this engine's row model); a table whose rows key differently needs an individual [`workspaceCollectionOptions`](#workspacecollectionoptions) call with its own `getKey`.
|
|
34
34
|
|
|
35
|
-
Each collection's lifetime is tied to the client: [`client.destroy()`](/reference/sync-client#destroy) cleans them up too, so switching workspaces is one call. Collection ids are scoped by workspace (`workspace-<workspaceId>-<table>`), so an app with two open workspaces never collides on TanStack DB's app-wide ids. The collections also serve as `mutate`'s optimistic surface — [intent mutations](/guide/mutations) run their shared `apply` against them locally, attached at creation so intents fired before a collection has subscribers still get their optimistic effect.
|
|
35
|
+
Each collection's lifetime is tied to the client: [`client.destroy()`](/reference/sync-client#destroy) cleans them up too, along with any live queries still reading them, so switching workspaces is one call. Collection ids are scoped by workspace (`workspace-<workspaceId>-<table>`), so an app with two open workspaces never collides on TanStack DB's app-wide ids. The collections also serve as `mutate`'s optimistic surface — [intent mutations](/guide/mutations) run their shared `apply` against them locally, attached at creation so intents fired before a collection has subscribers still get their optimistic effect.
|
|
36
36
|
|
|
37
37
|
### startSync
|
|
38
38
|
|
|
@@ -72,7 +72,7 @@ Maps a row to its collection key. Required only when the table's row schema has
|
|
|
72
72
|
|
|
73
73
|
`boolean` · default: TanStack's lazy default
|
|
74
74
|
|
|
75
|
-
Start the sync pipeline immediately instead of on first subscriber. `createCollections` flips this to `true` for you; here it stays lazy unless you opt in.
|
|
75
|
+
Start the sync pipeline immediately instead of on first subscriber. `createCollections` flips this to `true` for you; here it stays lazy unless you opt in. An eager collection is never garbage-collected for lack of subscribers: it lives until its `cleanup()`, which `createCollections` ties to `client.destroy()`. A lazy one keeps TanStack's `gcTime` default.
|
|
76
76
|
|
|
77
77
|
## Writes and the wire
|
|
78
78
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cf-sync/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Browser client for cf-sync: SyncClient (optimistic mutations, offline outbox, IndexedDB mirror, presence) and a TanStack DB collection adapter, with React hooks",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -40,11 +40,11 @@
|
|
|
40
40
|
"access": "public"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@cf-sync/protocol": "^0.2.
|
|
43
|
+
"@cf-sync/protocol": "^0.2.1"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"@tanstack/db": "^0.
|
|
47
|
-
"@tanstack/react-db": "^0.
|
|
46
|
+
"@tanstack/db": "^1.0.0-rc.1",
|
|
47
|
+
"@tanstack/react-db": "^1.0.0-rc.1",
|
|
48
48
|
"react": ">=18"
|
|
49
49
|
},
|
|
50
50
|
"peerDependenciesMeta": {
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
}
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
|
-
"@tanstack/db": "
|
|
59
|
+
"@tanstack/db": "1.0.0-rc.1.5",
|
|
60
60
|
"@types/react": "^19.0.0",
|
|
61
61
|
"@types/react-dom": "^19.2.3",
|
|
62
62
|
"fake-indexeddb": "^6.2.5",
|