-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathSharedWebStreamingSyncImplementation.ts
More file actions
229 lines (205 loc) · 6.84 KB
/
SharedWebStreamingSyncImplementation.ts
File metadata and controls
229 lines (205 loc) · 6.84 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { PowerSyncConnectionOptions, PowerSyncCredentials, SyncStatus, SyncStatusOptions } from '@powersync/common';
import * as Comlink from 'comlink';
import { AbstractSharedSyncClientProvider } from '../../worker/sync/AbstractSharedSyncClientProvider';
import {
ManualSharedSyncPayload,
SharedSyncClientEvent,
SharedSyncImplementation
} from '../../worker/sync/SharedSyncImplementation';
import { DEFAULT_CACHE_SIZE_KB, resolveWebSQLFlags, TemporaryStorageOption } from '../adapters/web-sql-flags';
import { WebDBAdapter } from '../adapters/WebDBAdapter';
import {
WebStreamingSyncImplementation,
WebStreamingSyncImplementationOptions
} from './WebStreamingSyncImplementation';
/**
* The shared worker will trigger methods on this side of the message port
* via this client provider.
*/
class SharedSyncClientProvider extends AbstractSharedSyncClientProvider {
constructor(
protected options: WebStreamingSyncImplementationOptions,
public statusChanged: (status: SyncStatusOptions) => void,
protected webDB: WebDBAdapter
) {
super();
}
async getDBWorkerPort(): Promise<MessagePort> {
const { port } = await this.webDB.shareConnection();
return Comlink.transfer(port, [port]);
}
async fetchCredentials(): Promise<PowerSyncCredentials | null> {
const credentials = await this.options.remote.getCredentials();
if (credentials == null) {
return null;
}
/**
* The credentials need to be serializable.
* Users might extend [PowerSyncCredentials] to contain
* items which are not serializable.
* This returns only the essential fields.
*/
return {
endpoint: credentials.endpoint,
token: credentials.token
};
}
async uploadCrud(): Promise<void> {
/**
* Don't return anything here, just incase something which is not
* serializable is returned from the `uploadCrud` function.
*/
await this.options.uploadCrud();
}
get logger() {
return this.options.logger;
}
trace(...x: any[]): void {
this.logger?.trace(...x);
}
debug(...x: any[]): void {
this.logger?.debug(...x);
}
info(...x: any[]): void {
this.logger?.info(...x);
}
log(...x: any[]): void {
this.logger?.log(...x);
}
warn(...x: any[]): void {
this.logger?.warn(...x);
}
error(...x: any[]): void {
this.logger?.error(...x);
}
time(label: string): void {
this.logger?.time(label);
}
timeEnd(label: string): void {
this.logger?.timeEnd(label);
}
}
export interface SharedWebStreamingSyncImplementationOptions extends WebStreamingSyncImplementationOptions {
db: WebDBAdapter;
}
export class SharedWebStreamingSyncImplementation extends WebStreamingSyncImplementation {
protected syncManager: Comlink.Remote<SharedSyncImplementation>;
protected clientProvider: SharedSyncClientProvider;
protected messagePort: MessagePort;
protected isInitialized: Promise<void>;
protected dbAdapter: WebDBAdapter;
constructor(options: SharedWebStreamingSyncImplementationOptions) {
super(options);
this.dbAdapter = options.db;
/**
* Configure or connect to the shared sync worker.
* This worker will manage all syncing operations remotely.
*/
const resolvedWorkerOptions = {
dbFilename: this.options.identifier!,
temporaryStorage: TemporaryStorageOption.MEMORY,
cacheSizeKb: DEFAULT_CACHE_SIZE_KB,
...options,
flags: resolveWebSQLFlags(options.flags)
};
const syncWorker = options.sync?.worker;
if (syncWorker) {
if (typeof syncWorker === 'function') {
this.messagePort = syncWorker(resolvedWorkerOptions).port;
} else {
this.messagePort = new SharedWorker(`${syncWorker}`, {
/* @vite-ignore */
name: `shared-sync-${this.webOptions.identifier}`
}).port;
}
} else {
this.messagePort = new SharedWorker(
new URL('../../worker/sync/SharedSyncImplementation.worker.js', import.meta.url),
{
/* @vite-ignore */
name: `shared-sync-${this.webOptions.identifier}`,
type: 'module'
}
).port;
}
this.syncManager = Comlink.wrap<SharedSyncImplementation>(this.messagePort);
this.triggerCrudUpload = this.syncManager.triggerCrudUpload;
/**
* Opens MessagePort to the existing shared DB worker.
* The sync worker cannot initiate connections directly to the
* DB worker, but a port to the DB worker can be transferred to the
* sync worker.
*/
const { crudUploadThrottleMs, identifier, retryDelayMs } = this.options;
const flags = { ...this.webOptions.flags, workers: undefined };
this.isInitialized = this.syncManager.setParams({
dbParams: this.dbAdapter.getConfiguration(),
streamOptions: {
crudUploadThrottleMs,
identifier,
retryDelayMs,
flags: flags
}
});
/**
* Pass along any sync status updates to this listener
*/
this.clientProvider = new SharedSyncClientProvider(
this.webOptions,
(status) => {
this.iterateListeners((l) => this.updateSyncStatus(status));
},
options.db
);
/**
* The sync worker will call this client provider when it needs
* to fetch credentials or upload data.
* This performs bi-directional method calling.
*/
Comlink.expose(this.clientProvider, this.messagePort);
}
/**
* Starts the sync process, this effectively acts as a call to
* `connect` if not yet connected.
*/
async connect(options?: PowerSyncConnectionOptions): Promise<void> {
await this.waitForReady();
// This is needed since a new tab won't have any reference to the
// shared worker sync implementation since that is only created on the first call to `connect`.
await this.disconnect();
return this.syncManager.connect(options);
}
async disconnect(): Promise<void> {
await this.waitForReady();
return this.syncManager.disconnect();
}
async getWriteCheckpoint(): Promise<string> {
await this.waitForReady();
return this.syncManager.getWriteCheckpoint();
}
async hasCompletedSync(): Promise<boolean> {
return this.syncManager.hasCompletedSync();
}
async dispose(): Promise<void> {
await this.waitForReady();
// Signal the shared worker that this client is closing its connection to the worker
const closeMessagePayload: ManualSharedSyncPayload = {
event: SharedSyncClientEvent.CLOSE_CLIENT,
data: {}
};
this.messagePort.postMessage(closeMessagePayload);
// Release the proxy
this.syncManager[Comlink.releaseProxy]();
this.messagePort.close();
}
async waitForReady() {
return this.isInitialized;
}
/**
* Used in tests to force a connection states
*/
private async _testUpdateStatus(status: SyncStatus) {
await this.isInitialized;
return (this.syncManager as any)['_testUpdateAllStatuses'](status.toJSON());
}
}