@powersync/common 0.0.0-dev-20250915110424 → 0.0.0-dev-20250916075127

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/index.d.cts CHANGED
@@ -1815,12 +1815,14 @@ declare enum WatchedQueryListenerEvent {
1815
1815
  ON_DATA = "onData",
1816
1816
  ON_ERROR = "onError",
1817
1817
  ON_STATE_CHANGE = "onStateChange",
1818
+ SETTINGS_WILL_UPDATE = "settingsWillUpdate",
1818
1819
  CLOSED = "closed"
1819
1820
  }
1820
1821
  interface WatchedQueryListener<Data> extends BaseListener {
1821
1822
  [WatchedQueryListenerEvent.ON_DATA]?: (data: Data) => void | Promise<void>;
1822
1823
  [WatchedQueryListenerEvent.ON_ERROR]?: (error: Error) => void | Promise<void>;
1823
1824
  [WatchedQueryListenerEvent.ON_STATE_CHANGE]?: (state: WatchedQueryState<Data>) => void | Promise<void>;
1825
+ [WatchedQueryListenerEvent.SETTINGS_WILL_UPDATE]?: () => void;
1824
1826
  [WatchedQueryListenerEvent.CLOSED]?: () => void | Promise<void>;
1825
1827
  }
1826
1828
  declare const DEFAULT_WATCH_THROTTLE_MS = 30;
@@ -1900,7 +1902,7 @@ declare abstract class AbstractQueryProcessor<Data = unknown[], Settings extends
1900
1902
  /**
1901
1903
  * Configures base DB listeners and links the query to listeners.
1902
1904
  */
1903
- protected init(): Promise<void>;
1905
+ protected init(signal: AbortSignal): Promise<void>;
1904
1906
  close(): Promise<void>;
1905
1907
  /**
1906
1908
  * Runs a callback and reports errors to the error listeners.
@@ -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;
@@ -54,7 +54,7 @@ export declare abstract class AbstractQueryProcessor<Data = unknown[], Settings
54
54
  /**
55
55
  * Configures base DB listeners and links the query to listeners.
56
56
  */
57
- protected init(): Promise<void>;
57
+ protected init(signal: AbortSignal): Promise<void>;
58
58
  close(): Promise<void>;
59
59
  /**
60
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 {
@@ -36,10 +37,11 @@ export class AbstractQueryProcessor extends MetaBaseObserver {
36
37
  }
37
38
  async updateSettingsInternal(settings, signal) {
38
39
  // This may have been aborted while awaiting or if multiple calls to `updateSettings` were made
39
- if (signal.aborted) {
40
+ if (this._closed || signal.aborted) {
40
41
  return;
41
42
  }
42
43
  this.options.watchOptions = settings;
44
+ this.iterateListeners((l) => l[WatchedQueryListenerEvent.SETTINGS_WILL_UPDATE]?.());
43
45
  if (!this.state.isFetching && this.reportFetching) {
44
46
  await this.updateState({
45
47
  isFetching: true
@@ -54,7 +56,7 @@ export class AbstractQueryProcessor extends MetaBaseObserver {
54
56
  * Updates the underlying query.
55
57
  */
56
58
  async updateSettings(settings) {
57
- // Abort any previous requests
59
+ // Abort the previous request
58
60
  this.abortController.abort();
59
61
  // Keep track of this controller's abort status
60
62
  const abortController = new AbortController();
@@ -64,6 +66,9 @@ export class AbstractQueryProcessor extends MetaBaseObserver {
64
66
  return this.updateSettingsInternal(settings, abortController.signal);
65
67
  }
66
68
  async updateState(update) {
69
+ if (this._closed) {
70
+ return;
71
+ }
67
72
  if (typeof update.error !== 'undefined') {
68
73
  await this.iterateAsyncListenersWithError(async (l) => l.onError?.(update.error));
69
74
  // An error always stops for the current fetching state
@@ -79,7 +84,7 @@ export class AbstractQueryProcessor extends MetaBaseObserver {
79
84
  /**
80
85
  * Configures base DB listeners and links the query to listeners.
81
86
  */
82
- async init() {
87
+ async init(signal) {
83
88
  const { db } = this.options;
84
89
  const disposeCloseListener = db.registerListener({
85
90
  closing: async () => {
@@ -101,15 +106,14 @@ export class AbstractQueryProcessor extends MetaBaseObserver {
101
106
  };
102
107
  // Initial setup
103
108
  await this.runWithReporting(async () => {
104
- await this.updateSettingsInternal(this.options.watchOptions, this.abortController.signal);
109
+ await this.updateSettingsInternal(this.options.watchOptions, signal);
105
110
  });
106
111
  }
107
112
  async close() {
108
- await this.initialized;
113
+ this._closed = true;
109
114
  this.abortController.abort();
110
115
  this.disposeListeners?.();
111
116
  this.disposeListeners = null;
112
- this._closed = true;
113
117
  this.iterateListeners((l) => l.closed?.());
114
118
  this.listeners.clear();
115
119
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powersync/common",
3
- "version": "0.0.0-dev-20250915110424",
3
+ "version": "0.0.0-dev-20250916075127",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org/",
6
6
  "access": "public"