@terreno/syncdb 57.2.0 → 57.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/README.md +13 -3
- package/dist/auth/betterAuthReactClient.d.ts +17 -0
- package/dist/auth/betterAuthReactClient.d.ts.map +1 -0
- package/dist/auth/betterAuthReactClient.js +11 -0
- package/dist/auth/betterAuthReactClient.js.map +1 -0
- package/dist/auth/betterAuthReactClient.test.d.ts +2 -0
- package/dist/auth/betterAuthReactClient.test.d.ts.map +1 -0
- package/dist/auth/betterAuthReactClient.test.js +43 -0
- package/dist/auth/betterAuthReactClient.test.js.map +1 -0
- package/dist/client.adminWindowMutation.test.d.ts +2 -0
- package/dist/client.adminWindowMutation.test.d.ts.map +1 -0
- package/dist/client.adminWindowMutation.test.js +65 -0
- package/dist/client.adminWindowMutation.test.js.map +1 -0
- package/dist/client.d.ts +22 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +109 -7
- package/dist/client.js.map +1 -1
- package/dist/client.test.js +435 -2
- package/dist/client.test.js.map +1 -1
- package/dist/codegen/emitSdk.js +3 -3
- package/dist/codegen/emitSdk.js.map +1 -1
- package/dist/codegen/generate.test.js +122 -4
- package/dist/codegen/generate.test.js.map +1 -1
- package/dist/codegen/safeIdentifiers.d.ts +5 -0
- package/dist/codegen/safeIdentifiers.d.ts.map +1 -1
- package/dist/codegen/safeIdentifiers.js +8 -0
- package/dist/codegen/safeIdentifiers.js.map +1 -1
- package/dist/codegen/safeIdentifiers.test.js +20 -8
- package/dist/codegen/safeIdentifiers.test.js.map +1 -1
- package/dist/codegen/types.d.ts +4 -3
- package/dist/codegen/types.d.ts.map +1 -1
- package/dist/debug/debugChannel.test.js +5 -0
- package/dist/debug/debugChannel.test.js.map +1 -1
- package/dist/debug/debugLog.d.ts +1 -0
- package/dist/debug/debugLog.d.ts.map +1 -1
- package/dist/debug/debugLog.js +1 -0
- package/dist/debug/debugLog.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.d.ts +2 -0
- package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.d.ts.map +1 -0
- package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.js +19 -0
- package/dist/isolated/defaultPersisterFactoryNativeErrors.isolated.js.map +1 -0
- package/dist/mutations/outbox.d.ts +7 -3
- package/dist/mutations/outbox.d.ts.map +1 -1
- package/dist/mutations/outbox.js +6 -0
- package/dist/mutations/outbox.js.map +1 -1
- package/dist/mutations/outbox.test.js +17 -0
- package/dist/mutations/outbox.test.js.map +1 -1
- package/dist/persisters/encryptedIndexedDbPersister.d.ts +2 -1
- package/dist/persisters/encryptedIndexedDbPersister.d.ts.map +1 -1
- package/dist/persisters/encryptedIndexedDbPersister.js +24 -0
- package/dist/persisters/encryptedIndexedDbPersister.js.map +1 -1
- package/dist/persisters/encryptedIndexedDbPersister.test.js +26 -0
- package/dist/persisters/encryptedIndexedDbPersister.test.js.map +1 -1
- package/dist/persisters/types.d.ts +9 -1
- package/dist/persisters/types.d.ts.map +1 -1
- package/dist/storage/idb.d.ts +1 -0
- package/dist/storage/idb.d.ts.map +1 -1
- package/dist/storage/idb.js +1 -0
- package/dist/storage/idb.js.map +1 -1
- package/dist/storage/schema.d.ts.map +1 -1
- package/dist/storage/schema.js +1 -0
- package/dist/storage/schema.js.map +1 -1
- package/dist/storage/store.d.ts +3 -2
- package/dist/storage/store.d.ts.map +1 -1
- package/dist/storage/types.d.ts +1 -7
- package/dist/storage/types.d.ts.map +1 -1
- package/dist/sync/deltaApplier.d.ts +2 -0
- package/dist/sync/deltaApplier.d.ts.map +1 -1
- package/dist/sync/deltaApplier.js +9 -0
- package/dist/sync/deltaApplier.js.map +1 -1
- package/dist/sync/deltaApplier.test.js +63 -0
- package/dist/sync/deltaApplier.test.js.map +1 -1
- package/dist/sync/entityRepair.d.ts +1 -0
- package/dist/sync/entityRepair.d.ts.map +1 -1
- package/dist/sync/entityRepair.js +1 -0
- package/dist/sync/entityRepair.js.map +1 -1
- package/dist/sync/httpChannel.d.ts +3 -1
- package/dist/sync/httpChannel.d.ts.map +1 -1
- package/dist/sync/httpChannel.js +3 -1
- package/dist/sync/httpChannel.js.map +1 -1
- package/dist/sync/httpChannel.test.js +68 -0
- package/dist/sync/httpChannel.test.js.map +1 -1
- package/dist/sync/replayCoordinator.d.ts +6 -12
- package/dist/sync/replayCoordinator.d.ts.map +1 -1
- package/dist/sync/replayCoordinator.js +9 -2
- package/dist/sync/replayCoordinator.js.map +1 -1
- package/dist/sync/socketTransport.d.ts +3 -45
- package/dist/sync/socketTransport.d.ts.map +1 -1
- package/dist/sync/socketTransport.js +31 -9
- package/dist/sync/socketTransport.js.map +1 -1
- package/dist/sync/socketTransport.test.js +7 -0
- package/dist/sync/socketTransport.test.js.map +1 -1
- package/dist/sync/transport.d.ts +3 -1
- package/dist/sync/transport.d.ts.map +1 -1
- package/dist/sync/windowHydrate.d.ts +25 -0
- package/dist/sync/windowHydrate.d.ts.map +1 -0
- package/dist/sync/windowHydrate.js +67 -0
- package/dist/sync/windowHydrate.js.map +1 -0
- package/dist/sync/windowHydrate.test.d.ts +2 -0
- package/dist/sync/windowHydrate.test.d.ts.map +1 -0
- package/dist/sync/windowHydrate.test.js +164 -0
- package/dist/sync/windowHydrate.test.js.map +1 -0
- package/dist/testing/fakeTransport.js +1 -1
- package/dist/testing/fakeTransport.js.map +1 -1
- package/dist/types.d.ts +23 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +17 -2
- package/src/auth/betterAuthReactClient.test.ts +50 -0
- package/src/auth/betterAuthReactClient.ts +33 -0
- package/src/client.adminWindowMutation.test.ts +69 -0
- package/src/client.test.ts +480 -2
- package/src/client.ts +147 -6
- package/src/codegen/emitSdk.ts +3 -3
- package/src/codegen/generate.test.ts +137 -4
- package/src/codegen/safeIdentifiers.test.ts +37 -10
- package/src/codegen/safeIdentifiers.ts +17 -0
- package/src/codegen/types.ts +3 -3
- package/src/debug/debugChannel.test.ts +6 -0
- package/src/debug/debugLog.ts +1 -0
- package/src/index.ts +4 -0
- package/src/isolated/defaultPersisterFactoryNativeErrors.isolated.ts +23 -0
- package/src/mutations/outbox.test.ts +20 -0
- package/src/mutations/outbox.ts +11 -3
- package/src/persisters/encryptedIndexedDbPersister.test.ts +29 -0
- package/src/persisters/encryptedIndexedDbPersister.ts +27 -1
- package/src/persisters/types.ts +10 -1
- package/src/storage/idb.ts +1 -0
- package/src/storage/schema.ts +1 -0
- package/src/storage/store.ts +2 -2
- package/src/storage/types.ts +1 -8
- package/src/sync/deltaApplier.test.ts +70 -0
- package/src/sync/deltaApplier.ts +10 -0
- package/src/sync/entityRepair.ts +1 -0
- package/src/sync/httpChannel.test.ts +82 -0
- package/src/sync/httpChannel.ts +5 -0
- package/src/sync/replayCoordinator.ts +10 -3
- package/src/sync/socketTransport.test.ts +10 -2
- package/src/sync/socketTransport.ts +64 -22
- package/src/sync/transport.ts +1 -1
- package/src/sync/windowHydrate.test.ts +179 -0
- package/src/sync/windowHydrate.ts +97 -0
- package/src/testing/fakeTransport.ts +1 -1
- package/src/types.ts +24 -0
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# @terreno/syncdb
|
|
2
2
|
|
|
3
|
-
Local-first data layer for
|
|
3
|
+
Local-first data layer for @terreno apps: TinyBase MergeableStore, durable outbox, websocket delta sync, encrypted web persistence.
|
|
4
|
+
|
|
5
|
+
Local-first data layer for Terreno apps. A TinyBase `MergeableStore` (encrypted IndexedDB on web, expo-sqlite on native) is the UI's source of truth: reads come from the local store, writes apply optimistically and enqueue in a durable outbox, and the server reconciles asynchronously over a socket delta protocol with HTTP snapshot catch-up. Every mutation executes the existing `@terreno/api` modelRouter write path — identical permissions, hooks, and validation as REST. Supersedes `@terreno/rtk` for data-synchronization concerns (see [the migration guide](https://github.com/flourishhealth/terreno/blob/master/docs/how-to/migrate-rtk-to-syncdb.md)).
|
|
4
6
|
|
|
5
7
|
## Architecture
|
|
6
8
|
|
|
@@ -27,7 +29,7 @@ Local-first data layer for Terreno apps. A TinyBase `MergeableStore` (encrypted
|
|
|
27
29
|
└── POST /sync/mutate (fallback while the socket is down)
|
|
28
30
|
```
|
|
29
31
|
|
|
30
|
-
##
|
|
32
|
+
## Install
|
|
31
33
|
|
|
32
34
|
```bash
|
|
33
35
|
bun install @terreno/syncdb
|
|
@@ -448,5 +450,13 @@ This writes `store/syncDbSdk.ts` with `SYNC_COLLECTIONS`, entity types, and hook
|
|
|
448
450
|
- **`Model.bulkWrite` is unavailable on synced models**: it skips Mongoose middleware, so it can never stamp seqs. `registerSync` replaces the static with one that throws — bulk updates to a synced collection have to loop per document.
|
|
449
451
|
- **Native plaintext by design**: no SQLCipher; the OS sandbox is deemed sufficient.
|
|
450
452
|
- **Whole-store persistence**: each save serializes and (on web) encrypts the full store — cost scales with store size, not change size. Bound it by scoping which collections sync; saves are debounced.
|
|
451
|
-
- **`realtime` + `sync` coexistence**: a model may enable both (distinct events, `sync` vs `sync:delta`, so clients never double-apply), at the cost of double emission work.
|
|
453
|
+
- **`realtime` + `sync` coexistence**: a model may enable both (distinct events, `sync` vs `sync:delta`, so clients never double-apply), at the cost of double emission work. `modelRouter` `realtime` is **deprecated and will be removed in Terreno 58**; use `sync` only.
|
|
452
454
|
- **Seq counter write amplification**: every synced write does an atomic `$inc` on a per-stream counter doc. Acceptable at current scale; Redis-based counters are the documented upgrade path.
|
|
455
|
+
|
|
456
|
+
## Documentation
|
|
457
|
+
|
|
458
|
+
Full API reference: [docs/reference/syncdb.md](https://github.com/flourishhealth/terreno/blob/master/docs/reference/syncdb.md)
|
|
459
|
+
|
|
460
|
+
## License and Contributing
|
|
461
|
+
|
|
462
|
+
Licensed under the [MIT License](https://github.com/flourishhealth/terreno/blob/master/LICENSE). See [CONTRIBUTING.md](https://github.com/flourishhealth/terreno/blob/master/CONTRIBUTING.md) for contribution guidelines.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { BetterAuthClientLike, BetterAuthGetSessionResult } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* The Better Auth *react* client delivers session changes through a nanostore atom
|
|
4
|
+
* (`$store.atoms.session`), but its `useSession` is a React hook, not the `.subscribe`
|
|
5
|
+
* surface {@link betterAuthAdapter} looks for. Without the bridge the adapter falls back to
|
|
6
|
+
* polling `getSession()`, producing constant `/api/auth/get-session` traffic.
|
|
7
|
+
*
|
|
8
|
+
* Returns a client the adapter can subscribe to, or a `getSession`-only client when the
|
|
9
|
+
* atom is missing (a future Better Auth client shape change), which keeps the polling
|
|
10
|
+
* fallback as the safety net.
|
|
11
|
+
*/
|
|
12
|
+
/** The only member of the Better Auth react client this bridge reads statically. */
|
|
13
|
+
export interface BetterAuthReactClientLike {
|
|
14
|
+
getSession: () => Promise<BetterAuthGetSessionResult>;
|
|
15
|
+
}
|
|
16
|
+
export declare const bridgeBetterAuthReactClient: (authClient: BetterAuthReactClientLike) => BetterAuthClientLike;
|
|
17
|
+
//# sourceMappingURL=betterAuthReactClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"betterAuthReactClient.d.ts","sourceRoot":"","sources":["../../src/auth/betterAuthReactClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,oBAAoB,EAAE,0BAA0B,EAAC,MAAM,SAAS,CAAC;AAE9E;;;;;;;;;GASG;AACH,oFAAoF;AACpF,MAAM,WAAW,yBAAyB;IACxC,UAAU,EAAE,MAAM,OAAO,CAAC,0BAA0B,CAAC,CAAC;CACvD;AAMD,eAAO,MAAM,2BAA2B,GACtC,YAAY,yBAAyB,KACpC,oBASF,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const bridgeBetterAuthReactClient = (authClient) => {
|
|
2
|
+
const sessionAtom = authClient
|
|
3
|
+
.$store?.atoms?.session;
|
|
4
|
+
return {
|
|
5
|
+
getSession: () => authClient.getSession(),
|
|
6
|
+
...(sessionAtom
|
|
7
|
+
? { useSession: { subscribe: (listener) => sessionAtom.subscribe(listener) } }
|
|
8
|
+
: {}),
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=betterAuthReactClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"betterAuthReactClient.js","sourceRoot":"","sources":["../../src/auth/betterAuthReactClient.ts"],"names":[],"mappings":"AAqBA,MAAM,CAAC,MAAM,2BAA2B,GAAG,CACzC,UAAqC,EACf,EAAE;IACxB,MAAM,WAAW,GAAI,UAA0E;SAC5F,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC;IAC1B,OAAO;QACL,UAAU,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,UAAU,EAAE;QACzC,GAAG,CAAC,WAAW;YACb,CAAC,CAAC,EAAC,UAAU,EAAE,EAAC,SAAS,EAAE,CAAC,QAAQ,EAAgB,EAAE,CAAC,WAAW,CAAC,SAAS,CAAC,QAAQ,CAAC,EAAC,EAAC;YACxF,CAAC,CAAC,EAAE,CAAC;KACR,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"betterAuthReactClient.test.d.ts","sourceRoot":"","sources":["../../src/auth/betterAuthReactClient.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { describe, it } from "bun:test";
|
|
2
|
+
import { assert } from "chai";
|
|
3
|
+
import { bridgeBetterAuthReactClient } from "./betterAuthReactClient";
|
|
4
|
+
const sessionResult = { data: { session: { token: "token-1" }, user: { id: "user-1" } } };
|
|
5
|
+
describe("bridgeBetterAuthReactClient", () => {
|
|
6
|
+
it("exposes the session atom as a subscribe surface", async () => {
|
|
7
|
+
const listeners = [];
|
|
8
|
+
let unsubscribed = false;
|
|
9
|
+
const reactClient = {
|
|
10
|
+
$store: {
|
|
11
|
+
atoms: {
|
|
12
|
+
session: {
|
|
13
|
+
subscribe: (listener) => {
|
|
14
|
+
listeners.push(listener);
|
|
15
|
+
return () => {
|
|
16
|
+
unsubscribed = true;
|
|
17
|
+
};
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
getSession: async () => sessionResult,
|
|
23
|
+
};
|
|
24
|
+
const bridged = bridgeBetterAuthReactClient(reactClient);
|
|
25
|
+
assert.deepEqual(await bridged.getSession(), sessionResult);
|
|
26
|
+
assert.isFunction(bridged.useSession?.subscribe);
|
|
27
|
+
const received = [];
|
|
28
|
+
const unsubscribe = bridged.useSession?.subscribe?.((value) => {
|
|
29
|
+
received.push(value);
|
|
30
|
+
});
|
|
31
|
+
assert.equal(listeners.length, 1);
|
|
32
|
+
listeners[0]?.(sessionResult);
|
|
33
|
+
assert.deepEqual(received, [sessionResult]);
|
|
34
|
+
unsubscribe?.();
|
|
35
|
+
assert.isTrue(unsubscribed);
|
|
36
|
+
});
|
|
37
|
+
it("omits useSession when the client exposes no session atom", async () => {
|
|
38
|
+
const bridged = bridgeBetterAuthReactClient({ getSession: async () => sessionResult });
|
|
39
|
+
assert.isUndefined(bridged.useSession);
|
|
40
|
+
assert.deepEqual(await bridged.getSession(), sessionResult);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
//# sourceMappingURL=betterAuthReactClient.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"betterAuthReactClient.test.js","sourceRoot":"","sources":["../../src/auth/betterAuthReactClient.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,QAAQ,EAAE,EAAE,EAAC,MAAM,UAAU,CAAC;AACtC,OAAO,EAAC,MAAM,EAAC,MAAM,MAAM,CAAC;AAC5B,OAAO,EAAC,2BAA2B,EAAC,MAAM,yBAAyB,CAAC;AAEpE,MAAM,aAAa,GAAG,EAAC,IAAI,EAAE,EAAC,OAAO,EAAE,EAAC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E5C,WAAW,EAAE,EAAE,CAAC;QAChB,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;IAC9B,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,0DAA0D,EAAE,KAAK,IAAI,EAAE;QACxE,MAAM,OAAO,GAAG,2BAA2B,CAAC,EAAC,UAAU,EAAE,KAAK,IAAI,EAAE,CAAC,aAAa,EAAC,CAAC,CAAC;QAErF,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;QACvC,MAAM,CAAC,SAAS,CAAC,MAAM,OAAO,CAAC,UAAU,EAAE,EAAE,aAAa,CAAC,CAAC;IAC9D,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.adminWindowMutation.test.d.ts","sourceRoot":"","sources":["../src/client.adminWindowMutation.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from "bun:test";
|
|
2
|
+
import { createSyncDb } from "./client";
|
|
3
|
+
import { createFakeTransport } from "./testing/fakeTransport";
|
|
4
|
+
const makeAuthProvider = (userId = "user-1") => {
|
|
5
|
+
const listeners = new Set();
|
|
6
|
+
const state = { userId };
|
|
7
|
+
return {
|
|
8
|
+
provider: {
|
|
9
|
+
getToken: async () => "token",
|
|
10
|
+
getUserId: async () => state.userId,
|
|
11
|
+
onAuthChange: (callback) => {
|
|
12
|
+
listeners.add(callback);
|
|
13
|
+
return () => {
|
|
14
|
+
listeners.delete(callback);
|
|
15
|
+
};
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
describe("createSyncDb admin-window mutation mode", () => {
|
|
21
|
+
afterEach(() => {
|
|
22
|
+
// no shared state
|
|
23
|
+
});
|
|
24
|
+
it("tags outbox rows with mutationMode adminWindow for window collections", async () => {
|
|
25
|
+
const transport = createFakeTransport();
|
|
26
|
+
const { provider } = makeAuthProvider();
|
|
27
|
+
const client = createSyncDb({
|
|
28
|
+
authProvider: provider,
|
|
29
|
+
baseUrl: "http://localhost:4000",
|
|
30
|
+
collections: ["todos"],
|
|
31
|
+
name: "admin-window-mutate-test",
|
|
32
|
+
transport,
|
|
33
|
+
windowCollections: ["todos"],
|
|
34
|
+
});
|
|
35
|
+
await client.start();
|
|
36
|
+
const { mutationId } = client.mutate({
|
|
37
|
+
collection: "todos",
|
|
38
|
+
data: { title: "window write" },
|
|
39
|
+
operation: "create",
|
|
40
|
+
});
|
|
41
|
+
expect(client.outbox.getMutation({ mutationId })?.mutationMode).toBe("adminWindow");
|
|
42
|
+
await client.stop();
|
|
43
|
+
});
|
|
44
|
+
it("does not tag product collection mutations", async () => {
|
|
45
|
+
const transport = createFakeTransport();
|
|
46
|
+
const { provider } = makeAuthProvider();
|
|
47
|
+
const client = createSyncDb({
|
|
48
|
+
authProvider: provider,
|
|
49
|
+
baseUrl: "http://localhost:4000",
|
|
50
|
+
collections: ["todos", "notes"],
|
|
51
|
+
name: "product-mutate-test",
|
|
52
|
+
transport,
|
|
53
|
+
windowCollections: ["todos"],
|
|
54
|
+
});
|
|
55
|
+
await client.start();
|
|
56
|
+
const { mutationId } = client.mutate({
|
|
57
|
+
collection: "notes",
|
|
58
|
+
data: { title: "product write" },
|
|
59
|
+
operation: "create",
|
|
60
|
+
});
|
|
61
|
+
expect(client.outbox.getMutation({ mutationId })?.mutationMode).toBeUndefined();
|
|
62
|
+
await client.stop();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
//# sourceMappingURL=client.adminWindowMutation.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.adminWindowMutation.test.js","sourceRoot":"","sources":["../src/client.adminWindowMutation.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAC,MAAM,UAAU,CAAC;AAEzD,OAAO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O;YACnB,IAAI,EAAE,EAAC,KAAK,EAAE,cAAc,EAAC;YAC7B,SAAS,EAAE,QAAQ;SACpB,CAAC,CAAC;QACH,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,EAAC,UAAU,EAAC,CAAC,EAAE,YAAY,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;QAClF,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;IACtB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,2CAA2C,EAAE,KAAK,IAAI,EAAE;QACzD,MAAM,SAAS,GAAG,mBAAmB,EAAE,CAAC;QACxC,MAAM,EAAC,QAAQ,EAAC,GAAG,gBAAgB,EAAE,CAAC;QACtC,MAAM,MAAM,GAAG,YAAY,CAAC;YAC1B,YAAY,EAAE,QAAQ;YACtB,OAAO,EAAE,uBAAuB;YAChC,WAAW,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC;YAC/B,IAAI,EAAE,qBAAqB;YAC3B,SAAS;YACT,iBAAiB,EAAE,CAAC,OAAO,CAAC;SAC7B,CAAC,CAAC;QACH,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;QACrB,MAAM,EAAC,UAAU,EAAC,GAAG,MAAM,CAAC,MAAM,CAAC;YACjC,UAAU,EAAE,OAAO;YACnB,IAAI,EAAE,EAAC,KAAK,EAAE,eAAe,EAAC;YAC9B,SAAS,EAAE,QAAQ;SACpB,CAAC,CAAC;QACH,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,EAAC,UAAU,EAAC,CAAC,EAAE,YAAY,CAAC,CAAC,aAAa,EAAE,CAAC;QAC9E,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;IACtB,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/dist/client.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type { DefaultPersisterFactoryConfig, PersisterFactory } from "./persiste
|
|
|
5
5
|
import { type SyncStore } from "./storage/store";
|
|
6
6
|
import { type HttpChannel } from "./sync/httpChannel";
|
|
7
7
|
import type { SyncTransport } from "./sync/transport";
|
|
8
|
+
import { type HydrateWindowEntitiesResult } from "./sync/windowHydrate";
|
|
8
9
|
import type { AuthProvider, ConflictResolutionStrategy, SyncMutationOperation, SyncStatus } from "./types";
|
|
9
10
|
/** Periodic reconcile interval when not configured (5 minutes). */
|
|
10
11
|
export declare const DEFAULT_RECONCILE_INTERVAL_MS: number;
|
|
@@ -33,6 +34,12 @@ export interface SyncDbConfig {
|
|
|
33
34
|
baseUrl?: string;
|
|
34
35
|
/** Transport override (tests inject a fake; default is the socket transport). */
|
|
35
36
|
transport?: SyncTransport;
|
|
37
|
+
/**
|
|
38
|
+
* Optional selected organization id for `X-Organization-Id` on HTTP sync
|
|
39
|
+
* calls and the `organizationId` field on socket mutate payloads. Read at
|
|
40
|
+
* send time so admin org switches apply without reconnecting.
|
|
41
|
+
*/
|
|
42
|
+
organizationIdProvider?: () => string | undefined;
|
|
36
43
|
/** HTTP channel override (default is built from baseUrl when present). */
|
|
37
44
|
httpChannel?: HttpChannel;
|
|
38
45
|
/** Persister factory override (default is the platform default factory). */
|
|
@@ -49,6 +56,12 @@ export interface SyncDbConfig {
|
|
|
49
56
|
idbSetImpl?: DefaultPersisterFactoryConfig["idbSetImpl"];
|
|
50
57
|
/** Periodic reconcile interval in ms; 0 disables (default 5 minutes). */
|
|
51
58
|
reconcileIntervalMs?: number;
|
|
59
|
+
/**
|
|
60
|
+
* Collections that subscribe in admin window mode: join `{collection}|admin`,
|
|
61
|
+
* skip `GET /sync/snapshot` paging (startup, catch-up, and the reconcile timer).
|
|
62
|
+
* Hydrate rows via REST membership + `/sync/entities` instead.
|
|
63
|
+
*/
|
|
64
|
+
windowCollections?: string[];
|
|
52
65
|
/** Rate limit for seq-jump-triggered reconciles per stream (default 30s). */
|
|
53
66
|
seqJumpReconcileMinIntervalMs?: number;
|
|
54
67
|
/** Millisecond clock, injectable for deterministic rate-limit tests. */
|
|
@@ -189,6 +202,15 @@ export interface SyncDb {
|
|
|
189
202
|
};
|
|
190
203
|
/** Snapshot-from-cursor catch-up for every collection (no-op without HTTP). */
|
|
191
204
|
reconcile: () => Promise<void>;
|
|
205
|
+
/**
|
|
206
|
+
* Upsert a window of known ids (admin membership). Missing rows are fetched
|
|
207
|
+
* from `GET /sync/entities`; ids the server does not return are ignored.
|
|
208
|
+
*/
|
|
209
|
+
hydrateWindow: (args: {
|
|
210
|
+
collection: string;
|
|
211
|
+
ids: string[];
|
|
212
|
+
restRows?: Record<string, unknown>;
|
|
213
|
+
}) => Promise<HydrateWindowEntitiesResult>;
|
|
192
214
|
/**
|
|
193
215
|
* Purge every known stream locally and re-bootstrap from cursor 0. Use when
|
|
194
216
|
* devices have diverged and a full server snapshot is needed without wiping
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAkB,KAAK,YAAY,EAAE,KAAK,mBAAmB,EAAC,MAAM,kBAAkB,CAAC;AAE9F,OAAO,EAAmC,KAAK,MAAM,EAAC,MAAM,oBAAoB,CAAC;AAGjF,OAAO,KAAK,EAAC,6BAA6B,EAAE,gBAAgB,EAAC,MAAM,oBAAoB,CAAC;AAExF,OAAO,EAAkB,KAAK,SAAS,EAAC,MAAM,iBAAiB,CAAC;AAOhE,OAAO,EAAuC,KAAK,WAAW,EAAC,MAAM,oBAAoB,CAAC;AAG1F,OAAO,KAAK,EAA8C,aAAa,EAAC,MAAM,kBAAkB,CAAC;AACjG,OAAO,KAAK,EACV,YAAY,EACZ,0BAA0B,EAM1B,qBAAqB,EACrB,UAAU,EACX,MAAM,SAAS,CAAC;AAEjB,mEAAmE;AACnE,eAAO,MAAM,6BAA6B,QAAa,CAAC;AAExD,+EAA+E;AAC/E,eAAO,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C;IAC9C,uDAAuD;IACvD,YAAY,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAClC,2DAA2D;IAC3D,eAAe,EAAE,CAAC,IAAI,EAAE;QAAC,UAAU,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,0BAA0B,CAAA;KAAC,KAAK,IAAI,CAAC;IAC5F;;;;;OAKG;IACH,WAAW,EAAE,CAAC,IAAI,EAAE;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAC,KAAK,IAAI,CAAC;IAChD;;;;;;;;OAQG;IACH,OAAO,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;IAC7B,0CAA0C;IAC1C,aAAa,EAAE,MAAM,UAAU,CAAC;IAChC;;;;;;OAMG;IACH,cAAc,EAAE,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,MAAM,IAAI,CAAC;IACrD;;;;OAIG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,YAAY,CAAC;CAC/B;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,YAAY,GAAI,QAAQ,YAAY,KAAG,MAunDnD,CAAC"}
|
package/dist/client.js
CHANGED
|
@@ -2,7 +2,7 @@ import { DateTime } from "luxon";
|
|
|
2
2
|
import { createServerKeyProvider, DEFAULT_KEY_CACHE_DB_NAME } from "./crypto/keyProviders";
|
|
3
3
|
import { attachDebugChannel } from "./debug/debugChannel";
|
|
4
4
|
import { resolveDebugLog } from "./debug/debugLog";
|
|
5
|
-
import { listConflicts, pruneGhostConflicts } from "./mutations/conflicts";
|
|
5
|
+
import { getConflict, listConflicts, pruneGhostConflicts } from "./mutations/conflicts";
|
|
6
6
|
import { createOutbox, generateMutationId } from "./mutations/outbox";
|
|
7
7
|
import { resolveConflict as applyConflictResolution } from "./mutations/resolveConflict";
|
|
8
8
|
import { createDefaultPersisterFactory } from "./persisters/defaultPersisterFactory";
|
|
@@ -17,6 +17,7 @@ import { repairMarkedEntities } from "./sync/entityRepair";
|
|
|
17
17
|
import { AuthRequiredError, createHttpChannel } from "./sync/httpChannel";
|
|
18
18
|
import { createReplayCoordinator } from "./sync/replayCoordinator";
|
|
19
19
|
import { createSocketTransport } from "./sync/socketTransport";
|
|
20
|
+
import { hydrateWindowEntities } from "./sync/windowHydrate";
|
|
20
21
|
/** Periodic reconcile interval when not configured (5 minutes). */
|
|
21
22
|
export const DEFAULT_RECONCILE_INTERVAL_MS = 5 * 60000;
|
|
22
23
|
/** Minimum interval between seq-jump-triggered reconciles per stream (30s). */
|
|
@@ -53,15 +54,33 @@ export const createSyncDb = (config) => {
|
|
|
53
54
|
authProvider: config.authProvider,
|
|
54
55
|
// biome-ignore lint/style/noNonNullAssertion: guarded above — no transport implies baseUrl.
|
|
55
56
|
baseUrl: config.baseUrl,
|
|
57
|
+
organizationIdProvider: config.organizationIdProvider,
|
|
56
58
|
});
|
|
57
59
|
const httpChannel = config.httpChannel ??
|
|
58
60
|
(config.baseUrl
|
|
59
|
-
? createHttpChannel({
|
|
61
|
+
? createHttpChannel({
|
|
62
|
+
authProvider: config.authProvider,
|
|
63
|
+
baseUrl: config.baseUrl,
|
|
64
|
+
organizationIdProvider: config.organizationIdProvider,
|
|
65
|
+
})
|
|
60
66
|
: undefined);
|
|
61
67
|
const store = createSyncStore({
|
|
62
68
|
collections: config.collections,
|
|
63
69
|
now: () => DateTime.fromMillis(now()).toISO() ?? new Date(now()).toISOString(),
|
|
64
70
|
});
|
|
71
|
+
const windowCollectionSet = new Set(config.windowCollections ?? []);
|
|
72
|
+
const isWindowCollection = (collection) => windowCollectionSet.has(collection);
|
|
73
|
+
const skipSnapshotPaging = ({ collection, stream, mode, }) => isWindowCollection(collection) || mode === "window" || stream.endsWith("|admin");
|
|
74
|
+
const subscribeConfiguredCollections = () => {
|
|
75
|
+
const windowed = config.collections.filter((collection) => isWindowCollection(collection));
|
|
76
|
+
const full = config.collections.filter((collection) => !isWindowCollection(collection));
|
|
77
|
+
if (full.length > 0) {
|
|
78
|
+
transport.subscribe(full);
|
|
79
|
+
}
|
|
80
|
+
if (windowed.length > 0) {
|
|
81
|
+
transport.subscribe(windowed, { mode: "window" });
|
|
82
|
+
}
|
|
83
|
+
};
|
|
65
84
|
const outbox = createOutbox({ store });
|
|
66
85
|
const debugLog = resolveDebugLog(config.debug);
|
|
67
86
|
// Mirror the debug log across browser windows/tabs (web only) so a debugger
|
|
@@ -84,6 +103,7 @@ export const createSyncDb = (config) => {
|
|
|
84
103
|
attachDebugBridge();
|
|
85
104
|
let currentUserId;
|
|
86
105
|
let persister;
|
|
106
|
+
const pendingDurableConflicts = new Map();
|
|
87
107
|
let connected = false;
|
|
88
108
|
let simulatedOffline = false;
|
|
89
109
|
let syncingCount = 0;
|
|
@@ -143,6 +163,13 @@ export const createSyncDb = (config) => {
|
|
|
143
163
|
listener();
|
|
144
164
|
}
|
|
145
165
|
};
|
|
166
|
+
const clearPendingDurableConflicts = () => {
|
|
167
|
+
if (pendingDurableConflicts.size === 0) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
pendingDurableConflicts.clear();
|
|
171
|
+
notifyStatusChange();
|
|
172
|
+
};
|
|
146
173
|
const setConnected = (value) => {
|
|
147
174
|
if (connected === value) {
|
|
148
175
|
return;
|
|
@@ -366,6 +393,9 @@ export const createSyncDb = (config) => {
|
|
|
366
393
|
return;
|
|
367
394
|
}
|
|
368
395
|
for (const [stream, collection] of streams) {
|
|
396
|
+
if (skipSnapshotPaging({ collection, stream })) {
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
369
399
|
await bootstrapStream({ channel: httpChannel, collection, store, stream });
|
|
370
400
|
if (isSuperseded()) {
|
|
371
401
|
return;
|
|
@@ -454,13 +484,16 @@ export const createSyncDb = (config) => {
|
|
|
454
484
|
* write). Per-stream paging is idempotent and cursor-guarded, so running it beside an
|
|
455
485
|
* in-flight reconcile is safe.
|
|
456
486
|
*/
|
|
457
|
-
const catchUpSubscribedStreams = async ({ collection, streams, }) => {
|
|
487
|
+
const catchUpSubscribedStreams = async ({ collection, mode, streams, }) => {
|
|
458
488
|
if (!httpChannel || simulatedOffline || authPaused || streams.length === 0) {
|
|
459
489
|
return;
|
|
460
490
|
}
|
|
461
491
|
if (!store.collections.includes(collection)) {
|
|
462
492
|
return;
|
|
463
493
|
}
|
|
494
|
+
if (mode === "window" || isWindowCollection(collection)) {
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
464
497
|
const myGeneration = generation;
|
|
465
498
|
const myUserId = currentUserId;
|
|
466
499
|
const isSuperseded = () => generation !== myGeneration || currentUserId !== myUserId;
|
|
@@ -470,6 +503,9 @@ export const createSyncDb = (config) => {
|
|
|
470
503
|
if (isSuperseded()) {
|
|
471
504
|
return;
|
|
472
505
|
}
|
|
506
|
+
if (skipSnapshotPaging({ collection, mode, stream })) {
|
|
507
|
+
continue;
|
|
508
|
+
}
|
|
473
509
|
store.addKnownStream({ collection, stream });
|
|
474
510
|
await bootstrapStream({ channel: httpChannel, collection, store, stream });
|
|
475
511
|
}
|
|
@@ -837,6 +873,7 @@ export const createSyncDb = (config) => {
|
|
|
837
873
|
persister,
|
|
838
874
|
store,
|
|
839
875
|
});
|
|
876
|
+
clearPendingDurableConflicts();
|
|
840
877
|
lastSeqJumpReconcileAt.clear();
|
|
841
878
|
persistenceMode = "durable";
|
|
842
879
|
await createAndStartPersister(userId);
|
|
@@ -889,6 +926,7 @@ export const createSyncDb = (config) => {
|
|
|
889
926
|
persister,
|
|
890
927
|
store,
|
|
891
928
|
});
|
|
929
|
+
clearPendingDurableConflicts();
|
|
892
930
|
lastSeqJumpReconcileAt.clear();
|
|
893
931
|
await createAndStartPersister(userId);
|
|
894
932
|
}
|
|
@@ -977,7 +1015,7 @@ export const createSyncDb = (config) => {
|
|
|
977
1015
|
warn("transport reconnect after user change failed; continuing offline")(error);
|
|
978
1016
|
}
|
|
979
1017
|
bindDeltaHandler();
|
|
980
|
-
|
|
1018
|
+
subscribeConfiguredCollections();
|
|
981
1019
|
};
|
|
982
1020
|
const handleStatusChange = ({ connected: isConnected, authExpired, }) => {
|
|
983
1021
|
setConnected(isConnected);
|
|
@@ -1195,7 +1233,7 @@ export const createSyncDb = (config) => {
|
|
|
1195
1233
|
if (generation !== myGeneration) {
|
|
1196
1234
|
return;
|
|
1197
1235
|
}
|
|
1198
|
-
|
|
1236
|
+
subscribeConfiguredCollections();
|
|
1199
1237
|
// C2: run an initial stream discovery + per-stream bootstrap on start (not only via
|
|
1200
1238
|
// the reconnect status event) so a client that starts offline-then-online, or with a
|
|
1201
1239
|
// warm socket, still backfills newly-joined streams and drains legacy cursors.
|
|
@@ -1230,6 +1268,7 @@ export const createSyncDb = (config) => {
|
|
|
1230
1268
|
if (persisterToStop) {
|
|
1231
1269
|
// Flush any pending autosave so a clean stop never loses local writes.
|
|
1232
1270
|
await persisterToStop.save();
|
|
1271
|
+
clearPendingDurableConflicts();
|
|
1233
1272
|
await persisterToStop.destroy();
|
|
1234
1273
|
}
|
|
1235
1274
|
});
|
|
@@ -1308,6 +1347,7 @@ export const createSyncDb = (config) => {
|
|
|
1308
1347
|
entityId,
|
|
1309
1348
|
...(maxAttempts !== undefined ? { maxAttempts } : {}),
|
|
1310
1349
|
mutationId,
|
|
1350
|
+
...(windowCollectionSet.has(collection) ? { mutationMode: "adminWindow" } : {}),
|
|
1311
1351
|
operation,
|
|
1312
1352
|
userId: currentUserId,
|
|
1313
1353
|
});
|
|
@@ -1325,7 +1365,51 @@ export const createSyncDb = (config) => {
|
|
|
1325
1365
|
return { id: entityId, mutationId };
|
|
1326
1366
|
};
|
|
1327
1367
|
const resolveConflict = ({ mutationId, strategy, }) => {
|
|
1328
|
-
|
|
1368
|
+
const conflict = getConflict({ mutationId, store });
|
|
1369
|
+
const didAddPendingConflict = Boolean(conflict);
|
|
1370
|
+
if (conflict) {
|
|
1371
|
+
pendingDurableConflicts.set(mutationId, conflict.collection);
|
|
1372
|
+
}
|
|
1373
|
+
try {
|
|
1374
|
+
applyConflictResolution({ mutationId, outbox, store, strategy });
|
|
1375
|
+
}
|
|
1376
|
+
catch (error) {
|
|
1377
|
+
if (didAddPendingConflict) {
|
|
1378
|
+
pendingDurableConflicts.delete(mutationId);
|
|
1379
|
+
}
|
|
1380
|
+
throw error;
|
|
1381
|
+
}
|
|
1382
|
+
// Conflict resolution is a user-confirmed durability boundary. The web
|
|
1383
|
+
// persister normally trails writes by 500ms; bypass that debounce now and
|
|
1384
|
+
// keep status conflicted until the write succeeds so reload waits on it.
|
|
1385
|
+
// Native and custom persisters fall back to TinyBase's immediate save().
|
|
1386
|
+
const resolutionGeneration = generation;
|
|
1387
|
+
const resolutionPersister = persister;
|
|
1388
|
+
const persistenceFlush = resolutionPersister
|
|
1389
|
+
? (resolutionPersister.flush?.bind(resolutionPersister) ??
|
|
1390
|
+
resolutionPersister.save.bind(resolutionPersister))
|
|
1391
|
+
: undefined;
|
|
1392
|
+
if (persistenceFlush) {
|
|
1393
|
+
void persistenceFlush()
|
|
1394
|
+
.then(() => {
|
|
1395
|
+
if (generation !== resolutionGeneration || persister !== resolutionPersister) {
|
|
1396
|
+
return;
|
|
1397
|
+
}
|
|
1398
|
+
pendingDurableConflicts.delete(mutationId);
|
|
1399
|
+
notifyStatusChange();
|
|
1400
|
+
})
|
|
1401
|
+
.catch((error) => {
|
|
1402
|
+
if (generation !== resolutionGeneration || persister !== resolutionPersister) {
|
|
1403
|
+
return;
|
|
1404
|
+
}
|
|
1405
|
+
persistenceMode = "error";
|
|
1406
|
+
warn("post-resolve persistence flush failed")(error);
|
|
1407
|
+
notifyStatusChange();
|
|
1408
|
+
});
|
|
1409
|
+
}
|
|
1410
|
+
else {
|
|
1411
|
+
pendingDurableConflicts.delete(mutationId);
|
|
1412
|
+
}
|
|
1329
1413
|
debugLog?.record({
|
|
1330
1414
|
detail: { strategy },
|
|
1331
1415
|
direction: "local",
|
|
@@ -1374,8 +1458,23 @@ export const createSyncDb = (config) => {
|
|
|
1374
1458
|
for (const conflict of conflicts) {
|
|
1375
1459
|
forCollection(conflict.collection).conflictCount += 1;
|
|
1376
1460
|
}
|
|
1461
|
+
for (const collection of pendingDurableConflicts.values()) {
|
|
1462
|
+
forCollection(collection).conflictCount += 1;
|
|
1463
|
+
}
|
|
1377
1464
|
return statuses;
|
|
1378
1465
|
};
|
|
1466
|
+
const hydrateWindow = async ({ collection, ids, restRows, }) => {
|
|
1467
|
+
if (!httpChannel) {
|
|
1468
|
+
throw new Error("hydrateWindow requires an HTTP channel");
|
|
1469
|
+
}
|
|
1470
|
+
return hydrateWindowEntities({
|
|
1471
|
+
channel: httpChannel,
|
|
1472
|
+
collection,
|
|
1473
|
+
ids,
|
|
1474
|
+
restRows,
|
|
1475
|
+
store,
|
|
1476
|
+
});
|
|
1477
|
+
};
|
|
1379
1478
|
const getSyncStatus = () => {
|
|
1380
1479
|
const drainProgress = currentUserId
|
|
1381
1480
|
? coordinator.getDrainProgress({ userId: currentUserId })
|
|
@@ -1386,7 +1485,7 @@ export const createSyncDb = (config) => {
|
|
|
1386
1485
|
? coordinator.getBlockedEntities({ userId: currentUserId }).length
|
|
1387
1486
|
: 0,
|
|
1388
1487
|
collections: buildCollectionStatuses({ conflicts }),
|
|
1389
|
-
conflictCount: conflicts.length,
|
|
1488
|
+
conflictCount: conflicts.length + pendingDurableConflicts.size,
|
|
1390
1489
|
draining: drainProgress.draining,
|
|
1391
1490
|
failedCount: currentUserId
|
|
1392
1491
|
? outbox.countByStatus({ status: "failed", userId: currentUserId })
|
|
@@ -1445,6 +1544,7 @@ export const createSyncDb = (config) => {
|
|
|
1445
1544
|
persister: persisterToStop,
|
|
1446
1545
|
store,
|
|
1447
1546
|
});
|
|
1547
|
+
clearPendingDurableConflicts();
|
|
1448
1548
|
lastSeqJumpReconcileAt.clear();
|
|
1449
1549
|
return;
|
|
1450
1550
|
}
|
|
@@ -1453,6 +1553,7 @@ export const createSyncDb = (config) => {
|
|
|
1453
1553
|
// Flush any pending autosave so a sign-out without a wipe never loses
|
|
1454
1554
|
// local writes (they belong to the user who may sign back in).
|
|
1455
1555
|
await persisterToStop.save();
|
|
1556
|
+
clearPendingDurableConflicts();
|
|
1456
1557
|
await persisterToStop.destroy();
|
|
1457
1558
|
}
|
|
1458
1559
|
});
|
|
@@ -1462,6 +1563,7 @@ export const createSyncDb = (config) => {
|
|
|
1462
1563
|
getSyncStatus,
|
|
1463
1564
|
goOffline,
|
|
1464
1565
|
goOnline,
|
|
1566
|
+
hydrateWindow,
|
|
1465
1567
|
mutate,
|
|
1466
1568
|
onStatusChange,
|
|
1467
1569
|
outbox,
|