1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Sync Lock Module
*
* Coordinates concurrent sync calls using the Web Locks API.
*
* Behavior:
* - Same-method coalescing: if a sync of the same method is in progress,
* subsequent callers share its result promise
* - Different-method serialization: different methods (e.g. syncState vs
* syncNoteTransport) wait for each other via the Web Lock, or via an
* in-process per-dbId promise chain when Web Locks are unavailable
* - Web Locks also serialize across tabs (Chrome 69+, Safari 15.4+)
*/
/**
* Check if the Web Locks API is available.
*/
export
// Coalesce map keyed by `${dbId}:${methodId}` -> in-flight promise.
const inFlight = ;
// Per-dbId promise tail used to serialize cross-method calls when Web Locks
// are unavailable. Each new task chains onto the current tail so different
// methods on the same dbId run sequentially within the tab.
const fallbackTails = ;
/**
* Build the coalesce-map key for an in-flight sync of `(dbId, methodId)`.
*
* @param {string} dbId
* @param {string} methodId
* @returns {string}
*/
/**
* Run `fn` while holding the per-db Web Lock. When Web Locks are unavailable,
* serializes `fn` against any other in-flight call on the same `dbId` via an
* in-process promise chain — the wasm-bindgen `WebClient` uses a synchronous
* `RefCell` for interior mutability in the browser, so overlapping
* cross-method borrows would throw "recursive use of an object detected
* which would lead to unsafe aliasing in rust".
*
* @param {string} dbId
* @param {() => Promise<T>} fn
* @returns {Promise<T>}
* @template T
*/
/**
* Run `fn` under the sync lock for (dbId, methodId).
*
* Concurrent calls with the same (dbId, methodId) share the same promise
* (coalescing). Concurrent calls on the same dbId with different methodIds
* serialize via the Web Lock.
*
* @param {string} dbId - Database ID
* @param {string} methodId - Method identifier (see MethodName constants)
* @param {() => Promise<T>} fn - Work to run under the lock
* @returns {Promise<T>}
*/
export