Skip to content

Commit a047193

Browse files
Support async functions as callback.
1 parent b406487 commit a047193

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

objectbox/lib/src/native/store.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,11 @@ class Store {
383383
}
384384

385385
// Isolate entry point must be static or top-level.
386-
static void _callFunctionWithStoreInIsolate<P, R>(IsoPass<P, R> isoPass) {
386+
static Future<void> _callFunctionWithStoreInIsolate<P, R>(
387+
IsoPass<P, R> isoPass) async {
387388
final store = Store.attach(isoPass.model, isoPass.dbDirectoryPath,
388389
queriesCaseSensitiveDefault: isoPass.queriesCaseSensitiveDefault);
389-
final result = isoPass.runFn(store);
390+
final result = await isoPass.runFn(store);
390391
store.close();
391392
// Note: maybe replace with Isolate.exit once min Dart SDK 2.15.
392393
isoPass.resultPort?.send(result);
@@ -398,7 +399,7 @@ class Store {
398399
/// Instances of [callback] must be top-level functions or static methods
399400
/// of classes, not closures or instance methods of objects.
400401
Future<R> runIsolated<P, R>(
401-
TxMode mode, R Function(Store, P) callback, P param) async {
402+
TxMode mode, FutureOr<R> Function(Store, P) callback, P param) async {
402403
final resultPort = ReceivePort();
403404
// Await isolate spawn to avoid waiting forever if it fails to spawn.
404405
await Isolate.spawn(
@@ -550,7 +551,7 @@ class IsoPass<P, R> {
550551
final P param;
551552

552553
/// Function to be called in isolate
553-
final R Function(Store, P) fn;
554+
final FutureOr<R> Function(Store, P) fn;
554555

555556
/// creates everything that needs to be passed to the isolate.
556557
const IsoPass(
@@ -563,5 +564,5 @@ class IsoPass<P, R> {
563564
this.param);
564565

565566
/// Called inside this class so types are not lost (dynamic instead of P and R).
566-
R runFn(Store store) => fn(store, param);
567+
FutureOr<R> runFn(Store store) => fn(store, param);
567568
}

objectbox/test/basics_test.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,16 @@ void main() {
207207
});
208208
}
209209

210-
String readStringAndRemove(Store store, int id) {
210+
Future<String> readStringAndRemove(Store store, int id) async {
211211
var box = store.box<TestEntity>();
212212
var testEntity = box.get(id);
213213
final result = testEntity!.tString! + '!';
214214
print('Result in 2nd isolate: $result');
215215
final removed = box.remove(id);
216216
print('Removed in 2nd isolate: $removed');
217217
print('Count in 2nd isolate after remove: ${box.count()}');
218-
return result;
218+
// Pointless Future to test async functions are supported.
219+
return await Future.delayed(const Duration(milliseconds: 10), () => result);
219220
}
220221

221222
class StoreAttachIsolateInit {

0 commit comments

Comments
 (0)