@powersync/web 1.29.1 → 1.31.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.
Files changed (60) hide show
  1. package/README.md +1 -1
  2. package/dist/0b19af1befc07ce338dd.wasm +0 -0
  3. package/dist/2632c3bda9473da74fd5.wasm +0 -0
  4. package/dist/64f5351ba3784bfe2f3e.wasm +0 -0
  5. package/dist/9318ca94aac4d0fe0135.wasm +0 -0
  6. package/dist/index.umd.js +7258 -1847
  7. package/dist/index.umd.js.map +1 -1
  8. package/dist/worker/SharedSyncImplementation.umd.js +548 -290
  9. package/dist/worker/SharedSyncImplementation.umd.js.map +1 -1
  10. package/dist/worker/WASQLiteDB.umd.js +192 -126
  11. package/dist/worker/WASQLiteDB.umd.js.map +1 -1
  12. package/dist/worker/node_modules_bson_lib_bson_mjs.umd.js +3 -3
  13. package/dist/worker/node_modules_bson_lib_bson_mjs.umd.js.map +1 -1
  14. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite-async_mjs.umd.js +22 -9
  15. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite-async_mjs.umd.js.map +1 -1
  16. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite_mjs.umd.js +22 -9
  17. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite_mjs.umd.js.map +1 -1
  18. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite-async_mjs.umd.js +22 -9
  19. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite-async_mjs.umd.js.map +1 -1
  20. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js +22 -9
  21. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js.map +1 -1
  22. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js.umd.js +9 -9
  23. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js.umd.js.map +1 -1
  24. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.umd.js +32 -35
  25. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.umd.js.map +1 -1
  26. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js +27 -20
  27. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js.map +1 -1
  28. package/lib/package.json +5 -5
  29. package/lib/src/db/PowerSyncDatabase.d.ts +1 -1
  30. package/lib/src/db/PowerSyncDatabase.js +4 -4
  31. package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.d.ts +17 -0
  32. package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.js +109 -19
  33. package/lib/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.d.ts +5 -1
  34. package/lib/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.js +14 -7
  35. package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.js +11 -2
  36. package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.d.ts +1 -1
  37. package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +2 -2
  38. package/lib/src/db/sync/SharedWebStreamingSyncImplementation.d.ts +2 -5
  39. package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js +51 -35
  40. package/lib/src/worker/sync/SharedSyncImplementation.d.ts +18 -8
  41. package/lib/src/worker/sync/SharedSyncImplementation.js +204 -108
  42. package/lib/src/worker/sync/SharedSyncImplementation.worker.js +1 -1
  43. package/lib/src/worker/sync/WorkerClient.d.ts +4 -5
  44. package/lib/src/worker/sync/WorkerClient.js +7 -9
  45. package/lib/tsconfig.tsbuildinfo +1 -1
  46. package/package.json +6 -6
  47. package/src/db/PowerSyncDatabase.ts +13 -15
  48. package/src/db/adapters/LockedAsyncDatabaseAdapter.ts +126 -25
  49. package/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.ts +18 -6
  50. package/src/db/adapters/wa-sqlite/WASQLiteConnection.ts +11 -3
  51. package/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts +3 -3
  52. package/src/db/sync/SharedWebStreamingSyncImplementation.ts +65 -47
  53. package/src/worker/db/WASQLiteDB.worker.ts +0 -1
  54. package/src/worker/sync/SharedSyncImplementation.ts +234 -119
  55. package/src/worker/sync/SharedSyncImplementation.worker.ts +1 -1
  56. package/src/worker/sync/WorkerClient.ts +9 -13
  57. package/dist/10072fe45f0a8fab0a0e.wasm +0 -0
  58. package/dist/6e435e51534839845554.wasm +0 -0
  59. package/dist/a730f7ca717b02234beb.wasm +0 -0
  60. package/dist/aa2f408d64445fed090e.wasm +0 -0
@@ -1,4 +1,4 @@
1
- import { AbortOperation, BaseObserver, ConnectionManager, createLogger, SqliteBucketStorage, SyncStatus } from '@powersync/common';
1
+ import { AbortOperation, BaseObserver, ConnectionManager, SqliteBucketStorage, SyncStatus, createLogger } from '@powersync/common';
2
2
  import { Mutex } from 'async-mutex';
3
3
  import * as Comlink from 'comlink';
4
4
  import { WebRemote } from '../../db/sync/WebRemote';
@@ -35,7 +35,6 @@ export class SharedSyncImplementation extends BaseObserver {
35
35
  statusListener;
36
36
  fetchCredentialsController;
37
37
  uploadDataController;
38
- dbAdapter;
39
38
  syncParams;
40
39
  logger;
41
40
  lastConnectOptions;
@@ -44,10 +43,10 @@ export class SharedSyncImplementation extends BaseObserver {
44
43
  connectionManager;
45
44
  syncStatus;
46
45
  broadCastLogger;
46
+ distributedDB;
47
47
  constructor() {
48
48
  super();
49
49
  this.ports = [];
50
- this.dbAdapter = null;
51
50
  this.syncParams = null;
52
51
  this.logger = createLogger('shared-sync');
53
52
  this.lastConnectOptions = undefined;
@@ -60,26 +59,23 @@ export class SharedSyncImplementation extends BaseObserver {
60
59
  }
61
60
  });
62
61
  });
62
+ // Should be configured once we get params
63
+ this.distributedDB = null;
63
64
  this.syncStatus = new SyncStatus({});
64
65
  this.broadCastLogger = new BroadcastLogger(this.ports);
65
66
  this.connectionManager = new ConnectionManager({
66
67
  createSyncImplementation: async () => {
67
- return this.portMutex.runExclusive(async () => {
68
- await this.waitForReady();
69
- if (!this.dbAdapter) {
70
- await this.openInternalDB();
68
+ await this.waitForReady();
69
+ const sync = this.generateStreamingImplementation();
70
+ const onDispose = sync.registerListener({
71
+ statusChanged: (status) => {
72
+ this.updateAllStatuses(status.toJSON());
71
73
  }
72
- const sync = this.generateStreamingImplementation();
73
- const onDispose = sync.registerListener({
74
- statusChanged: (status) => {
75
- this.updateAllStatuses(status.toJSON());
76
- }
77
- });
78
- return {
79
- sync,
80
- onDispose
81
- };
82
74
  });
75
+ return {
76
+ sync,
77
+ onDispose
78
+ };
83
79
  },
84
80
  logger: this.logger
85
81
  });
@@ -90,6 +86,30 @@ export class SharedSyncImplementation extends BaseObserver {
90
86
  get isConnected() {
91
87
  return this.connectionManager.syncStreamImplementation?.isConnected ?? false;
92
88
  }
89
+ /**
90
+ * Gets the last client port which we know is safe from unexpected closes.
91
+ */
92
+ async getLastWrappedPort() {
93
+ // Find the last port which is not closing
94
+ return await this.portMutex.runExclusive(() => {
95
+ for (let i = this.ports.length - 1; i >= 0; i--) {
96
+ if (!this.ports[i].isClosing) {
97
+ return this.ports[i];
98
+ }
99
+ }
100
+ return;
101
+ });
102
+ }
103
+ /**
104
+ * In some very rare cases a specific tab might not respond to requests.
105
+ * This returns a random port which is not closing.
106
+ */
107
+ async getRandomWrappedPort() {
108
+ return await this.portMutex.runExclusive(() => {
109
+ const nonClosingPorts = this.ports.filter((p) => !p.isClosing);
110
+ return nonClosingPorts[Math.floor(Math.random() * nonClosingPorts.length)];
111
+ });
112
+ }
93
113
  async waitForStatus(status) {
94
114
  return this.withSyncImplementation(async (sync) => {
95
115
  return sync.waitForStatus(status);
@@ -130,28 +150,53 @@ export class SharedSyncImplementation extends BaseObserver {
130
150
  async setParams(params) {
131
151
  await this.portMutex.runExclusive(async () => {
132
152
  this.collectActiveSubscriptions();
133
- if (this.syncParams) {
134
- // Cannot modify already existing sync implementation params
135
- // But we can ask for a DB adapter, if required, at this point.
136
- if (!this.dbAdapter) {
137
- await this.openInternalDB();
138
- }
139
- return;
140
- }
141
- // First time setting params
142
- this.syncParams = params;
143
- if (params.streamOptions?.flags?.broadcastLogs) {
144
- this.logger = this.broadCastLogger;
145
- }
146
- self.onerror = (event) => {
147
- // Share any uncaught events on the broadcast logger
148
- this.logger.error('Uncaught exception in PowerSync shared sync worker', event);
149
- };
150
- if (!this.dbAdapter) {
151
- await this.openInternalDB();
153
+ });
154
+ if (this.syncParams) {
155
+ // Cannot modify already existing sync implementation params
156
+ return;
157
+ }
158
+ // First time setting params
159
+ this.syncParams = params;
160
+ if (params.streamOptions?.flags?.broadcastLogs) {
161
+ this.logger = this.broadCastLogger;
162
+ }
163
+ const lockedAdapter = new LockedAsyncDatabaseAdapter({
164
+ name: params.dbParams.dbFilename,
165
+ openConnection: async () => {
166
+ // Gets a connection from the clients when a new connection is requested.
167
+ const db = await this.openInternalDB();
168
+ db.registerListener({
169
+ closing: () => {
170
+ lockedAdapter.reOpenInternalDB();
171
+ }
172
+ });
173
+ return db;
174
+ },
175
+ logger: this.logger,
176
+ reOpenOnConnectionClosed: true
177
+ });
178
+ this.distributedDB = lockedAdapter;
179
+ await lockedAdapter.init();
180
+ lockedAdapter.registerListener({
181
+ databaseReOpened: () => {
182
+ // We may have missed some table updates while the database was closed.
183
+ // We can poke the crud in case we missed any updates.
184
+ this.connectionManager.syncStreamImplementation?.triggerCrudUpload();
185
+ /**
186
+ * FIXME or IMPROVE ME
187
+ * The Rust client implementation stores sync state on the connection level.
188
+ * Reopening the database causes a state machine error which should cause the
189
+ * StreamingSyncImplementation to reconnect. It would be nicer if we could trigger
190
+ * this reconnect earlier.
191
+ * This reconnect is not required for IndexedDB.
192
+ */
152
193
  }
153
- this.iterateListeners((l) => l.initialized?.());
154
194
  });
195
+ self.onerror = (event) => {
196
+ // Share any uncaught events on the broadcast logger
197
+ this.logger.error('Uncaught exception in PowerSync shared sync worker', event);
198
+ };
199
+ this.iterateListeners((l) => l.initialized?.());
155
200
  }
156
201
  async dispose() {
157
202
  await this.waitForReady();
@@ -180,7 +225,8 @@ export class SharedSyncImplementation extends BaseObserver {
180
225
  port,
181
226
  clientProvider: Comlink.wrap(port),
182
227
  currentSubscriptions: [],
183
- closeListeners: []
228
+ closeListeners: [],
229
+ isClosing: false
184
230
  };
185
231
  this.ports.push(portProvider);
186
232
  // Give the newly connected client the latest status
@@ -196,14 +242,16 @@ export class SharedSyncImplementation extends BaseObserver {
196
242
  * clients.
197
243
  */
198
244
  async removePort(port) {
245
+ // Ports might be removed faster than we can process them.
246
+ port.isClosing = true;
199
247
  // Remove the port within a mutex context.
200
248
  // Warns if the port is not found. This should not happen in practice.
201
249
  // We return early if the port is not found.
202
- const { trackedPort, shouldReconnect } = await this.portMutex.runExclusive(async () => {
250
+ return await this.portMutex.runExclusive(async () => {
203
251
  const index = this.ports.findIndex((p) => p == port);
204
252
  if (index < 0) {
205
253
  this.logger.warn(`Could not remove port ${port} since it is not present in active ports.`);
206
- return {};
254
+ return () => { };
207
255
  }
208
256
  const trackedPort = this.ports[index];
209
257
  // Remove from the list of active ports
@@ -217,35 +265,13 @@ export class SharedSyncImplementation extends BaseObserver {
217
265
  abortController.controller.abort(new AbortOperation('Closing pending requests after client port is removed'));
218
266
  }
219
267
  });
220
- const shouldReconnect = !!this.connectionManager.syncStreamImplementation && this.ports.length > 0;
221
- return {
222
- shouldReconnect,
223
- trackedPort
224
- };
225
- });
226
- if (!trackedPort) {
227
- // We could not find the port to remove
228
- return () => { };
229
- }
230
- for (const closeListener of trackedPort.closeListeners) {
231
- await closeListener();
232
- }
233
- if (this.dbAdapter && this.dbAdapter == trackedPort.db) {
234
- // Unconditionally close the connection because the database it's writing to has just been closed.
235
- // The connection has been closed previously, this might throw. We should be able to ignore it.
236
- await this.connectionManager
237
- .disconnect()
238
- .catch((ex) => this.logger.warn('Error while disconnecting. Will attempt to reconnect.', ex));
239
- // Clearing the adapter will result in a new one being opened in connect
240
- this.dbAdapter = null;
241
- if (shouldReconnect) {
242
- await this.connectionManager.connect(CONNECTOR_PLACEHOLDER, this.lastConnectOptions ?? {});
268
+ // Close the worker wrapped database connection, we can't accurately rely on this connection
269
+ for (const closeListener of trackedPort.closeListeners) {
270
+ await closeListener();
243
271
  }
244
- }
245
- // Re-index subscriptions, the subscriptions of the removed port would no longer be considered.
246
- this.collectActiveSubscriptions();
247
- // Release proxy
248
- return () => trackedPort.clientProvider[Comlink.releaseProxy]();
272
+ this.collectActiveSubscriptions();
273
+ return () => trackedPort.clientProvider[Comlink.releaseProxy]();
274
+ });
249
275
  }
250
276
  triggerCrudUpload() {
251
277
  this.withSyncImplementation(async (sync) => {
@@ -282,10 +308,13 @@ export class SharedSyncImplementation extends BaseObserver {
282
308
  const syncParams = this.syncParams;
283
309
  // Create a new StreamingSyncImplementation for each connect call. This is usually done is all SDKs.
284
310
  return new WebStreamingSyncImplementation({
285
- adapter: new SqliteBucketStorage(this.dbAdapter, this.logger),
311
+ adapter: new SqliteBucketStorage(this.distributedDB, this.logger),
286
312
  remote: new WebRemote({
287
313
  invalidateCredentials: async () => {
288
- const lastPort = this.ports[this.ports.length - 1];
314
+ const lastPort = await this.getLastWrappedPort();
315
+ if (!lastPort) {
316
+ throw new Error('No client port found to invalidate credentials');
317
+ }
289
318
  try {
290
319
  this.logger.log('calling the last port client provider to invalidate credentials');
291
320
  lastPort.clientProvider.invalidateCredentials();
@@ -295,7 +324,10 @@ export class SharedSyncImplementation extends BaseObserver {
295
324
  }
296
325
  },
297
326
  fetchCredentials: async () => {
298
- const lastPort = this.ports[this.ports.length - 1];
327
+ const lastPort = await this.getLastWrappedPort();
328
+ if (!lastPort) {
329
+ throw new Error('No client port found to fetch credentials');
330
+ }
299
331
  return new Promise(async (resolve, reject) => {
300
332
  const abortController = new AbortController();
301
333
  this.fetchCredentialsController = {
@@ -317,7 +349,10 @@ export class SharedSyncImplementation extends BaseObserver {
317
349
  }
318
350
  }, this.logger),
319
351
  uploadCrud: async () => {
320
- const lastPort = this.ports[this.ports.length - 1];
352
+ const lastPort = await this.getLastWrappedPort();
353
+ if (!lastPort) {
354
+ throw new Error('No client port found to upload crud');
355
+ }
321
356
  return new Promise(async (resolve, reject) => {
322
357
  const abortController = new AbortController();
323
358
  this.uploadDataController = {
@@ -343,38 +378,81 @@ export class SharedSyncImplementation extends BaseObserver {
343
378
  logger: this.logger
344
379
  });
345
380
  }
381
+ /**
382
+ * Opens a worker wrapped database connection. Using the last connected client port.
383
+ */
346
384
  async openInternalDB() {
347
- const lastClient = this.ports[this.ports.length - 1];
348
- if (!lastClient) {
385
+ const client = await this.getRandomWrappedPort();
386
+ if (!client) {
349
387
  // Should not really happen in practice
350
388
  throw new Error(`Could not open DB connection since no client is connected.`);
351
389
  }
352
- const workerPort = await lastClient.clientProvider.getDBWorkerPort();
390
+ // Fail-safe timeout for opening a database connection.
391
+ const timeout = setTimeout(() => {
392
+ abortController.abort();
393
+ }, 10_000);
394
+ /**
395
+ * Handle cases where the client might close while opening a connection.
396
+ */
397
+ const abortController = new AbortController();
398
+ const closeListener = () => {
399
+ abortController.abort();
400
+ };
401
+ const removeCloseListener = () => {
402
+ const index = client.closeListeners.indexOf(closeListener);
403
+ if (index >= 0) {
404
+ client.closeListeners.splice(index, 1);
405
+ }
406
+ };
407
+ client.closeListeners.push(closeListener);
408
+ const workerPort = await withAbort({
409
+ action: () => client.clientProvider.getDBWorkerPort(),
410
+ signal: abortController.signal,
411
+ cleanupOnAbort: (port) => {
412
+ port.close();
413
+ }
414
+ }).catch((ex) => {
415
+ removeCloseListener();
416
+ throw ex;
417
+ });
353
418
  const remote = Comlink.wrap(workerPort);
354
419
  const identifier = this.syncParams.dbParams.dbFilename;
355
- const db = await remote(this.syncParams.dbParams);
356
- const locked = new LockedAsyncDatabaseAdapter({
357
- name: identifier,
358
- openConnection: async () => {
359
- const wrapped = new WorkerWrappedAsyncDatabaseConnection({
360
- remote,
361
- baseConnection: db,
362
- identifier,
363
- // It's possible for this worker to outlive the client hosting the database for us. We need to be prepared for
364
- // that and ensure pending requests are aborted when the tab is closed.
365
- remoteCanCloseUnexpectedly: true
366
- });
367
- lastClient.closeListeners.push(async () => {
368
- this.logger.info('Aborting open connection because associated tab closed.');
369
- await wrapped.close().catch((ex) => this.logger.warn('error closing database connection', ex));
370
- wrapped.markRemoteClosed();
371
- });
372
- return wrapped;
373
- },
374
- logger: this.logger
420
+ /**
421
+ * The open could fail if the tab is closed while we're busy opening the database.
422
+ * This operation is typically executed inside an exclusive portMutex lock.
423
+ * We typically execute the closeListeners using the portMutex in a different context.
424
+ * We can't rely on the closeListeners to abort the operation if the tab is closed.
425
+ */
426
+ const db = await withAbort({
427
+ action: () => remote(this.syncParams.dbParams),
428
+ signal: abortController.signal,
429
+ cleanupOnAbort: (db) => {
430
+ db.close();
431
+ }
432
+ }).finally(() => {
433
+ // We can remove the close listener here since we no longer need it past this point.
434
+ removeCloseListener();
435
+ });
436
+ clearTimeout(timeout);
437
+ const wrapped = new WorkerWrappedAsyncDatabaseConnection({
438
+ remote,
439
+ baseConnection: db,
440
+ identifier,
441
+ // It's possible for this worker to outlive the client hosting the database for us. We need to be prepared for
442
+ // that and ensure pending requests are aborted when the tab is closed.
443
+ remoteCanCloseUnexpectedly: true
444
+ });
445
+ client.closeListeners.push(async () => {
446
+ this.logger.info('Aborting open connection because associated tab closed.');
447
+ /**
448
+ * Don't await this close operation. It might never resolve if the tab is closed.
449
+ * We mark the remote as closed first, this will reject any pending requests.
450
+ * We then call close. The close operation is configured to fire-and-forget, the main promise will reject immediately.
451
+ */
452
+ wrapped.markRemoteClosed();
453
+ wrapped.close().catch((ex) => this.logger.warn('error closing database connection', ex));
375
454
  });
376
- await locked.init();
377
- this.dbAdapter = lastClient.db = locked;
455
+ return wrapped;
378
456
  }
379
457
  /**
380
458
  * A method to update the all shared statuses for each
@@ -384,16 +462,34 @@ export class SharedSyncImplementation extends BaseObserver {
384
462
  this.syncStatus = new SyncStatus(status);
385
463
  this.ports.forEach((p) => p.clientProvider.statusChanged(status));
386
464
  }
387
- /**
388
- * A function only used for unit tests which updates the internal
389
- * sync stream client and all tab client's sync status
390
- */
391
- async _testUpdateAllStatuses(status) {
392
- if (!this.connectionManager.syncStreamImplementation) {
393
- throw new Error('Cannot update status without a sync stream implementation');
465
+ }
466
+ /**
467
+ * Runs the action with an abort controller.
468
+ */
469
+ function withAbort(options) {
470
+ const { action, signal, cleanupOnAbort } = options;
471
+ return new Promise((resolve, reject) => {
472
+ if (signal.aborted) {
473
+ reject(new AbortOperation('Operation aborted by abort controller'));
474
+ return;
394
475
  }
395
- // Only assigning, don't call listeners for this test
396
- this.connectionManager.syncStreamImplementation.syncStatus = new SyncStatus(status);
397
- this.updateAllStatuses(status);
398
- }
476
+ function handleAbort() {
477
+ signal.removeEventListener('abort', handleAbort);
478
+ reject(new AbortOperation('Operation aborted by abort controller'));
479
+ }
480
+ signal.addEventListener('abort', handleAbort, { once: true });
481
+ function completePromise(action) {
482
+ signal.removeEventListener('abort', handleAbort);
483
+ action();
484
+ }
485
+ action()
486
+ .then((data) => {
487
+ // We already rejected due to the abort, allow for cleanup
488
+ if (signal.aborted) {
489
+ return completePromise(() => cleanupOnAbort?.(data));
490
+ }
491
+ completePromise(() => resolve(data));
492
+ })
493
+ .catch((e) => completePromise(() => reject(e)));
494
+ });
399
495
  }
@@ -7,5 +7,5 @@ logger.useDefaults();
7
7
  const sharedSyncImplementation = new SharedSyncImplementation();
8
8
  _self.onconnect = async function (event) {
9
9
  const port = event.ports[0];
10
- await new WorkerClient(sharedSyncImplementation, port).initialize();
10
+ new WorkerClient(sharedSyncImplementation, port);
11
11
  };
@@ -1,5 +1,5 @@
1
- import { SharedSyncImplementation, SharedSyncInitOptions } from './SharedSyncImplementation';
2
- import { ILogLevel, PowerSyncConnectionOptions, SubscribedStream, SyncStatusOptions } from '@powersync/common';
1
+ import { ILogLevel, PowerSyncConnectionOptions, SubscribedStream } from '@powersync/common';
2
+ import { SharedSyncImplementation, SharedSyncInitOptions, WrappedSyncPort } from './SharedSyncImplementation';
3
3
  /**
4
4
  * A client to the shared sync worker.
5
5
  *
@@ -10,8 +10,8 @@ export declare class WorkerClient {
10
10
  private readonly sync;
11
11
  private readonly port;
12
12
  private resolvedPort;
13
+ protected resolvedPortPromise: Promise<WrappedSyncPort> | null;
13
14
  constructor(sync: SharedSyncImplementation, port: MessagePort);
14
- initialize(): Promise<void>;
15
15
  private removePort;
16
16
  /**
17
17
  * Called by a client after obtaining a lock with a random name.
@@ -19,7 +19,7 @@ export declare class WorkerClient {
19
19
  * When the client tab is closed, its lock will be returned. So when the shared worker attempts to acquire the lock,
20
20
  * it can consider the connection to be closed.
21
21
  */
22
- addLockBasedCloseSignal(name: string): void;
22
+ addLockBasedCloseSignal(name: string): Promise<void>;
23
23
  setLogLevel(level: ILogLevel): void;
24
24
  triggerCrudUpload(): void;
25
25
  setParams(params: SharedSyncInitOptions, subscriptions: SubscribedStream[]): Promise<void>;
@@ -28,5 +28,4 @@ export declare class WorkerClient {
28
28
  connect(options?: PowerSyncConnectionOptions): Promise<void>;
29
29
  updateSubscriptions(subscriptions: SubscribedStream[]): void;
30
30
  disconnect(): Promise<void>;
31
- _testUpdateAllStatuses(status: SyncStatusOptions): Promise<void>;
32
31
  }
@@ -1,6 +1,6 @@
1
1
  import * as Comlink from 'comlink';
2
- import { SharedSyncClientEvent } from './SharedSyncImplementation';
3
2
  import { getNavigatorLocks } from '../../shared/navigator';
3
+ import { SharedSyncClientEvent } from './SharedSyncImplementation';
4
4
  /**
5
5
  * A client to the shared sync worker.
6
6
  *
@@ -11,11 +11,11 @@ export class WorkerClient {
11
11
  sync;
12
12
  port;
13
13
  resolvedPort = null;
14
+ resolvedPortPromise = null;
14
15
  constructor(sync, port) {
15
16
  this.sync = sync;
16
17
  this.port = port;
17
- }
18
- async initialize() {
18
+ Comlink.expose(this, this.port);
19
19
  /**
20
20
  * Adds an extra listener which can remove this port
21
21
  * from the list of monitored ports.
@@ -26,8 +26,6 @@ export class WorkerClient {
26
26
  await this.removePort();
27
27
  }
28
28
  });
29
- this.resolvedPort = await this.sync.addPort(this.port);
30
- Comlink.expose(this, this.port);
31
29
  }
32
30
  async removePort() {
33
31
  if (this.resolvedPort) {
@@ -48,7 +46,10 @@ export class WorkerClient {
48
46
  * When the client tab is closed, its lock will be returned. So when the shared worker attempts to acquire the lock,
49
47
  * it can consider the connection to be closed.
50
48
  */
51
- addLockBasedCloseSignal(name) {
49
+ async addLockBasedCloseSignal(name) {
50
+ // Only add the port once the lock has been obtained on the client.
51
+ this.resolvedPort = await this.sync.addPort(this.port);
52
+ // Don't await this lock request
52
53
  getNavigatorLocks().request(name, async () => {
53
54
  await this.removePort();
54
55
  });
@@ -80,7 +81,4 @@ export class WorkerClient {
80
81
  disconnect() {
81
82
  return this.sync.disconnect();
82
83
  }
83
- async _testUpdateAllStatuses(status) {
84
- return this.sync._testUpdateAllStatuses(status);
85
- }
86
84
  }