Skip to content

Commit

Permalink
General backend-worker comms improvements
Browse files Browse the repository at this point in the history
Change imports/exports
  • Loading branch information
JaffaKetchup committed Dec 30, 2023
1 parent 2fe0e7a commit 56a2721
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 224 deletions.
6 changes: 2 additions & 4 deletions lib/flutter_map_tile_caching.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import 'package:path_provider/path_provider.dart';
import 'package:stream_transform/stream_transform.dart';
import 'package:watcher/watcher.dart';

import 'src/backend/export_std.dart';
import 'src/backend/exports.dart';
import 'src/bulk_download/instance.dart';
import 'src/bulk_download/rate_limited_stream.dart';
import 'src/bulk_download/tile_loops/shared.dart';
Expand All @@ -45,18 +45,16 @@ import 'src/db/registry.dart';
import 'src/db/tools.dart';
import 'src/errors/browsing.dart';
import 'src/errors/initialisation.dart';
import 'src/errors/store_not_ready.dart';
import 'src/misc/exts.dart';
import 'src/misc/int_extremes.dart';
import 'src/misc/obscure_query_params.dart';
import 'src/misc/typedefs.dart';
import 'src/providers/image_provider.dart';

export 'src/backend/export_std.dart';
export 'src/backend/exports.dart';
export 'src/errors/browsing.dart';
export 'src/errors/damaged_store.dart';
export 'src/errors/initialisation.dart';
export 'src/errors/store_not_ready.dart';
export 'src/misc/typedefs.dart';

part 'src/bulk_download/download_progress.dart';
Expand Down
36 changes: 0 additions & 36 deletions lib/fmtc_module_api.dart

This file was deleted.

4 changes: 0 additions & 4 deletions lib/src/backend/export_plus.dart

This file was deleted.

2 changes: 0 additions & 2 deletions lib/src/backend/export_std.dart

This file was deleted.

5 changes: 5 additions & 0 deletions lib/src/backend/exports.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export 'impls/objectbox/backend.dart';
export 'interfaces/backend.dart';
export 'interfaces/models.dart';
export 'interfaces/no_sync.dart';
export 'utils/errors.dart';
51 changes: 35 additions & 16 deletions lib/src/backend/impls/objectbox/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import 'package:meta/meta.dart' as meta;
import 'package:path_provider/path_provider.dart';

import '../../../misc/exts.dart';
import '../../impl_tools/errors.dart';
import '../../impl_tools/no_sync.dart';
import '../../interfaces/backend.dart';
import '../../interfaces/no_sync.dart';
import '../../utils/errors.dart';
import 'models/generated/objectbox.g.dart';
import 'models/models.dart';

Expand Down Expand Up @@ -42,11 +42,12 @@ class _ObjectBoxBackendImpl
final Map<int, Completer<Map<String, dynamic>?>> _workerRes = {};
late int _workerId;
late Completer<void> _workerComplete;
late StreamSubscription<dynamic> _workerHandler;

// `deleteOldestTile` tracking & debouncing
int _dotLength = 0;
Timer? _dotDebouncer;
String? _dotStore;
late int _dotLength;
late Timer _dotDebouncer;
late String? _dotStore;

Future<Map<String, dynamic>?> _sendCmd({
required _WorkerCmdType type,
Expand Down Expand Up @@ -92,25 +93,33 @@ class _ObjectBoxBackendImpl
}) async {
if (_sendPort != null) throw RootAlreadyInitialised();

// Setup worker isolate
final receivePort = ReceivePort();
// Reset non-comms-related non-resource-intensive state
_workerId = 0;
_workerRes.clear();
_dotStore = null;
_dotLength = 0;

// Prepare to recieve `SendPort` from worker
_workerRes[0] = Completer();
unawaited(
_workerRes[0]!.future.then((res) {
_sendPort = res!['sendPort'];
_workerRes.remove(0);
}),
);

// Setup worker comms/response handler
final receivePort = ReceivePort();
_workerComplete = Completer();
receivePort.listen(
_workerHandler = receivePort.listen(
(evt) {
evt as ({int id, Map<String, dynamic>? data});
_workerRes[evt.id]!.complete(evt.data);
},
onDone: () => _workerComplete.complete(),
);

// Spawn worker isolate
await Isolate.spawn(
_worker,
(
Expand All @@ -133,20 +142,30 @@ class _ObjectBoxBackendImpl
}) async {
expectInitialised;

if (!immediate) {
await Future.wait(_workerRes.values.map((e) => e.future));
}
// Wait for all currently underway operations to complete before destroying
// the isolate (if not `immediate`)
if (!immediate) await Future.wait(_workerRes.values.map((e) => e.future));

// Send self-destruct cmd to worker, and don't wait for any response
unawaited(
_sendCmd(
type: _WorkerCmdType.destroy_,
args: {'deleteRoot': deleteRoot},
),
);

// Wait for worker to exit (worker handler will exit and signal)
await _workerComplete.future;

_sendPort = null;
// Resource-intensive state cleanup only (other cleanup done during init)
_sendPort = null; // Indicate ready for re-init
await _workerHandler.cancel();
_dotDebouncer.cancel();

print('passed _workerHandler cancel');

// Kill any remaining operations with an error (they'll never recieve a
// response from the worker)
for (final completer in _workerRes.values) {
completer.complete({'error': RootUnavailable()});
}
Expand Down Expand Up @@ -289,14 +308,14 @@ class _ObjectBoxBackendImpl
// If the store has changed, failing to reset the batch/queue will mean
// tiles are removed from the wrong store
_dotStore = storeName;
if (_dotDebouncer != null && _dotDebouncer!.isActive) {
_dotDebouncer!.cancel();
if (_dotDebouncer.isActive) {
_dotDebouncer.cancel();
_sendRemoveOldestTileCmd(storeName);
}
}

if (_dotDebouncer != null && _dotDebouncer!.isActive) {
_dotDebouncer!.cancel();
if (_dotDebouncer.isActive) {
_dotDebouncer.cancel();
_dotDebouncer = Timer(
const Duration(milliseconds: 500),
() => _sendRemoveOldestTileCmd(storeName),
Expand Down
2 changes: 0 additions & 2 deletions lib/src/backend/impls/objectbox/models/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ base class ObjectBoxStore extends BackendStore {
@Unique()
String name;

@override
int numberOfTiles;

@override
double numberOfBytes;

@override
Expand Down
1 change: 1 addition & 0 deletions lib/src/backend/impls/objectbox/worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ Future<void> _worker(
storeQuery.close();

assert(store.tiles.isEmpty);
// TODO: Hits & misses

stores.put(
store
Expand Down
2 changes: 0 additions & 2 deletions lib/src/backend/interfaces/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import 'package:meta/meta.dart';
import 'package:path_provider/path_provider.dart';

import '../../../flutter_map_tile_caching.dart';
import '../impl_tools/errors.dart';
import 'models.dart';

/// An abstract interface that FMTC will use to communicate with a storage
/// 'backend' (usually one root)
Expand Down
2 changes: 0 additions & 2 deletions lib/src/backend/interfaces/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import 'package:meta/meta.dart';

abstract base class BackendStore {
abstract String name;
abstract int numberOfTiles;
abstract double numberOfBytes;
abstract int hits;
abstract int misses;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:typed_data';

import '../interfaces/backend.dart';
import '../interfaces/models.dart';
import 'errors.dart';
import '../utils/errors.dart';
import 'backend.dart';
import 'models.dart';

/// A shortcut to declare that an [FMTCBackend] does not support any synchronous
/// versions of methods
Expand Down
File renamed without changes.
39 changes: 0 additions & 39 deletions lib/src/errors/store_not_ready.dart

This file was deleted.

Loading

0 comments on commit 56a2721

Please sign in to comment.