@powersync/common 0.0.0-dev-20260504100448 → 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.
- 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,7 +1,9 @@
|
|
|
1
|
-
import { BaseListener,
|
|
1
|
+
import { BaseListener, BaseObserverInterface } from './BaseObserver.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Represents the counts of listeners for each event type in a BaseListener.
|
|
5
|
+
*
|
|
6
|
+
* @public
|
|
5
7
|
*/
|
|
6
8
|
export type ListenerCounts<Listener extends BaseListener> = Partial<Record<keyof Listener, number>> & {
|
|
7
9
|
total: number;
|
|
@@ -9,73 +11,25 @@ export type ListenerCounts<Listener extends BaseListener> = Partial<Record<keyof
|
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
13
|
* Meta listener which reports the counts of listeners for each event type.
|
|
14
|
+
*
|
|
15
|
+
* @public
|
|
12
16
|
*/
|
|
13
17
|
export interface MetaListener<ParentListener extends BaseListener> extends BaseListener {
|
|
14
18
|
listenersChanged?: (counts: ListenerCounts<ParentListener>) => void;
|
|
15
19
|
}
|
|
16
20
|
|
|
17
|
-
|
|
18
|
-
|
|
21
|
+
/**
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
export interface ListenerMetaManager<Listener extends BaseListener> extends BaseObserverInterface<
|
|
25
|
+
MetaListener<Listener>
|
|
26
|
+
> {
|
|
19
27
|
counts: ListenerCounts<Listener>;
|
|
20
28
|
}
|
|
21
29
|
|
|
22
|
-
export interface MetaBaseObserverInterface<Listener extends BaseListener> extends BaseObserverInterface<Listener> {
|
|
23
|
-
listenerMeta: ListenerMetaManager<Listener>;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
30
|
/**
|
|
27
|
-
*
|
|
31
|
+
* @public
|
|
28
32
|
*/
|
|
29
|
-
export
|
|
30
|
-
|
|
31
|
-
implements MetaBaseObserverInterface<Listener>
|
|
32
|
-
{
|
|
33
|
-
protected get listenerCounts(): ListenerCounts<Listener> {
|
|
34
|
-
const counts = {} as Partial<Record<keyof Listener, number>>;
|
|
35
|
-
let total = 0;
|
|
36
|
-
for (const listener of this.listeners) {
|
|
37
|
-
for (const key in listener) {
|
|
38
|
-
if (listener[key]) {
|
|
39
|
-
counts[key] = (counts[key] ?? 0) + 1;
|
|
40
|
-
total++;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
return {
|
|
45
|
-
...counts,
|
|
46
|
-
total
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
get listenerMeta(): ListenerMetaManager<Listener> {
|
|
51
|
-
return {
|
|
52
|
-
counts: this.listenerCounts,
|
|
53
|
-
// Allows registering a meta listener that will be notified of changes in listener counts
|
|
54
|
-
registerListener: (listener: Partial<MetaListener<Listener>>) => {
|
|
55
|
-
return this.metaListener.registerListener(listener);
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
protected metaListener: BaseObserver<MetaListener<Listener>>;
|
|
61
|
-
|
|
62
|
-
constructor() {
|
|
63
|
-
super();
|
|
64
|
-
this.metaListener = new BaseObserver<MetaListener<Listener>>();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
registerListener(listener: Partial<Listener>): () => void {
|
|
68
|
-
const dispose = super.registerListener(listener);
|
|
69
|
-
const updatedCount = this.listenerCounts;
|
|
70
|
-
this.metaListener.iterateListeners((l) => {
|
|
71
|
-
l.listenersChanged?.(updatedCount);
|
|
72
|
-
});
|
|
73
|
-
return () => {
|
|
74
|
-
dispose();
|
|
75
|
-
const updatedCount = this.listenerCounts;
|
|
76
|
-
this.metaListener.iterateListeners((l) => {
|
|
77
|
-
l.listenersChanged?.(updatedCount);
|
|
78
|
-
});
|
|
79
|
-
};
|
|
80
|
-
}
|
|
33
|
+
export interface MetaBaseObserverInterface<Listener extends BaseListener> extends BaseObserverInterface<Listener> {
|
|
34
|
+
listenerMeta: ListenerMetaManager<Listener>;
|
|
81
35
|
}
|
package/src/utils/mutex.ts
CHANGED
|
@@ -1,192 +1,7 @@
|
|
|
1
|
-
import { Queue } from './queue.js';
|
|
2
|
-
|
|
3
|
-
export type UnlockFn = () => void;
|
|
4
|
-
|
|
5
1
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* @internal This class is meant to be used in PowerSync SDKs only, and is not part of the public API.
|
|
2
|
+
* @internal This is implemented in `@powersync/shared-internals`, but we need it in the attachment service
|
|
3
|
+
* implementation.
|
|
9
4
|
*/
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
private readonly available: Queue<T>;
|
|
13
|
-
|
|
14
|
-
readonly size: number;
|
|
15
|
-
// Linked list of waiters. We don't expect the wait list to become particularly large, and this allows removing
|
|
16
|
-
// aborted waiters from the middle of the list efficiently.
|
|
17
|
-
private firstWaiter?: SemaphoreWaitNode<T>;
|
|
18
|
-
private lastWaiter?: SemaphoreWaitNode<T>;
|
|
19
|
-
|
|
20
|
-
constructor(elements: Iterable<T>) {
|
|
21
|
-
this.available = new Queue(elements);
|
|
22
|
-
this.size = this.available.length;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
private addWaiter(requestedItems: number, onAcquire: () => void): SemaphoreWaitNode<T> {
|
|
26
|
-
const node: SemaphoreWaitNode<T> = {
|
|
27
|
-
isActive: true,
|
|
28
|
-
acquiredItems: [],
|
|
29
|
-
remainingItems: requestedItems,
|
|
30
|
-
onAcquire,
|
|
31
|
-
prev: this.lastWaiter
|
|
32
|
-
};
|
|
33
|
-
if (this.lastWaiter) {
|
|
34
|
-
this.lastWaiter.next = node;
|
|
35
|
-
this.lastWaiter = node;
|
|
36
|
-
} else {
|
|
37
|
-
// First waiter
|
|
38
|
-
this.lastWaiter = this.firstWaiter = node;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return node;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
private deactivateWaiter(waiter: SemaphoreWaitNode<T>) {
|
|
45
|
-
const { prev, next } = waiter;
|
|
46
|
-
waiter.isActive = false;
|
|
47
|
-
|
|
48
|
-
if (prev) prev.next = next;
|
|
49
|
-
if (next) next.prev = prev;
|
|
50
|
-
if (waiter == this.firstWaiter) this.firstWaiter = next;
|
|
51
|
-
if (waiter == this.lastWaiter) this.lastWaiter = prev;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
private requestPermits(amount: number, abort?: AbortSignal): Promise<{ items: T[]; release: UnlockFn }> {
|
|
55
|
-
if (amount <= 0 || amount > this.size) {
|
|
56
|
-
throw new Error(`Invalid amount of items requested (${amount}), must be between 1 and ${this.size}`);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return new Promise((resolve, reject) => {
|
|
60
|
-
function rejectAborted() {
|
|
61
|
-
reject(abort?.reason ?? new Error('Semaphore acquire aborted'));
|
|
62
|
-
}
|
|
63
|
-
if (abort?.aborted) {
|
|
64
|
-
return rejectAborted();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
let waiter: SemaphoreWaitNode<T>;
|
|
68
|
-
|
|
69
|
-
const markCompleted = () => {
|
|
70
|
-
const items = waiter.acquiredItems;
|
|
71
|
-
waiter.acquiredItems = []; // Avoid releasing items twice.
|
|
72
|
-
|
|
73
|
-
for (const element of items) {
|
|
74
|
-
// Give to next waiter, if possible.
|
|
75
|
-
const nextWaiter = this.firstWaiter;
|
|
76
|
-
if (nextWaiter) {
|
|
77
|
-
nextWaiter.acquiredItems.push(element);
|
|
78
|
-
nextWaiter.remainingItems--;
|
|
79
|
-
if (nextWaiter.remainingItems == 0) {
|
|
80
|
-
nextWaiter.onAcquire();
|
|
81
|
-
}
|
|
82
|
-
} else {
|
|
83
|
-
// No pending waiter, return lease into pool.
|
|
84
|
-
this.available.addLast(element);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
const onAbort = () => {
|
|
90
|
-
abort?.removeEventListener('abort', onAbort);
|
|
91
|
-
|
|
92
|
-
if (waiter.isActive) {
|
|
93
|
-
this.deactivateWaiter(waiter);
|
|
94
|
-
rejectAborted();
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
const resolvePromise = () => {
|
|
99
|
-
this.deactivateWaiter(waiter);
|
|
100
|
-
abort?.removeEventListener('abort', onAbort);
|
|
101
|
-
|
|
102
|
-
const items = waiter.acquiredItems;
|
|
103
|
-
resolve({ items, release: markCompleted });
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
waiter = this.addWaiter(amount, resolvePromise);
|
|
107
|
-
|
|
108
|
-
// If there are items in the pool that haven't been assigned, we can pull them into this waiter. Note that this is
|
|
109
|
-
// only the case if we're the first waiter (otherwise, items would have been assigned to an earlier waiter).
|
|
110
|
-
while (!this.available.isEmpty && waiter.remainingItems > 0) {
|
|
111
|
-
waiter.acquiredItems.push(this.available.removeFirst());
|
|
112
|
-
waiter.remainingItems--;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (waiter.remainingItems == 0) {
|
|
116
|
-
return resolvePromise();
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
abort?.addEventListener('abort', onAbort);
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Requests a single item from the pool.
|
|
125
|
-
*
|
|
126
|
-
* The returned `release` callback must be invoked to return the item into the pool.
|
|
127
|
-
*/
|
|
128
|
-
async requestOne(abort?: AbortSignal): Promise<{ item: T; release: UnlockFn }> {
|
|
129
|
-
const { items, release } = await this.requestPermits(1, abort);
|
|
130
|
-
return { release, item: items[0] };
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Requests access to all items from the pool.
|
|
135
|
-
*
|
|
136
|
-
* The returned `release` callback must be invoked to return items into the pool.
|
|
137
|
-
*/
|
|
138
|
-
requestAll(abort?: AbortSignal): Promise<{ items: T[]; release: UnlockFn }> {
|
|
139
|
-
return this.requestPermits(this.size, abort);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
interface SemaphoreWaitNode<T> {
|
|
144
|
-
/**
|
|
145
|
-
* Whether the waiter is currently active (not aborted and not fullfilled).
|
|
146
|
-
*/
|
|
147
|
-
isActive: boolean;
|
|
148
|
-
acquiredItems: T[];
|
|
149
|
-
remainingItems: number;
|
|
150
|
-
onAcquire: () => void;
|
|
151
|
-
prev?: SemaphoreWaitNode<T>;
|
|
152
|
-
next?: SemaphoreWaitNode<T>;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* An asynchronous mutex implementation.
|
|
157
|
-
*
|
|
158
|
-
* @internal This class is meant to be used in PowerSync SDKs only, and is not part of the public API.
|
|
159
|
-
*/
|
|
160
|
-
export class Mutex {
|
|
161
|
-
private inner = new Semaphore([null]);
|
|
162
|
-
|
|
163
|
-
async acquire(abort?: AbortSignal): Promise<UnlockFn> {
|
|
164
|
-
const { release } = await this.inner.requestOne(abort);
|
|
165
|
-
return release;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
async runExclusive<T>(fn: () => PromiseLike<T> | T, abort?: AbortSignal): Promise<T> {
|
|
169
|
-
const returnMutex = await this.acquire(abort);
|
|
170
|
-
|
|
171
|
-
try {
|
|
172
|
-
return await fn();
|
|
173
|
-
} finally {
|
|
174
|
-
returnMutex();
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Creates a signal aborting after the set timeout.
|
|
181
|
-
*/
|
|
182
|
-
export function timeoutSignal(timeout: number): AbortSignal;
|
|
183
|
-
export function timeoutSignal(timeout?: number): AbortSignal | undefined;
|
|
184
|
-
|
|
185
|
-
export function timeoutSignal(timeout?: number): AbortSignal | undefined {
|
|
186
|
-
if (timeout == null) return;
|
|
187
|
-
if ('timeout' in AbortSignal) return AbortSignal.timeout(timeout);
|
|
188
|
-
|
|
189
|
-
const controller = new AbortController();
|
|
190
|
-
setTimeout(() => controller.abort(new Error('Timeout waiting for lock')), timeout);
|
|
191
|
-
return controller.signal;
|
|
5
|
+
export interface Mutex {
|
|
6
|
+
runExclusive<T>(fn: () => PromiseLike<T> | T, abort?: AbortSignal): Promise<T>;
|
|
192
7
|
}
|