@powersync/common 1.49.0 → 1.51.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 +214 -39
- package/dist/bundle.cjs.map +1 -1
- package/dist/bundle.mjs +210 -37
- package/dist/bundle.mjs.map +1 -1
- package/dist/bundle.node.cjs +214 -38
- package/dist/bundle.node.cjs.map +1 -1
- package/dist/bundle.node.mjs +210 -36
- package/dist/bundle.node.mjs.map +1 -1
- package/dist/index.d.cts +75 -16
- package/lib/attachments/AttachmentQueue.d.ts +10 -4
- package/lib/attachments/AttachmentQueue.js +10 -4
- package/lib/attachments/AttachmentQueue.js.map +1 -1
- package/lib/attachments/AttachmentService.js +2 -3
- package/lib/attachments/AttachmentService.js.map +1 -1
- package/lib/attachments/SyncingService.d.ts +2 -1
- package/lib/attachments/SyncingService.js +4 -5
- package/lib/attachments/SyncingService.js.map +1 -1
- package/lib/client/AbstractPowerSyncDatabase.d.ts +5 -1
- package/lib/client/AbstractPowerSyncDatabase.js +6 -2
- package/lib/client/AbstractPowerSyncDatabase.js.map +1 -1
- package/lib/db/DBAdapter.d.ts +7 -1
- package/lib/db/DBAdapter.js.map +1 -1
- package/lib/utils/mutex.d.ts +47 -5
- package/lib/utils/mutex.js +146 -21
- package/lib/utils/mutex.js.map +1 -1
- package/lib/utils/queue.d.ts +16 -0
- package/lib/utils/queue.js +42 -0
- package/lib/utils/queue.js.map +1 -0
- package/package.json +1 -2
- package/src/attachments/AttachmentQueue.ts +10 -4
- package/src/attachments/AttachmentService.ts +2 -3
- package/src/attachments/README.md +6 -4
- package/src/attachments/SyncingService.ts +4 -5
- package/src/client/AbstractPowerSyncDatabase.ts +6 -2
- package/src/db/DBAdapter.ts +7 -1
- package/src/utils/mutex.ts +184 -26
- package/src/utils/queue.ts +48 -0
package/src/utils/mutex.ts
CHANGED
|
@@ -1,34 +1,192 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Queue } from './queue.js';
|
|
2
|
+
|
|
3
|
+
export type UnlockFn = () => void;
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
|
-
*
|
|
6
|
+
* An asynchronous semaphore implementation with associated items per lease.
|
|
7
|
+
*
|
|
8
|
+
* @internal This class is meant to be used in PowerSync SDKs only, and is not part of the public API.
|
|
5
9
|
*/
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
10
|
+
export class Semaphore<T> {
|
|
11
|
+
// Available items that are not currently assigned to a waiter.
|
|
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--;
|
|
24
113
|
}
|
|
25
|
-
if (timedOut) return;
|
|
26
114
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
} catch (ex) {
|
|
30
|
-
reject(ex);
|
|
115
|
+
if (waiter.remainingItems == 0) {
|
|
116
|
+
return resolvePromise();
|
|
31
117
|
}
|
|
118
|
+
|
|
119
|
+
abort?.addEventListener('abort', onAbort);
|
|
32
120
|
});
|
|
33
|
-
}
|
|
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;
|
|
34
192
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
}
|