@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.
- package/lib/src/db/PowerSyncDatabase.js +0 -1
- package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.js +0 -1
- package/lib/src/db/adapters/AbstractWebSQLOpenFactory.js +0 -1
- package/lib/src/db/adapters/SSRDBAdapter.js +0 -1
- package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.js +4 -5
- package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +0 -1
- package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.js +0 -1
- package/lib/src/db/adapters/web-sql-flags.js +0 -1
- package/lib/src/db/sync/SSRWebStreamingSyncImplementation.js +0 -1
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.d.ts +3 -3
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js +7 -4
- package/lib/src/db/sync/WebRemote.js +0 -1
- package/lib/src/db/sync/WebStreamingSyncImplementation.js +0 -1
- package/lib/src/index.js +0 -1
- package/lib/src/shared/open-db.js +12 -2
- package/lib/src/shared/types.d.ts +2 -2
- package/lib/src/shared/types.js +0 -1
- package/lib/src/worker/db/SharedWASQLiteDB.worker.js +0 -1
- package/lib/src/worker/db/WASQLiteDB.worker.js +0 -1
- package/lib/src/worker/db/open-worker-database.js +0 -1
- package/lib/src/worker/sync/AbstractSharedSyncClientProvider.js +0 -1
- package/lib/src/worker/sync/BroadcastLogger.js +0 -1
- package/lib/src/worker/sync/SharedSyncImplementation.d.ts +7 -3
- package/lib/src/worker/sync/SharedSyncImplementation.js +93 -69
- package/lib/src/worker/sync/SharedSyncImplementation.worker.js +0 -1
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +5 -14
- package/dist/_2e65-_0749-_dcb9-_0b6f-_ff61-_ec48-_1dff-_983a.index.umd.js +0 -84
- package/dist/_2e65-_0749-_dcb9-_0b6f-_ff61-_ec48-_1dff-_983a.index.umd.js.map +0 -1
- package/dist/a7933e62dba1e4ff0664.wasm +0 -0
- package/dist/index.umd.js +0 -4816
- package/dist/index.umd.js.map +0 -1
- package/dist/lib_src_worker_db_SharedWASQLiteDB_worker_js.index.umd.js +0 -490
- package/dist/lib_src_worker_db_SharedWASQLiteDB_worker_js.index.umd.js.map +0 -1
- package/dist/lib_src_worker_db_WASQLiteDB_worker_js.index.umd.js +0 -443
- package/dist/lib_src_worker_db_WASQLiteDB_worker_js.index.umd.js.map +0 -1
- package/dist/lib_src_worker_sync_SharedSyncImplementation_worker_js.index.umd.js +0 -1698
- package/dist/lib_src_worker_sync_SharedSyncImplementation_worker_js.index.umd.js.map +0 -1
- package/dist/vendors-node_modules_async-mutex_index_mjs-node_modules_comlink_dist_esm_comlink_mjs-node_mod-037150.index.umd.js +0 -2339
- 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
- package/dist/vendors-node_modules_bson_lib_bson_mjs.index.umd.js +0 -4437
- package/dist/vendors-node_modules_bson_lib_bson_mjs.index.umd.js.map +0 -1
- package/dist/vendors-node_modules_crypto-browserify_index_js.index.umd.js +0 -33502
- package/dist/vendors-node_modules_crypto-browserify_index_js.index.umd.js.map +0 -1
- package/dist/vendors-node_modules_journeyapps_wa-sqlite_dist_wa-sqlite-async_mjs.index.umd.js +0 -173
- package/dist/vendors-node_modules_journeyapps_wa-sqlite_dist_wa-sqlite-async_mjs.index.umd.js.map +0 -1
- package/dist/vendors-node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.index.umd.js +0 -1926
- package/dist/vendors-node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.index.umd.js.map +0 -1
- package/lib/src/db/PowerSyncDatabase.js.map +0 -1
- package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.js.map +0 -1
- package/lib/src/db/adapters/AbstractWebSQLOpenFactory.js.map +0 -1
- package/lib/src/db/adapters/SSRDBAdapter.js.map +0 -1
- package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.js.map +0 -1
- package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js.map +0 -1
- package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.js.map +0 -1
- package/lib/src/db/adapters/web-sql-flags.js.map +0 -1
- package/lib/src/db/sync/SSRWebStreamingSyncImplementation.js.map +0 -1
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js.map +0 -1
- package/lib/src/db/sync/WebRemote.js.map +0 -1
- package/lib/src/db/sync/WebStreamingSyncImplementation.js.map +0 -1
- package/lib/src/index.js.map +0 -1
- package/lib/src/shared/open-db.js.map +0 -1
- package/lib/src/shared/types.js.map +0 -1
- package/lib/src/worker/db/SharedWASQLiteDB.worker.js.map +0 -1
- package/lib/src/worker/db/WASQLiteDB.worker.js.map +0 -1
- package/lib/src/worker/db/open-worker-database.js.map +0 -1
- package/lib/src/worker/sync/AbstractSharedSyncClientProvider.js.map +0 -1
- package/lib/src/worker/sync/BroadcastLogger.js.map +0 -1
- package/lib/src/worker/sync/SharedSyncImplementation.js.map +0 -1
- package/lib/src/worker/sync/SharedSyncImplementation.worker.js.map +0 -1
|
@@ -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((
|
|
61
|
-
this.iterateListeners((cb) => cb.tablesUpdated?.(
|
|
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((
|
|
67
|
-
this.iterateListeners((cb) => cb.tablesUpdated?.(
|
|
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
|
|
@@ -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 {
|
|
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
|
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
|
-
|
|
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 = (
|
|
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>>];
|
package/lib/src/shared/types.js
CHANGED
|
@@ -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
|
|
@@ -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 {
|
|
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
|
|
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.
|
|
70
|
+
if (this.dbAdapter) {
|
|
64
71
|
// Cannot modify already existing sync implementation
|
|
65
72
|
return;
|
|
66
73
|
}
|
|
67
|
-
|
|
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', () =>
|
|
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', () =>
|
|
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
|
-
|
|
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
|