@@ -5,7 +5,6 @@ import { ErrorAsyncLocksLockBoxConflict } from './errors';
5
5
6
6
class LockBox < L extends Lockable = Lockable > implements Lockable {
7
7
protected _locks : Map < string , L > = new Map ( ) ;
8
- // protected lockReleasers: Map<string, ResourceRelease> = new Map();
9
8
10
9
public lock ( ...requests : Array < LockRequest < L > > ) : ResourceAcquire < LockBox < L > > {
11
10
return async ( ) => {
@@ -44,24 +43,11 @@ class LockBox<L extends Lockable = Lockable> implements Lockable {
44
43
const lockAcquire = lock . lock ( ...lockingParams ) ;
45
44
const [ lockRelease ] = await lockAcquire ( ) ;
46
45
locks . push ( [ key , lockRelease , lock ] ) ;
47
-
48
- // This doesn't work if we use RWLockWriter
49
- // this is because the lockRelease can be multiple ones
50
- // for any given lock, you may have multiple readers
51
- // locking a single key, this ends up overwriting it...
52
- // this means it is not equivalent
53
- // and would have to be held by the user
54
- // but the user can't do this unless it has direct access
55
- // because otherwise it has overlapping locks
56
-
57
- // this.lockReleasers.set(key, lockRelease);
58
-
59
46
}
60
47
} catch ( e ) {
61
48
// Release all intermediate locks in reverse order
62
49
locks . reverse ( ) ;
63
50
for ( const [ key , lockRelease , lock ] of locks ) {
64
- // this.lockReleasers.delete(key);
65
51
await lockRelease ( ) ;
66
52
// If it is still locked, then it is held by a different context
67
53
// only delete if no contexts are locking the lock
@@ -79,14 +65,7 @@ class LockBox<L extends Lockable = Lockable> implements Lockable {
79
65
// Release all locks in reverse order
80
66
locks . reverse ( ) ;
81
67
for ( const [ key , lockRelease , lock ] of locks ) {
82
-
83
- // // The lock may already been unlocked early
84
- // if (this.lockReleasers.has(key)) {
85
- // this.lockReleasers.delete(key);
86
- // }
87
-
88
68
await lockRelease ( ) ;
89
-
90
69
// If it is still locked, then it is held by a different context
91
70
// only delete if no contexts are locking the lock
92
71
if ( ! lock . isLocked ( ) ) {
@@ -99,13 +78,18 @@ class LockBox<L extends Lockable = Lockable> implements Lockable {
99
78
} ;
100
79
}
101
80
102
- public lockMulti ( ...requests : Array < LockRequest < L > > ) : Array < [ ToString , ResourceAcquire < L > ] > {
81
+ public lockMulti (
82
+ ...requests : Array < LockRequest < L > >
83
+ ) : Array < [ ToString , ResourceAcquire < L > ] > {
103
84
// Convert to strings
104
85
// This creates a copy of the requests
105
- let requests_ : Array < [ string , ToString , new ( ) => L , ...Parameters < L [ 'lock' ] > ] > =
106
- requests . map ( ( [ key , ...rest ] ) =>
107
- typeof key === 'string' ? [ key , key , ...rest ] : [ key . toString ( ) , key , ...rest ] ,
108
- ) ;
86
+ let requests_ : Array <
87
+ [ string , ToString , new ( ) => L , ...Parameters < L [ 'lock' ] > ]
88
+ > = requests . map ( ( [ key , ...rest ] ) =>
89
+ typeof key === 'string'
90
+ ? [ key , key , ...rest ]
91
+ : [ key . toString ( ) , key , ...rest ] ,
92
+ ) ;
109
93
// Sort to ensure lock hierarchy
110
94
requests_ . sort ( ( [ key1 ] , [ key2 ] ) => {
111
95
// Deterministic string comparison according to 16-bit code units
@@ -158,31 +142,14 @@ class LockBox<L extends Lockable = Lockable> implements Lockable {
158
142
this . _locks . delete ( key ) ;
159
143
}
160
144
} ,
161
- lock
145
+ lock ,
162
146
] ;
163
- }
147
+ } ,
164
148
] ) ;
165
149
}
166
150
return lockAcquires ;
167
151
}
168
152
169
- // /**
170
- // * Unlock a sequence of lock keys
171
- // * Unlocking will be done in the order of the keys
172
- // * This allows imperative early unlocking of keys
173
- // * Prefer to use the lock releaser returned by `this.lock`
174
- // */
175
- // public async unlock(...keys: Array<ToString>): Promise<void> {
176
- // for (const k of keys) {
177
- // const key = k.toString();
178
- // const lockRelease = this.lockReleasers.get(key);
179
- // if (lockRelease != null) {
180
- // this.lockReleasers.delete(key);
181
- // await lockRelease();
182
- // }
183
- // }
184
- // }
185
-
186
153
get locks ( ) : ReadonlyMap < string , L > {
187
154
return this . _locks ;
188
155
}
@@ -246,14 +213,15 @@ class LockBox<L extends Lockable = Lockable> implements Lockable {
246
213
) : Promise < T > {
247
214
const f = params . pop ( ) as ( multiLocks : Array < [ ToString , L ] > ) => Promise < T > ;
248
215
const lockAcquires = this . lockMulti ( ...( params as Array < LockRequest < L > > ) ) ;
249
- const lockAcquires_ : Array < ResourceAcquire < [ ToString , L ] > > = lockAcquires . map (
250
- ( [ key , lockAcquire ] ) => ( ...r ) => lockAcquire ( ...r ) . then (
251
- ( [ lockRelease , lock ] ) => [
252
- lockRelease ,
253
- [ key , lock ]
254
- ] as [ ResourceRelease , [ ToString , L ] ]
255
- )
256
- ) ;
216
+ const lockAcquires_ : Array < ResourceAcquire < [ ToString , L ] > > =
217
+ lockAcquires . map (
218
+ ( [ key , lockAcquire ] ) =>
219
+ ( ...r ) =>
220
+ lockAcquire ( ...r ) . then (
221
+ ( [ lockRelease , lock ] ) =>
222
+ [ lockRelease , [ key , lock ] ] as [ ResourceRelease , [ ToString , L ] ] ,
223
+ ) ,
224
+ ) ;
257
225
return withF ( lockAcquires_ , f ) ;
258
226
}
259
227
@@ -275,24 +243,26 @@ class LockBox<L extends Lockable = Lockable> implements Lockable {
275
243
public withMultiG < T , TReturn , TNext > (
276
244
...params : [
277
245
...requests : Array < LockRequest < L > > ,
278
- g : ( multiLocks : Array < [ ToString , L ] > ) => AsyncGenerator < T , TReturn , TNext > ,
246
+ g : (
247
+ multiLocks : Array < [ ToString , L ] > ,
248
+ ) => AsyncGenerator < T , TReturn , TNext > ,
279
249
]
280
250
) {
281
251
const g = params . pop ( ) as (
282
252
multiLocks : Array < [ ToString , L ] > ,
283
253
) => AsyncGenerator < T , TReturn , TNext > ;
284
254
const lockAcquires = this . lockMulti ( ...( params as Array < LockRequest < L > > ) ) ;
285
- const lockAcquires_ : Array < ResourceAcquire < [ ToString , L ] > > = lockAcquires . map (
286
- ( [ key , lockAcquire ] ) => ( ...r ) => lockAcquire ( ...r ) . then (
287
- ( [ lockRelease , lock ] ) => [
288
- lockRelease ,
289
- [ key , lock ]
290
- ] as [ ResourceRelease , [ ToString , L ] ]
291
- )
292
- ) ;
255
+ const lockAcquires_ : Array < ResourceAcquire < [ ToString , L ] > > =
256
+ lockAcquires . map (
257
+ ( [ key , lockAcquire ] ) =>
258
+ ( ...r ) =>
259
+ lockAcquire ( ...r ) . then (
260
+ ( [ lockRelease , lock ] ) =>
261
+ [ lockRelease , [ key , lock ] ] as [ ResourceRelease , [ ToString , L ] ] ,
262
+ ) ,
263
+ ) ;
293
264
return withG ( lockAcquires_ , g ) ;
294
265
}
295
-
296
266
}
297
267
298
268
export default LockBox ;
0 commit comments