@powersync/common 0.0.0-dev-20260503073249 → 0.0.0-dev-20260630141119

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 (284) hide show
  1. package/lib/attachments/AttachmentContext.d.ts +9 -8
  2. package/lib/attachments/AttachmentContext.js +8 -3
  3. package/lib/attachments/AttachmentContext.js.map +1 -1
  4. package/lib/attachments/AttachmentErrorHandler.d.ts +6 -6
  5. package/lib/attachments/AttachmentQueue.d.ts +85 -36
  6. package/lib/attachments/AttachmentQueue.js +19 -21
  7. package/lib/attachments/AttachmentQueue.js.map +1 -1
  8. package/lib/attachments/AttachmentService.d.ts +3 -3
  9. package/lib/attachments/AttachmentService.js +4 -3
  10. package/lib/attachments/AttachmentService.js.map +1 -1
  11. package/lib/attachments/LocalStorageAdapter.d.ts +14 -8
  12. package/lib/attachments/LocalStorageAdapter.js +3 -0
  13. package/lib/attachments/LocalStorageAdapter.js.map +1 -1
  14. package/lib/attachments/RemoteStorageAdapter.d.ts +4 -4
  15. package/lib/attachments/Schema.d.ts +14 -6
  16. package/lib/attachments/Schema.js +8 -3
  17. package/lib/attachments/Schema.js.map +1 -1
  18. package/lib/attachments/SyncingService.d.ts +2 -2
  19. package/lib/attachments/SyncingService.js +8 -3
  20. package/lib/attachments/SyncingService.js.map +1 -1
  21. package/lib/attachments/WatchedAttachmentItem.d.ts +3 -1
  22. package/lib/client/{AbstractPowerSyncDatabase.d.ts → CommonPowerSyncDatabase.d.ts} +112 -238
  23. package/lib/client/CommonPowerSyncDatabase.js +2 -0
  24. package/lib/client/CommonPowerSyncDatabase.js.map +1 -0
  25. package/lib/client/Query.d.ts +9 -0
  26. package/lib/client/SQLOpenFactory.d.ts +44 -10
  27. package/lib/client/SQLOpenFactory.js +1 -19
  28. package/lib/client/SQLOpenFactory.js.map +1 -1
  29. package/lib/client/compilableQueryWatch.d.ts +8 -2
  30. package/lib/client/compilableQueryWatch.js +4 -2
  31. package/lib/client/compilableQueryWatch.js.map +1 -1
  32. package/lib/client/connection/PowerSyncBackendConnector.d.ts +6 -3
  33. package/lib/client/connection/PowerSyncCredentials.d.ts +3 -0
  34. package/lib/client/runOnSchemaChange.d.ts +5 -2
  35. package/lib/client/runOnSchemaChange.js +3 -0
  36. package/lib/client/runOnSchemaChange.js.map +1 -1
  37. package/lib/client/sync/bucket/CrudBatch.d.ts +2 -0
  38. package/lib/client/sync/bucket/CrudBatch.js +2 -0
  39. package/lib/client/sync/bucket/CrudBatch.js.map +1 -1
  40. package/lib/client/sync/bucket/CrudEntry.d.ts +9 -31
  41. package/lib/client/sync/bucket/CrudEntry.js +2 -97
  42. package/lib/client/sync/bucket/CrudEntry.js.map +1 -1
  43. package/lib/client/sync/bucket/CrudTransaction.d.ts +3 -0
  44. package/lib/client/sync/bucket/CrudTransaction.js +3 -0
  45. package/lib/client/sync/bucket/CrudTransaction.js.map +1 -1
  46. package/lib/client/sync/options.d.ts +65 -0
  47. package/lib/client/sync/options.js +25 -0
  48. package/lib/client/sync/options.js.map +1 -0
  49. package/lib/client/sync/stream/JsonValue.d.ts +10 -0
  50. package/lib/client/sync/stream/JsonValue.js +2 -0
  51. package/lib/client/sync/stream/JsonValue.js.map +1 -0
  52. package/lib/client/sync/sync-streams.d.ts +23 -8
  53. package/lib/client/triggers/TriggerManager.d.ts +28 -56
  54. package/lib/client/triggers/TriggerManager.js +2 -1
  55. package/lib/client/triggers/TriggerManager.js.map +1 -1
  56. package/lib/client/triggers/sanitizeSQL.d.ts +4 -0
  57. package/lib/client/triggers/sanitizeSQL.js +4 -0
  58. package/lib/client/triggers/sanitizeSQL.js.map +1 -1
  59. package/lib/client/watched/GetAllQuery.d.ts +7 -3
  60. package/lib/client/watched/GetAllQuery.js +3 -1
  61. package/lib/client/watched/GetAllQuery.js.map +1 -1
  62. package/lib/client/watched/WatchedQuery.d.ts +20 -6
  63. package/lib/client/watched/WatchedQuery.js +3 -5
  64. package/lib/client/watched/WatchedQuery.js.map +1 -1
  65. package/lib/client/watched/processors/DifferentialQueryProcessor.d.ts +15 -45
  66. package/lib/client/watched/processors/DifferentialQueryProcessor.js +1 -172
  67. package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +1 -1
  68. package/lib/client/watched/processors/OnChangeQueryProcessor.d.ts +4 -22
  69. package/lib/client/watched/processors/OnChangeQueryProcessor.js +1 -82
  70. package/lib/client/watched/processors/OnChangeQueryProcessor.js.map +1 -1
  71. package/lib/client/watched/processors/comparators.d.ts +8 -0
  72. package/lib/client/watched/processors/comparators.js +4 -0
  73. package/lib/client/watched/processors/comparators.js.map +1 -1
  74. package/lib/db/DBAdapter.d.ts +90 -113
  75. package/lib/db/DBAdapter.js +70 -89
  76. package/lib/db/DBAdapter.js.map +1 -1
  77. package/lib/db/QueryResult.d.ts +104 -0
  78. package/lib/db/QueryResult.js +96 -0
  79. package/lib/db/QueryResult.js.map +1 -0
  80. package/lib/db/crud/SyncProgress.d.ts +7 -14
  81. package/lib/db/crud/SyncProgress.js +1 -60
  82. package/lib/db/crud/SyncProgress.js.map +1 -1
  83. package/lib/db/crud/SyncStatus.d.ts +54 -91
  84. package/lib/db/crud/SyncStatus.js +1 -245
  85. package/lib/db/crud/SyncStatus.js.map +1 -1
  86. package/lib/db/crud/UploadQueueStatus.d.ts +3 -0
  87. package/lib/db/crud/UploadQueueStatus.js +3 -0
  88. package/lib/db/crud/UploadQueueStatus.js.map +1 -1
  89. package/lib/db/schema/Column.d.ts +22 -1
  90. package/lib/db/schema/Column.js +10 -4
  91. package/lib/db/schema/Column.js.map +1 -1
  92. package/lib/db/schema/Index.d.ts +8 -3
  93. package/lib/db/schema/Index.js +4 -1
  94. package/lib/db/schema/Index.js.map +1 -1
  95. package/lib/db/schema/IndexedColumn.d.ts +8 -3
  96. package/lib/db/schema/IndexedColumn.js +4 -1
  97. package/lib/db/schema/IndexedColumn.js.map +1 -1
  98. package/lib/db/schema/RawTable.d.ts +7 -1
  99. package/lib/db/schema/Schema.d.ts +10 -29
  100. package/lib/db/schema/Schema.js +3 -1
  101. package/lib/db/schema/Schema.js.map +1 -1
  102. package/lib/db/schema/Table.d.ts +69 -91
  103. package/lib/db/schema/Table.js +88 -87
  104. package/lib/db/schema/Table.js.map +1 -1
  105. package/lib/index.d.ts +7 -27
  106. package/lib/index.js +6 -27
  107. package/lib/index.js.map +1 -1
  108. package/lib/types/types.d.ts +6 -0
  109. package/lib/utils/BaseObserver.d.ts +12 -0
  110. package/lib/utils/BaseObserver.js +3 -0
  111. package/lib/utils/BaseObserver.js.map +1 -1
  112. package/lib/utils/Logger.d.ts +60 -22
  113. package/lib/utils/Logger.js +38 -30
  114. package/lib/utils/Logger.js.map +1 -1
  115. package/lib/utils/MetaBaseObserver.d.ts +11 -11
  116. package/lib/utils/MetaBaseObserver.js +1 -50
  117. package/lib/utils/MetaBaseObserver.js.map +1 -1
  118. package/lib/utils/mutex.d.ts +3 -45
  119. package/lib/utils/mutex.js +1 -153
  120. package/lib/utils/mutex.js.map +1 -1
  121. package/package.json +8 -42
  122. package/src/attachments/AttachmentContext.ts +15 -13
  123. package/src/attachments/AttachmentErrorHandler.ts +6 -6
  124. package/src/attachments/AttachmentQueue.ts +99 -42
  125. package/src/attachments/AttachmentService.ts +7 -6
  126. package/src/attachments/LocalStorageAdapter.ts +14 -8
  127. package/src/attachments/README.md +2 -0
  128. package/src/attachments/RemoteStorageAdapter.ts +4 -4
  129. package/src/attachments/Schema.ts +14 -6
  130. package/src/attachments/SyncingService.ts +10 -6
  131. package/src/attachments/WatchedAttachmentItem.ts +3 -1
  132. package/src/client/CommonPowerSyncDatabase.ts +540 -0
  133. package/src/client/Query.ts +9 -0
  134. package/src/client/SQLOpenFactory.ts +47 -19
  135. package/src/client/compilableQueryWatch.ts +9 -4
  136. package/src/client/connection/PowerSyncBackendConnector.ts +6 -3
  137. package/src/client/connection/PowerSyncCredentials.ts +3 -0
  138. package/src/client/runOnSchemaChange.ts +5 -2
  139. package/src/client/sync/bucket/CrudBatch.ts +2 -0
  140. package/src/client/sync/bucket/CrudEntry.ts +10 -101
  141. package/src/client/sync/bucket/CrudTransaction.ts +3 -0
  142. package/src/client/sync/options.ts +77 -0
  143. package/src/client/sync/stream/JsonValue.ts +11 -0
  144. package/src/client/sync/sync-streams.ts +23 -10
  145. package/src/client/triggers/TriggerManager.ts +29 -59
  146. package/src/client/triggers/sanitizeSQL.ts +5 -0
  147. package/src/client/watched/GetAllQuery.ts +8 -4
  148. package/src/client/watched/WatchedQuery.ts +20 -11
  149. package/src/client/watched/processors/DifferentialQueryProcessor.ts +19 -219
  150. package/src/client/watched/processors/OnChangeQueryProcessor.ts +4 -109
  151. package/src/client/watched/processors/comparators.ts +8 -0
  152. package/src/db/DBAdapter.ts +160 -175
  153. package/src/db/QueryResult.ts +195 -0
  154. package/src/db/crud/SyncProgress.ts +8 -43
  155. package/src/db/crud/SyncStatus.ts +66 -216
  156. package/src/db/crud/UploadQueueStatus.ts +3 -0
  157. package/src/db/schema/Column.ts +22 -5
  158. package/src/db/schema/Index.ts +9 -3
  159. package/src/db/schema/IndexedColumn.ts +9 -3
  160. package/src/db/schema/RawTable.ts +7 -1
  161. package/src/db/schema/Schema.ts +12 -7
  162. package/src/db/schema/Table.ts +130 -180
  163. package/src/index.ts +7 -27
  164. package/src/types/types.ts +6 -0
  165. package/src/utils/BaseObserver.ts +12 -0
  166. package/src/utils/Logger.ts +86 -31
  167. package/src/utils/MetaBaseObserver.ts +14 -60
  168. package/src/utils/mutex.ts +4 -189
  169. package/dist/bundle.cjs +0 -14781
  170. package/dist/bundle.cjs.map +0 -1
  171. package/dist/bundle.mjs +0 -14689
  172. package/dist/bundle.mjs.map +0 -1
  173. package/dist/bundle.node.cjs +0 -12258
  174. package/dist/bundle.node.cjs.map +0 -1
  175. package/dist/bundle.node.mjs +0 -12166
  176. package/dist/bundle.node.mjs.map +0 -1
  177. package/dist/index.d.cts +0 -4391
  178. package/lib/client/AbstractPowerSyncDatabase.js +0 -987
  179. package/lib/client/AbstractPowerSyncDatabase.js.map +0 -1
  180. package/lib/client/AbstractPowerSyncOpenFactory.d.ts +0 -22
  181. package/lib/client/AbstractPowerSyncOpenFactory.js +0 -25
  182. package/lib/client/AbstractPowerSyncOpenFactory.js.map +0 -1
  183. package/lib/client/ConnectionManager.d.ts +0 -112
  184. package/lib/client/ConnectionManager.js +0 -294
  185. package/lib/client/ConnectionManager.js.map +0 -1
  186. package/lib/client/CustomQuery.d.ts +0 -22
  187. package/lib/client/CustomQuery.js +0 -43
  188. package/lib/client/CustomQuery.js.map +0 -1
  189. package/lib/client/constants.d.ts +0 -1
  190. package/lib/client/constants.js +0 -2
  191. package/lib/client/constants.js.map +0 -1
  192. package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +0 -102
  193. package/lib/client/sync/bucket/BucketStorageAdapter.js +0 -19
  194. package/lib/client/sync/bucket/BucketStorageAdapter.js.map +0 -1
  195. package/lib/client/sync/bucket/OpType.d.ts +0 -16
  196. package/lib/client/sync/bucket/OpType.js +0 -23
  197. package/lib/client/sync/bucket/OpType.js.map +0 -1
  198. package/lib/client/sync/bucket/OplogEntry.d.ts +0 -23
  199. package/lib/client/sync/bucket/OplogEntry.js +0 -36
  200. package/lib/client/sync/bucket/OplogEntry.js.map +0 -1
  201. package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +0 -61
  202. package/lib/client/sync/bucket/SqliteBucketStorage.js +0 -324
  203. package/lib/client/sync/bucket/SqliteBucketStorage.js.map +0 -1
  204. package/lib/client/sync/bucket/SyncDataBatch.d.ts +0 -6
  205. package/lib/client/sync/bucket/SyncDataBatch.js +0 -12
  206. package/lib/client/sync/bucket/SyncDataBatch.js.map +0 -1
  207. package/lib/client/sync/bucket/SyncDataBucket.d.ts +0 -40
  208. package/lib/client/sync/bucket/SyncDataBucket.js +0 -40
  209. package/lib/client/sync/bucket/SyncDataBucket.js.map +0 -1
  210. package/lib/client/sync/stream/AbstractRemote.d.ts +0 -161
  211. package/lib/client/sync/stream/AbstractRemote.js +0 -483
  212. package/lib/client/sync/stream/AbstractRemote.js.map +0 -1
  213. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +0 -244
  214. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +0 -990
  215. package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +0 -1
  216. package/lib/client/sync/stream/WebsocketClientTransport.d.ts +0 -15
  217. package/lib/client/sync/stream/WebsocketClientTransport.js +0 -61
  218. package/lib/client/sync/stream/WebsocketClientTransport.js.map +0 -1
  219. package/lib/client/sync/stream/core-instruction.d.ts +0 -72
  220. package/lib/client/sync/stream/core-instruction.js +0 -27
  221. package/lib/client/sync/stream/core-instruction.js.map +0 -1
  222. package/lib/client/sync/stream/streaming-sync-types.d.ts +0 -143
  223. package/lib/client/sync/stream/streaming-sync-types.js +0 -26
  224. package/lib/client/sync/stream/streaming-sync-types.js.map +0 -1
  225. package/lib/client/triggers/MemoryTriggerClaimManager.d.ts +0 -6
  226. package/lib/client/triggers/MemoryTriggerClaimManager.js +0 -21
  227. package/lib/client/triggers/MemoryTriggerClaimManager.js.map +0 -1
  228. package/lib/client/triggers/TriggerManagerImpl.d.ts +0 -39
  229. package/lib/client/triggers/TriggerManagerImpl.js +0 -405
  230. package/lib/client/triggers/TriggerManagerImpl.js.map +0 -1
  231. package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +0 -68
  232. package/lib/client/watched/processors/AbstractQueryProcessor.js +0 -151
  233. package/lib/client/watched/processors/AbstractQueryProcessor.js.map +0 -1
  234. package/lib/db/ConnectionClosedError.d.ts +0 -10
  235. package/lib/db/ConnectionClosedError.js +0 -21
  236. package/lib/db/ConnectionClosedError.js.map +0 -1
  237. package/lib/db/schema/TableV2.d.ts +0 -9
  238. package/lib/db/schema/TableV2.js +0 -9
  239. package/lib/db/schema/TableV2.js.map +0 -1
  240. package/lib/utils/AbortOperation.d.ts +0 -9
  241. package/lib/utils/AbortOperation.js +0 -19
  242. package/lib/utils/AbortOperation.js.map +0 -1
  243. package/lib/utils/ControlledExecutor.d.ts +0 -25
  244. package/lib/utils/ControlledExecutor.js +0 -51
  245. package/lib/utils/ControlledExecutor.js.map +0 -1
  246. package/lib/utils/async.d.ts +0 -14
  247. package/lib/utils/async.js +0 -46
  248. package/lib/utils/async.js.map +0 -1
  249. package/lib/utils/parseQuery.d.ts +0 -6
  250. package/lib/utils/parseQuery.js +0 -17
  251. package/lib/utils/parseQuery.js.map +0 -1
  252. package/lib/utils/queue.d.ts +0 -16
  253. package/lib/utils/queue.js +0 -42
  254. package/lib/utils/queue.js.map +0 -1
  255. package/lib/utils/stream_transform.d.ts +0 -39
  256. package/lib/utils/stream_transform.js +0 -206
  257. package/lib/utils/stream_transform.js.map +0 -1
  258. package/src/client/AbstractPowerSyncDatabase.ts +0 -1362
  259. package/src/client/AbstractPowerSyncOpenFactory.ts +0 -39
  260. package/src/client/ConnectionManager.ts +0 -402
  261. package/src/client/CustomQuery.ts +0 -56
  262. package/src/client/constants.ts +0 -1
  263. package/src/client/sync/bucket/BucketStorageAdapter.ts +0 -118
  264. package/src/client/sync/bucket/OpType.ts +0 -23
  265. package/src/client/sync/bucket/OplogEntry.ts +0 -50
  266. package/src/client/sync/bucket/SqliteBucketStorage.ts +0 -395
  267. package/src/client/sync/bucket/SyncDataBatch.ts +0 -11
  268. package/src/client/sync/bucket/SyncDataBucket.ts +0 -49
  269. package/src/client/sync/stream/AbstractRemote.ts +0 -624
  270. package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +0 -1285
  271. package/src/client/sync/stream/WebsocketClientTransport.ts +0 -80
  272. package/src/client/sync/stream/core-instruction.ts +0 -99
  273. package/src/client/sync/stream/streaming-sync-types.ts +0 -210
  274. package/src/client/triggers/MemoryTriggerClaimManager.ts +0 -25
  275. package/src/client/triggers/TriggerManagerImpl.ts +0 -496
  276. package/src/client/watched/processors/AbstractQueryProcessor.ts +0 -226
  277. package/src/db/ConnectionClosedError.ts +0 -23
  278. package/src/db/schema/TableV2.ts +0 -9
  279. package/src/utils/AbortOperation.ts +0 -17
  280. package/src/utils/ControlledExecutor.ts +0 -72
  281. package/src/utils/async.ts +0 -50
  282. package/src/utils/parseQuery.ts +0 -25
  283. package/src/utils/queue.ts +0 -48
  284. package/src/utils/stream_transform.ts +0 -252
@@ -1,226 +0,0 @@
1
- import { AbstractPowerSyncDatabase } from '../../../client/AbstractPowerSyncDatabase.js';
2
- import { MetaBaseObserver } from '../../../utils/MetaBaseObserver.js';
3
- import {
4
- WatchedQuery,
5
- WatchedQueryListener,
6
- WatchedQueryListenerEvent,
7
- WatchedQueryOptions,
8
- WatchedQueryState
9
- } from '../WatchedQuery.js';
10
-
11
- /**
12
- * @internal
13
- */
14
- export interface AbstractQueryProcessorOptions<Data, Settings extends WatchedQueryOptions = WatchedQueryOptions> {
15
- db: AbstractPowerSyncDatabase;
16
- watchOptions: Settings;
17
- placeholderData: Data;
18
- }
19
-
20
- /**
21
- * @internal
22
- */
23
- export interface LinkQueryOptions<Data, Settings extends WatchedQueryOptions = WatchedQueryOptions> {
24
- abortSignal: AbortSignal;
25
- settings: Settings;
26
- }
27
-
28
- type MutableDeep<T> =
29
- T extends ReadonlyArray<infer U>
30
- ? U[] // convert readonly arrays to mutable arrays
31
- : T;
32
-
33
- /**
34
- * @internal Mutable version of {@link WatchedQueryState}.
35
- * This is used internally to allow updates to the state.
36
- */
37
- export type MutableWatchedQueryState<Data> = {
38
- -readonly [P in keyof WatchedQueryState<Data>]: MutableDeep<WatchedQueryState<Data>[P]>;
39
- };
40
-
41
- type WatchedQueryProcessorListener<Data> = WatchedQueryListener<Data>;
42
-
43
- /**
44
- * Performs underlying watching and yields a stream of results.
45
- * @internal
46
- */
47
- export abstract class AbstractQueryProcessor<
48
- Data = unknown[],
49
- Settings extends WatchedQueryOptions = WatchedQueryOptions
50
- >
51
- extends MetaBaseObserver<WatchedQueryProcessorListener<Data>>
52
- implements WatchedQuery<Data, Settings>
53
- {
54
- readonly state: WatchedQueryState<Data>;
55
-
56
- protected abortController: AbortController;
57
- protected initialized: Promise<void>;
58
- protected _closed: boolean;
59
- protected disposeListeners: (() => void) | null;
60
-
61
- get closed() {
62
- return this._closed;
63
- }
64
-
65
- constructor(protected options: AbstractQueryProcessorOptions<Data, Settings>) {
66
- super();
67
- this.abortController = new AbortController();
68
- this._closed = false;
69
- this.state = this.constructInitialState();
70
- this.disposeListeners = null;
71
- this.initialized = this.init(this.abortController.signal);
72
- }
73
-
74
- protected constructInitialState(): WatchedQueryState<Data> {
75
- return {
76
- isLoading: true,
77
- isFetching: this.reportFetching, // Only set to true if we will report updates in future
78
- error: null,
79
- lastUpdated: null,
80
- data: this.options.placeholderData
81
- };
82
- }
83
-
84
- protected get reportFetching() {
85
- return this.options.watchOptions.reportFetching ?? true;
86
- }
87
-
88
- protected async updateSettingsInternal(settings: Settings, signal: AbortSignal) {
89
- // This may have been aborted while awaiting or if multiple calls to `updateSettings` were made
90
- if (this._closed || signal.aborted) {
91
- return;
92
- }
93
-
94
- this.options.watchOptions = settings;
95
-
96
- this.iterateListeners((l) => l[WatchedQueryListenerEvent.SETTINGS_WILL_UPDATE]?.());
97
-
98
- if (!this.state.isFetching && this.reportFetching) {
99
- await this.updateState({
100
- isFetching: true
101
- });
102
- }
103
-
104
- await this.runWithReporting(() =>
105
- this.linkQuery({
106
- abortSignal: signal,
107
- settings
108
- })
109
- );
110
- }
111
-
112
- /**
113
- * Updates the underlying query.
114
- */
115
- async updateSettings(settings: Settings) {
116
- // Abort the previous request
117
- this.abortController.abort();
118
-
119
- // Keep track of this controller's abort status
120
- const abortController = new AbortController();
121
- // Allow this to be aborted externally
122
- this.abortController = abortController;
123
-
124
- await this.initialized;
125
- return this.updateSettingsInternal(settings, abortController.signal);
126
- }
127
-
128
- /**
129
- * This method is used to link a query to the subscribers of this listener class.
130
- * This method should perform actual query watching and report results via {@link updateState} method.
131
- */
132
- protected abstract linkQuery(options: LinkQueryOptions<Data>): Promise<void>;
133
-
134
- protected async updateState(update: Partial<MutableWatchedQueryState<Data>>) {
135
- if (this._closed) {
136
- return;
137
- }
138
-
139
- if (typeof update.error !== 'undefined') {
140
- await this.iterateAsyncListenersWithError(async (l) => l.onError?.(update.error!));
141
- // An error always stops for the current fetching state
142
- update.isFetching = false;
143
- update.isLoading = false;
144
- }
145
-
146
- Object.assign(this.state, { lastUpdated: new Date() } satisfies Partial<WatchedQueryState<Data>>, update);
147
-
148
- if (typeof update.data !== 'undefined') {
149
- await this.iterateAsyncListenersWithError(async (l) => l.onData?.(this.state.data));
150
- }
151
- await this.iterateAsyncListenersWithError(async (l) => l.onStateChange?.(this.state));
152
- }
153
-
154
- /**
155
- * Configures base DB listeners and links the query to listeners.
156
- */
157
- protected async init(signal: AbortSignal) {
158
- const { db } = this.options;
159
-
160
- const disposeCloseListener = db.registerListener({
161
- closing: async () => {
162
- await this.close();
163
- }
164
- });
165
-
166
- // Wait for the schema to be set before listening to changes
167
- await db.waitForReady();
168
- const disposeSchemaListener = db.registerListener({
169
- schemaChanged: async () => {
170
- await this.runWithReporting(async () => {
171
- await this.updateSettings(this.options.watchOptions);
172
- });
173
- }
174
- });
175
-
176
- this.disposeListeners = () => {
177
- disposeCloseListener();
178
- disposeSchemaListener();
179
- };
180
-
181
- // Initial setup
182
- await this.runWithReporting(async () => {
183
- await this.updateSettingsInternal(this.options.watchOptions, signal);
184
- });
185
- }
186
-
187
- async close() {
188
- this._closed = true;
189
- this.abortController.abort();
190
- this.disposeListeners?.();
191
- this.disposeListeners = null;
192
- this.iterateListeners((l) => l.closed?.());
193
- this.listeners.clear();
194
- }
195
-
196
- /**
197
- * Runs a callback and reports errors to the error listeners.
198
- */
199
- protected async runWithReporting<T>(callback: () => Promise<T>): Promise<void> {
200
- try {
201
- await callback();
202
- } catch (error) {
203
- // This will update the error on the state and iterate error listeners
204
- await this.updateState({ error });
205
- }
206
- }
207
-
208
- /**
209
- * Iterate listeners and reports errors to onError handlers.
210
- */
211
- protected async iterateAsyncListenersWithError(
212
- callback: (listener: Partial<WatchedQueryProcessorListener<Data>>) => Promise<void> | void
213
- ) {
214
- try {
215
- await this.iterateAsyncListeners(async (l) => callback(l));
216
- } catch (error) {
217
- try {
218
- await this.iterateAsyncListeners(async (l) => l.onError?.(error));
219
- } catch (error) {
220
- // Errors here are ignored
221
- // since we are already in an error state
222
- this.options.db.logger.error('Watched query error handler threw an Error', error);
223
- }
224
- }
225
- }
226
- }
@@ -1,23 +0,0 @@
1
- /**
2
- * Thrown when an underlying database connection is closed.
3
- * This is particularly relevant when worker connections are marked as closed while
4
- * operations are still in progress.
5
- */
6
- export class ConnectionClosedError extends Error {
7
- static NAME = 'ConnectionClosedError';
8
-
9
- static MATCHES(input: any) {
10
- /**
11
- * If there are weird package issues which cause multiple versions of classes to be present, the instanceof
12
- * check might fail. This also performs a failsafe check.
13
- * This might also happen if the Error is serialized and parsed over a bridging channel like a MessagePort.
14
- */
15
- return (
16
- input instanceof ConnectionClosedError || (input instanceof Error && input.name == ConnectionClosedError.NAME)
17
- );
18
- }
19
- constructor(message: string) {
20
- super(message);
21
- this.name = ConnectionClosedError.NAME;
22
- }
23
- }
@@ -1,9 +0,0 @@
1
- import { ColumnsType } from './Column.js';
2
- import { Table } from './Table.js';
3
-
4
- /**
5
- Generate a new table from the columns and indexes
6
- @deprecated You should use {@link Table} instead as it now allows TableV2 syntax.
7
- This will be removed in the next major release.
8
- */
9
- export class TableV2<Columns extends ColumnsType = ColumnsType> extends Table<Columns> {}
@@ -1,17 +0,0 @@
1
- /**
2
- * Calls to Abortcontroller.abort(reason: any) will result in the
3
- * `reason` being thrown. This is not necessarily an error,
4
- * but extends error for better logging purposes.
5
- */
6
- export class AbortOperation extends Error {
7
- constructor(protected reason: string) {
8
- super(reason);
9
- // Set the prototype explicitly
10
- Object.setPrototypeOf(this, AbortOperation.prototype);
11
-
12
- // Capture stack trace
13
- if (Error.captureStackTrace) {
14
- Error.captureStackTrace(this, AbortOperation);
15
- }
16
- }
17
- }
@@ -1,72 +0,0 @@
1
- export interface ControlledExecutorOptions {
2
- /**
3
- * If throttling is enabled, it ensures only one task runs at a time,
4
- * and only one additional task can be scheduled to run after the current task completes. The pending task will be overwritten by the latest task.
5
- * Enabled by default.
6
- */
7
- throttleEnabled?: boolean;
8
- }
9
-
10
- export class ControlledExecutor<T> {
11
- private task: (param: T) => Promise<void> | void;
12
-
13
- /**
14
- * Represents the currently running task, which could be a Promise or undefined if no task is running.
15
- */
16
- private runningTask: undefined | (Promise<void> | void);
17
-
18
- private pendingTaskParam: T | undefined;
19
-
20
- /**
21
- * Flag to determine if throttling is enabled, which controls whether tasks are queued or run immediately.
22
- */
23
- private isThrottling: boolean;
24
-
25
- private closed: boolean;
26
-
27
- constructor(task: (param: T) => Promise<void> | void, options?: ControlledExecutorOptions) {
28
- this.task = task;
29
- const { throttleEnabled = true } = options ?? {};
30
- this.isThrottling = throttleEnabled;
31
- this.closed = false;
32
- }
33
-
34
- schedule(param: T) {
35
- if (this.closed) {
36
- return;
37
- }
38
- if (!this.isThrottling) {
39
- this.task(param);
40
- return;
41
- }
42
-
43
- if (this.runningTask) {
44
- // set or replace the pending task param with latest one
45
- this.pendingTaskParam = param;
46
- return;
47
- }
48
-
49
- this.execute(param);
50
- }
51
-
52
- dispose() {
53
- this.closed = true;
54
-
55
- if (this.runningTask) {
56
- this.runningTask = undefined;
57
- }
58
- }
59
-
60
- private async execute(param: T) {
61
- this.runningTask = this.task(param);
62
- await this.runningTask;
63
- this.runningTask = undefined;
64
-
65
- if (this.pendingTaskParam) {
66
- const pendingParam = this.pendingTaskParam;
67
- this.pendingTaskParam = undefined;
68
-
69
- this.execute(pendingParam);
70
- }
71
- }
72
- }
@@ -1,50 +0,0 @@
1
- /**
2
- * Throttle a function to be called at most once every "wait" milliseconds,
3
- * on the trailing edge.
4
- *
5
- * Roughly equivalent to lodash/throttle with {leading: false, trailing: true}
6
- */
7
- export function throttleTrailing(func: () => void, wait: number) {
8
- let timeoutId: ReturnType<typeof setTimeout> | null = null;
9
-
10
- const later = () => {
11
- func();
12
- timeoutId = null;
13
- };
14
-
15
- return function () {
16
- if (timeoutId == null) {
17
- timeoutId = setTimeout(later, wait);
18
- }
19
- };
20
- }
21
-
22
- /**
23
- * Throttle a function to be called at most once every "wait" milliseconds,
24
- * on the leading and trailing edge.
25
- *
26
- * Roughly equivalent to lodash/throttle with {leading: true, trailing: true}
27
- */
28
- export function throttleLeadingTrailing(func: () => void, wait: number) {
29
- let timeoutId: ReturnType<typeof setTimeout> | null = null;
30
- let lastCallTime: number = 0;
31
-
32
- const invokeFunction = () => {
33
- func();
34
- lastCallTime = Date.now();
35
- timeoutId = null;
36
- };
37
-
38
- return function () {
39
- const now = Date.now();
40
- const timeToWait = wait - (now - lastCallTime);
41
-
42
- if (timeToWait <= 0) {
43
- // Leading edge: Call the function immediately if enough time has passed
44
- invokeFunction();
45
- } else if (!timeoutId) {
46
- // Set a timeout for the trailing edge if not already set
47
- timeoutId = setTimeout(invokeFunction, timeToWait);
48
- }
49
- };
50
- }
@@ -1,25 +0,0 @@
1
- import type { CompilableQuery } from '../types/types.js';
2
-
3
- export interface ParsedQuery {
4
- sqlStatement: string;
5
- parameters: any[];
6
- }
7
-
8
- export const parseQuery = <T>(query: string | CompilableQuery<T>, parameters: any[]): ParsedQuery => {
9
- let sqlStatement: string;
10
-
11
- if (typeof query == 'string') {
12
- sqlStatement = query;
13
- } else {
14
- const hasAdditionalParameters = parameters.length > 0;
15
- if (hasAdditionalParameters) {
16
- throw new Error('You cannot pass parameters to a compiled query.');
17
- }
18
-
19
- const compiled = query.compile();
20
- sqlStatement = compiled.sql;
21
- parameters = compiled.parameters as any[];
22
- }
23
-
24
- return { sqlStatement, parameters: parameters };
25
- };
@@ -1,48 +0,0 @@
1
- /**
2
- * A simple fixed-capacity queue implementation.
3
- *
4
- * Unlike a naive queue implemented by `array.push()` and `array.shift()`, this avoids moving array elements around
5
- * and is `O(1)` for {@link addLast} and {@link removeFirst}.
6
- */
7
- export class Queue<T> {
8
- private table: (T | undefined)[];
9
- // Index of the first element in the table.
10
- private head: number;
11
- // Amount of items currently in the queue.
12
- private _length: number;
13
-
14
- constructor(initialItems: Iterable<T>) {
15
- this.table = [...initialItems];
16
- this.head = 0;
17
- this._length = this.table.length;
18
- }
19
-
20
- get isEmpty(): boolean {
21
- return this.length == 0;
22
- }
23
-
24
- get length(): number {
25
- return this._length;
26
- }
27
-
28
- removeFirst(): T {
29
- if (this.isEmpty) {
30
- throw new Error('Queue is empty');
31
- }
32
-
33
- const result = this.table[this.head] as T;
34
- this._length--;
35
- this.table[this.head] = undefined;
36
- this.head = (this.head + 1) % this.table.length;
37
- return result;
38
- }
39
-
40
- addLast(element: T) {
41
- if (this.length == this.table.length) {
42
- throw new Error('Queue is full');
43
- }
44
-
45
- this.table[(this.head + this._length) % this.table.length] = element;
46
- this._length++;
47
- }
48
- }