Support background isolate in moor_ffi

This commit is contained in:
Simon Binder 2019-09-25 10:36:43 +02:00
parent e292a7cff8
commit e0fc4a3af6
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 24 additions and 10 deletions

View File

@ -96,5 +96,4 @@ In all other project files that use moor apis (e.g. a `Value` class for companio
Finally, replace usages of `FlutterQueryExecutor` with `VmDatabase`.
Note that, at the moment, there is no direct counterpart for `FlutterQueryExecutor.inDatabasePath`
and that the async API using a background isolate is not available for moor yet.
Note that, at the moment, there is no direct counterpart for `FlutterQueryExecutor.inDatabasePath`.

View File

@ -7,13 +7,18 @@ class VmDatabase extends DelegatedDatabase {
/// Creates a database that will store its result in the [file], creating it
/// if it doesn't exist.
factory VmDatabase(File file, {bool logStatements = false}) {
return VmDatabase._(_VmDelegate(file), logStatements);
///
/// If [background] is enabled (defaults to false), the database will be
/// opened on a background isolate. This is much slower, but reduces work on
/// the UI thread.
factory VmDatabase(File file,
{bool logStatements = false, bool background = false}) {
return VmDatabase._(_VmDelegate(file, background), logStatements);
}
/// Creates a database won't persist its changes on disk.
/// Creates an in-memory database won't persist its changes on disk.
factory VmDatabase.memory({bool logStatements = false}) {
return VmDatabase._(_VmDelegate(null), logStatements);
return VmDatabase._(_VmDelegate(null, false), logStatements);
}
}
@ -21,8 +26,9 @@ class _VmDelegate extends DatabaseDelegate {
BaseDatabase _db;
final File file;
final bool background;
_VmDelegate(this.file);
_VmDelegate(this.file, this.background);
@override
final TransactionDelegate transactionDelegate = const NoTransactionDelegate();
@ -34,10 +40,18 @@ class _VmDelegate extends DatabaseDelegate {
Future<bool> get isOpen => Future.value(_db != null);
@override
Future<void> open([GeneratedDatabase db]) {
Future<void> open([GeneratedDatabase db]) async {
if (file != null) {
_db = Database.openFile(file);
if (background) {
_db = await IsolateDb.openFile(file);
} else {
_db = Database.openFile(file);
}
} else {
assert(
!background,
"moor_ffi doesn't support in-memory databases on a background "
'isolate');
_db = Database.memory();
}
versionDelegate = _VmVersionDelegate(_db);

View File

@ -1,5 +1,5 @@
name: moor_ffi
description: "Experimental sqlite bindings using dart:ffi"
description: "Provides experimental sqlite bindings using dart:ffi, including a moor executor"
version: 0.0.1
author: Simon Binder <oss@simonbinder.eu>
homepage: https://github.com/simolus3/moor/tree/develop/moor_ffi
@ -10,6 +10,7 @@ environment:
dependencies:
moor: ">=1.7.0 <2.1.0"
collection: ^1.0.0
dev_dependencies:
test: ^1.6.0