@powersync/web 0.0.0-dev-20250915110424 → 0.0.0-dev-20250922104723

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.umd.js CHANGED
@@ -38502,8 +38502,7 @@ class PowerSyncDatabase extends _powersync_common__WEBPACK_IMPORTED_MODULE_0__.A
38502
38502
  const remote = new _sync_WebRemote__WEBPACK_IMPORTED_MODULE_7__.WebRemote(connector, this.logger);
38503
38503
  const syncOptions = {
38504
38504
  ...this.options,
38505
- retryDelayMs: options.retryDelayMs,
38506
- crudUploadThrottleMs: options.crudUploadThrottleMs,
38505
+ ...options,
38507
38506
  flags: this.resolvedFlags,
38508
38507
  adapter: this.bucketStorageAdapter,
38509
38508
  remote,
@@ -39881,6 +39880,10 @@ class SSRStreamingSyncImplementation extends _powersync_common__WEBPACK_IMPORTED
39881
39880
  * This is a no-op in SSR mode.
39882
39881
  */
39883
39882
  triggerCrudUpload() { }
39883
+ /**
39884
+ * No-op in SSR mode.
39885
+ */
39886
+ updateSubscriptions() { }
39884
39887
  }
39885
39888
 
39886
39889
 
@@ -39903,6 +39906,8 @@ __webpack_require__.r(__webpack_exports__);
39903
39906
  /* harmony import */ var _worker_sync_SharedSyncImplementation__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../worker/sync/SharedSyncImplementation */ "./lib/src/worker/sync/SharedSyncImplementation.js");
39904
39907
  /* harmony import */ var _adapters_web_sql_flags__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../adapters/web-sql-flags */ "./lib/src/db/adapters/web-sql-flags.js");
39905
39908
  /* harmony import */ var _WebStreamingSyncImplementation__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./WebStreamingSyncImplementation */ "./lib/src/db/sync/WebStreamingSyncImplementation.js");
39909
+ /* harmony import */ var _shared_navigator__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ../../shared/navigator */ "./lib/src/shared/navigator.js");
39910
+
39906
39911
 
39907
39912
 
39908
39913
 
@@ -39980,12 +39985,16 @@ class SharedSyncClientProvider extends _worker_sync_AbstractSharedSyncClientProv
39980
39985
  this.logger?.timeEnd(label);
39981
39986
  }
39982
39987
  }
39988
+ /**
39989
+ * The local part of the sync implementation on the web, which talks to a sync implementation hosted in a shared worker.
39990
+ */
39983
39991
  class SharedWebStreamingSyncImplementation extends _WebStreamingSyncImplementation__WEBPACK_IMPORTED_MODULE_4__.WebStreamingSyncImplementation {
39984
39992
  syncManager;
39985
39993
  clientProvider;
39986
39994
  messagePort;
39987
39995
  isInitialized;
39988
39996
  dbAdapter;
39997
+ abortOnClose = new AbortController();
39989
39998
  constructor(options) {
39990
39999
  super(options);
39991
40000
  this.dbAdapter = options.db;
@@ -40038,7 +40047,7 @@ class SharedWebStreamingSyncImplementation extends _WebStreamingSyncImplementati
40038
40047
  retryDelayMs,
40039
40048
  flags: flags
40040
40049
  }
40041
- });
40050
+ }, options.subscriptions);
40042
40051
  /**
40043
40052
  * Pass along any sync status updates to this listener
40044
40053
  */
@@ -40051,6 +40060,17 @@ class SharedWebStreamingSyncImplementation extends _WebStreamingSyncImplementati
40051
40060
  * This performs bi-directional method calling.
40052
40061
  */
40053
40062
  comlink__WEBPACK_IMPORTED_MODULE_0__.expose(this.clientProvider, this.messagePort);
40063
+ // Request a random lock until this client is disposed. The name of the lock is sent to the shared worker, which
40064
+ // will also attempt to acquire it. Since the lock is returned when the tab is closed, this allows the share worker
40065
+ // to free resources associated with this tab.
40066
+ (0,_shared_navigator__WEBPACK_IMPORTED_MODULE_5__.getNavigatorLocks)().request(`tab-close-signal-${crypto.randomUUID()}`, async (lock) => {
40067
+ if (!this.abortOnClose.signal.aborted) {
40068
+ this.syncManager.addLockBasedCloseSignal(lock.name);
40069
+ await new Promise((r) => {
40070
+ this.abortOnClose.signal.onabort = () => r();
40071
+ });
40072
+ }
40073
+ });
40054
40074
  }
40055
40075
  /**
40056
40076
  * Starts the sync process, this effectively acts as a call to
@@ -40089,6 +40109,7 @@ class SharedWebStreamingSyncImplementation extends _WebStreamingSyncImplementati
40089
40109
  };
40090
40110
  this.messagePort.postMessage(closeMessagePayload);
40091
40111
  });
40112
+ this.abortOnClose.abort();
40092
40113
  // Release the proxy
40093
40114
  this.syncManager[comlink__WEBPACK_IMPORTED_MODULE_0__.releaseProxy]();
40094
40115
  this.messagePort.close();
@@ -40096,12 +40117,15 @@ class SharedWebStreamingSyncImplementation extends _WebStreamingSyncImplementati
40096
40117
  async waitForReady() {
40097
40118
  return this.isInitialized;
40098
40119
  }
40120
+ updateSubscriptions(subscriptions) {
40121
+ this.syncManager.updateSubscriptions(subscriptions);
40122
+ }
40099
40123
  /**
40100
40124
  * Used in tests to force a connection states
40101
40125
  */
40102
40126
  async _testUpdateStatus(status) {
40103
40127
  await this.isInitialized;
40104
- return this.syncManager['_testUpdateAllStatuses'](status.toJSON());
40128
+ return this.syncManager._testUpdateAllStatuses(status.toJSON());
40105
40129
  }
40106
40130
  }
40107
40131
 
@@ -40609,6 +40633,7 @@ class SharedSyncImplementation extends _powersync_common__WEBPACK_IMPORTED_MODUL
40609
40633
  logger;
40610
40634
  lastConnectOptions;
40611
40635
  portMutex;
40636
+ subscriptions = [];
40612
40637
  connectionManager;
40613
40638
  syncStatus;
40614
40639
  broadCastLogger;
@@ -40671,6 +40696,23 @@ class SharedSyncImplementation extends _powersync_common__WEBPACK_IMPORTED_MODUL
40671
40696
  async waitForReady() {
40672
40697
  return this.isInitialized;
40673
40698
  }
40699
+ collectActiveSubscriptions() {
40700
+ this.logger.debug('Collecting active stream subscriptions across tabs');
40701
+ const active = new Map();
40702
+ for (const port of this.ports) {
40703
+ for (const stream of port.currentSubscriptions) {
40704
+ const serializedKey = JSON.stringify(stream);
40705
+ active.set(serializedKey, stream);
40706
+ }
40707
+ }
40708
+ this.subscriptions = [...active.values()];
40709
+ this.logger.debug('Collected stream subscriptions', this.subscriptions);
40710
+ this.connectionManager.syncStreamImplementation?.updateSubscriptions(this.subscriptions);
40711
+ }
40712
+ updateSubscriptions(port, subscriptions) {
40713
+ port.currentSubscriptions = subscriptions;
40714
+ this.collectActiveSubscriptions();
40715
+ }
40674
40716
  setLogLevel(level) {
40675
40717
  this.logger.setLevel(level);
40676
40718
  this.broadCastLogger.setLevel(level);
@@ -40680,6 +40722,7 @@ class SharedSyncImplementation extends _powersync_common__WEBPACK_IMPORTED_MODUL
40680
40722
  */
40681
40723
  async setParams(params) {
40682
40724
  await this.portMutex.runExclusive(async () => {
40725
+ this.collectActiveSubscriptions();
40683
40726
  if (this.syncParams) {
40684
40727
  // Cannot modify already existing sync implementation params
40685
40728
  // But we can ask for a DB adapter, if required, at this point.
@@ -40725,10 +40768,11 @@ class SharedSyncImplementation extends _powersync_common__WEBPACK_IMPORTED_MODUL
40725
40768
  * Adds a new client tab's message port to the list of connected ports
40726
40769
  */
40727
40770
  async addPort(port) {
40728
- await this.portMutex.runExclusive(() => {
40771
+ return await this.portMutex.runExclusive(() => {
40729
40772
  const portProvider = {
40730
40773
  port,
40731
- clientProvider: comlink__WEBPACK_IMPORTED_MODULE_2__.wrap(port)
40774
+ clientProvider: comlink__WEBPACK_IMPORTED_MODULE_2__.wrap(port),
40775
+ currentSubscriptions: []
40732
40776
  };
40733
40777
  this.ports.push(portProvider);
40734
40778
  // Give the newly connected client the latest status
@@ -40736,6 +40780,7 @@ class SharedSyncImplementation extends _powersync_common__WEBPACK_IMPORTED_MODUL
40736
40780
  if (status) {
40737
40781
  portProvider.clientProvider.statusChanged(status.toJSON());
40738
40782
  }
40783
+ return portProvider;
40739
40784
  });
40740
40785
  }
40741
40786
  /**
@@ -40747,7 +40792,7 @@ class SharedSyncImplementation extends _powersync_common__WEBPACK_IMPORTED_MODUL
40747
40792
  // Warns if the port is not found. This should not happen in practice.
40748
40793
  // We return early if the port is not found.
40749
40794
  const { trackedPort, shouldReconnect } = await this.portMutex.runExclusive(async () => {
40750
- const index = this.ports.findIndex((p) => p.port == port);
40795
+ const index = this.ports.findIndex((p) => p == port);
40751
40796
  if (index < 0) {
40752
40797
  this.logger.warn(`Could not remove port ${port} since it is not present in active ports.`);
40753
40798
  return {};
@@ -40760,7 +40805,7 @@ class SharedSyncImplementation extends _powersync_common__WEBPACK_IMPORTED_MODUL
40760
40805
  * not resolve. Abort them here.
40761
40806
  */
40762
40807
  [this.fetchCredentialsController, this.uploadDataController].forEach((abortController) => {
40763
- if (abortController?.activePort.port == port) {
40808
+ if (abortController?.activePort == port) {
40764
40809
  abortController.controller.abort(new _powersync_common__WEBPACK_IMPORTED_MODULE_0__.AbortOperation('Closing pending requests after client port is removed'));
40765
40810
  }
40766
40811
  });
@@ -40787,6 +40832,8 @@ class SharedSyncImplementation extends _powersync_common__WEBPACK_IMPORTED_MODUL
40787
40832
  if (trackedPort.db) {
40788
40833
  await trackedPort.db.close();
40789
40834
  }
40835
+ // Re-index subscriptions, the subscriptions of the removed port would no longer be considered.
40836
+ this.collectActiveSubscriptions();
40790
40837
  // Release proxy
40791
40838
  return () => trackedPort.clientProvider[comlink__WEBPACK_IMPORTED_MODULE_2__.releaseProxy]();
40792
40839
  }
@@ -40881,6 +40928,7 @@ class SharedSyncImplementation extends _powersync_common__WEBPACK_IMPORTED_MODUL
40881
40928
  });
40882
40929
  },
40883
40930
  ...syncParams.streamOptions,
40931
+ subscriptions: this.subscriptions,
40884
40932
  // Logger cannot be transferred just yet
40885
40933
  logger: this.logger
40886
40934
  });