Skip to content

Commit

Permalink
perf: Directory.listAllIsolate()
Browse files Browse the repository at this point in the history
easier to allow multiple dir listing in the same isolate
  • Loading branch information
MSOB7YY committed Jan 22, 2024
1 parent 1b889ef commit f227d05
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 39 deletions.
4 changes: 2 additions & 2 deletions lib/controller/ffmpeg_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class NamidaFFMPEG {
final dirFiles = <FileSystemEntity>[];

for (final d in dirs) {
dirFiles.addAll(Directory(d).listSyncSafe(recursive: recursive));
dirFiles.addAll(await Directory(d).listAllIsolate(recursive: recursive));
}

dirFiles.retainWhere((element) => element is File);
Expand Down Expand Up @@ -238,7 +238,7 @@ class NamidaFFMPEG {
if (!await requestManageStoragePermission()) return;

final dio = Dio();
final allFiles = Directory(directoryPath).listSyncSafe(recursive: recursive);
final allFiles = await Directory(directoryPath).listAllIsolate(recursive: recursive);
final totalFilesLength = allFiles.length;
int currentProgress = 0;
int currentFailed = 0;
Expand Down
22 changes: 22 additions & 0 deletions lib/core/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,28 @@ extension DirectoryUtils on Directory {
return [];
}
}

Future<List<FileSystemEntity>> listAllIsolate({bool recursive = false, bool followLinks = true}) async {
try {
final params = {
'dirPath': path,
'recursive': recursive,
'followLinks': followLinks,
};
final res = await compute(_listAllIsolate, params);
return res;
} catch (e) {
printy(e, isError: true);
return [];
}
}
}

List<FileSystemEntity> _listAllIsolate(Map params) {
final dirPath = params['dirPath'] as String;
final recursive = params['recursive'] as bool;
final followLinks = params['followLinks'] as bool;
return Directory(dirPath).listSync(recursive: recursive, followLinks: followLinks);
}

extension FileUtils on File {
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/dialogs/general_popup_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ Future<void> showGeneralPopupDialog(
final dirPath = await FilePicker.platform.getDirectoryPath();
if (dirPath == null) return;

final files = Directory(dirPath).listSyncSafe();
final files = await Directory(dirPath).listAllIsolate();
files.removeWhere((element) => element is! File);
if (files.isEmpty) {
snackyy(title: lang.ERROR, message: lang.NO_TRACKS_FOUND_IN_DIRECTORY);
Expand Down
42 changes: 27 additions & 15 deletions lib/youtube/controller/youtube_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,25 +150,37 @@ class YoutubeController {
if (res.$2.isNotEmpty) tempChannelInfo = res.$2;
}

static (Map<String, VideoInfo?> mv, Map<String, YoutubeChannel?> mc) _loadInfoToMemoryIsolate((String pv, String pc) paths) {
static Future<(Map<String, VideoInfo?>, Map<String, YoutubeChannel?>)> _loadInfoToMemoryIsolate((String pv, String pc) paths) async {
final mv = <String, VideoInfo?>{};
final mc = <String, YoutubeChannel?>{};
Directory(paths.$1).listSyncSafe().loop((e, index) {
if (e is File) {
try {
final map = e.readAsJsonSync();
mv[e.path.getFilenameWOExt] = VideoInfo.fromMap(map);
} catch (_) {}
}

final completer1 = Completer<void>();
final completer2 = Completer<void>();

Directory(paths.$1).listAllIsolate().then((value) {
value.loop((e, index) {
if (e is File) {
try {
final map = e.readAsJsonSync();
mv[e.path.getFilenameWOExt] = VideoInfo.fromMap(map);
} catch (_) {}
}
});
completer1.complete();
});
Directory(paths.$2).listSyncSafe().loop((e, index) {
if (e is File) {
try {
final map = e.readAsJsonSync();
mc[e.path.getFilenameWOExt] = YoutubeChannel.fromMap(map);
} catch (_) {}
}
Directory(paths.$2).listAllIsolate().then((value) {
value.loop((e, index) {
if (e is File) {
try {
final map = e.readAsJsonSync();
mc[e.path.getFilenameWOExt] = YoutubeChannel.fromMap(map);
} catch (_) {}
}
});
completer2.complete();
});
await completer1.future;
await completer2.future;
return (mv, mc);
}

Expand Down
53 changes: 32 additions & 21 deletions lib/youtube/controller/youtube_local_search_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class YTLocalSearchController {
isLoadingLookupLists.value = false;
}

static void _prepareResourcesAndSearch(Map params) {
static void _prepareResourcesAndSearch(Map params) async {
final tempStreamInfo = params['tempStreamInfo'] as Map<String, StreamInfoItem>;
final dirStreamInfo = params['dirStreamInfo'] as String;
final dirVideoInfo = params['dirVideoInfo'] as String;
Expand Down Expand Up @@ -230,30 +230,41 @@ class YTLocalSearchController {
lookupItemAvailable[id] = (list: 1, index: lookupListStreamInfo.length - 1);
}

Directory(dirStreamInfo).listSyncSafe().loop((file, _) {
try {
final res = (file as File).readAsJsonSync();
if (res != null) {
final id = res['id'];
if (id != null && lookupItemAvailable[id] == null) {
lookupListStreamInfoMap.add(res);
lookupItemAvailable[id] = (list: 2, index: lookupListStreamInfoMap.length - 1);
final completer1 = Completer<void>();
final completer2 = Completer<void>();

Directory(dirStreamInfo).listAllIsolate().then((value) {
value.loop((file, _) {
try {
final res = (file as File).readAsJsonSync();
if (res != null) {
final id = res['id'];
if (id != null && lookupItemAvailable[id] == null) {
lookupListStreamInfoMap.add(res);
lookupItemAvailable[id] = (list: 2, index: lookupListStreamInfoMap.length - 1);
}
}
}
} catch (_) {}
} catch (_) {}
});
completer1.complete();
});
Directory(dirVideoInfo).listSyncSafe().loop((file, _) {
try {
final res = (file as File).readAsJsonSync();
if (res != null) {
final id = res['id'];
if (id != null && lookupItemAvailable[id] == null) {
lookupListVideoInfoMap.add(res.cast());
lookupItemAvailable[id] = (list: 3, index: lookupListVideoInfoMap.length - 1);
Directory(dirVideoInfo).listAllIsolate().then((value) {
value.loop((file, _) {
try {
final res = (file as File).readAsJsonSync();
if (res != null) {
final id = res['id'];
if (id != null && lookupItemAvailable[id] == null) {
lookupListVideoInfoMap.add(res.cast());
lookupItemAvailable[id] = (list: 3, index: lookupListVideoInfoMap.length - 1);
}
}
}
} catch (_) {}
} catch (_) {}
});
completer2.complete();
});
await completer1.future;
await completer2.future;
for (final id in tempBackupYTVH.keys) {
if (lookupItemAvailable[id] == null) {
final val = tempBackupYTVH[id]!;
Expand Down

0 comments on commit f227d05

Please sign in to comment.