Skip to content

Commit

Permalink
Removed synchronous operations to improve simplicity and DX
Browse files Browse the repository at this point in the history
Attach most of backend management to frontend
Fixed bug in `removeOldestTile` logic
Removed `TileCannotUpdate` error as it is a library error not a user error
Improved documentation and make use of templates and macros to de-duplicate doc-strings
Minor internal improvements elsewhere
  • Loading branch information
JaffaKetchup committed Dec 31, 2023
1 parent 56a2721 commit c2e2a66
Show file tree
Hide file tree
Showing 19 changed files with 93 additions and 934 deletions.
6 changes: 0 additions & 6 deletions lib/flutter_map_tile_caching.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ 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';
import 'src/db/defs/metadata.dart';
import 'src/db/defs/recovery.dart';
import 'src/db/defs/store_descriptor.dart';
import 'src/db/defs/tile.dart';
import 'src/db/registry.dart';
import 'src/db/tools.dart';
import 'src/errors/browsing.dart';
import 'src/errors/initialisation.dart';
import 'src/misc/exts.dart';
Expand Down
1 change: 0 additions & 1 deletion lib/src/backend/exports.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export 'impls/objectbox/backend.dart';
export 'interfaces/backend.dart';
export 'interfaces/models.dart';
export 'interfaces/no_sync.dart';
export 'utils/errors.dart';
48 changes: 19 additions & 29 deletions lib/src/backend/impls/objectbox/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:path_provider/path_provider.dart';

import '../../../misc/exts.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 All @@ -30,9 +29,7 @@ abstract interface class ObjectBoxBackendInternal
static final _instance = _ObjectBoxBackendImpl._();
}

class _ObjectBoxBackendImpl
with FMTCBackendNoSync
implements ObjectBoxBackendInternal {
class _ObjectBoxBackendImpl implements ObjectBoxBackendInternal {
_ObjectBoxBackendImpl._();

void get expectInitialised => _sendPort ?? (throw RootUnavailable());
Expand Down Expand Up @@ -73,7 +70,7 @@ class _ObjectBoxBackendImpl
@override
String get friendlyIdentifier => 'ObjectBox';

/// {@macro fmtc_backend_initialise}
/// {@macro fmtc.backend.initialise}
///
/// This implementation additionally accepts the following [implSpecificArgs]:
///
Expand Down Expand Up @@ -286,21 +283,11 @@ class _ObjectBoxBackendImpl
args: {'storeName': storeName, 'url': url},
))!['wasOrphaned'];

void _sendRemoveOldestTileCmd(String storeName) {
_sendCmd(
type: _WorkerCmdType.removeOldestTile,
args: {
'storeName': storeName,
'number': _dotLength,
},
);
_dotLength = 0;
}

@override
void removeOldestTileSync({
Future<void> removeOldestTile({
required String storeName,
}) {
required int numToRemove,
}) async {
// Attempts to avoid flooding worker with requests to delete oldest tile,
// and 'batches' them instead

Expand All @@ -310,28 +297,31 @@ class _ObjectBoxBackendImpl
_dotStore = storeName;
if (_dotDebouncer.isActive) {
_dotDebouncer.cancel();
_sendRemoveOldestTileCmd(storeName);
_sendROTCmd(storeName);
_dotLength += numToRemove;
}
}

if (_dotDebouncer.isActive) {
_dotDebouncer.cancel();
_dotDebouncer = Timer(
const Duration(milliseconds: 500),
() => _sendRemoveOldestTileCmd(storeName),
() => _sendROTCmd(storeName),
);
_dotLength += numToRemove;
return;
}

_dotDebouncer = Timer(
const Duration(seconds: 1),
() => _sendRemoveOldestTileCmd(storeName),
);
_dotDebouncer =
Timer(const Duration(seconds: 1), () => _sendROTCmd(storeName));
_dotLength += numToRemove;
}

@override
Future<void> removeOldestTile({
required String storeName,
}) async =>
removeOldestTileSync(storeName: storeName);
void _sendROTCmd(String storeName) {
_sendCmd(
type: _WorkerCmdType.removeOldestTile,
args: {'storeName': storeName, 'number': _dotLength},
);
_dotLength = 0;
}
}
4 changes: 3 additions & 1 deletion lib/src/backend/impls/objectbox/worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,9 @@ Future<void> _worker(
);
break;
case (true, true): // FMTC internal error
throw TileCannotUpdate(url: url);
throw StateError(
'FMTC ObjectBox backend internal state error: $url',
);
}
},
);
Expand Down
Loading

0 comments on commit c2e2a66

Please sign in to comment.