File tree 3 files changed +25
-5
lines changed
3 files changed +25
-5
lines changed Original file line number Diff line number Diff line change @@ -86,7 +86,7 @@ class WebDatabase
86
86
Future <T > readLock <T >(Future <T > Function (SqliteReadContext tx) callback,
87
87
{Duration ? lockTimeout, String ? debugContext}) async {
88
88
if (_mutex case var mutex? ) {
89
- return await mutex.lock (() async {
89
+ return await mutex.lock (timeout : lockTimeout, () async {
90
90
final context = _SharedContext (this );
91
91
try {
92
92
return await callback (context);
@@ -135,7 +135,7 @@ class WebDatabase
135
135
Future <T > writeLock <T >(Future <T > Function (SqliteWriteContext tx) callback,
136
136
{Duration ? lockTimeout, String ? debugContext, bool ? flush}) async {
137
137
if (_mutex case var mutex? ) {
138
- return await mutex.lock (() async {
138
+ return await mutex.lock (timeout : lockTimeout, () async {
139
139
final context = _ExclusiveContext (this );
140
140
try {
141
141
return await callback (context);
Original file line number Diff line number Diff line change @@ -16,10 +16,9 @@ external Navigator get _navigator;
16
16
/// Web implementation of [Mutex]
17
17
class MutexImpl implements Mutex {
18
18
late final mutex.Mutex fallback;
19
- String ? identifier;
20
19
final String resolvedIdentifier;
21
20
22
- MutexImpl ({this . identifier})
21
+ MutexImpl ({String ? identifier})
23
22
24
23
/// On web a lock name is required for Navigator locks.
25
24
/// Having exclusive Mutex instances requires a somewhat unique lock name.
@@ -40,7 +39,7 @@ class MutexImpl implements Mutex {
40
39
41
40
@override
42
41
Future <T > lock <T >(Future <T > Function () callback, {Duration ? timeout}) {
43
- if (( _navigator as JSObject ). hasProperty ('locks' .toJS).toDart ) {
42
+ if (_navigator. has ('locks' ) ) {
44
43
return _webLock (callback, timeout: timeout);
45
44
} else {
46
45
return _fallbackLock (callback, timeout: timeout);
Original file line number Diff line number Diff line change @@ -209,6 +209,27 @@ void main() {
209
209
expect (await db.get ('SELECT description FROM test_data' ),
210
210
{'description' : 'test' });
211
211
});
212
+
213
+ test ('respects lock timeouts' , () async {
214
+ // Unfortunately this test can't use fakeAsync because it uses actual
215
+ // lock APIs on the web.
216
+ final db = await testUtils.setupDatabase (path: path);
217
+ final lockAcquired = Completer ();
218
+
219
+ final completion = db.writeLock ((context) async {
220
+ lockAcquired.complete ();
221
+ await Future .delayed (const Duration (seconds: 1 ));
222
+ });
223
+
224
+ await lockAcquired.future;
225
+ await expectLater (
226
+ () => db.writeLock (
227
+ lockTimeout: Duration (milliseconds: 200 ), (_) async => {}),
228
+ throwsA (isA <TimeoutException >()),
229
+ );
230
+
231
+ await completion;
232
+ });
212
233
});
213
234
}
214
235
You can’t perform that action at this time.
0 commit comments