Skip to content

Commit

Permalink
Fixed bugs
Browse files Browse the repository at this point in the history
Improved `ImportResult`
  • Loading branch information
JaffaKetchup committed Apr 2, 2024
1 parent f198d3a commit 6057137
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 178 deletions.
9 changes: 6 additions & 3 deletions example/lib/screens/export_import/export_import.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
Expand Down Expand Up @@ -199,11 +200,13 @@ class _ExportImportPopupState extends State<ExportImportPopup> {
} else {
setState(() => isProcessing = true);
final stopwatch = Stopwatch()..start();
await FMTCRoot.external(
final importResult = FMTCRoot.external(
pathToArchive: pathController.text,
).import(
strategy: selectedConflictStrategy,
);
unawaited(importResult.storesToStates.then(print));
final numImportedTiles = await importResult.complete;
stopwatch.stop();
if (context.mounted) {
final elapsedTime =
Expand All @@ -212,8 +215,8 @@ class _ExportImportPopupState extends State<ExportImportPopup> {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Successfully exported stores (in $elapsedTime '
'secs)',
'Successfully imported $numImportedTiles tiles '
'(in $elapsedTime secs)',
),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ class RootStatsPane extends StatefulWidget {
}

class _RootStatsPaneState extends State<RootStatsPane> {
late final watchStream = FMTCRoot.stats.watchStores(
triggerImmediately: true,
);
late final watchStream = FMTCRoot.stats.watchStores(triggerImmediately: true);

@override
Widget build(BuildContext context) => Container(
Expand Down
1 change: 0 additions & 1 deletion lib/src/backend/impls/objectbox/backend/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'dart:io';
import 'dart:isolate';

import 'package:flutter/services.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';

Expand Down
84 changes: 36 additions & 48 deletions lib/src/backend/impls/objectbox/backend/internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ class _ObjectBoxBackendImpl implements FMTCObjectBoxBackendInternal {
onCancel: () async {
_workerResStreamed.remove(id); // Free memory
// Cancel the worker stream if the worker is alive
if (!_workerComplete.isCompleted) {
await _sendCmdOneShot(type: type.streamCancel!, args: {'id': id});
if ((type.hasInternalStreamSub ?? false) &&
!_workerComplete.isCompleted) {
await _sendCmdOneShot(
type: _WorkerCmdType.cancelInternalStreamSub,
args: {'id': id},
);
}
},
);
Expand Down Expand Up @@ -170,7 +174,7 @@ class _ObjectBoxBackendImpl implements FMTCObjectBoxBackendInternal {
final err = evt.data?['error'];
if (err != null) {
if (evt.data?['expectStream'] == true) {
_workerResStreamed[evt.id]!.addError(err, evt.data!['stackTrace']);
_workerResStreamed[evt.id]?.addError(err, evt.data!['stackTrace']);
} else {
_workerResOneShot[evt.id]!
.completeError(err, evt.data!['stackTrace']);
Expand All @@ -179,9 +183,11 @@ class _ObjectBoxBackendImpl implements FMTCObjectBoxBackendInternal {
}

if (evt.data?['expectStream'] == true) {
// TODO: FIX worker should stop sending events if possible,
// otherwise, don't use null check (eg. import stream)
_workerResStreamed[evt.id]!.add(evt.data);
// May be `null` if cmd was streamed result, but has no way to prevent
// future results even after the listener has stopped
//
// See `_WorkerCmdType.hasInternalStreamSub` for info.
_workerResStreamed[evt.id]?.add(evt.data);
} else {
_workerResOneShot[evt.id]!.complete(evt.data);
}
Expand Down Expand Up @@ -554,6 +560,10 @@ class _ObjectBoxBackendImpl implements FMTCObjectBoxBackendInternal {
required List<String> storeNames,
required String path,
}) async {
if (storeNames.isEmpty) {
throw ArgumentError.value(storeNames, 'storeNames', 'must not be empty');
}

final type = await FileSystemEntity.type(path);
if (type == FileSystemEntityType.directory) {
throw ImportExportPathNotFile();
Expand All @@ -566,62 +576,40 @@ class _ObjectBoxBackendImpl implements FMTCObjectBoxBackendInternal {
}

@override
@experimental // TODO: Finish implementation
Future<ImportResult> importStores({
ImportResult importStores({
required String path,
required ImportConflictStrategy strategy,
required List<String>? storeNames,
}) async {
await _checkImportPathType(path);

_sendCmdStreamed(
type: _WorkerCmdType.importStores,
args: {'path': path, 'strategy': strategy, 'stores': storeNames},
).listen(print);

return (
stores: Future.sync(
() => <({bool conflict, String importingName, String? newName})>[],
),
complete: Future.sync(() => null),
);

/*final storesStreamController = StreamController<
({String importingName, bool conflict, String? newName})>();
}) {
Stream<Map<String, dynamic>?> checkTypeAndStartImport() async* {
await _checkImportPathType(path);
yield* _sendCmdStreamed(
type: _WorkerCmdType.importStores,
args: {'path': path, 'strategy': strategy, 'stores': storeNames},
);
}

final complete = Completer<void>();
final storesToStates = Completer<StoresToStates>();
final complete = Completer<int>();

late final StreamSubscription<Map<String, dynamic>?> listener;
listener = _sendCmdStreamed(
type: _WorkerCmdType.importStores,
args: {'path': path, 'strategy': strategy, 'stores': storeNames},
).listen(
cancelOnError: true,
listener = checkTypeAndStartImport().listen(
(evt) {
if (evt!.containsKey('finished')) {
complete.complete();
listener.cancel();
return;
if (evt!.containsKey('storesToStates')) {
storesToStates.complete(evt['storesToStates']);
}
if (evt.containsKey('tiles')) {
storesStreamController.close();
return;
if (evt.containsKey('complete')) {
complete.complete(evt['complete']);
listener.cancel();
}
storesStreamController.add(
(
importingName: evt['storeName'],
conflict: evt['conflict'],
newName: evt['newStoreName'],
),
);
},
cancelOnError: true,
);

return (
stores: storesStreamController.stream.toList(),
storesToStates: storesToStates.future,
complete: complete.future,
);*/
);
}

@override
Expand Down
Loading

0 comments on commit 6057137

Please sign in to comment.