@powersync/common 1.37.0 → 1.38.1

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.
@@ -223,11 +223,11 @@ export class AbstractPowerSyncDatabase extends BaseObserver {
223
223
  .map((n) => parseInt(n));
224
224
  }
225
225
  catch (e) {
226
- throw new Error(`Unsupported powersync extension version. Need >=0.3.11 <1.0.0, got: ${this.sdkVersion}. Details: ${e.message}`);
226
+ throw new Error(`Unsupported powersync extension version. Need >=0.4.5 <1.0.0, got: ${this.sdkVersion}. Details: ${e.message}`);
227
227
  }
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}`);
228
+ // Validate >=0.4.5 <1.0.0
229
+ if (versionInts[0] != 0 || versionInts[1] < 4 || (versionInts[1] == 4 && versionInts[2] < 5)) {
230
+ throw new Error(`Unsupported powersync extension version. Need >=0.4.5 <1.0.0, got: ${this.sdkVersion}`);
231
231
  }
232
232
  }
233
233
  async updateHasSynced() {
@@ -439,7 +439,9 @@ The next upload iteration will be delayed.`);
439
439
  ...DEFAULT_STREAM_CONNECTION_OPTIONS,
440
440
  ...(options ?? {})
441
441
  };
442
- if (resolvedOptions.clientImplementation == SyncClientImplementation.JAVASCRIPT) {
442
+ const clientImplementation = resolvedOptions.clientImplementation;
443
+ this.updateSyncStatus({ clientImplementation });
444
+ if (clientImplementation == SyncClientImplementation.JAVASCRIPT) {
443
445
  await this.legacyStreamingSyncIteration(signal, resolvedOptions);
444
446
  }
445
447
  else {
@@ -937,7 +939,8 @@ The next upload iteration will be delayed.`);
937
939
  ...this.syncStatus.dataFlowStatus,
938
940
  ...options.dataFlow
939
941
  },
940
- priorityStatusEntries: options.priorityStatusEntries ?? this.syncStatus.priorityStatusEntries
942
+ priorityStatusEntries: options.priorityStatusEntries ?? this.syncStatus.priorityStatusEntries,
943
+ clientImplementation: options.clientImplementation ?? this.syncStatus.clientImplementation
941
944
  });
942
945
  if (!this.syncStatus.isEqual(updatedStatus)) {
943
946
  this.syncStatus = updatedStatus;
@@ -323,7 +323,7 @@ export interface TriggerManager {
323
323
  * },
324
324
  * onChange: async (context) => {
325
325
  * // Fetches the todo records that were inserted during this diff
326
- * const newTodos = await context.getAll<Database['todos']>(`
326
+ * const newTodos = await context.withDiff<Database['todos']>(`
327
327
  * SELECT
328
328
  * todos.*
329
329
  * FROM
@@ -65,12 +65,14 @@ export declare enum WatchedQueryListenerEvent {
65
65
  ON_DATA = "onData",
66
66
  ON_ERROR = "onError",
67
67
  ON_STATE_CHANGE = "onStateChange",
68
+ SETTINGS_WILL_UPDATE = "settingsWillUpdate",
68
69
  CLOSED = "closed"
69
70
  }
70
71
  export interface WatchedQueryListener<Data> extends BaseListener {
71
72
  [WatchedQueryListenerEvent.ON_DATA]?: (data: Data) => void | Promise<void>;
72
73
  [WatchedQueryListenerEvent.ON_ERROR]?: (error: Error) => void | Promise<void>;
73
74
  [WatchedQueryListenerEvent.ON_STATE_CHANGE]?: (state: WatchedQueryState<Data>) => void | Promise<void>;
75
+ [WatchedQueryListenerEvent.SETTINGS_WILL_UPDATE]?: () => void;
74
76
  [WatchedQueryListenerEvent.CLOSED]?: () => void | Promise<void>;
75
77
  }
76
78
  export declare const DEFAULT_WATCH_THROTTLE_MS = 30;
@@ -3,6 +3,7 @@ export var WatchedQueryListenerEvent;
3
3
  WatchedQueryListenerEvent["ON_DATA"] = "onData";
4
4
  WatchedQueryListenerEvent["ON_ERROR"] = "onError";
5
5
  WatchedQueryListenerEvent["ON_STATE_CHANGE"] = "onStateChange";
6
+ WatchedQueryListenerEvent["SETTINGS_WILL_UPDATE"] = "settingsWillUpdate";
6
7
  WatchedQueryListenerEvent["CLOSED"] = "closed";
7
8
  })(WatchedQueryListenerEvent || (WatchedQueryListenerEvent = {}));
8
9
  export const DEFAULT_WATCH_THROTTLE_MS = 30;
@@ -40,6 +40,7 @@ export declare abstract class AbstractQueryProcessor<Data = unknown[], Settings
40
40
  constructor(options: AbstractQueryProcessorOptions<Data, Settings>);
41
41
  protected constructInitialState(): WatchedQueryState<Data>;
42
42
  protected get reportFetching(): boolean;
43
+ protected updateSettingsInternal(settings: Settings, signal: AbortSignal): Promise<void>;
43
44
  /**
44
45
  * Updates the underlying query.
45
46
  */
@@ -53,7 +54,7 @@ export declare abstract class AbstractQueryProcessor<Data = unknown[], Settings
53
54
  /**
54
55
  * Configures base DB listeners and links the query to listeners.
55
56
  */
56
- protected init(): Promise<void>;
57
+ protected init(signal: AbortSignal): Promise<void>;
57
58
  close(): Promise<void>;
58
59
  /**
59
60
  * Runs a callback and reports errors to the error listeners.
@@ -1,4 +1,5 @@
1
1
  import { MetaBaseObserver } from '../../../utils/MetaBaseObserver.js';
2
+ import { WatchedQueryListenerEvent } from '../WatchedQuery.js';
2
3
  /**
3
4
  * Performs underlying watching and yields a stream of results.
4
5
  * @internal
@@ -20,7 +21,7 @@ export class AbstractQueryProcessor extends MetaBaseObserver {
20
21
  this._closed = false;
21
22
  this.state = this.constructInitialState();
22
23
  this.disposeListeners = null;
23
- this.initialized = this.init();
24
+ this.initialized = this.init(this.abortController.signal);
24
25
  }
25
26
  constructInitialState() {
26
27
  return {
@@ -34,25 +35,40 @@ export class AbstractQueryProcessor extends MetaBaseObserver {
34
35
  get reportFetching() {
35
36
  return this.options.watchOptions.reportFetching ?? true;
36
37
  }
37
- /**
38
- * Updates the underlying query.
39
- */
40
- async updateSettings(settings) {
41
- await this.initialized;
38
+ async updateSettingsInternal(settings, signal) {
39
+ // This may have been aborted while awaiting or if multiple calls to `updateSettings` were made
40
+ if (this._closed || signal.aborted) {
41
+ return;
42
+ }
43
+ this.options.watchOptions = settings;
44
+ this.iterateListeners((l) => l[WatchedQueryListenerEvent.SETTINGS_WILL_UPDATE]?.());
42
45
  if (!this.state.isFetching && this.reportFetching) {
43
46
  await this.updateState({
44
47
  isFetching: true
45
48
  });
46
49
  }
47
- this.options.watchOptions = settings;
48
- this.abortController.abort();
49
- this.abortController = new AbortController();
50
50
  await this.runWithReporting(() => this.linkQuery({
51
- abortSignal: this.abortController.signal,
51
+ abortSignal: signal,
52
52
  settings
53
53
  }));
54
54
  }
55
+ /**
56
+ * Updates the underlying query.
57
+ */
58
+ async updateSettings(settings) {
59
+ // Abort the previous request
60
+ this.abortController.abort();
61
+ // Keep track of this controller's abort status
62
+ const abortController = new AbortController();
63
+ // Allow this to be aborted externally
64
+ this.abortController = abortController;
65
+ await this.initialized;
66
+ return this.updateSettingsInternal(settings, abortController.signal);
67
+ }
55
68
  async updateState(update) {
69
+ if (this._closed) {
70
+ return;
71
+ }
56
72
  if (typeof update.error !== 'undefined') {
57
73
  await this.iterateAsyncListenersWithError(async (l) => l.onError?.(update.error));
58
74
  // An error always stops for the current fetching state
@@ -68,7 +84,7 @@ export class AbstractQueryProcessor extends MetaBaseObserver {
68
84
  /**
69
85
  * Configures base DB listeners and links the query to listeners.
70
86
  */
71
- async init() {
87
+ async init(signal) {
72
88
  const { db } = this.options;
73
89
  const disposeCloseListener = db.registerListener({
74
90
  closing: async () => {
@@ -89,16 +105,15 @@ export class AbstractQueryProcessor extends MetaBaseObserver {
89
105
  disposeSchemaListener();
90
106
  };
91
107
  // Initial setup
92
- this.runWithReporting(async () => {
93
- await this.updateSettings(this.options.watchOptions);
108
+ await this.runWithReporting(async () => {
109
+ await this.updateSettingsInternal(this.options.watchOptions, signal);
94
110
  });
95
111
  }
96
112
  async close() {
97
- await this.initialized;
113
+ this._closed = true;
98
114
  this.abortController.abort();
99
115
  this.disposeListeners?.();
100
116
  this.disposeListeners = null;
101
- this._closed = true;
102
117
  this.iterateListeners((l) => l.closed?.());
103
118
  this.listeners.clear();
104
119
  }
@@ -113,7 +113,7 @@ export class DifferentialQueryProcessor extends AbstractQueryProcessor {
113
113
  });
114
114
  db.onChangeWithCallback({
115
115
  onChange: async () => {
116
- if (this.closed) {
116
+ if (this.closed || abortSignal.aborted) {
117
117
  return;
118
118
  }
119
119
  // This fires for each change of the relevant tables
@@ -130,6 +130,9 @@ export class DifferentialQueryProcessor extends AbstractQueryProcessor {
130
130
  parameters: [...compiledQuery.parameters],
131
131
  db: this.options.db
132
132
  });
133
+ if (abortSignal.aborted) {
134
+ return;
135
+ }
133
136
  if (this.reportFetching) {
134
137
  partialStateUpdate.isFetching = false;
135
138
  }
@@ -145,6 +148,9 @@ export class DifferentialQueryProcessor extends AbstractQueryProcessor {
145
148
  data: diff.all
146
149
  });
147
150
  }
151
+ if (this.state.error) {
152
+ partialStateUpdate.error = null;
153
+ }
148
154
  if (Object.keys(partialStateUpdate).length > 0) {
149
155
  await this.updateState(partialStateUpdate);
150
156
  }
@@ -26,7 +26,7 @@ export class OnChangeQueryProcessor extends AbstractQueryProcessor {
26
26
  });
27
27
  db.onChangeWithCallback({
28
28
  onChange: async () => {
29
- if (this.closed) {
29
+ if (this.closed || abortSignal.aborted) {
30
30
  return;
31
31
  }
32
32
  // This fires for each change of the relevant tables
@@ -43,6 +43,9 @@ export class OnChangeQueryProcessor extends AbstractQueryProcessor {
43
43
  parameters: [...compiledQuery.parameters],
44
44
  db: this.options.db
45
45
  });
46
+ if (abortSignal.aborted) {
47
+ return;
48
+ }
46
49
  if (this.reportFetching) {
47
50
  partialStateUpdate.isFetching = false;
48
51
  }
@@ -55,6 +58,9 @@ export class OnChangeQueryProcessor extends AbstractQueryProcessor {
55
58
  data: result
56
59
  });
57
60
  }
61
+ if (this.state.error) {
62
+ partialStateUpdate.error = null;
63
+ }
58
64
  if (Object.keys(partialStateUpdate).length > 0) {
59
65
  await this.updateState(partialStateUpdate);
60
66
  }
@@ -1,3 +1,4 @@
1
+ import { SyncClientImplementation } from '../../client/sync/stream/AbstractStreamingSyncImplementation.js';
1
2
  import { InternalProgressInformation, SyncProgress } from './SyncProgress.js';
2
3
  export type SyncDataFlowStatus = Partial<{
3
4
  downloading: boolean;
@@ -32,10 +33,18 @@ export type SyncStatusOptions = {
32
33
  lastSyncedAt?: Date;
33
34
  hasSynced?: boolean;
34
35
  priorityStatusEntries?: SyncPriorityStatus[];
36
+ clientImplementation?: SyncClientImplementation;
35
37
  };
36
38
  export declare class SyncStatus {
37
39
  protected options: SyncStatusOptions;
38
40
  constructor(options: SyncStatusOptions);
41
+ /**
42
+ * Returns the used sync client implementation (either the one implemented in JavaScript or the newer Rust-based
43
+ * implementation).
44
+ *
45
+ * This information is only available after a connection has been requested.
46
+ */
47
+ get clientImplementation(): SyncClientImplementation | undefined;
39
48
  /**
40
49
  * Indicates if the client is currently connected to the PowerSync service.
41
50
  *
@@ -4,6 +4,15 @@ export class SyncStatus {
4
4
  constructor(options) {
5
5
  this.options = options;
6
6
  }
7
+ /**
8
+ * Returns the used sync client implementation (either the one implemented in JavaScript or the newer Rust-based
9
+ * implementation).
10
+ *
11
+ * This information is only available after a connection has been requested.
12
+ */
13
+ get clientImplementation() {
14
+ return this.options.clientImplementation;
15
+ }
7
16
  /**
8
17
  * Indicates if the client is currently connected to the PowerSync service.
9
18
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powersync/common",
3
- "version": "1.37.0",
3
+ "version": "1.38.1",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org/",
6
6
  "access": "public"