-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ allow entering yt local search page before resources are prepared ref: #61
- Loading branch information
Showing
7 changed files
with
644 additions
and
373 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.