Skip to content

Commit

Permalink
perf: run all searches on isolate
Browse files Browse the repository at this point in the history
+ allow entering yt local search page before resources are prepared
ref: #61
  • Loading branch information
MSOB7YY committed Jan 1, 2024
1 parent 9c91361 commit 2c67645
Show file tree
Hide file tree
Showing 7 changed files with 644 additions and 373 deletions.
57 changes: 57 additions & 0 deletions lib/controller/search_ports_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'dart:async';
import 'dart:isolate';

import 'package:namida/core/enums.dart';
import 'package:namida/core/extensions.dart';

typedef _PortsComm = ({ReceivePort items, Completer<SendPort> search});

class SearchPortsProvider {
static final SearchPortsProvider inst = SearchPortsProvider._internal();
SearchPortsProvider._internal();

final _ports = <MediaType, _PortsComm?>{};

Future<void> _disposePort(_PortsComm port) async {
port.items.close();
(await port.search.future).send('dispose');
}

void disposeAll() {
for (final p in _ports.values) {
if (p != null) _disposePort(p);
}
_ports.clear();
}

Future<void> closePorts(MediaType type) async {
final port = _ports[type];
if (port != null) {
await _disposePort(port);
_ports[type] = null;
}
}

Future<SendPort> preparePorts({
required MediaType type,
required void Function(dynamic result) onResult,
required Future<void> Function(SendPort itemsSendPort) isolateFunction,
bool force = false,
}) async {
final portC = _ports[type];
if (portC != null && !force) return await portC.search.future;

await closePorts(type);
_ports[type] = (items: ReceivePort(), search: Completer<SendPort>());
final port = _ports[type];
port!.items.listen((result) {
if (result is SendPort) {
port.search.completeIfWasnt(result);
} else {
onResult(result);
}
});
await isolateFunction(port.items.sendPort);
return await port.search.future;
}
}

0 comments on commit 2c67645

Please sign in to comment.