@powersync/common 0.0.0-dev-20250922105207 → 0.0.0-dev-20250925183014

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 (29) hide show
  1. package/dist/bundle.cjs +4 -4
  2. package/dist/bundle.mjs +5 -5
  3. package/dist/index.d.cts +1047 -838
  4. package/lib/client/AbstractPowerSyncDatabase.d.ts +16 -4
  5. package/lib/client/AbstractPowerSyncDatabase.js +36 -26
  6. package/lib/client/ConnectionManager.d.ts +26 -2
  7. package/lib/client/ConnectionManager.js +114 -2
  8. package/lib/client/SQLOpenFactory.d.ts +0 -2
  9. package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +9 -1
  10. package/lib/client/sync/bucket/BucketStorageAdapter.js +1 -0
  11. package/lib/client/sync/stream/AbstractRemote.js +3 -0
  12. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +24 -3
  13. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +54 -39
  14. package/lib/client/sync/stream/core-instruction.d.ts +20 -1
  15. package/lib/client/sync/stream/core-instruction.js +26 -1
  16. package/lib/client/sync/sync-streams.d.ts +98 -0
  17. package/lib/client/sync/sync-streams.js +1 -0
  18. package/lib/client/triggers/TriggerManager.d.ts +1 -1
  19. package/lib/client/watched/WatchedQuery.d.ts +2 -0
  20. package/lib/client/watched/WatchedQuery.js +1 -0
  21. package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +2 -1
  22. package/lib/client/watched/processors/AbstractQueryProcessor.js +30 -15
  23. package/lib/client/watched/processors/DifferentialQueryProcessor.js +7 -1
  24. package/lib/client/watched/processors/OnChangeQueryProcessor.js +7 -1
  25. package/lib/db/crud/SyncStatus.d.ts +37 -1
  26. package/lib/db/crud/SyncStatus.js +61 -0
  27. package/lib/index.d.ts +1 -0
  28. package/lib/index.js +1 -0
  29. 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,7 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
183
185
  signal?: AbortSignal;
184
186
  priority?: number;
185
187
  }): Promise<void>;
188
+ private waitForStatus;
186
189
  /**
187
190
  * Allows for extended implementations to execute custom initialization
188
191
  * logic as part of the total init process
@@ -194,7 +197,7 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
194
197
  */
195
198
  protected initialize(): Promise<void>;
196
199
  private _loadVersion;
197
- protected updateHasSynced(): Promise<void>;
200
+ protected resolveOfflineSyncStatus(): Promise<void>;
198
201
  /**
199
202
  * 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
203
  *
@@ -206,7 +209,7 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
206
209
  * While initializing is automatic, this helps to catch and report initialization errors.
207
210
  */
208
211
  init(): Promise<void>;
209
- resolvedConnectionOptions(options?: PowerSyncConnectionOptions): RequiredAdditionalConnectionOptions;
212
+ protected resolvedConnectionOptions(options: CreateSyncImplementationOptions): CreateSyncImplementationOptions & RequiredAdditionalConnectionOptions;
210
213
  /**
211
214
  * @deprecated Use {@link AbstractPowerSyncDatabase#close} instead.
212
215
  * Clears all listeners registered by {@link AbstractPowerSyncDatabase#registerListener}.
@@ -236,6 +239,15 @@ export declare abstract class AbstractPowerSyncDatabase extends BaseObserver<Pow
236
239
  * To preserve data in local-only tables, set clearLocal to false.
237
240
  */
238
241
  disconnectAndClear(options?: DisconnectAndClearOptions): Promise<void>;
242
+ /**
243
+ * Create a sync stream to query its status or to subscribe to it.
244
+ *
245
+ * @param name The name of the stream to subscribe to.
246
+ * @param params Optional parameters for the stream subscription.
247
+ * @returns A {@link SyncStream} instance that can be subscribed to.
248
+ * @experimental Sync streams are currently in alpha.
249
+ */
250
+ syncStream(name: string, params?: Record<string, any>): SyncStream;
239
251
  /**
240
252
  * Close the database, releasing resources.
241
253
  *
@@ -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,13 +186,16 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
176
186
  const statusMatches = priority === undefined
177
187
  ? (status) => status.hasSynced
178
188
  : (status) => status.statusForPriority(priority).hasSynced;
179
- if (statusMatches(this.currentStatus)) {
189
+ return this.waitForStatus(statusMatches, signal);
190
+ }
191
+ async waitForStatus(predicate, signal) {
192
+ if (predicate(this.currentStatus)) {
180
193
  return;
181
194
  }
182
195
  return new Promise((resolve) => {
183
196
  const dispose = this.registerListener({
184
197
  statusChanged: (status) => {
185
- if (statusMatches(status)) {
198
+ if (predicate(status)) {
186
199
  dispose();
187
200
  resolve();
188
201
  }
@@ -203,7 +216,7 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
203
216
  await this.bucketStorageAdapter.init();
204
217
  await this._loadVersion();
205
218
  await this.updateSchema(this.options.schema);
206
- await this.updateHasSynced();
219
+ await this.resolveOfflineSyncStatus();
207
220
  await this.database.execute('PRAGMA RECURSIVE_TRIGGERS=TRUE');
208
221
  this.ready = true;
209
222
  this.iterateListeners((cb) => cb.initialized?.());
@@ -223,33 +236,19 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
223
236
  .map((n) => parseInt(n));
224
237
  }
225
238
  catch (e) {
226
- throw new Error(`Unsupported powersync extension version. Need >=0.3.11 <1.0.0, got: ${this.sdkVersion}. Details: ${e.message}`);
239
+ throw new Error(`Unsupported powersync extension version. Need >=0.4.5 <1.0.0, got: ${this.sdkVersion}. Details: ${e.message}`);
227
240
  }
228
- // Validate >=0.3.11 <1.0.0
229
- if (versionInts[0] != 0 || versionInts[1] < 3 || (versionInts[1] == 3 && versionInts[2] < 11)) {
230
- throw new Error(`Unsupported powersync extension version. Need >=0.3.11 <1.0.0, got: ${this.sdkVersion}`);
241
+ // Validate >=0.4.5 <1.0.0
242
+ if (versionInts[0] != 0 || versionInts[1] < 4 || (versionInts[1] == 4 && versionInts[2] < 5)) {
243
+ throw new Error(`Unsupported powersync extension version. Need >=0.4.5 <1.0.0, got: ${this.sdkVersion}`);
231
244
  }
232
245
  }
233
- async updateHasSynced() {
234
- const result = await this.database.getAll('SELECT priority, last_synced_at FROM ps_sync_state ORDER BY priority DESC');
235
- let lastCompleteSync;
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;
246
+ async resolveOfflineSyncStatus() {
247
+ const result = await this.database.get('SELECT powersync_offline_sync_status() as r');
248
+ const parsed = JSON.parse(result.r);
248
249
  const updatedStatus = new SyncStatus({
249
250
  ...this.currentStatus.toJSON(),
250
- hasSynced,
251
- priorityStatusEntries,
252
- lastSyncedAt: lastCompleteSync
251
+ ...coreStatusToJs(parsed)
253
252
  });
254
253
  if (!updatedStatus.isEqual(this.currentStatus)) {
255
254
  this.currentStatus = updatedStatus;
@@ -346,6 +345,17 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
346
345
  this.currentStatus = new SyncStatus({});
347
346
  this.iterateListeners((l) => l.statusChanged?.(this.currentStatus));
348
347
  }
348
+ /**
349
+ * Create a sync stream to query its status or to subscribe to it.
350
+ *
351
+ * @param name The name of the stream to subscribe to.
352
+ * @param params Optional parameters for the stream subscription.
353
+ * @returns A {@link SyncStream} instance that can be subscribed to.
354
+ * @experimental Sync streams are currently in alpha.
355
+ */
356
+ syncStream(name, params) {
357
+ return this.connectionManager.stream(this.subscriptions, name, params ?? null);
358
+ }
349
359
  /**
350
360
  * Close the database, releasing resources.
351
361
  *
@@ -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: InternalConnectionOptions): Promise<ConnectionManagerSyncImplementationResult>;
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,8 @@ 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
+ private get activeStreams();
102
+ private subscriptionsMayHaveChanged;
79
103
  }
80
104
  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: 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, options);
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,107 @@ 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
+ get activeStreams() {
228
+ return [...this.locallyActiveSubscriptions.values()].map((a) => ({ name: a.name, params: a.parameters }));
229
+ }
230
+ subscriptionsMayHaveChanged() {
231
+ if (this.syncStreamImplementation) {
232
+ this.syncStreamImplementation.updateSubscriptions(this.activeStreams);
233
+ }
234
+ }
235
+ }
236
+ class ActiveSubscription {
237
+ name;
238
+ parameters;
239
+ logger;
240
+ waitForFirstSync;
241
+ clearSubscription;
242
+ refcount = 0;
243
+ constructor(name, parameters, logger, waitForFirstSync, clearSubscription) {
244
+ this.name = name;
245
+ this.parameters = parameters;
246
+ this.logger = logger;
247
+ this.waitForFirstSync = waitForFirstSync;
248
+ this.clearSubscription = clearSubscription;
249
+ }
250
+ decrementRefCount() {
251
+ this.refcount--;
252
+ if (this.refcount == 0) {
253
+ this.clearSubscription();
254
+ }
255
+ }
256
+ }
257
+ class SyncStreamSubscriptionHandle {
258
+ subscription;
259
+ active = true;
260
+ constructor(subscription) {
261
+ this.subscription = subscription;
262
+ subscription.refcount++;
263
+ _finalizer?.register(this, subscription);
264
+ }
265
+ get name() {
266
+ return this.subscription.name;
267
+ }
268
+ get parameters() {
269
+ return this.subscription.parameters;
270
+ }
271
+ waitForFirstSync(abort) {
272
+ return this.subscription.waitForFirstSync(abort);
273
+ }
274
+ unsubscribe() {
275
+ if (this.active) {
276
+ this.active = false;
277
+ _finalizer?.unregister(this);
278
+ this.subscription.decrementRefCount();
279
+ }
280
+ }
174
281
  }
282
+ const _finalizer = 'FinalizationRegistry' in globalThis
283
+ ? new FinalizationRegistry((sub) => {
284
+ 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.`);
285
+ })
286
+ : null;
@@ -1,4 +1,3 @@
1
- import { ILogger } from 'js-logger';
2
1
  import { DBAdapter } from '../db/DBAdapter.js';
3
2
  export interface SQLOpenOptions {
4
3
  /**
@@ -22,7 +21,6 @@ export interface SQLOpenOptions {
22
21
  * debugMode: process.env.NODE_ENV !== 'production'
23
22
  */
24
23
  debugMode?: boolean;
25
- logger?: ILogger;
26
24
  }
27
25
  export interface SQLOpenFactory {
28
26
  /**
@@ -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 = {}));
@@ -382,6 +382,9 @@ export class AbstractRemote {
382
382
  * Aborting the active fetch request while it is being consumed seems to throw
383
383
  * an unhandled exception on the window level.
384
384
  */
385
+ if (abortSignal?.aborted) {
386
+ throw new AbortOperation('Abort request received before making postStreamRaw request');
387
+ }
385
388
  const controller = new AbortController();
386
389
  let requestResolved = false;
387
390
  abortSignal?.addEventListener('abort', () => {
@@ -62,8 +62,9 @@ export interface LockOptions<T> {
62
62
  type: LockType;
63
63
  signal?: AbortSignal;
64
64
  }
65
- export interface AbstractStreamingSyncImplementationOptions extends AdditionalConnectionOptions {
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 type RequiredAdditionalConnectionOptions = Required<AdditionalConnectionOptions>;
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<void>;
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 {};