Expose isolateSetup option from drift_flutter

This commit is contained in:
Simon Binder
2025-11-05 09:31:04 +01:00
parent 3025034570
commit 7782ee8541
5 changed files with 47 additions and 1 deletions
+4
View File
@@ -1,3 +1,7 @@
## 0.2.8-dev
- Add `isolateSetup` parameter to `DriftNativeOptions` to setup isolates spawned to host database connections.
## 0.2.7
- Add `isolateDebugLog` parameter to `DriftNativeOptions` to debug internal messages between isolates.
+8
View File
@@ -130,6 +130,13 @@ final class DriftNativeOptions {
/// can't be sent over isolates.
final void Function(CommonDatabase db)? setup;
/// An optional callback to be invoked when `drift_flutter` spawns a
/// background isolate to host database connections.
///
/// This could be used to configure how `libsqlite3` is loaded, or setup state
/// that needs to be accessible in the background isolate.
final void Function()? isolateSetup;
/// Create drift options effective when opening drift databases on native
/// platforms.
const DriftNativeOptions({
@@ -138,6 +145,7 @@ final class DriftNativeOptions {
this.databasePath,
this.databaseDirectory,
this.tempDirectoryPath,
this.isolateSetup,
this.setup,
}) : assert(
databasePath == null || databaseDirectory == null,
+3
View File
@@ -139,6 +139,7 @@ QueryExecutor driftDatabase({
await databaseFile(),
setup: native?.setup,
isolateDebugLog: native?.isolateDebugLog ?? false,
isolateSetup: native?.isolateSetup,
);
}));
}
@@ -155,6 +156,8 @@ void _isolateEntrypoint(_EntrypointMessage message) {
if (IsolateNameServer.registerPortWithName(
connections.sendPort, portName(message.name))) {
final controlPortName = isolateControlPortName(message.name);
message.options.isolateSetup?.call();
final server = DriftIsolate.inCurrent(
() => NativeDatabase(File(message.path), setup: message.options.setup),
port: connections,
+1 -1
View File
@@ -1,6 +1,6 @@
name: drift_flutter
description: Easily set up drift databases across platforms in Flutter apps.
version: 0.2.7
version: 0.2.8-dev
repository: https://github.com/simolus3/drift
homepage: https://drift.simonbinder.eu/
issue_tracker: https://github.com/simolus3/drift/issues
+31
View File
@@ -111,6 +111,20 @@ void main() {
await database.close();
});
test('isolateSetup', () async {
final file = d.path('test_isolate_setup');
final database = SimpleDatabase(driftDatabase(
name: 'database',
native: DriftNativeOptions(isolateSetup: () {
File(file).createSync();
}),
));
await database.customSelect('SELECT 1').get();
expect(File(file).existsSync(), isTrue);
});
group('shared between isolates', () {
const options = DriftNativeOptions(shareAcrossIsolates: true);
@@ -186,6 +200,23 @@ void main() {
await database.customSelect('SELECT hello_dart() as r;').get();
expect(row.data['r'], 'Hello from Dart!');
});
test('isolateSetup', () async {
final file = d.path('test_isolate_setup');
final database = SimpleDatabase(driftDatabase(
name: 'database',
native: DriftNativeOptions(
shareAcrossIsolates: true,
isolateSetup: () {
File(file).createSync();
},
),
));
await database.customSelect('SELECT 1').get();
expect(File(file).existsSync(), isTrue);
});
});
group('pingWithTimeout', () {