@powersync/common 1.53.2 → 1.55.0
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/bundle.cjs +922 -772
- package/dist/bundle.cjs.map +1 -1
- package/dist/bundle.mjs +922 -772
- package/dist/bundle.mjs.map +1 -1
- package/dist/bundle.node.cjs +923 -619
- package/dist/bundle.node.cjs.map +1 -1
- package/dist/bundle.node.mjs +923 -619
- package/dist/bundle.node.mjs.map +1 -1
- package/dist/index.d.cts +749 -205
- package/lib/attachments/AttachmentContext.d.ts +7 -6
- package/lib/attachments/AttachmentContext.js +2 -1
- package/lib/attachments/AttachmentContext.js.map +1 -1
- package/lib/attachments/AttachmentErrorHandler.d.ts +6 -6
- package/lib/attachments/AttachmentQueue.d.ts +82 -33
- package/lib/attachments/AttachmentQueue.js +16 -18
- package/lib/attachments/AttachmentQueue.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 +12 -4
- package/lib/attachments/Schema.js +8 -3
- package/lib/attachments/Schema.js.map +1 -1
- package/lib/attachments/WatchedAttachmentItem.d.ts +3 -1
- package/lib/client/AbstractPowerSyncDatabase.d.ts +110 -60
- package/lib/client/AbstractPowerSyncDatabase.js +77 -74
- package/lib/client/AbstractPowerSyncDatabase.js.map +1 -1
- package/lib/client/AbstractPowerSyncOpenFactory.d.ts +6 -0
- package/lib/client/AbstractPowerSyncOpenFactory.js +3 -0
- package/lib/client/AbstractPowerSyncOpenFactory.js.map +1 -1
- package/lib/client/ConnectionManager.d.ts +4 -1
- package/lib/client/ConnectionManager.js +1 -1
- package/lib/client/ConnectionManager.js.map +1 -1
- package/lib/client/Query.d.ts +9 -0
- package/lib/client/SQLOpenFactory.d.ts +12 -0
- package/lib/client/SQLOpenFactory.js +6 -0
- package/lib/client/SQLOpenFactory.js.map +1 -1
- package/lib/client/compilableQueryWatch.d.ts +6 -0
- package/lib/client/compilableQueryWatch.js +3 -0
- package/lib/client/compilableQueryWatch.js.map +1 -1
- package/lib/client/connection/PowerSyncBackendConnector.d.ts +3 -0
- package/lib/client/connection/PowerSyncCredentials.d.ts +3 -0
- package/lib/client/constants.d.ts +3 -0
- package/lib/client/constants.js +3 -0
- package/lib/client/constants.js.map +1 -1
- package/lib/client/runOnSchemaChange.d.ts +3 -0
- package/lib/client/runOnSchemaChange.js +3 -0
- package/lib/client/runOnSchemaChange.js.map +1 -1
- package/lib/client/sync/bucket/BucketStorageAdapter.d.ts +12 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.js +6 -0
- package/lib/client/sync/bucket/BucketStorageAdapter.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 -0
- package/lib/client/sync/bucket/CrudEntry.js +4 -0
- 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/bucket/SqliteBucketStorage.d.ts +3 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.js +3 -0
- package/lib/client/sync/bucket/SqliteBucketStorage.js.map +1 -1
- package/lib/client/sync/stream/AbstractRemote.d.ts +30 -1
- package/lib/client/sync/stream/AbstractRemote.js +59 -27
- package/lib/client/sync/stream/AbstractRemote.js.map +1 -1
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.d.ts +55 -5
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js +32 -4
- package/lib/client/sync/stream/AbstractStreamingSyncImplementation.js.map +1 -1
- package/lib/client/sync/stream/JsonValue.d.ts +3 -0
- package/lib/client/sync/stream/WebsocketClientTransport.js +2 -1
- package/lib/client/sync/stream/WebsocketClientTransport.js.map +1 -1
- package/lib/client/sync/sync-streams.d.ts +22 -7
- package/lib/client/triggers/TriggerManager.d.ts +19 -18
- package/lib/client/triggers/TriggerManager.js +2 -1
- package/lib/client/triggers/TriggerManager.js.map +1 -1
- package/lib/client/triggers/TriggerManagerImpl.d.ts +1 -1
- package/lib/client/triggers/TriggerManagerImpl.js +3 -3
- package/lib/client/triggers/TriggerManagerImpl.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 +4 -0
- package/lib/client/watched/GetAllQuery.js +2 -0
- package/lib/client/watched/GetAllQuery.js.map +1 -1
- package/lib/client/watched/WatchedQuery.d.ts +24 -2
- package/lib/client/watched/WatchedQuery.js +9 -0
- package/lib/client/watched/WatchedQuery.js.map +1 -1
- package/lib/client/watched/processors/AbstractQueryProcessor.d.ts +1 -1
- package/lib/client/watched/processors/AbstractQueryProcessor.js.map +1 -1
- package/lib/client/watched/processors/DifferentialQueryProcessor.d.ts +20 -0
- package/lib/client/watched/processors/DifferentialQueryProcessor.js +4 -0
- package/lib/client/watched/processors/DifferentialQueryProcessor.js.map +1 -1
- package/lib/client/watched/processors/OnChangeQueryProcessor.d.ts +4 -0
- 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/ConnectionClosedError.d.ts +2 -0
- package/lib/db/ConnectionClosedError.js +2 -0
- package/lib/db/ConnectionClosedError.js.map +1 -1
- package/lib/db/DBAdapter.d.ts +56 -6
- package/lib/db/DBAdapter.js +15 -3
- package/lib/db/DBAdapter.js.map +1 -1
- package/lib/db/crud/SyncProgress.d.ts +6 -1
- package/lib/db/crud/SyncProgress.js +2 -0
- package/lib/db/crud/SyncProgress.js.map +1 -1
- package/lib/db/crud/SyncStatus.d.ts +36 -38
- package/lib/db/crud/SyncStatus.js +19 -14
- 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 +28 -0
- package/lib/db/schema/Column.js +16 -3
- package/lib/db/schema/Column.js.map +1 -1
- package/lib/db/schema/Index.d.ts +9 -0
- package/lib/db/schema/Index.js +6 -0
- package/lib/db/schema/Index.js.map +1 -1
- package/lib/db/schema/IndexedColumn.d.ts +9 -0
- package/lib/db/schema/IndexedColumn.js +6 -0
- 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 +6 -1
- package/lib/db/schema/Schema.js +3 -1
- package/lib/db/schema/Schema.js.map +1 -1
- package/lib/db/schema/Table.d.ts +27 -3
- package/lib/db/schema/Table.js +9 -0
- package/lib/db/schema/Table.js.map +1 -1
- package/lib/db/schema/TableV2.d.ts +2 -0
- package/lib/db/schema/TableV2.js +2 -0
- package/lib/db/schema/TableV2.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/types/types.d.ts +6 -0
- package/lib/utils/AbortOperation.d.ts +2 -0
- package/lib/utils/AbortOperation.js +2 -0
- package/lib/utils/AbortOperation.js.map +1 -1
- 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/ControlledExecutor.d.ts +6 -0
- package/lib/utils/ControlledExecutor.js +3 -0
- package/lib/utils/ControlledExecutor.js.map +1 -1
- package/lib/utils/Logger.d.ts +9 -0
- package/lib/utils/Logger.js +6 -0
- package/lib/utils/Logger.js.map +1 -1
- package/lib/utils/async.d.ts +26 -0
- package/lib/utils/async.js +114 -27
- package/lib/utils/async.js.map +1 -1
- package/lib/utils/compatibility.d.ts +8 -0
- package/lib/utils/compatibility.js +9 -0
- package/lib/utils/compatibility.js.map +1 -0
- package/lib/utils/mutex.d.ts +8 -0
- package/lib/utils/mutex.js +3 -0
- package/lib/utils/mutex.js.map +1 -1
- package/lib/utils/parseQuery.d.ts +6 -0
- package/lib/utils/parseQuery.js +3 -0
- package/lib/utils/parseQuery.js.map +1 -1
- package/lib/utils/stream_transform.d.ts +3 -1
- package/lib/utils/stream_transform.js.map +1 -1
- package/package.json +3 -3
- package/src/attachments/AttachmentContext.ts +7 -6
- package/src/attachments/AttachmentErrorHandler.ts +6 -6
- package/src/attachments/AttachmentQueue.ts +93 -35
- 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 +12 -4
- package/src/attachments/WatchedAttachmentItem.ts +3 -1
- package/src/client/AbstractPowerSyncDatabase.ts +135 -91
- package/src/client/AbstractPowerSyncOpenFactory.ts +6 -0
- package/src/client/ConnectionManager.ts +4 -1
- package/src/client/Query.ts +9 -0
- package/src/client/SQLOpenFactory.ts +12 -0
- package/src/client/compilableQueryWatch.ts +6 -0
- package/src/client/connection/PowerSyncBackendConnector.ts +3 -0
- package/src/client/connection/PowerSyncCredentials.ts +3 -0
- package/src/client/constants.ts +3 -0
- package/src/client/runOnSchemaChange.ts +3 -0
- package/src/client/sync/bucket/BucketStorageAdapter.ts +12 -0
- package/src/client/sync/bucket/CrudBatch.ts +2 -0
- package/src/client/sync/bucket/CrudEntry.ts +9 -0
- package/src/client/sync/bucket/CrudTransaction.ts +3 -0
- package/src/client/sync/bucket/SqliteBucketStorage.ts +3 -0
- package/src/client/sync/stream/AbstractRemote.ts +76 -34
- package/src/client/sync/stream/AbstractStreamingSyncImplementation.ts +55 -5
- package/src/client/sync/stream/JsonValue.ts +3 -0
- package/src/client/sync/stream/WebsocketClientTransport.ts +3 -1
- package/src/client/sync/sync-streams.ts +22 -9
- package/src/client/triggers/TriggerManager.ts +19 -18
- package/src/client/triggers/TriggerManagerImpl.ts +5 -5
- package/src/client/triggers/sanitizeSQL.ts +5 -0
- package/src/client/watched/GetAllQuery.ts +5 -1
- package/src/client/watched/WatchedQuery.ts +24 -2
- package/src/client/watched/processors/AbstractQueryProcessor.ts +6 -6
- package/src/client/watched/processors/DifferentialQueryProcessor.ts +28 -5
- package/src/client/watched/processors/OnChangeQueryProcessor.ts +9 -3
- package/src/client/watched/processors/comparators.ts +8 -0
- package/src/db/ConnectionClosedError.ts +2 -0
- package/src/db/DBAdapter.ts +58 -6
- package/src/db/crud/SyncProgress.ts +6 -1
- package/src/db/crud/SyncStatus.ts +40 -21
- package/src/db/crud/UploadQueueStatus.ts +3 -0
- package/src/db/schema/Column.ts +28 -3
- package/src/db/schema/Index.ts +9 -0
- package/src/db/schema/IndexedColumn.ts +9 -0
- package/src/db/schema/RawTable.ts +7 -1
- package/src/db/schema/Schema.ts +8 -3
- package/src/db/schema/Table.ts +30 -5
- package/src/db/schema/TableV2.ts +2 -0
- package/src/index.ts +1 -1
- package/src/types/types.ts +6 -0
- package/src/utils/AbortOperation.ts +2 -0
- package/src/utils/BaseObserver.ts +12 -0
- package/src/utils/ControlledExecutor.ts +6 -0
- package/src/utils/Logger.ts +9 -0
- package/src/utils/async.ts +136 -28
- package/src/utils/compatibility.ts +9 -0
- package/src/utils/mutex.ts +12 -0
- package/src/utils/parseQuery.ts +6 -0
- package/src/utils/stream_transform.ts +3 -1
package/src/index.ts
CHANGED
|
@@ -35,7 +35,7 @@ export * from './db/DBAdapter.js';
|
|
|
35
35
|
export * from './db/schema/Column.js';
|
|
36
36
|
export * from './db/schema/Index.js';
|
|
37
37
|
export * from './db/schema/IndexedColumn.js';
|
|
38
|
-
export {
|
|
38
|
+
export { PendingStatement, PendingStatementParameter, RawTableType } from './db/schema/RawTable.js';
|
|
39
39
|
export * from './db/schema/Schema.js';
|
|
40
40
|
export * from './db/schema/Table.js';
|
|
41
41
|
export * from './db/schema/TableV2.js';
|
package/src/types/types.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @public
|
|
3
|
+
*/
|
|
1
4
|
export interface CompilableQuery<T> {
|
|
2
5
|
execute(): Promise<T[]>;
|
|
3
6
|
compile(): CompiledQuery;
|
|
4
7
|
}
|
|
5
8
|
|
|
9
|
+
/**
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
6
12
|
export interface CompiledQuery {
|
|
7
13
|
readonly sql: string;
|
|
8
14
|
readonly parameters: ReadonlyArray<unknown>;
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
* Calls to Abortcontroller.abort(reason: any) will result in the
|
|
3
3
|
* `reason` being thrown. This is not necessarily an error,
|
|
4
4
|
* but extends error for better logging purposes.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
5
7
|
*/
|
|
6
8
|
export class AbortOperation extends Error {
|
|
7
9
|
constructor(protected reason: string) {
|
|
@@ -1,13 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @public
|
|
3
|
+
*/
|
|
1
4
|
export interface Disposable {
|
|
2
5
|
dispose: () => Promise<void> | void;
|
|
3
6
|
}
|
|
4
7
|
|
|
8
|
+
/**
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
5
11
|
export type BaseListener = Record<string, ((...event: any) => any) | undefined>;
|
|
6
12
|
|
|
13
|
+
/**
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
7
16
|
export interface BaseObserverInterface<T extends BaseListener> {
|
|
8
17
|
registerListener(listener: Partial<T>): () => void;
|
|
9
18
|
}
|
|
10
19
|
|
|
20
|
+
/**
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
11
23
|
export class BaseObserver<T extends BaseListener = BaseListener> implements BaseObserverInterface<T> {
|
|
12
24
|
protected listeners = new Set<Partial<T>>();
|
|
13
25
|
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal
|
|
3
|
+
*/
|
|
1
4
|
export interface ControlledExecutorOptions {
|
|
2
5
|
/**
|
|
3
6
|
* If throttling is enabled, it ensures only one task runs at a time,
|
|
@@ -7,6 +10,9 @@ export interface ControlledExecutorOptions {
|
|
|
7
10
|
throttleEnabled?: boolean;
|
|
8
11
|
}
|
|
9
12
|
|
|
13
|
+
/**
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
10
16
|
export class ControlledExecutor<T> {
|
|
11
17
|
private task: (param: T) => Promise<void> | void;
|
|
12
18
|
|
package/src/utils/Logger.ts
CHANGED
|
@@ -4,6 +4,9 @@ export { GlobalLogger, ILogger, ILoggerOpts, ILogHandler, ILogLevel } from 'js-l
|
|
|
4
4
|
|
|
5
5
|
const TypedLogger: ILogger = Logger as any;
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* @public
|
|
9
|
+
*/
|
|
7
10
|
export const LogLevel = {
|
|
8
11
|
TRACE: TypedLogger.TRACE,
|
|
9
12
|
DEBUG: TypedLogger.DEBUG,
|
|
@@ -14,6 +17,9 @@ export const LogLevel = {
|
|
|
14
17
|
OFF: TypedLogger.OFF
|
|
15
18
|
};
|
|
16
19
|
|
|
20
|
+
/**
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
17
23
|
export interface CreateLoggerOptions {
|
|
18
24
|
logLevel?: ILogLevel;
|
|
19
25
|
}
|
|
@@ -25,6 +31,7 @@ export interface CreateLoggerOptions {
|
|
|
25
31
|
* across all loggers created with `createLogger`. Adjusting settings on this
|
|
26
32
|
* base logger affects all loggers derived from it unless explicitly overridden.
|
|
27
33
|
*
|
|
34
|
+
* @public
|
|
28
35
|
*/
|
|
29
36
|
export function createBaseLogger() {
|
|
30
37
|
return Logger;
|
|
@@ -36,6 +43,8 @@ export function createBaseLogger() {
|
|
|
36
43
|
* Named loggers allow specific modules or areas of your application to have
|
|
37
44
|
* their own logging levels and behaviors. These loggers inherit configuration
|
|
38
45
|
* from the base logger by default but can override settings independently.
|
|
46
|
+
*
|
|
47
|
+
* @public
|
|
39
48
|
*/
|
|
40
49
|
export function createLogger(name: string, options: CreateLoggerOptions = {}): ILogger {
|
|
41
50
|
const logger = Logger.get(name);
|
package/src/utils/async.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { symbolAsyncIterator } from './compatibility.js';
|
|
2
|
+
import { doneResult, valueResult } from './stream_transform.js';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Throttle a function to be called at most once every "wait" milliseconds,
|
|
3
6
|
* on the trailing edge.
|
|
@@ -34,44 +37,149 @@ export interface AsyncNotifier {
|
|
|
34
37
|
}
|
|
35
38
|
|
|
36
39
|
export function asyncNotifier(): AsyncNotifier {
|
|
37
|
-
|
|
38
|
-
let hasPendingNotification = false;
|
|
40
|
+
const queue = new EventQueue<void>();
|
|
39
41
|
|
|
40
42
|
return {
|
|
41
43
|
notify() {
|
|
42
|
-
if (
|
|
43
|
-
|
|
44
|
-
waitingConsumer = null;
|
|
44
|
+
if (queue.countOutstandingEvents > 0) {
|
|
45
|
+
// Already has an outstanding event, no need to buffer another one.
|
|
45
46
|
} else {
|
|
46
|
-
|
|
47
|
+
queue.notify();
|
|
47
48
|
}
|
|
48
49
|
},
|
|
49
50
|
waitForNotification(signal: AbortSignal) {
|
|
50
|
-
return
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
return queue.waitForEvent(signal);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
type QueueWaiter<T> = { resolve: (event: T) => void; reject: (error: unknown) => void };
|
|
57
|
+
|
|
58
|
+
export interface QueueOptions {
|
|
59
|
+
eventDelivered?: () => void;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export class EventQueue<T> {
|
|
63
|
+
private waitingConsumer: QueueWaiter<T> | undefined;
|
|
64
|
+
private readonly outstandingEvents: Array<(waiter: QueueWaiter<T>) => void>;
|
|
65
|
+
|
|
66
|
+
constructor(private readonly options: QueueOptions = {}) {
|
|
67
|
+
this.outstandingEvents = [];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The amount of buffered events not yet dispatched to listeners.
|
|
72
|
+
*/
|
|
73
|
+
get countOutstandingEvents(): number {
|
|
74
|
+
return this.outstandingEvents.length;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private notifyInner(dispatch: (waiter: QueueWaiter<T>) => void) {
|
|
78
|
+
const existing = this.waitingConsumer;
|
|
79
|
+
this.waitingConsumer = undefined;
|
|
80
|
+
|
|
81
|
+
const dispatchAndNotifyListeners = (waiter: QueueWaiter<T>) => {
|
|
82
|
+
dispatch(waiter);
|
|
83
|
+
this.options.eventDelivered?.();
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
if (existing) {
|
|
87
|
+
dispatchAndNotifyListeners(existing);
|
|
88
|
+
} else {
|
|
89
|
+
this.outstandingEvents.push(dispatchAndNotifyListeners);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
notify(value: T) {
|
|
94
|
+
this.notifyInner((l) => l.resolve(value));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
notifyError(error: unknown) {
|
|
98
|
+
this.notifyInner((l) => l.reject(error));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
waitForEvent(signal: AbortSignal): Promise<T | undefined> {
|
|
102
|
+
return new Promise((resolve, reject) => {
|
|
103
|
+
if (this.waitingConsumer != null) {
|
|
104
|
+
throw new Error('Illegal call to waitForEvent, already has a waiter.');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const complete = () => {
|
|
108
|
+
signal?.removeEventListener('abort', onAbort);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const onAbort = () => {
|
|
112
|
+
complete();
|
|
113
|
+
this.waitingConsumer = undefined;
|
|
114
|
+
resolve(undefined);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const waiter: QueueWaiter<T> = {
|
|
118
|
+
resolve: (value) => {
|
|
119
|
+
complete();
|
|
120
|
+
resolve(value);
|
|
121
|
+
},
|
|
122
|
+
reject: (error) => {
|
|
123
|
+
complete();
|
|
124
|
+
reject(error);
|
|
53
125
|
}
|
|
126
|
+
};
|
|
54
127
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
128
|
+
if (signal.aborted) {
|
|
129
|
+
resolve(undefined);
|
|
130
|
+
} else if (this.countOutstandingEvents > 0) {
|
|
131
|
+
const [event] = this.outstandingEvents.splice(0, 1);
|
|
132
|
+
event(waiter);
|
|
133
|
+
} else {
|
|
134
|
+
this.waitingConsumer = waiter;
|
|
135
|
+
signal.addEventListener('abort', onAbort);
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}
|
|
65
139
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
140
|
+
/**
|
|
141
|
+
* Creates an async iterable backed by event queues.
|
|
142
|
+
*
|
|
143
|
+
* @param run A function invoked for every new listener. It receives a queue backing the async iterator.
|
|
144
|
+
* @param abort An additional abort signal. The `run` callback will also be aborted when `AsyncIterator.return` is
|
|
145
|
+
* called.
|
|
146
|
+
* @returns An object conforming to the async iterable protocol.
|
|
147
|
+
*/
|
|
148
|
+
static queueBasedAsyncIterable<T>(
|
|
149
|
+
run: (queue: EventQueue<T>, abort: AbortSignal) => void,
|
|
150
|
+
abort?: AbortSignal
|
|
151
|
+
): AsyncIterable<T> {
|
|
152
|
+
return {
|
|
153
|
+
[symbolAsyncIterator]: () => {
|
|
154
|
+
const queue = new EventQueue<T>();
|
|
155
|
+
const controller = new AbortController();
|
|
70
156
|
|
|
71
|
-
|
|
72
|
-
|
|
157
|
+
function dispose() {
|
|
158
|
+
controller.abort();
|
|
159
|
+
abort?.removeEventListener('abort', dispose);
|
|
73
160
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
161
|
+
|
|
162
|
+
if (abort) {
|
|
163
|
+
if (abort.aborted) {
|
|
164
|
+
controller.abort();
|
|
165
|
+
} else {
|
|
166
|
+
abort.addEventListener('abort', dispose);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
run(queue, controller.signal);
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
async next(): Promise<IteratorResult<T>> {
|
|
174
|
+
const event = await queue.waitForEvent(controller.signal);
|
|
175
|
+
return event == null ? doneResult : valueResult(event);
|
|
176
|
+
},
|
|
177
|
+
async return(): Promise<IteratorResult<T>> {
|
|
178
|
+
dispose();
|
|
179
|
+
return doneResult;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
77
185
|
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Some JavaScript engines, in particular older versions of React Native, don't support Symbol.asyncIterator.
|
|
3
|
+
*
|
|
4
|
+
* For those, users relying on async generators typically lower them with a transpiler and [this polyfill](https://github.com/Azure/azure-sdk-for-js/blob/%40azure/core-asynciterator-polyfill_1.0.2/sdk/core/core-asynciterator-polyfill/src/index.ts#L4-L6).
|
|
5
|
+
* This definition is compatible with that polyfill, so transpiled apps can use async iterables created by the PowerSync
|
|
6
|
+
* SDK.
|
|
7
|
+
*/
|
|
8
|
+
export const symbolAsyncIterator: typeof Symbol.asyncIterator =
|
|
9
|
+
Symbol.asyncIterator ?? Symbol.for('Symbol.asyncIterator');
|
package/src/utils/mutex.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { Queue } from './queue.js';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
3
6
|
export type UnlockFn = () => void;
|
|
4
7
|
|
|
5
8
|
/**
|
|
@@ -178,10 +181,19 @@ export class Mutex {
|
|
|
178
181
|
|
|
179
182
|
/**
|
|
180
183
|
* Creates a signal aborting after the set timeout.
|
|
184
|
+
*
|
|
185
|
+
* @internal
|
|
181
186
|
*/
|
|
182
187
|
export function timeoutSignal(timeout: number): AbortSignal;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* @internal
|
|
191
|
+
*/
|
|
183
192
|
export function timeoutSignal(timeout?: number): AbortSignal | undefined;
|
|
184
193
|
|
|
194
|
+
/**
|
|
195
|
+
* @internal
|
|
196
|
+
*/
|
|
185
197
|
export function timeoutSignal(timeout?: number): AbortSignal | undefined {
|
|
186
198
|
if (timeout == null) return;
|
|
187
199
|
if ('timeout' in AbortSignal) return AbortSignal.timeout(timeout);
|
package/src/utils/parseQuery.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import type { CompilableQuery } from '../types/types.js';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
3
6
|
export interface ParsedQuery {
|
|
4
7
|
sqlStatement: string;
|
|
5
8
|
parameters: any[];
|
|
6
9
|
}
|
|
7
10
|
|
|
11
|
+
/**
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
8
14
|
export const parseQuery = <T>(query: string | CompilableQuery<T>, parameters: any[]): ParsedQuery => {
|
|
9
15
|
let sqlStatement: string;
|
|
10
16
|
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
* An async iterator that can't be cancelled.
|
|
3
3
|
*
|
|
4
4
|
* To keep data flow simple, we always pass an explicit cancellation token when subscribing to async streams. Once the
|
|
5
|
-
*
|
|
5
|
+
* `AbortSignal` aborts, iterators are supposed to clean up and then emit a final `{done: true}` event. This means
|
|
6
6
|
* that there's no way to distinguish between streams that have completed normally and streams that have been cancelled,
|
|
7
7
|
* but that is acceptable for our uses of this.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
8
10
|
*/
|
|
9
11
|
export type SimpleAsyncIterator<T> = Pick<AsyncIterator<T>, 'next'>;
|
|
10
12
|
|