@powersync/common 1.38.0 → 1.39.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.
- package/dist/bundle.cjs +4 -4
- package/dist/bundle.mjs +5 -5
- package/dist/index.d.cts +295 -84
- package/lib/client/AbstractPowerSyncDatabase.d.ts +19 -4
- package/lib/client/AbstractPowerSyncDatabase.js +44 -26
- package/lib/client/ConnectionManager.d.ts +32 -2
- package/lib/client/ConnectionManager.js +115 -2
- package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +9 -1
- package/lib/client/sync/bucket/BucketStorageAdapter.js +1 -0
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +24 -3
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +46 -37
- package/lib/client/sync/stream/core-instruction.d.ts +20 -1
- package/lib/client/sync/stream/core-instruction.js +26 -1
- package/lib/client/sync/sync-streams.d.ts +98 -0
- package/lib/client/sync/sync-streams.js +1 -0
- package/lib/client/triggers/TriggerManager.d.ts +1 -1
- package/lib/client/watched/WatchedQuery.d.ts +2 -0
- package/lib/client/watched/WatchedQuery.js +1 -0
- package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +2 -1
- package/lib/client/watched/processors/AbstractQueryProcessor.js +30 -15
- package/lib/client/watched/processors/DifferentialQueryProcessor.js +3 -0
- package/lib/client/watched/processors/OnChangeQueryProcessor.js +3 -0
- package/lib/db/crud/SyncStatus.d.ts +28 -1
- package/lib/db/crud/SyncStatus.js +52 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/package.json +1 -1
|
@@ -5,7 +5,7 @@ import { SyncStatus } from '../db/crud/SyncStatus.js';
|
|
|
5
5
|
import { UploadQueueStats } from '../db/crud/UploadQueueStatus.js';
|
|
6
6
|
import { Schema } from '../db/schema/Schema.js';
|
|
7
7
|
import { BaseObserver } from '../utils/BaseObserver.js';
|
|
8
|
-
import { ConnectionManager } from './ConnectionManager.js';
|
|
8
|
+
import { ConnectionManager, CreateSyncImplementationOptions } from './ConnectionManager.js';
|
|
9
9
|
import { ArrayQueryDefinition, Query } from './Query.js';
|
|
10
10
|
import { SQLOpenFactory, SQLOpenOptions } from './SQLOpenFactory.js';
|
|
11
11
|
import { PowerSyncBackendConnector } from './connection/PowerSyncBackendConnector.js';
|
|
@@ -16,6 +16,7 @@ import { StreamingSyncImplementation, StreamingSyncImplementationListener, type
|
|
|
16
16
|
import { TriggerManager } from './triggers/TriggerManager.js';
|
|
17
17
|
import { WatchCompatibleQuery } from './watched/WatchedQuery.js';
|
|
18
18
|
import { WatchedQueryComparator } from './watched/processors/comparators.js';
|
|
19
|
+
import { SyncStream } from './sync/sync-streams.js';
|
|
19
20
|
export interface DisconnectAndClearOptions {
|
|
20
21
|
/** When set to false, data in local-only tables is preserved. */
|
|
21
22
|
clearLocal?: boolean;
|
|
@@ -129,6 +130,7 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
|
|
|
129
130
|
protected bucketStorageAdapter: BucketStorageAdapter;
|
|
130
131
|
protected _isReadyPromise: Promise<void>;
|
|
131
132
|
protected connectionManager: ConnectionManager;
|
|
133
|
+
private subscriptions;
|
|
132
134
|
get syncStreamImplementation(): StreamingSyncImplementation | null;
|
|
133
135
|
protected _schema: Schema;
|
|
134
136
|
private _database;
|
|
@@ -164,7 +166,7 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
|
|
|
164
166
|
* Opens the DBAdapter given open options using a default open factory
|
|
165
167
|
*/
|
|
166
168
|
protected abstract openDBAdapter(options: PowerSyncDatabaseOptionsWithSettings): DBAdapter;
|
|
167
|
-
protected abstract generateSyncStreamImplementation(connector: PowerSyncBackendConnector, options: RequiredAdditionalConnectionOptions): StreamingSyncImplementation;
|
|
169
|
+
protected abstract generateSyncStreamImplementation(connector: PowerSyncBackendConnector, options: CreateSyncImplementationOptions & RequiredAdditionalConnectionOptions): StreamingSyncImplementation;
|
|
168
170
|
protected abstract generateBucketStorageAdapter(): BucketStorageAdapter;
|
|
169
171
|
/**
|
|
170
172
|
* @returns A promise which will resolve once initialization is completed.
|
|
@@ -183,6 +185,10 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
|
|
|
183
185
|
signal?: AbortSignal;
|
|
184
186
|
priority?: number;
|
|
185
187
|
}): Promise<void>;
|
|
188
|
+
/**
|
|
189
|
+
* Waits for the first sync status for which the `status` callback returns a truthy value.
|
|
190
|
+
*/
|
|
191
|
+
waitForStatus(predicate: (status: SyncStatus) => any, signal?: AbortSignal): Promise<void>;
|
|
186
192
|
/**
|
|
187
193
|
* Allows for extended implementations to execute custom initialization
|
|
188
194
|
* logic as part of the total init process
|
|
@@ -194,7 +200,7 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
|
|
|
194
200
|
*/
|
|
195
201
|
protected initialize(): Promise<void>;
|
|
196
202
|
private _loadVersion;
|
|
197
|
-
protected
|
|
203
|
+
protected resolveOfflineSyncStatus(): Promise<void>;
|
|
198
204
|
/**
|
|
199
205
|
* Replace the schema with a new version. This is for advanced use cases - typically the schema should just be specified once in the constructor.
|
|
200
206
|
*
|
|
@@ -206,7 +212,7 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
|
|
|
206
212
|
* While initializing is automatic, this helps to catch and report initialization errors.
|
|
207
213
|
*/
|
|
208
214
|
init(): Promise<void>;
|
|
209
|
-
resolvedConnectionOptions(options
|
|
215
|
+
protected resolvedConnectionOptions(options: CreateSyncImplementationOptions): CreateSyncImplementationOptions & RequiredAdditionalConnectionOptions;
|
|
210
216
|
/**
|
|
211
217
|
* @deprecated Use {@link AbstractPowerSyncDatabase#close} instead.
|
|
212
218
|
* Clears all listeners registered by {@link AbstractPowerSyncDatabase#registerListener}.
|
|
@@ -236,6 +242,15 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
|
|
|
236
242
|
* To preserve data in local-only tables, set clearLocal to false.
|
|
237
243
|
*/
|
|
238
244
|
disconnectAndClear(options?: DisconnectAndClearOptions): Promise<void>;
|
|
245
|
+
/**
|
|
246
|
+
* Create a sync stream to query its status or to subscribe to it.
|
|
247
|
+
*
|
|
248
|
+
* @param name The name of the stream to subscribe to.
|
|
249
|
+
* @param params Optional parameters for the stream subscription.
|
|
250
|
+
* @returns A {@link SyncStream} instance that can be subscribed to.
|
|
251
|
+
* @experimental Sync streams are currently in alpha.
|
|
252
|
+
*/
|
|
253
|
+
syncStream(name: string, params?: Record<string, any>): SyncStream;
|
|
239
254
|
/**
|
|
240
255
|
* Close the database, releasing resources.
|
|
241
256
|
*
|
|
@@ -2,7 +2,6 @@ import { Mutex } from 'async-mutex';
|
|
|
2
2
|
import { EventIterator } from 'event-iterator';
|
|
3
3
|
import Logger from 'js-logger';
|
|
4
4
|
import { isBatchedUpdateNotification } from '../db/DBAdapter.js';
|
|
5
|
-
import { FULL_SYNC_PRIORITY } from '../db/crud/SyncProgress.js';
|
|
6
5
|
import { SyncStatus } from '../db/crud/SyncStatus.js';
|
|
7
6
|
import { UploadQueueStats } from '../db/crud/UploadQueueStatus.js';
|
|
8
7
|
import { BaseObserver } from '../utils/BaseObserver.js';
|
|
@@ -19,6 +18,7 @@ import { DEFAULT_CRUD_UPLOAD_THROTTLE_MS, DEFAULT_RETRY_DELAY_MS } from './sync/
|
|
|
19
18
|
import { TriggerManagerImpl } from './triggers/TriggerManagerImpl.js';
|
|
20
19
|
import { DEFAULT_WATCH_THROTTLE_MS } from './watched/WatchedQuery.js';
|
|
21
20
|
import { OnChangeQueryProcessor } from './watched/processors/OnChangeQueryProcessor.js';
|
|
21
|
+
import { coreStatusToJs } from './sync/stream/core-instruction.js';
|
|
22
22
|
const POWERSYNC_TABLE_MATCH = /(^ps_data__|^ps_data_local__)/;
|
|
23
23
|
const DEFAULT_DISCONNECT_CLEAR_OPTIONS = {
|
|
24
24
|
clearLocal: true
|
|
@@ -59,6 +59,7 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
59
59
|
bucketStorageAdapter;
|
|
60
60
|
_isReadyPromise;
|
|
61
61
|
connectionManager;
|
|
62
|
+
subscriptions;
|
|
62
63
|
get syncStreamImplementation() {
|
|
63
64
|
return this.connectionManager.syncStreamImplementation;
|
|
64
65
|
}
|
|
@@ -100,6 +101,15 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
100
101
|
this.sdkVersion = '';
|
|
101
102
|
this.runExclusiveMutex = new Mutex();
|
|
102
103
|
// Start async init
|
|
104
|
+
this.subscriptions = {
|
|
105
|
+
firstStatusMatching: (predicate, abort) => this.waitForStatus(predicate, abort),
|
|
106
|
+
resolveOfflineSyncStatus: () => this.resolveOfflineSyncStatus(),
|
|
107
|
+
rustSubscriptionsCommand: async (payload) => {
|
|
108
|
+
await this.writeTransaction((tx) => {
|
|
109
|
+
return tx.execute('select powersync_control(?,?)', ['subscriptions', JSON.stringify(payload)]);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
};
|
|
103
113
|
this.connectionManager = new ConnectionManager({
|
|
104
114
|
createSyncImplementation: async (connector, options) => {
|
|
105
115
|
await this.waitForReady();
|
|
@@ -176,22 +186,33 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
176
186
|
const statusMatches = priority === undefined
|
|
177
187
|
? (status) => status.hasSynced
|
|
178
188
|
: (status) => status.statusForPriority(priority).hasSynced;
|
|
179
|
-
|
|
189
|
+
return this.waitForStatus(statusMatches, signal);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Waits for the first sync status for which the `status` callback returns a truthy value.
|
|
193
|
+
*/
|
|
194
|
+
async waitForStatus(predicate, signal) {
|
|
195
|
+
if (predicate(this.currentStatus)) {
|
|
180
196
|
return;
|
|
181
197
|
}
|
|
182
198
|
return new Promise((resolve) => {
|
|
183
199
|
const dispose = this.registerListener({
|
|
184
200
|
statusChanged: (status) => {
|
|
185
|
-
if (
|
|
186
|
-
|
|
187
|
-
resolve();
|
|
201
|
+
if (predicate(status)) {
|
|
202
|
+
abort();
|
|
188
203
|
}
|
|
189
204
|
}
|
|
190
205
|
});
|
|
191
|
-
|
|
206
|
+
function abort() {
|
|
192
207
|
dispose();
|
|
193
208
|
resolve();
|
|
194
|
-
}
|
|
209
|
+
}
|
|
210
|
+
if (signal?.aborted) {
|
|
211
|
+
abort();
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
signal?.addEventListener('abort', abort);
|
|
215
|
+
}
|
|
195
216
|
});
|
|
196
217
|
}
|
|
197
218
|
/**
|
|
@@ -203,7 +224,7 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
203
224
|
await this.bucketStorageAdapter.init();
|
|
204
225
|
await this._loadVersion();
|
|
205
226
|
await this.updateSchema(this.options.schema);
|
|
206
|
-
await this.
|
|
227
|
+
await this.resolveOfflineSyncStatus();
|
|
207
228
|
await this.database.execute('PRAGMA RECURSIVE_TRIGGERS=TRUE');
|
|
208
229
|
this.ready = true;
|
|
209
230
|
this.iterateListeners((cb) => cb.initialized?.());
|
|
@@ -230,26 +251,12 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
230
251
|
throw new Error(`Unsupported powersync extension version. Need >=0.4.5 <1.0.0, got: ${this.sdkVersion}`);
|
|
231
252
|
}
|
|
232
253
|
}
|
|
233
|
-
async
|
|
234
|
-
const result = await this.database.
|
|
235
|
-
|
|
236
|
-
const priorityStatusEntries = [];
|
|
237
|
-
for (const { priority, last_synced_at } of result) {
|
|
238
|
-
const parsedDate = new Date(last_synced_at + 'Z');
|
|
239
|
-
if (priority == FULL_SYNC_PRIORITY) {
|
|
240
|
-
// This lowest-possible priority represents a complete sync.
|
|
241
|
-
lastCompleteSync = parsedDate;
|
|
242
|
-
}
|
|
243
|
-
else {
|
|
244
|
-
priorityStatusEntries.push({ priority, hasSynced: true, lastSyncedAt: parsedDate });
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
const hasSynced = lastCompleteSync != null;
|
|
254
|
+
async resolveOfflineSyncStatus() {
|
|
255
|
+
const result = await this.database.get('SELECT powersync_offline_sync_status() as r');
|
|
256
|
+
const parsed = JSON.parse(result.r);
|
|
248
257
|
const updatedStatus = new SyncStatus({
|
|
249
258
|
...this.currentStatus.toJSON(),
|
|
250
|
-
|
|
251
|
-
priorityStatusEntries,
|
|
252
|
-
lastSyncedAt: lastCompleteSync
|
|
259
|
+
...coreStatusToJs(parsed)
|
|
253
260
|
});
|
|
254
261
|
if (!updatedStatus.isEqual(this.currentStatus)) {
|
|
255
262
|
this.currentStatus = updatedStatus;
|
|
@@ -346,6 +353,17 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
|
|
|
346
353
|
this.currentStatus = new SyncStatus({});
|
|
347
354
|
this.iterateListeners((l) => l.statusChanged?.(this.currentStatus));
|
|
348
355
|
}
|
|
356
|
+
/**
|
|
357
|
+
* Create a sync stream to query its status or to subscribe to it.
|
|
358
|
+
*
|
|
359
|
+
* @param name The name of the stream to subscribe to.
|
|
360
|
+
* @param params Optional parameters for the stream subscription.
|
|
361
|
+
* @returns A {@link SyncStream} instance that can be subscribed to.
|
|
362
|
+
* @experimental Sync streams are currently in alpha.
|
|
363
|
+
*/
|
|
364
|
+
syncStream(name, params) {
|
|
365
|
+
return this.connectionManager.stream(this.subscriptions, name, params ?? null);
|
|
366
|
+
}
|
|
349
367
|
/**
|
|
350
368
|
* Close the database, releasing resources.
|
|
351
369
|
*
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { ILogger } from 'js-logger';
|
|
2
2
|
import { BaseListener, BaseObserver } from '../utils/BaseObserver.js';
|
|
3
3
|
import { PowerSyncBackendConnector } from './connection/PowerSyncBackendConnector.js';
|
|
4
|
-
import { InternalConnectionOptions, StreamingSyncImplementation } from './sync/stream/AbstractStreamingSyncImplementation.js';
|
|
4
|
+
import { AdditionalConnectionOptions, InternalConnectionOptions, StreamingSyncImplementation, SubscribedStream } from './sync/stream/AbstractStreamingSyncImplementation.js';
|
|
5
|
+
import { SyncStream } from './sync/sync-streams.js';
|
|
6
|
+
import { SyncStatus } from '../db/crud/SyncStatus.js';
|
|
5
7
|
/**
|
|
6
8
|
* @internal
|
|
7
9
|
*/
|
|
@@ -13,11 +15,24 @@ export interface ConnectionManagerSyncImplementationResult {
|
|
|
13
15
|
*/
|
|
14
16
|
onDispose: () => Promise<void> | void;
|
|
15
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* The subset of {@link AbstractStreamingSyncImplementationOptions} managed by the connection manager.
|
|
20
|
+
*
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export interface CreateSyncImplementationOptions extends AdditionalConnectionOptions {
|
|
24
|
+
subscriptions: SubscribedStream[];
|
|
25
|
+
}
|
|
26
|
+
export interface InternalSubscriptionAdapter {
|
|
27
|
+
firstStatusMatching(predicate: (status: SyncStatus) => any, abort?: AbortSignal): Promise<void>;
|
|
28
|
+
resolveOfflineSyncStatus(): Promise<void>;
|
|
29
|
+
rustSubscriptionsCommand(payload: any): Promise<void>;
|
|
30
|
+
}
|
|
16
31
|
/**
|
|
17
32
|
* @internal
|
|
18
33
|
*/
|
|
19
34
|
export interface ConnectionManagerOptions {
|
|
20
|
-
createSyncImplementation(connector: PowerSyncBackendConnector, options:
|
|
35
|
+
createSyncImplementation(connector: PowerSyncBackendConnector, options: CreateSyncImplementationOptions): Promise<ConnectionManagerSyncImplementationResult>;
|
|
21
36
|
logger: ILogger;
|
|
22
37
|
}
|
|
23
38
|
type StoredConnectionOptions = {
|
|
@@ -63,6 +78,12 @@ export declare class ConnectionManager extends BaseObserver<ConnectionManagerLis
|
|
|
63
78
|
* is disposed.
|
|
64
79
|
*/
|
|
65
80
|
protected syncDisposer: (() => Promise<void> | void) | null;
|
|
81
|
+
/**
|
|
82
|
+
* Subscriptions managed in this connection manager.
|
|
83
|
+
*
|
|
84
|
+
* On the web, these local subscriptions are merged across tabs by a shared worker.
|
|
85
|
+
*/
|
|
86
|
+
private locallyActiveSubscriptions;
|
|
66
87
|
constructor(options: ConnectionManagerOptions);
|
|
67
88
|
get logger(): ILogger;
|
|
68
89
|
close(): Promise<void>;
|
|
@@ -76,5 +97,14 @@ export declare class ConnectionManager extends BaseObserver<ConnectionManagerLis
|
|
|
76
97
|
disconnect(): Promise<void>;
|
|
77
98
|
protected disconnectInternal(): Promise<void>;
|
|
78
99
|
protected performDisconnect(): Promise<void>;
|
|
100
|
+
stream(adapter: InternalSubscriptionAdapter, name: string, parameters: Record<string, any> | null): SyncStream;
|
|
101
|
+
/**
|
|
102
|
+
* @internal exposed for testing
|
|
103
|
+
*/
|
|
104
|
+
get activeStreams(): {
|
|
105
|
+
name: string;
|
|
106
|
+
params: Record<string, any> | null;
|
|
107
|
+
}[];
|
|
108
|
+
private subscriptionsMayHaveChanged;
|
|
79
109
|
}
|
|
80
110
|
export {};
|
|
@@ -32,6 +32,12 @@ export class ConnectionManager extends BaseObserver {
|
|
|
32
32
|
* is disposed.
|
|
33
33
|
*/
|
|
34
34
|
syncDisposer;
|
|
35
|
+
/**
|
|
36
|
+
* Subscriptions managed in this connection manager.
|
|
37
|
+
*
|
|
38
|
+
* On the web, these local subscriptions are merged across tabs by a shared worker.
|
|
39
|
+
*/
|
|
40
|
+
locallyActiveSubscriptions = new Map();
|
|
35
41
|
constructor(options) {
|
|
36
42
|
super();
|
|
37
43
|
this.options = options;
|
|
@@ -55,7 +61,7 @@ export class ConnectionManager extends BaseObserver {
|
|
|
55
61
|
// Update pending options to the latest values
|
|
56
62
|
this.pendingConnectionOptions = {
|
|
57
63
|
connector,
|
|
58
|
-
options
|
|
64
|
+
options
|
|
59
65
|
};
|
|
60
66
|
// Disconnecting here provides aborting in progress connection attempts.
|
|
61
67
|
// The connectInternal method will clear pending options once it starts connecting (with the options).
|
|
@@ -114,7 +120,10 @@ export class ConnectionManager extends BaseObserver {
|
|
|
114
120
|
const { connector, options } = this.pendingConnectionOptions;
|
|
115
121
|
appliedOptions = options;
|
|
116
122
|
this.pendingConnectionOptions = null;
|
|
117
|
-
const { sync, onDispose } = await this.options.createSyncImplementation(connector,
|
|
123
|
+
const { sync, onDispose } = await this.options.createSyncImplementation(connector, {
|
|
124
|
+
subscriptions: this.activeStreams,
|
|
125
|
+
...options
|
|
126
|
+
});
|
|
118
127
|
this.iterateListeners((l) => l.syncStreamCreated?.(sync));
|
|
119
128
|
this.syncStreamImplementation = sync;
|
|
120
129
|
this.syncDisposer = onDispose;
|
|
@@ -171,4 +180,108 @@ export class ConnectionManager extends BaseObserver {
|
|
|
171
180
|
await sync?.dispose();
|
|
172
181
|
await disposer?.();
|
|
173
182
|
}
|
|
183
|
+
stream(adapter, name, parameters) {
|
|
184
|
+
const desc = { name, parameters };
|
|
185
|
+
const waitForFirstSync = (abort) => {
|
|
186
|
+
return adapter.firstStatusMatching((s) => s.forStream(desc)?.subscription.hasSynced, abort);
|
|
187
|
+
};
|
|
188
|
+
return {
|
|
189
|
+
...desc,
|
|
190
|
+
subscribe: async (options) => {
|
|
191
|
+
// NOTE: We also run this command if a subscription already exists, because this increases the expiry date
|
|
192
|
+
// (relevant if the app is closed before connecting again, where the last subscribe call determines the ttl).
|
|
193
|
+
await adapter.rustSubscriptionsCommand({
|
|
194
|
+
subscribe: {
|
|
195
|
+
stream: {
|
|
196
|
+
name,
|
|
197
|
+
params: parameters
|
|
198
|
+
},
|
|
199
|
+
ttl: options?.ttl,
|
|
200
|
+
priority: options?.priority
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
if (!this.syncStreamImplementation) {
|
|
204
|
+
// We're not connected. So, update the offline sync status to reflect the new subscription.
|
|
205
|
+
// (With an active iteration, the sync client would include it in its state).
|
|
206
|
+
await adapter.resolveOfflineSyncStatus();
|
|
207
|
+
}
|
|
208
|
+
const key = `${name}|${JSON.stringify(parameters)}`;
|
|
209
|
+
let subscription = this.locallyActiveSubscriptions.get(key);
|
|
210
|
+
if (subscription == null) {
|
|
211
|
+
const clearSubscription = () => {
|
|
212
|
+
this.locallyActiveSubscriptions.delete(key);
|
|
213
|
+
this.subscriptionsMayHaveChanged();
|
|
214
|
+
};
|
|
215
|
+
subscription = new ActiveSubscription(name, parameters, this.logger, waitForFirstSync, clearSubscription);
|
|
216
|
+
this.locallyActiveSubscriptions.set(key, subscription);
|
|
217
|
+
this.subscriptionsMayHaveChanged();
|
|
218
|
+
}
|
|
219
|
+
return new SyncStreamSubscriptionHandle(subscription);
|
|
220
|
+
},
|
|
221
|
+
unsubscribeAll: async () => {
|
|
222
|
+
await adapter.rustSubscriptionsCommand({ unsubscribe: { name, params: parameters } });
|
|
223
|
+
this.subscriptionsMayHaveChanged();
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* @internal exposed for testing
|
|
229
|
+
*/
|
|
230
|
+
get activeStreams() {
|
|
231
|
+
return [...this.locallyActiveSubscriptions.values()].map((a) => ({ name: a.name, params: a.parameters }));
|
|
232
|
+
}
|
|
233
|
+
subscriptionsMayHaveChanged() {
|
|
234
|
+
this.syncStreamImplementation?.updateSubscriptions(this.activeStreams);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
class ActiveSubscription {
|
|
238
|
+
name;
|
|
239
|
+
parameters;
|
|
240
|
+
logger;
|
|
241
|
+
waitForFirstSync;
|
|
242
|
+
clearSubscription;
|
|
243
|
+
refcount = 0;
|
|
244
|
+
constructor(name, parameters, logger, waitForFirstSync, clearSubscription) {
|
|
245
|
+
this.name = name;
|
|
246
|
+
this.parameters = parameters;
|
|
247
|
+
this.logger = logger;
|
|
248
|
+
this.waitForFirstSync = waitForFirstSync;
|
|
249
|
+
this.clearSubscription = clearSubscription;
|
|
250
|
+
}
|
|
251
|
+
decrementRefCount() {
|
|
252
|
+
this.refcount--;
|
|
253
|
+
if (this.refcount == 0) {
|
|
254
|
+
this.clearSubscription();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
class SyncStreamSubscriptionHandle {
|
|
259
|
+
subscription;
|
|
260
|
+
active = true;
|
|
261
|
+
constructor(subscription) {
|
|
262
|
+
this.subscription = subscription;
|
|
263
|
+
subscription.refcount++;
|
|
264
|
+
_finalizer?.register(this, subscription);
|
|
265
|
+
}
|
|
266
|
+
get name() {
|
|
267
|
+
return this.subscription.name;
|
|
268
|
+
}
|
|
269
|
+
get parameters() {
|
|
270
|
+
return this.subscription.parameters;
|
|
271
|
+
}
|
|
272
|
+
waitForFirstSync(abort) {
|
|
273
|
+
return this.subscription.waitForFirstSync(abort);
|
|
274
|
+
}
|
|
275
|
+
unsubscribe() {
|
|
276
|
+
if (this.active) {
|
|
277
|
+
this.active = false;
|
|
278
|
+
_finalizer?.unregister(this);
|
|
279
|
+
this.subscription.decrementRefCount();
|
|
280
|
+
}
|
|
281
|
+
}
|
|
174
282
|
}
|
|
283
|
+
const _finalizer = 'FinalizationRegistry' in globalThis
|
|
284
|
+
? new FinalizationRegistry((sub) => {
|
|
285
|
+
sub.logger.warn(`A subscription to ${sub.name} with params ${JSON.stringify(sub.parameters)} leaked! Please ensure calling unsubscribe() when you don't need a subscription anymore. For global subscriptions, consider storing them in global fields to avoid this warning.`);
|
|
286
|
+
})
|
|
287
|
+
: null;
|
|
@@ -10,6 +10,7 @@ export interface Checkpoint {
|
|
|
10
10
|
last_op_id: OpId;
|
|
11
11
|
buckets: BucketChecksum[];
|
|
12
12
|
write_checkpoint?: string;
|
|
13
|
+
streams?: any[];
|
|
13
14
|
}
|
|
14
15
|
export interface BucketState {
|
|
15
16
|
bucket: string;
|
|
@@ -43,6 +44,12 @@ export interface BucketChecksum {
|
|
|
43
44
|
* Count of operations - informational only.
|
|
44
45
|
*/
|
|
45
46
|
count?: number;
|
|
47
|
+
/**
|
|
48
|
+
* The JavaScript client does not use this field, which is why it's defined to be `any`. We rely on the structure of
|
|
49
|
+
* this interface to pass custom `BucketChecksum`s to the Rust client in unit tests, which so all fields need to be
|
|
50
|
+
* present.
|
|
51
|
+
*/
|
|
52
|
+
subscriptions?: any;
|
|
46
53
|
}
|
|
47
54
|
export declare enum PSInternalTable {
|
|
48
55
|
DATA = "ps_data",
|
|
@@ -57,7 +64,8 @@ export declare enum PowerSyncControlCommand {
|
|
|
57
64
|
STOP = "stop",
|
|
58
65
|
START = "start",
|
|
59
66
|
NOTIFY_TOKEN_REFRESHED = "refreshed_token",
|
|
60
|
-
NOTIFY_CRUD_UPLOAD_COMPLETED = "completed_upload"
|
|
67
|
+
NOTIFY_CRUD_UPLOAD_COMPLETED = "completed_upload",
|
|
68
|
+
UPDATE_SUBSCRIPTIONS = "update_subscriptions"
|
|
61
69
|
}
|
|
62
70
|
export interface BucketStorageListener extends BaseListener {
|
|
63
71
|
crudUpdate: () => void;
|
|
@@ -14,4 +14,5 @@ export var PowerSyncControlCommand;
|
|
|
14
14
|
PowerSyncControlCommand["START"] = "start";
|
|
15
15
|
PowerSyncControlCommand["NOTIFY_TOKEN_REFRESHED"] = "refreshed_token";
|
|
16
16
|
PowerSyncControlCommand["NOTIFY_CRUD_UPLOAD_COMPLETED"] = "completed_upload";
|
|
17
|
+
PowerSyncControlCommand["UPDATE_SUBSCRIPTIONS"] = "update_subscriptions";
|
|
17
18
|
})(PowerSyncControlCommand || (PowerSyncControlCommand = {}));
|
|
@@ -62,8 +62,9 @@ export interface LockOptions<T> {
|
|
|
62
62
|
type: LockType;
|
|
63
63
|
signal?: AbortSignal;
|
|
64
64
|
}
|
|
65
|
-
export interface AbstractStreamingSyncImplementationOptions extends
|
|
65
|
+
export interface AbstractStreamingSyncImplementationOptions extends RequiredAdditionalConnectionOptions {
|
|
66
66
|
adapter: BucketStorageAdapter;
|
|
67
|
+
subscriptions: SubscribedStream[];
|
|
67
68
|
uploadCrud: () => Promise<void>;
|
|
68
69
|
/**
|
|
69
70
|
* An identifier for which PowerSync DB this sync implementation is
|
|
@@ -115,6 +116,12 @@ export interface BaseConnectionOptions {
|
|
|
115
116
|
* These parameters are passed to the sync rules, and will be available under the`user_parameters` object.
|
|
116
117
|
*/
|
|
117
118
|
params?: Record<string, StreamingSyncRequestParameterType>;
|
|
119
|
+
/**
|
|
120
|
+
* Whether to include streams that have `auto_subscribe: true` in their definition.
|
|
121
|
+
*
|
|
122
|
+
* This defaults to `true`.
|
|
123
|
+
*/
|
|
124
|
+
includeDefaultStreams?: boolean;
|
|
118
125
|
/**
|
|
119
126
|
* The serialized schema - mainly used to forward information about raw tables to the sync client.
|
|
120
127
|
*/
|
|
@@ -135,7 +142,9 @@ export interface AdditionalConnectionOptions {
|
|
|
135
142
|
crudUploadThrottleMs?: number;
|
|
136
143
|
}
|
|
137
144
|
/** @internal */
|
|
138
|
-
export
|
|
145
|
+
export interface RequiredAdditionalConnectionOptions extends Required<AdditionalConnectionOptions> {
|
|
146
|
+
subscriptions: SubscribedStream[];
|
|
147
|
+
}
|
|
139
148
|
export interface StreamingSyncImplementation extends BaseObserverInterface<StreamingSyncImplementationListener>, Disposable {
|
|
140
149
|
/**
|
|
141
150
|
* Connects to the sync service
|
|
@@ -155,6 +164,7 @@ export interface StreamingSyncImplementation extends BaseObserverInterface<Strea
|
|
|
155
164
|
waitForReady(): Promise<void>;
|
|
156
165
|
waitForStatus(status: SyncStatusOptions): Promise<void>;
|
|
157
166
|
waitUntilStatusMatches(predicate: (status: SyncStatus) => boolean): Promise<void>;
|
|
167
|
+
updateSubscriptions(subscriptions: SubscribedStream[]): void;
|
|
158
168
|
}
|
|
159
169
|
export declare const DEFAULT_CRUD_UPLOAD_THROTTLE_MS = 1000;
|
|
160
170
|
export declare const DEFAULT_RETRY_DELAY_MS = 5000;
|
|
@@ -164,6 +174,10 @@ export declare const DEFAULT_STREAMING_SYNC_OPTIONS: {
|
|
|
164
174
|
};
|
|
165
175
|
export type RequiredPowerSyncConnectionOptions = Required<BaseConnectionOptions>;
|
|
166
176
|
export declare const DEFAULT_STREAM_CONNECTION_OPTIONS: RequiredPowerSyncConnectionOptions;
|
|
177
|
+
export type SubscribedStream = {
|
|
178
|
+
name: string;
|
|
179
|
+
params: Record<string, any> | null;
|
|
180
|
+
};
|
|
167
181
|
export declare abstract class AbstractStreamingSyncImplementation extends BaseObserver<StreamingSyncImplementationListener> implements StreamingSyncImplementation {
|
|
168
182
|
protected _lastSyncedAt: Date | null;
|
|
169
183
|
protected options: AbstractStreamingSyncImplementationOptions;
|
|
@@ -172,8 +186,10 @@ export declare abstract class AbstractStreamingSyncImplementation extends BaseOb
|
|
|
172
186
|
protected crudUpdateListener?: () => void;
|
|
173
187
|
protected streamingSyncPromise?: Promise<void>;
|
|
174
188
|
protected logger: ILogger;
|
|
189
|
+
private activeStreams;
|
|
175
190
|
private isUploadingCrud;
|
|
176
191
|
private notifyCompletedUploads?;
|
|
192
|
+
private handleActiveStreamsChange?;
|
|
177
193
|
syncStatus: SyncStatus;
|
|
178
194
|
triggerCrudUpload: () => void;
|
|
179
195
|
constructor(options: AbstractStreamingSyncImplementationOptions);
|
|
@@ -210,11 +226,16 @@ export declare abstract class AbstractStreamingSyncImplementation extends BaseOb
|
|
|
210
226
|
* @returns Whether the database is now using the new, fixed subkey format.
|
|
211
227
|
*/
|
|
212
228
|
private requireKeyFormat;
|
|
213
|
-
protected streamingSyncIteration(signal: AbortSignal, options?: PowerSyncConnectionOptions): Promise<
|
|
229
|
+
protected streamingSyncIteration(signal: AbortSignal, options?: PowerSyncConnectionOptions): Promise<RustIterationResult | null>;
|
|
214
230
|
private legacyStreamingSyncIteration;
|
|
215
231
|
private rustSyncIteration;
|
|
216
232
|
private updateSyncStatusForStartingCheckpoint;
|
|
217
233
|
private applyCheckpoint;
|
|
218
234
|
protected updateSyncStatus(options: SyncStatusOptions): void;
|
|
219
235
|
private delayRetry;
|
|
236
|
+
updateSubscriptions(subscriptions: SubscribedStream[]): void;
|
|
237
|
+
}
|
|
238
|
+
interface RustIterationResult {
|
|
239
|
+
immediateRestart: boolean;
|
|
220
240
|
}
|
|
241
|
+
export {};
|