Skip to content

Commit

Permalink
Fix exclusively connection close on web
Browse files Browse the repository at this point in the history
Closes #3089
  • Loading branch information
simolus3 committed Jul 12, 2024
1 parent db72e13 commit 7b2168c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
5 changes: 5 additions & 0 deletions drift/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.19.1

- Fix `exclusively` breaking the database connection on the web. Please note
that this requires an updated `drift_worker.js` to fix.

## 2.19.0

- Add `exclusively` method to database classes, allowing a block to temporarily
Expand Down
7 changes: 4 additions & 3 deletions drift/lib/src/web/wasm_setup/shared.dart
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ class DriftServerController {
);

if (close != null) {
return db.interceptWith(_CloseVfsOnClose(close));
return db.interceptWith(_CloseVfsOnClose(db, close));
} else {
return db;
}
Expand All @@ -341,13 +341,14 @@ class DriftServerController {

class _CloseVfsOnClose extends QueryInterceptor {
final FutureOr<void> Function() _close;
final QueryExecutor _root;

_CloseVfsOnClose(this._close);
_CloseVfsOnClose(this._root, this._close);

@override
Future<void> close(QueryExecutor inner) async {
await inner.close();
if (inner is! TransactionExecutor) {
if (identical(_root, inner)) {
await _close();
}
}
Expand Down
2 changes: 1 addition & 1 deletion drift/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: drift
description: Drift is a reactive library to store relational data in Dart and Flutter applications.
version: 2.19.0
version: 2.19.1
repository: https://github.com/simolus3/drift
homepage: https://drift.simonbinder.eu/
issue_tracker: https://github.com/simolus3/drift/issues
Expand Down
8 changes: 7 additions & 1 deletion extras/integration_tests/web_wasm/lib/driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TestAssetServer {
await buildRunner.close();
}

static Future<TestAssetServer> start() async {
static Future<TestAssetServer> start({bool debug = false}) async {
final packageConfig =
await loadPackageConfigUri((await Isolate.packageConfig)!);
final ownPackage = packageConfig['web_wasm']!.root;
Expand All @@ -43,6 +43,8 @@ class TestAssetServer {
'run',
'build_runner',
'daemon',
if (debug)
'--define=build_web_compilers:entrypoint=dart2js_args=["-Dsqlite3.wasm.worker.debug=true"]'
],
logHandler: (log) => print(log.message),
);
Expand Down Expand Up @@ -143,6 +145,10 @@ class DriftWebDriver {
await driver.executeAsync('insert("", arguments[0])', []);
}

Future<void> runExclusiveBlock() async {
await driver.executeAsync('do_exclusive("", arguments[0])', []);
}

Future<int> get amountOfRows async {
return await driver.executeAsync('get_rows("", arguments[0])', []);
}
Expand Down
8 changes: 8 additions & 0 deletions extras/integration_tests/web_wasm/test/drift_wasm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ void main() {
});
}
}

test('supports exclusively API', () async {
await driver.openDatabase();
expect(await driver.amountOfRows, 0);

await driver.runExclusiveBlock();
expect(await driver.amountOfRows, 1);
});
});
}
}
9 changes: 9 additions & 0 deletions extras/integration_tests/web_wasm/web/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ void main() {
(WebStorageApi.byName[decoded[0] as String]!, decoded[1] as String),
);
});
_addCallbackForWebDriver('do_exclusive', (arg) async {
final database = openedDatabase!;
await database.exclusively(() async {
await database.transaction(() async {
await database.testTable
.insertOne(TestTableCompanion.insert(content: 'from exclusive'));
});
});
});

document.getElementById('selfcheck')?.onClick.listen((event) async {
print('starting');
Expand Down

0 comments on commit 7b2168c

Please sign in to comment.