@powersync/web 0.0.0-dev-20240807140418 → 0.0.0-dev-20240815073141

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 (70) hide show
  1. package/lib/src/db/PowerSyncDatabase.js +0 -1
  2. package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.js +0 -1
  3. package/lib/src/db/adapters/AbstractWebSQLOpenFactory.js +0 -1
  4. package/lib/src/db/adapters/SSRDBAdapter.js +0 -1
  5. package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.js +4 -5
  6. package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +0 -1
  7. package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.js +0 -1
  8. package/lib/src/db/adapters/web-sql-flags.js +0 -1
  9. package/lib/src/db/sync/SSRWebStreamingSyncImplementation.js +0 -1
  10. package/lib/src/db/sync/SharedWebStreamingSyncImplementation.d.ts +3 -3
  11. package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js +7 -4
  12. package/lib/src/db/sync/WebRemote.js +0 -1
  13. package/lib/src/db/sync/WebStreamingSyncImplementation.js +0 -1
  14. package/lib/src/index.js +0 -1
  15. package/lib/src/shared/open-db.js +12 -2
  16. package/lib/src/shared/types.d.ts +2 -2
  17. package/lib/src/shared/types.js +0 -1
  18. package/lib/src/worker/db/SharedWASQLiteDB.worker.js +0 -1
  19. package/lib/src/worker/db/WASQLiteDB.worker.js +0 -1
  20. package/lib/src/worker/db/open-worker-database.js +0 -1
  21. package/lib/src/worker/sync/AbstractSharedSyncClientProvider.js +0 -1
  22. package/lib/src/worker/sync/BroadcastLogger.js +0 -1
  23. package/lib/src/worker/sync/SharedSyncImplementation.d.ts +7 -3
  24. package/lib/src/worker/sync/SharedSyncImplementation.js +93 -69
  25. package/lib/src/worker/sync/SharedSyncImplementation.worker.js +0 -1
  26. package/lib/tsconfig.tsbuildinfo +1 -1
  27. package/package.json +5 -14
  28. package/dist/_2e65-_0749-_dcb9-_0b6f-_ff61-_ec48-_1dff-_983a.index.umd.js +0 -84
  29. package/dist/_2e65-_0749-_dcb9-_0b6f-_ff61-_ec48-_1dff-_983a.index.umd.js.map +0 -1
  30. package/dist/a7933e62dba1e4ff0664.wasm +0 -0
  31. package/dist/index.umd.js +0 -4816
  32. package/dist/index.umd.js.map +0 -1
  33. package/dist/lib_src_worker_db_SharedWASQLiteDB_worker_js.index.umd.js +0 -490
  34. package/dist/lib_src_worker_db_SharedWASQLiteDB_worker_js.index.umd.js.map +0 -1
  35. package/dist/lib_src_worker_db_WASQLiteDB_worker_js.index.umd.js +0 -443
  36. package/dist/lib_src_worker_db_WASQLiteDB_worker_js.index.umd.js.map +0 -1
  37. package/dist/lib_src_worker_sync_SharedSyncImplementation_worker_js.index.umd.js +0 -1698
  38. package/dist/lib_src_worker_sync_SharedSyncImplementation_worker_js.index.umd.js.map +0 -1
  39. package/dist/vendors-node_modules_async-mutex_index_mjs-node_modules_comlink_dist_esm_comlink_mjs-node_mod-037150.index.umd.js +0 -2339
  40. package/dist/vendors-node_modules_async-mutex_index_mjs-node_modules_comlink_dist_esm_comlink_mjs-node_mod-037150.index.umd.js.map +0 -1
  41. package/dist/vendors-node_modules_bson_lib_bson_mjs.index.umd.js +0 -4437
  42. package/dist/vendors-node_modules_bson_lib_bson_mjs.index.umd.js.map +0 -1
  43. package/dist/vendors-node_modules_crypto-browserify_index_js.index.umd.js +0 -33502
  44. package/dist/vendors-node_modules_crypto-browserify_index_js.index.umd.js.map +0 -1
  45. package/dist/vendors-node_modules_journeyapps_wa-sqlite_dist_wa-sqlite-async_mjs.index.umd.js +0 -173
  46. package/dist/vendors-node_modules_journeyapps_wa-sqlite_dist_wa-sqlite-async_mjs.index.umd.js.map +0 -1
  47. package/dist/vendors-node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.index.umd.js +0 -1926
  48. package/dist/vendors-node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.index.umd.js.map +0 -1
  49. package/lib/src/db/PowerSyncDatabase.js.map +0 -1
  50. package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.js.map +0 -1
  51. package/lib/src/db/adapters/AbstractWebSQLOpenFactory.js.map +0 -1
  52. package/lib/src/db/adapters/SSRDBAdapter.js.map +0 -1
  53. package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.js.map +0 -1
  54. package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js.map +0 -1
  55. package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.js.map +0 -1
  56. package/lib/src/db/adapters/web-sql-flags.js.map +0 -1
  57. package/lib/src/db/sync/SSRWebStreamingSyncImplementation.js.map +0 -1
  58. package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js.map +0 -1
  59. package/lib/src/db/sync/WebRemote.js.map +0 -1
  60. package/lib/src/db/sync/WebStreamingSyncImplementation.js.map +0 -1
  61. package/lib/src/index.js.map +0 -1
  62. package/lib/src/shared/open-db.js.map +0 -1
  63. package/lib/src/shared/types.js.map +0 -1
  64. package/lib/src/worker/db/SharedWASQLiteDB.worker.js.map +0 -1
  65. package/lib/src/worker/db/WASQLiteDB.worker.js.map +0 -1
  66. package/lib/src/worker/db/open-worker-database.js.map +0 -1
  67. package/lib/src/worker/sync/AbstractSharedSyncClientProvider.js.map +0 -1
  68. package/lib/src/worker/sync/BroadcastLogger.js.map +0 -1
  69. package/lib/src/worker/sync/SharedSyncImplementation.js.map +0 -1
  70. package/lib/src/worker/sync/SharedSyncImplementation.worker.js.map +0 -1
@@ -118,4 +118,3 @@ export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
118
118
  }
119
119
  }
120
120
  }
121
- //# sourceMappingURL=PowerSyncDatabase.js.map
@@ -24,4 +24,3 @@ export class AbstractWebPowerSyncDatabaseOpenFactory extends AbstractPowerSyncDa
24
24
  return new PowerSyncDatabase(options);
25
25
  }
26
26
  }
27
- //# sourceMappingURL=AbstractWebPowerSyncDatabaseOpenFactory.js.map
@@ -28,4 +28,3 @@ export class AbstractWebSQLOpenFactory {
28
28
  return this.openAdapter();
29
29
  }
30
30
  }
31
- //# sourceMappingURL=AbstractWebSQLOpenFactory.js.map
@@ -63,4 +63,3 @@ export class SSRDBAdapter extends BaseObserver {
63
63
  };
64
64
  }
65
65
  }
66
- //# sourceMappingURL=SSRDBAdapter.js.map
@@ -57,14 +57,14 @@ export class WASQLiteDBAdapter extends BaseObserver {
57
57
  ? Comlink.wrap(this.options.workerPort)
58
58
  : getWorkerDatabaseOpener(this.options.dbFilename, enableMultiTabs);
59
59
  this.methods = await dbOpener(this.options.dbFilename);
60
- this.methods.registerOnTableChange(Comlink.proxy((opType, tableName, rowId) => {
61
- this.iterateListeners((cb) => cb.tablesUpdated?.({ opType, table: tableName, rowId }));
60
+ this.methods.registerOnTableChange(Comlink.proxy((event) => {
61
+ this.iterateListeners((cb) => cb.tablesUpdated?.(event));
62
62
  }));
63
63
  return;
64
64
  }
65
65
  this.methods = await _openDB(this.options.dbFilename, { useWebWorker: false });
66
- this.methods.registerOnTableChange((opType, tableName, rowId) => {
67
- this.iterateListeners((cb) => cb.tablesUpdated?.({ opType, table: tableName, rowId }));
66
+ this.methods.registerOnTableChange((event) => {
67
+ this.iterateListeners((cb) => cb.tablesUpdated?.(event));
68
68
  });
69
69
  }
70
70
  async execute(query, params) {
@@ -208,4 +208,3 @@ export class WASQLiteDBAdapter extends BaseObserver {
208
208
  };
209
209
  }
210
210
  }
211
- //# sourceMappingURL=WASQLiteDBAdapter.js.map
@@ -11,4 +11,3 @@ export class WASQLiteOpenFactory extends AbstractWebSQLOpenFactory {
11
11
  });
12
12
  }
13
13
  }
14
- //# sourceMappingURL=WASQLiteOpenFactory.js.map
@@ -19,4 +19,3 @@ export class WASQLitePowerSyncDatabaseOpenFactory extends AbstractWebPowerSyncDa
19
19
  return new PowerSyncDatabase(options);
20
20
  }
21
21
  }
22
- //# sourceMappingURL=WASQLitePowerSyncDatabaseOpenFactory.js.map
@@ -28,4 +28,3 @@ export function resolveWebSQLFlags(flags) {
28
28
  }
29
29
  return resolvedFlags;
30
30
  }
31
- //# sourceMappingURL=web-sql-flags.js.map
@@ -13,4 +13,3 @@ export class SSRStreamingSyncImplementation extends AbstractStreamingSyncImpleme
13
13
  return mutex.runExclusive(lockOptions.callback);
14
14
  }
15
15
  }
16
- //# sourceMappingURL=SSRWebStreamingSyncImplementation.js.map
@@ -1,8 +1,8 @@
1
+ import { PowerSyncConnectionOptions, PowerSyncCredentials, SyncStatusOptions } from '@powersync/common';
1
2
  import * as Comlink from 'comlink';
2
- import { WebStreamingSyncImplementation, WebStreamingSyncImplementationOptions } from './WebStreamingSyncImplementation';
3
- import { SharedSyncImplementation } from '../../worker/sync/SharedSyncImplementation';
4
3
  import { AbstractSharedSyncClientProvider } from '../../worker/sync/AbstractSharedSyncClientProvider';
5
- import { PowerSyncConnectionOptions, PowerSyncCredentials, SyncStatusOptions } from '@powersync/common';
4
+ import { SharedSyncImplementation } from '../../worker/sync/SharedSyncImplementation';
5
+ import { WebStreamingSyncImplementation, WebStreamingSyncImplementationOptions } from './WebStreamingSyncImplementation';
6
6
  /**
7
7
  * The shared worker will trigger methods on this side of the message port
8
8
  * via this client provider.
@@ -1,8 +1,8 @@
1
1
  import * as Comlink from 'comlink';
2
- import { WebStreamingSyncImplementation } from './WebStreamingSyncImplementation';
3
- import { SharedSyncClientEvent } from '../../worker/sync/SharedSyncImplementation';
4
- import { AbstractSharedSyncClientProvider } from '../../worker/sync/AbstractSharedSyncClientProvider';
5
2
  import { openWorkerDatabasePort } from '../../worker/db/open-worker-database';
3
+ import { AbstractSharedSyncClientProvider } from '../../worker/sync/AbstractSharedSyncClientProvider';
4
+ import { SharedSyncClientEvent } from '../../worker/sync/SharedSyncImplementation';
5
+ import { WebStreamingSyncImplementation } from './WebStreamingSyncImplementation';
6
6
  /**
7
7
  * The shared worker will trigger methods on this side of the message port
8
8
  * via this client provider.
@@ -122,6 +122,9 @@ export class SharedWebStreamingSyncImplementation extends WebStreamingSyncImplem
122
122
  */
123
123
  async connect(options) {
124
124
  await this.waitForReady();
125
+ // This is needed since a new tab won't have any reference to the
126
+ // shared worker sync implementation since that is only created on the first call to `connect`.
127
+ await this.disconnect();
125
128
  return this.syncManager.connect(options);
126
129
  }
127
130
  async disconnect() {
@@ -145,6 +148,7 @@ export class SharedWebStreamingSyncImplementation extends WebStreamingSyncImplem
145
148
  this.messagePort.postMessage(closeMessagePayload);
146
149
  // Release the proxy
147
150
  this.syncManager[Comlink.releaseProxy]();
151
+ this.messagePort.close();
148
152
  }
149
153
  async waitForReady() {
150
154
  return this.isInitialized;
@@ -156,4 +160,3 @@ export class SharedWebStreamingSyncImplementation extends WebStreamingSyncImplem
156
160
  return this.syncManager['_testUpdateAllStatuses'](status.toJSON());
157
161
  }
158
162
  }
159
- //# sourceMappingURL=SharedWebStreamingSyncImplementation.js.map
@@ -31,4 +31,3 @@ export class WebRemote extends AbstractRemote {
31
31
  return this._bson;
32
32
  }
33
33
  }
34
- //# sourceMappingURL=WebRemote.js.map
@@ -13,4 +13,3 @@ export class WebStreamingSyncImplementation extends AbstractStreamingSyncImpleme
13
13
  return navigator.locks.request(identifier, { signal: lockOptions.signal }, lockOptions.callback);
14
14
  }
15
15
  }
16
- //# sourceMappingURL=WebStreamingSyncImplementation.js.map
package/lib/src/index.js CHANGED
@@ -9,4 +9,3 @@ export * from './db/adapters/AbstractWebPowerSyncDatabaseOpenFactory';
9
9
  export * from './db/adapters/web-sql-flags';
10
10
  export * from './db/adapters/AbstractWebSQLOpenFactory';
11
11
  export * from './db/adapters/wa-sqlite/WASQLiteOpenFactory';
12
- //# sourceMappingURL=index.js.map
@@ -16,8 +16,19 @@ export async function _openDB(dbFileName, options = { useWebWorker: true }) {
16
16
  * Listeners are exclusive to the DB connection.
17
17
  */
18
18
  const listeners = new Map();
19
+ let updatedTables = new Set();
20
+ let updateTimer = null;
21
+ function fireUpdates() {
22
+ updateTimer = null;
23
+ const event = { tables: [...updatedTables], groupedUpdates: {}, rawUpdates: [] };
24
+ updatedTables.clear();
25
+ Array.from(listeners.values()).forEach((l) => l(event));
26
+ }
19
27
  sqlite3.register_table_onchange_hook(db, (opType, tableName, rowId) => {
20
- Array.from(listeners.values()).forEach((l) => l(opType, tableName, rowId));
28
+ updatedTables.add(tableName);
29
+ if (updateTimer == null) {
30
+ updateTimer = setTimeout(fireUpdates, 0);
31
+ }
21
32
  });
22
33
  /**
23
34
  * This executes single SQL statements inside a requested lock.
@@ -179,4 +190,3 @@ export async function _openDB(dbFileName, options = { useWebWorker: true }) {
179
190
  close: () => sqlite3.close(db)
180
191
  };
181
192
  }
182
- //# sourceMappingURL=open-db.js.map
@@ -1,4 +1,4 @@
1
- import type { QueryResult } from '@powersync/common';
1
+ import type { BatchedUpdateNotification, QueryResult } from '@powersync/common';
2
2
  export type WASQLExecuteResult = Omit<QueryResult, 'rows'> & {
3
3
  rows: {
4
4
  _array: any[];
@@ -17,6 +17,6 @@ export type DBFunctionsInterface = {
17
17
  export type DBWorkerInterface = DBFunctionsInterface;
18
18
  export type WASQLiteExecuteMethod = (sql: string, params?: any[]) => Promise<WASQLExecuteResult>;
19
19
  export type WASQLiteExecuteBatchMethod = (sql: string, params?: any[]) => Promise<WASQLExecuteResult>;
20
- export type OnTableChangeCallback = (opType: number, tableName: string, rowId: number) => void;
20
+ export type OnTableChangeCallback = (event: BatchedUpdateNotification) => void;
21
21
  export type OpenDB = (dbFileName: string) => DBWorkerInterface;
22
22
  export type SQLBatchTuple = [string] | [string, Array<any> | Array<Array<any>>];
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=types.js.map
@@ -47,4 +47,3 @@ addEventListener('unload', () => {
47
47
  db.close?.();
48
48
  });
49
49
  });
50
- //# sourceMappingURL=SharedWASQLiteDB.worker.js.map
@@ -1,4 +1,3 @@
1
1
  import * as Comlink from 'comlink';
2
2
  import { _openDB } from '../../shared/open-db';
3
3
  Comlink.expose(async (dbFileName) => Comlink.proxy(await _openDB(dbFileName)));
4
- //# sourceMappingURL=WASQLiteDB.worker.js.map
@@ -28,4 +28,3 @@ export function openWorkerDatabasePort(workerIdentifier, multipleTabs = true) {
28
28
  export function getWorkerDatabaseOpener(workerIdentifier, multipleTabs = true) {
29
29
  return Comlink.wrap(openWorkerDatabasePort(workerIdentifier, multipleTabs));
30
30
  }
31
- //# sourceMappingURL=open-worker-database.js.map
@@ -3,4 +3,3 @@
3
3
  */
4
4
  export class AbstractSharedSyncClientProvider {
5
5
  }
6
- //# sourceMappingURL=AbstractSharedSyncClientProvider.js.map
@@ -102,4 +102,3 @@ export class BroadcastLogger {
102
102
  return sanitizedParams;
103
103
  }
104
104
  }
105
- //# sourceMappingURL=BroadcastLogger.js.map
@@ -1,7 +1,7 @@
1
+ import { type AbstractStreamingSyncImplementation, type LockOptions, type PowerSyncConnectionOptions, type StreamingSyncImplementation, type StreamingSyncImplementationListener, type SyncStatusOptions, BaseObserver, DBAdapter, SyncStatus } from '@powersync/common';
1
2
  import * as Comlink from 'comlink';
2
3
  import { type ILogger } from 'js-logger';
3
- import { type AbstractStreamingSyncImplementation, type StreamingSyncImplementation, type LockOptions, type StreamingSyncImplementationListener, type SyncStatusOptions, type PowerSyncConnectionOptions, BaseObserver, SyncStatus } from '@powersync/common';
4
- import { WebStreamingSyncImplementationOptions } from '../../db/sync/WebStreamingSyncImplementation';
4
+ import { WebStreamingSyncImplementation, WebStreamingSyncImplementationOptions } from '../../db/sync/WebStreamingSyncImplementation';
5
5
  import { AbstractSharedSyncClientProvider } from './AbstractSharedSyncClientProvider';
6
6
  /**
7
7
  * Manual message events for shared sync clients
@@ -37,11 +37,14 @@ export type RemoteOperationAbortController = {
37
37
  */
38
38
  export declare class SharedSyncImplementation extends BaseObserver<SharedSyncImplementationListener> implements StreamingSyncImplementation {
39
39
  protected ports: WrappedSyncPort[];
40
- protected syncStreamClient?: AbstractStreamingSyncImplementation;
40
+ protected syncStreamClient: AbstractStreamingSyncImplementation | null;
41
41
  protected isInitialized: Promise<void>;
42
42
  protected statusListener?: () => void;
43
43
  protected fetchCredentialsController?: RemoteOperationAbortController;
44
44
  protected uploadDataController?: RemoteOperationAbortController;
45
+ protected dbAdapter: DBAdapter | null;
46
+ protected syncParams: SharedSyncInitOptions | null;
47
+ protected logger: ILogger;
45
48
  syncStatus: SyncStatus;
46
49
  broadCastLogger: ILogger;
47
50
  constructor();
@@ -75,6 +78,7 @@ export declare class SharedSyncImplementation extends BaseObserver<SharedSyncImp
75
78
  obtainLock<T>(lockOptions: LockOptions<T>): Promise<T>;
76
79
  hasCompletedSync(): Promise<boolean>;
77
80
  getWriteCheckpoint(): Promise<string>;
81
+ protected generateStreamingImplementation(): WebStreamingSyncImplementation;
78
82
  /**
79
83
  * A method to update the all shared statuses for each
80
84
  * client.
@@ -1,9 +1,9 @@
1
+ import { AbortOperation, BaseObserver, SqliteBucketStorage, SyncStatus } from '@powersync/common';
2
+ import { Mutex } from 'async-mutex';
1
3
  import * as Comlink from 'comlink';
2
4
  import Logger from 'js-logger';
3
- import { BaseObserver, SqliteBucketStorage, SyncStatus, AbortOperation } from '@powersync/common';
4
- import { WebStreamingSyncImplementation } from '../../db/sync/WebStreamingSyncImplementation';
5
- import { Mutex } from 'async-mutex';
6
5
  import { WebRemote } from '../../db/sync/WebRemote';
6
+ import { WebStreamingSyncImplementation } from '../../db/sync/WebStreamingSyncImplementation';
7
7
  import { WASQLiteDBAdapter } from '../../db/adapters/wa-sqlite/WASQLiteDBAdapter';
8
8
  import { BroadcastLogger } from './BroadcastLogger';
9
9
  /**
@@ -27,11 +27,18 @@ export class SharedSyncImplementation extends BaseObserver {
27
27
  statusListener;
28
28
  fetchCredentialsController;
29
29
  uploadDataController;
30
+ dbAdapter;
31
+ syncParams;
32
+ logger;
30
33
  syncStatus;
31
34
  broadCastLogger;
32
35
  constructor() {
33
36
  super();
34
37
  this.ports = [];
38
+ this.dbAdapter = null;
39
+ this.syncParams = null;
40
+ this.syncStreamClient = null;
41
+ this.logger = Logger.get('shared-sync');
35
42
  this.isInitialized = new Promise((resolve) => {
36
43
  const callback = this.registerListener({
37
44
  initialized: () => {
@@ -60,74 +67,24 @@ export class SharedSyncImplementation extends BaseObserver {
60
67
  * Configures the DBAdapter connection and a streaming sync client.
61
68
  */
62
69
  async init(dbWorkerPort, params) {
63
- if (this.syncStreamClient) {
70
+ if (this.dbAdapter) {
64
71
  // Cannot modify already existing sync implementation
65
72
  return;
66
73
  }
67
- const logger = params.streamOptions?.flags?.broadcastLogs ? this.broadCastLogger : Logger.get('shared-sync');
74
+ this.dbAdapter = new WASQLiteDBAdapter({
75
+ dbFilename: params.dbName,
76
+ workerPort: dbWorkerPort,
77
+ flags: { enableMultiTabs: true, useWebWorker: true },
78
+ logger: this.logger
79
+ });
80
+ this.syncParams = params;
81
+ if (params.streamOptions?.flags?.broadcastLogs) {
82
+ this.logger = this.broadCastLogger;
83
+ }
68
84
  self.onerror = (event) => {
69
85
  // Share any uncaught events on the broadcast logger
70
- logger.error('Uncaught exception in PowerSync shared sync worker', event);
86
+ this.logger.error('Uncaught exception in PowerSync shared sync worker', event);
71
87
  };
72
- this.syncStreamClient = new WebStreamingSyncImplementation({
73
- adapter: new SqliteBucketStorage(new WASQLiteDBAdapter({
74
- dbFilename: params.dbName,
75
- workerPort: dbWorkerPort,
76
- flags: { enableMultiTabs: true, useWebWorker: true },
77
- logger
78
- }), new Mutex(), logger),
79
- remote: new WebRemote({
80
- fetchCredentials: async () => {
81
- const lastPort = this.ports[this.ports.length - 1];
82
- return new Promise(async (resolve, reject) => {
83
- const abortController = new AbortController();
84
- this.fetchCredentialsController = {
85
- controller: abortController,
86
- activePort: lastPort
87
- };
88
- abortController.signal.onabort = reject;
89
- try {
90
- resolve(await lastPort.clientProvider.fetchCredentials());
91
- }
92
- catch (ex) {
93
- reject(ex);
94
- }
95
- finally {
96
- this.fetchCredentialsController = undefined;
97
- }
98
- });
99
- }
100
- }),
101
- uploadCrud: async () => {
102
- const lastPort = this.ports[this.ports.length - 1];
103
- return new Promise(async (resolve, reject) => {
104
- const abortController = new AbortController();
105
- this.uploadDataController = {
106
- controller: abortController,
107
- activePort: lastPort
108
- };
109
- // Resolving will make it retry
110
- abortController.signal.onabort = () => resolve();
111
- try {
112
- resolve(await lastPort.clientProvider.uploadCrud());
113
- }
114
- catch (ex) {
115
- reject(ex);
116
- }
117
- finally {
118
- this.uploadDataController = undefined;
119
- }
120
- });
121
- },
122
- ...params.streamOptions,
123
- // Logger cannot be transferred just yet
124
- logger
125
- });
126
- this.syncStreamClient.registerListener({
127
- statusChanged: (status) => {
128
- this.updateAllStatuses(status.toJSON());
129
- }
130
- });
131
88
  this.iterateListeners((l) => l.initialized?.());
132
89
  }
133
90
  async dispose() {
@@ -144,12 +101,24 @@ export class SharedSyncImplementation extends BaseObserver {
144
101
  async connect(options) {
145
102
  await this.waitForReady();
146
103
  // This effectively queues connect and disconnect calls. Ensuring multiple tabs' requests are synchronized
147
- return navigator.locks.request('shared-sync-connect', () => this.syncStreamClient?.connect(options));
104
+ return navigator.locks.request('shared-sync-connect', async () => {
105
+ this.syncStreamClient = this.generateStreamingImplementation();
106
+ this.syncStreamClient.registerListener({
107
+ statusChanged: (status) => {
108
+ this.updateAllStatuses(status.toJSON());
109
+ }
110
+ });
111
+ await this.syncStreamClient.connect(options);
112
+ });
148
113
  }
149
114
  async disconnect() {
150
115
  await this.waitForReady();
151
116
  // This effectively queues connect and disconnect calls. Ensuring multiple tabs' requests are synchronized
152
- return navigator.locks.request('shared-sync-connect', () => this.syncStreamClient?.disconnect());
117
+ return navigator.locks.request('shared-sync-connect', async () => {
118
+ await this.syncStreamClient?.disconnect();
119
+ await this.syncStreamClient?.dispose();
120
+ this.syncStreamClient = null;
121
+ });
153
122
  }
154
123
  /**
155
124
  * Adds a new client tab's message port to the list of connected ports
@@ -205,6 +174,61 @@ export class SharedSyncImplementation extends BaseObserver {
205
174
  await this.waitForReady();
206
175
  return this.syncStreamClient.getWriteCheckpoint();
207
176
  }
177
+ generateStreamingImplementation() {
178
+ // This should only be called after initialization has completed
179
+ const syncParams = this.syncParams;
180
+ // Create a new StreamingSyncImplementation for each connect call. This is usually done is all SDKs.
181
+ return new WebStreamingSyncImplementation({
182
+ adapter: new SqliteBucketStorage(this.dbAdapter, new Mutex(), this.logger),
183
+ remote: new WebRemote({
184
+ fetchCredentials: async () => {
185
+ const lastPort = this.ports[this.ports.length - 1];
186
+ return new Promise(async (resolve, reject) => {
187
+ const abortController = new AbortController();
188
+ this.fetchCredentialsController = {
189
+ controller: abortController,
190
+ activePort: lastPort
191
+ };
192
+ abortController.signal.onabort = reject;
193
+ try {
194
+ console.log('calling the last port client provider for credentials');
195
+ resolve(await lastPort.clientProvider.fetchCredentials());
196
+ }
197
+ catch (ex) {
198
+ reject(ex);
199
+ }
200
+ finally {
201
+ this.fetchCredentialsController = undefined;
202
+ }
203
+ });
204
+ }
205
+ }),
206
+ uploadCrud: async () => {
207
+ const lastPort = this.ports[this.ports.length - 1];
208
+ return new Promise(async (resolve, reject) => {
209
+ const abortController = new AbortController();
210
+ this.uploadDataController = {
211
+ controller: abortController,
212
+ activePort: lastPort
213
+ };
214
+ // Resolving will make it retry
215
+ abortController.signal.onabort = () => resolve();
216
+ try {
217
+ resolve(await lastPort.clientProvider.uploadCrud());
218
+ }
219
+ catch (ex) {
220
+ reject(ex);
221
+ }
222
+ finally {
223
+ this.uploadDataController = undefined;
224
+ }
225
+ });
226
+ },
227
+ ...syncParams.streamOptions,
228
+ // Logger cannot be transferred just yet
229
+ logger: this.logger
230
+ });
231
+ }
208
232
  /**
209
233
  * A method to update the all shared statuses for each
210
234
  * client.
@@ -219,11 +243,11 @@ export class SharedSyncImplementation extends BaseObserver {
219
243
  */
220
244
  _testUpdateAllStatuses(status) {
221
245
  if (!this.syncStreamClient) {
222
- console.warn('no stream client has been initialized yet');
246
+ // This is just for testing purposes
247
+ this.syncStreamClient = this.generateStreamingImplementation();
223
248
  }
224
249
  // Only assigning, don't call listeners for this test
225
250
  this.syncStreamClient.syncStatus = new SyncStatus(status);
226
251
  this.updateAllStatuses(status);
227
252
  }
228
253
  }
229
- //# sourceMappingURL=SharedSyncImplementation.js.map
@@ -20,4 +20,3 @@ _self.onconnect = function (event) {
20
20
  Comlink.expose(sharedSyncImplementation, port);
21
21
  sharedSyncImplementation.addPort(port);
22
22
  };
23
- //# sourceMappingURL=SharedSyncImplementation.worker.js.map