-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathbrowser-persistence.ts
More file actions
158 lines (139 loc) · 4.75 KB
/
browser-persistence.ts
File metadata and controls
158 lines (139 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import {
SingleProcessCoordinator,
createSQLiteCorePersistenceAdapter,
} from '@tanstack/db-sqlite-persistence-core'
import { BrowserCollectionCoordinator } from './browser-coordinator'
import { BrowserWASQLiteDriver } from './wa-sqlite-driver'
import type {
PersistedCollectionCoordinator,
PersistedCollectionMode,
PersistedCollectionPersistence,
SQLiteCoreAdapterOptions,
SQLiteDriver,
} from '@tanstack/db-sqlite-persistence-core'
import type { BrowserWASQLiteDatabase } from './wa-sqlite-driver'
export type { BrowserWASQLiteDatabase } from './wa-sqlite-driver'
type BrowserSQLiteCoreSchemaMismatchPolicy =
| `sync-present-reset`
| `sync-absent-error`
| `reset`
export type BrowserWASQLiteSchemaMismatchPolicy =
| BrowserSQLiteCoreSchemaMismatchPolicy
| `throw`
export type BrowserWASQLitePersistenceOptions = Omit<
SQLiteCoreAdapterOptions,
`driver` | `schemaVersion` | `schemaMismatchPolicy`
> & {
database: BrowserWASQLiteDatabase
coordinator?: PersistedCollectionCoordinator
schemaMismatchPolicy?: BrowserWASQLiteSchemaMismatchPolicy
}
function normalizeSchemaMismatchPolicy(
policy: BrowserWASQLiteSchemaMismatchPolicy,
): BrowserSQLiteCoreSchemaMismatchPolicy {
if (policy === `throw`) {
return `sync-absent-error`
}
return policy
}
function resolveSchemaMismatchPolicy(
explicitPolicy: BrowserWASQLiteSchemaMismatchPolicy | undefined,
mode: PersistedCollectionMode,
): BrowserSQLiteCoreSchemaMismatchPolicy {
if (explicitPolicy) {
return normalizeSchemaMismatchPolicy(explicitPolicy)
}
return mode === `sync-present` ? `sync-present-reset` : `sync-absent-error`
}
function createAdapterCacheKey(
schemaMismatchPolicy: BrowserSQLiteCoreSchemaMismatchPolicy,
schemaVersion: number | undefined,
): string {
const schemaVersionKey =
schemaVersion === undefined ? `schema:default` : `schema:${schemaVersion}`
return `${schemaMismatchPolicy}|${schemaVersionKey}`
}
function createInternalSQLiteDriver(
options: BrowserWASQLitePersistenceOptions,
): SQLiteDriver {
return new BrowserWASQLiteDriver({
database: options.database,
})
}
function resolveAdapterBaseOptions(
options: BrowserWASQLitePersistenceOptions,
): Omit<
SQLiteCoreAdapterOptions,
`driver` | `schemaVersion` | `schemaMismatchPolicy`
> {
return {
appliedTxPruneMaxRows: options.appliedTxPruneMaxRows,
appliedTxPruneMaxAgeSeconds: options.appliedTxPruneMaxAgeSeconds,
pullSinceReloadThreshold: options.pullSinceReloadThreshold,
}
}
/**
* Creates a shared browser wa-sqlite persistence instance that can be reused by
* many collections on the same database. This is single-tab wiring using
* SingleProcessCoordinator semantics (no election required).
*/
export function createBrowserWASQLitePersistence(
options: BrowserWASQLitePersistenceOptions,
): PersistedCollectionPersistence {
const { coordinator, schemaMismatchPolicy } = options
const driver = createInternalSQLiteDriver(options)
const adapterBaseOptions = resolveAdapterBaseOptions(options)
const resolvedCoordinator = coordinator ?? new SingleProcessCoordinator()
const adapterCache = new Map<
string,
ReturnType<typeof createSQLiteCorePersistenceAdapter>
>()
const getAdapterForCollection = (
mode: PersistedCollectionMode,
schemaVersion: number | undefined,
) => {
const resolvedSchemaMismatchPolicy = resolveSchemaMismatchPolicy(
schemaMismatchPolicy,
mode,
)
const cacheKey = createAdapterCacheKey(
resolvedSchemaMismatchPolicy,
schemaVersion,
)
const cachedAdapter = adapterCache.get(cacheKey)
if (cachedAdapter) {
return cachedAdapter
}
const adapter = createSQLiteCorePersistenceAdapter({
...adapterBaseOptions,
driver,
schemaMismatchPolicy: resolvedSchemaMismatchPolicy,
...(schemaVersion === undefined ? {} : { schemaVersion }),
})
adapterCache.set(cacheKey, adapter)
// Wire the adapter into the multi-tab coordinator so it can handle
// leader-side RPCs (applyCommittedTx, pullSince, ensureIndex, etc.)
if (resolvedCoordinator instanceof BrowserCollectionCoordinator) {
resolvedCoordinator.setAdapter(adapter)
}
return adapter
}
const createCollectionPersistence = (
mode: PersistedCollectionMode,
schemaVersion: number | undefined,
): PersistedCollectionPersistence => ({
adapter: getAdapterForCollection(mode, schemaVersion),
coordinator: resolvedCoordinator,
})
const defaultPersistence = createCollectionPersistence(
`sync-absent`,
undefined,
)
return {
...defaultPersistence,
resolvePersistenceForCollection: ({ mode, schemaVersion }) =>
createCollectionPersistence(mode, schemaVersion),
resolvePersistenceForMode: (mode) =>
createCollectionPersistence(mode, undefined),
}
}