@powersync/common 0.0.0-dev-20260504100448 → 0.0.0-dev-20260630144038
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/lib/attachments/AttachmentContext.d.ts +9 -8
- package/lib/attachments/AttachmentContext.js +8 -3
- package/lib/attachments/AttachmentContext.js.map +1 -1
- package/lib/attachments/AttachmentErrorHandler.d.ts +6 -6
- package/lib/attachments/AttachmentQueue.d.ts +85 -36
- package/lib/attachments/AttachmentQueue.js +19 -21
- package/lib/attachments/AttachmentQueue.js.map +1 -1
- package/lib/attachments/AttachmentService.d.ts +3 -3
- package/lib/attachments/AttachmentService.js +4 -3
- package/lib/attachments/AttachmentService.js.map +1 -1
- package/lib/attachments/LocalStorageAdapter.d.ts +14 -8
- package/lib/attachments/LocalStorageAdapter.js +3 -0
- package/lib/attachments/LocalStorageAdapter.js.map +1 -1
- package/lib/attachments/RemoteStorageAdapter.d.ts +4 -4
- package/lib/attachments/Schema.d.ts +14 -6
- package/lib/attachments/Schema.js +8 -3
- package/lib/attachments/Schema.js.map +1 -1
- package/lib/attachments/SyncingService.d.ts +2 -2
- package/lib/attachments/SyncingService.js +8 -3
- package/lib/attachments/SyncingService.js.map +1 -1
- package/lib/attachments/WatchedAttachmentItem.d.ts +3 -1
- package/lib/client/{AbstractPowerSyncDatabase.d.ts → CommonPowerSyncDatabase.d.ts} +112 -238
- package/lib/client/CommonPowerSyncDatabase.js +2 -0
- package/lib/client/CommonPowerSyncDatabase.js.map +1 -0
- package/lib/client/Query.d.ts +9 -0
- package/lib/client/SQLOpenFactory.d.ts +44 -10
- package/lib/client/SQLOpenFactory.js +1 -19
- package/lib/client/SQLOpenFactory.js.map +1 -1
- package/lib/client/compilableQueryWatch.d.ts +8 -2
- package/lib/client/compilableQueryWatch.js +4 -2
- package/lib/client/compilableQueryWatch.js.map +1 -1
- package/lib/client/connection/PowerSyncBackendConnector.d.ts +6 -3
- package/lib/client/connection/PowerSyncCredentials.d.ts +3 -0
- package/lib/client/runOnSchemaChange.d.ts +5 -2
- package/lib/client/runOnSchemaChange.js +3 -0
- package/lib/client/runOnSchemaChange.js.map +1 -1
- package/lib/client/sync/bucket/CrudBatch.d.ts +2 -0
- package/lib/client/sync/bucket/CrudBatch.js +2 -0
- package/lib/client/sync/bucket/CrudBatch.js.map +1 -1
- package/lib/client/sync/bucket/CrudEntry.d.ts +9 -31
- package/lib/client/sync/bucket/CrudEntry.js +2 -97
- package/lib/client/sync/bucket/CrudEntry.js.map +1 -1
- package/lib/client/sync/bucket/CrudTransaction.d.ts +3 -0
- package/lib/client/sync/bucket/CrudTransaction.js +3 -0
- package/lib/client/sync/bucket/CrudTransaction.js.map +1 -1
- package/lib/client/sync/options.d.ts +65 -0
- package/lib/client/sync/options.js +25 -0
- package/lib/client/sync/options.js.map +1 -0
- package/lib/client/sync/stream/JsonValue.d.ts +3 -0
- package/lib/client/sync/sync-streams.d.ts +23 -8
- package/lib/client/triggers/TriggerManager.d.ts +28 -56
- package/lib/client/triggers/TriggerManager.js +2 -1
- package/lib/client/triggers/TriggerManager.js.map +1 -1
- package/lib/client/triggers/sanitizeSQL.d.ts +4 -0
- package/lib/client/triggers/sanitizeSQL.js +4 -0
- package/lib/client/triggers/sanitizeSQL.js.map +1 -1
- package/lib/client/watched/GetAllQuery.d.ts +7 -3
- package/lib/client/watched/GetAllQuery.js +3 -1
- package/lib/client/watched/GetAllQuery.js.map +1 -1
- package/lib/client/watched/WatchedQuery.d.ts +20 -6
- package/lib/client/watched/WatchedQuery.js +3 -5
- package/lib/client/watched/WatchedQuery.js.map +1 -1
- package/lib/client/watched/processors/DifferentialQueryProcessor.d.ts +15 -45
- package/lib/client/watched/processors/DifferentialQueryProcessor.js +1 -172
- package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +1 -1
- package/lib/client/watched/processors/OnChangeQueryProcessor.d.ts +4 -22
- package/lib/client/watched/processors/OnChangeQueryProcessor.js +1 -82
- package/lib/client/watched/processors/OnChangeQueryProcessor.js.map +1 -1
- package/lib/client/watched/processors/comparators.d.ts +8 -0
- package/lib/client/watched/processors/comparators.js +4 -0
- package/lib/client/watched/processors/comparators.js.map +1 -1
- package/lib/db/DBAdapter.d.ts +90 -113
- package/lib/db/DBAdapter.js +70 -89
- package/lib/db/DBAdapter.js.map +1 -1
- package/lib/db/QueryResult.d.ts +104 -0
- package/lib/db/QueryResult.js +96 -0
- package/lib/db/QueryResult.js.map +1 -0
- package/lib/db/crud/SyncProgress.d.ts +7 -14
- package/lib/db/crud/SyncProgress.js +1 -60
- package/lib/db/crud/SyncProgress.js.map +1 -1
- package/lib/db/crud/SyncStatus.d.ts +54 -91
- package/lib/db/crud/SyncStatus.js +1 -245
- package/lib/db/crud/SyncStatus.js.map +1 -1
- package/lib/db/crud/UploadQueueStatus.d.ts +3 -0
- package/lib/db/crud/UploadQueueStatus.js +3 -0
- package/lib/db/crud/UploadQueueStatus.js.map +1 -1
- package/lib/db/schema/Column.d.ts +22 -1
- package/lib/db/schema/Column.js +10 -4
- package/lib/db/schema/Column.js.map +1 -1
- package/lib/db/schema/Index.d.ts +8 -3
- package/lib/db/schema/Index.js +4 -1
- package/lib/db/schema/Index.js.map +1 -1
- package/lib/db/schema/IndexedColumn.d.ts +8 -3
- package/lib/db/schema/IndexedColumn.js +4 -1
- package/lib/db/schema/IndexedColumn.js.map +1 -1
- package/lib/db/schema/RawTable.d.ts +7 -1
- package/lib/db/schema/Schema.d.ts +10 -29
- package/lib/db/schema/Schema.js +3 -1
- package/lib/db/schema/Schema.js.map +1 -1
- package/lib/db/schema/Table.d.ts +69 -91
- package/lib/db/schema/Table.js +88 -87
- package/lib/db/schema/Table.js.map +1 -1
- package/lib/index.d.ts +6 -22
- package/lib/index.js +5 -22
- package/lib/index.js.map +1 -1
- package/lib/types/types.d.ts +6 -0
- package/lib/utils/BaseObserver.d.ts +12 -0
- package/lib/utils/BaseObserver.js +3 -0
- package/lib/utils/BaseObserver.js.map +1 -1
- package/lib/utils/Logger.d.ts +60 -22
- package/lib/utils/Logger.js +38 -30
- package/lib/utils/Logger.js.map +1 -1
- package/lib/utils/MetaBaseObserver.d.ts +11 -11
- package/lib/utils/MetaBaseObserver.js +1 -50
- package/lib/utils/MetaBaseObserver.js.map +1 -1
- package/lib/utils/mutex.d.ts +3 -45
- package/lib/utils/mutex.js +1 -153
- package/lib/utils/mutex.js.map +1 -1
- package/package.json +9 -46
- package/src/attachments/AttachmentContext.ts +15 -13
- package/src/attachments/AttachmentErrorHandler.ts +6 -6
- package/src/attachments/AttachmentQueue.ts +99 -42
- package/src/attachments/AttachmentService.ts +7 -6
- package/src/attachments/LocalStorageAdapter.ts +14 -8
- package/src/attachments/README.md +2 -0
- package/src/attachments/RemoteStorageAdapter.ts +4 -4
- package/src/attachments/Schema.ts +14 -6
- package/src/attachments/SyncingService.ts +10 -6
- package/src/attachments/WatchedAttachmentItem.ts +3 -1
- package/src/client/CommonPowerSyncDatabase.ts +540 -0
- package/src/client/Query.ts +9 -0
- package/src/client/SQLOpenFactory.ts +47 -19
- package/src/client/compilableQueryWatch.ts +9 -4
- package/src/client/connection/PowerSyncBackendConnector.ts +6 -3
- package/src/client/connection/PowerSyncCredentials.ts +3 -0
- package/src/client/runOnSchemaChange.ts +5 -2
- package/src/client/sync/bucket/CrudBatch.ts +2 -0
- package/src/client/sync/bucket/CrudEntry.ts +10 -101
- package/src/client/sync/bucket/CrudTransaction.ts +3 -0
- package/src/client/sync/options.ts +77 -0
- package/src/client/sync/stream/JsonValue.ts +3 -0
- package/src/client/sync/sync-streams.ts +23 -10
- package/src/client/triggers/TriggerManager.ts +29 -59
- package/src/client/triggers/sanitizeSQL.ts +5 -0
- package/src/client/watched/GetAllQuery.ts +8 -4
- package/src/client/watched/WatchedQuery.ts +20 -11
- package/src/client/watched/processors/DifferentialQueryProcessor.ts +19 -219
- package/src/client/watched/processors/OnChangeQueryProcessor.ts +4 -109
- package/src/client/watched/processors/comparators.ts +8 -0
- package/src/db/DBAdapter.ts +160 -175
- package/src/db/QueryResult.ts +195 -0
- package/src/db/crud/SyncProgress.ts +8 -43
- package/src/db/crud/SyncStatus.ts +66 -216
- package/src/db/crud/UploadQueueStatus.ts +3 -0
- package/src/db/schema/Column.ts +22 -5
- package/src/db/schema/Index.ts +9 -3
- package/src/db/schema/IndexedColumn.ts +9 -3
- package/src/db/schema/RawTable.ts +7 -1
- package/src/db/schema/Schema.ts +12 -7
- package/src/db/schema/Table.ts +130 -180
- package/src/index.ts +6 -22
- package/src/types/types.ts +6 -0
- package/src/utils/BaseObserver.ts +12 -0
- package/src/utils/Logger.ts +86 -31
- package/src/utils/MetaBaseObserver.ts +14 -60
- package/src/utils/mutex.ts +4 -189
- package/dist/bundle.cjs +0 -14136
- package/dist/bundle.cjs.map +0 -1
- package/dist/bundle.mjs +0 -14056
- package/dist/bundle.mjs.map +0 -1
- package/dist/bundle.node.cjs +0 -11613
- package/dist/bundle.node.cjs.map +0 -1
- package/dist/bundle.node.mjs +0 -11533
- package/dist/bundle.node.mjs.map +0 -1
- package/dist/index.d.cts +0 -4052
- package/legacy/sync_protocol.d.ts +0 -103
- package/lib/client/AbstractPowerSyncDatabase.js +0 -987
- package/lib/client/AbstractPowerSyncDatabase.js.map +0 -1
- package/lib/client/AbstractPowerSyncOpenFactory.d.ts +0 -22
- package/lib/client/AbstractPowerSyncOpenFactory.js +0 -25
- package/lib/client/AbstractPowerSyncOpenFactory.js.map +0 -1
- package/lib/client/ConnectionManager.d.ts +0 -112
- package/lib/client/ConnectionManager.js +0 -294
- package/lib/client/ConnectionManager.js.map +0 -1
- package/lib/client/CustomQuery.d.ts +0 -22
- package/lib/client/CustomQuery.js +0 -43
- package/lib/client/CustomQuery.js.map +0 -1
- package/lib/client/constants.d.ts +0 -1
- package/lib/client/constants.js +0 -2
- package/lib/client/constants.js.map +0 -1
- package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +0 -40
- package/lib/client/sync/bucket/BucketStorageAdapter.js +0 -19
- package/lib/client/sync/bucket/BucketStorageAdapter.js.map +0 -1
- package/lib/client/sync/bucket/SqliteBucketStorage.d.ts +0 -34
- package/lib/client/sync/bucket/SqliteBucketStorage.js +0 -162
- package/lib/client/sync/bucket/SqliteBucketStorage.js.map +0 -1
- package/lib/client/sync/stream/AbstractRemote.d.ts +0 -151
- package/lib/client/sync/stream/AbstractRemote.js +0 -473
- package/lib/client/sync/stream/AbstractRemote.js.map +0 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +0 -218
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +0 -663
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +0 -1
- package/lib/client/sync/stream/WebsocketClientTransport.d.ts +0 -15
- package/lib/client/sync/stream/WebsocketClientTransport.js +0 -61
- package/lib/client/sync/stream/WebsocketClientTransport.js.map +0 -1
- package/lib/client/sync/stream/core-instruction.d.ts +0 -71
- package/lib/client/sync/stream/core-instruction.js +0 -27
- package/lib/client/sync/stream/core-instruction.js.map +0 -1
- package/lib/client/triggers/MemoryTriggerClaimManager.d.ts +0 -6
- package/lib/client/triggers/MemoryTriggerClaimManager.js +0 -21
- package/lib/client/triggers/MemoryTriggerClaimManager.js.map +0 -1
- package/lib/client/triggers/TriggerManagerImpl.d.ts +0 -39
- package/lib/client/triggers/TriggerManagerImpl.js +0 -405
- package/lib/client/triggers/TriggerManagerImpl.js.map +0 -1
- package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +0 -68
- package/lib/client/watched/processors/AbstractQueryProcessor.js +0 -151
- package/lib/client/watched/processors/AbstractQueryProcessor.js.map +0 -1
- package/lib/db/ConnectionClosedError.d.ts +0 -10
- package/lib/db/ConnectionClosedError.js +0 -21
- package/lib/db/ConnectionClosedError.js.map +0 -1
- package/lib/db/schema/TableV2.d.ts +0 -9
- package/lib/db/schema/TableV2.js +0 -9
- package/lib/db/schema/TableV2.js.map +0 -1
- package/lib/utils/AbortOperation.d.ts +0 -9
- package/lib/utils/AbortOperation.js +0 -19
- package/lib/utils/AbortOperation.js.map +0 -1
- package/lib/utils/ControlledExecutor.d.ts +0 -25
- package/lib/utils/ControlledExecutor.js +0 -51
- package/lib/utils/ControlledExecutor.js.map +0 -1
- package/lib/utils/async.d.ts +0 -14
- package/lib/utils/async.js +0 -46
- package/lib/utils/async.js.map +0 -1
- package/lib/utils/parseQuery.d.ts +0 -6
- package/lib/utils/parseQuery.js +0 -17
- package/lib/utils/parseQuery.js.map +0 -1
- package/lib/utils/queue.d.ts +0 -16
- package/lib/utils/queue.js +0 -42
- package/lib/utils/queue.js.map +0 -1
- package/lib/utils/stream_transform.d.ts +0 -39
- package/lib/utils/stream_transform.js +0 -206
- package/lib/utils/stream_transform.js.map +0 -1
- package/src/client/AbstractPowerSyncDatabase.ts +0 -1362
- package/src/client/AbstractPowerSyncOpenFactory.ts +0 -39
- package/src/client/ConnectionManager.ts +0 -402
- package/src/client/CustomQuery.ts +0 -56
- package/src/client/constants.ts +0 -1
- package/src/client/sync/bucket/BucketStorageAdapter.ts +0 -49
- package/src/client/sync/bucket/SqliteBucketStorage.ts +0 -199
- package/src/client/sync/stream/AbstractRemote.ts +0 -602
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +0 -905
- package/src/client/sync/stream/WebsocketClientTransport.ts +0 -80
- package/src/client/sync/stream/core-instruction.ts +0 -98
- package/src/client/triggers/MemoryTriggerClaimManager.ts +0 -25
- package/src/client/triggers/TriggerManagerImpl.ts +0 -496
- package/src/client/watched/processors/AbstractQueryProcessor.ts +0 -226
- package/src/db/ConnectionClosedError.ts +0 -23
- package/src/db/schema/TableV2.ts +0 -9
- package/src/utils/AbortOperation.ts +0 -17
- package/src/utils/ControlledExecutor.ts +0 -72
- package/src/utils/async.ts +0 -50
- package/src/utils/parseQuery.ts +0 -25
- package/src/utils/queue.ts +0 -48
- 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
|
-
}
|
package/src/db/schema/TableV2.ts
DELETED
|
@@ -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
|
-
}
|
package/src/utils/async.ts
DELETED
|
@@ -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
|
-
}
|
package/src/utils/parseQuery.ts
DELETED
|
@@ -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
|
-
};
|
package/src/utils/queue.ts
DELETED
|
@@ -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
|
-
}
|