Refactor: extract async utilities to its own package

This commit is contained in:
Ming Ming 2023-08-27 22:09:10 +08:00
parent 0d2d9feac4
commit 0ca85f2598
21 changed files with 90 additions and 16 deletions

View file

@ -8,12 +8,12 @@ import 'package:nc_photos/entity/sqlite/files_query_builder.dart';
import 'package:nc_photos/entity/sqlite/isolate_util.dart';
import 'package:nc_photos/entity/sqlite/table.dart';
import 'package:nc_photos/entity/sqlite/type_converter.dart';
import 'package:nc_photos/future_extension.dart';
import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/mobile/platform.dart'
if (dart.library.html) 'package:nc_photos/web/platform.dart' as platform;
import 'package:nc_photos/object_extension.dart';
import 'package:np_async/np_async.dart';
import 'package:np_codegen/np_codegen.dart';
import 'package:np_collection/np_collection.dart';
import 'package:np_platform_lock/np_platform_lock.dart';

View file

@ -1,3 +0,0 @@
extension FutureNotNullExtension<T> on Future<T?> {
Future<T> notNull() async => (await this)!;
}

View file

@ -1,5 +1,5 @@
import 'package:flutter/services.dart';
import 'package:nc_photos/future_extension.dart';
import 'package:np_async/np_async.dart';
class Activity {
static Future<String?> consumeInitialRoute() =>

View file

@ -18,11 +18,11 @@ import 'package:nc_photos/entity/file_util.dart' as file_util;
import 'package:nc_photos/entity/pref.dart';
import 'package:nc_photos/event/event.dart';
import 'package:nc_photos/event/native_event.dart';
import 'package:nc_photos/future_extension.dart';
import 'package:nc_photos/language_util.dart' as language_util;
import 'package:nc_photos/reverse_geocoder.dart';
import 'package:nc_photos/use_case/update_missing_metadata.dart';
import 'package:nc_photos_plugin/nc_photos_plugin.dart';
import 'package:np_async/np_async.dart';
import 'package:np_codegen/np_codegen.dart';
part 'service.g.dart';

View file

@ -7,7 +7,6 @@ import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/bloc/scan_account_dir.dart';
import 'package:nc_photos/compute_queue.dart';
import 'package:nc_photos/debug_util.dart';
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/entity/file.dart';
@ -28,6 +27,7 @@ import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart';
import 'package:nc_photos/widget/selection_app_bar.dart';
import 'package:nc_photos/widget/viewer.dart';
import 'package:nc_photos/widget/zoom_menu_button.dart';
import 'package:np_async/np_async.dart';
import 'package:np_codegen/np_codegen.dart';
part 'archive_browser.g.dart';

View file

@ -5,7 +5,6 @@ import 'package:logging/logging.dart';
import 'package:nc_photos/app_init.dart' as app_init;
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/bloc/scan_local_dir.dart';
import 'package:nc_photos/compute_queue.dart';
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/entity/file_util.dart' as file_util;
import 'package:nc_photos/entity/local_file.dart';
@ -25,6 +24,7 @@ import 'package:nc_photos/widget/photo_list_util.dart' as photo_list_util;
import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart';
import 'package:nc_photos/widget/selection_app_bar.dart';
import 'package:nc_photos_plugin/nc_photos_plugin.dart';
import 'package:np_async/np_async.dart';
import 'package:np_codegen/np_codegen.dart';
import 'package:np_collection/np_collection.dart';
import 'package:np_platform_util/np_platform_util.dart';

View file

@ -14,7 +14,6 @@ import 'package:nc_photos/account.dart';
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/bloc/progress.dart';
import 'package:nc_photos/bloc/scan_account_dir.dart';
import 'package:nc_photos/compute_queue.dart';
import 'package:nc_photos/controller/account_controller.dart';
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/download_handler.dart';
@ -51,6 +50,7 @@ import 'package:nc_photos/widget/selection_app_bar.dart';
import 'package:nc_photos/widget/settings.dart';
import 'package:nc_photos/widget/viewer.dart';
import 'package:nc_photos/widget/zoom_menu_button.dart';
import 'package:np_async/np_async.dart';
import 'package:np_codegen/np_codegen.dart';
import 'package:np_platform_util/np_platform_util.dart';
import 'package:visibility_detector/visibility_detector.dart';

View file

@ -8,7 +8,6 @@ import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/bloc/search.dart';
import 'package:nc_photos/compute_queue.dart';
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/download_handler.dart';
import 'package:nc_photos/entity/file.dart';
@ -37,6 +36,7 @@ import 'package:nc_photos/widget/search_landing.dart';
import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart';
import 'package:nc_photos/widget/selection_app_bar.dart';
import 'package:nc_photos/widget/viewer.dart';
import 'package:np_async/np_async.dart';
import 'package:np_codegen/np_codegen.dart';
import 'package:np_ui/np_ui.dart';

View file

@ -3,7 +3,6 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/async_util.dart' as async_util;
import 'package:nc_photos/bloc/list_tag.dart';
import 'package:nc_photos/bloc/search_suggestion.dart';
import 'package:nc_photos/entity/tag.dart';
@ -11,6 +10,7 @@ import 'package:nc_photos/exception_util.dart' as exception_util;
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/widget/dialog_scaffold.dart';
import 'package:np_async/np_async.dart';
import 'package:np_string/np_string.dart';
class TagPickerDialog extends StatefulWidget {
@ -137,8 +137,7 @@ class _TagPickerDialogState extends State<TagPickerDialog> {
Future<Iterable<Tag>> _onSearch(String pattern) async {
_suggestionBloc.add(SearchSuggestionBlocSearchEvent(pattern.toCi()));
await Future.delayed(const Duration(milliseconds: 250));
await async_util
.wait(() => _suggestionBloc.state is! SearchSuggestionBlocLoading);
await wait(() => _suggestionBloc.state is! SearchSuggestionBlocLoading);
if (_suggestionBloc.state is SearchSuggestionBlocSuccess) {
return _suggestionBloc.state.results;
} else {

View file

@ -8,7 +8,6 @@ import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/bloc/ls_trashbin.dart';
import 'package:nc_photos/compute_queue.dart';
import 'package:nc_photos/debug_util.dart';
import 'package:nc_photos/di_container.dart';
import 'package:nc_photos/entity/file.dart';
@ -30,6 +29,7 @@ import 'package:nc_photos/widget/selectable_item_stream_list_mixin.dart';
import 'package:nc_photos/widget/selection_app_bar.dart';
import 'package:nc_photos/widget/trashbin_viewer.dart';
import 'package:nc_photos/widget/zoom_menu_button.dart';
import 'package:np_async/np_async.dart';
import 'package:np_codegen/np_codegen.dart';
part 'trashbin_browser.g.dart';

View file

@ -948,6 +948,13 @@ packages:
relative: true
source: path
version: "0.0.1"
np_async:
dependency: "direct main"
description:
path: "../np_async"
relative: true
source: path
version: "0.0.1"
np_codegen:
dependency: "direct main"
description:

View file

@ -99,6 +99,8 @@ dependencies:
path: ../plugin
np_api:
path: ../np_api
np_async:
path: ../np_async
np_codegen:
path: ../codegen
np_common:

View file

@ -18,7 +18,7 @@ import 'package:nc_photos/entity/share.dart';
import 'package:nc_photos/entity/sharee.dart';
import 'package:nc_photos/entity/tag.dart';
import 'package:nc_photos/exception_event.dart';
import 'package:nc_photos/future_util.dart' as future_util;
import 'package:np_async/np_async.dart';
import 'package:np_common/or_null.dart';
import 'package:np_string/np_string.dart';
import 'package:path/path.dart' as path_lib;
@ -63,7 +63,7 @@ class MockAlbumMemoryRepo extends MockAlbumRepo {
@override
getAll(Account account, List<File> albumFiles) async* {
final results = await future_util.waitOr(
final results = await waitOr(
albumFiles.map((f) => get(account, f)),
(error, stackTrace) => ExceptionEvent(error, stackTrace),
);

30
np_async/.gitignore vendored Normal file
View file

@ -0,0 +1,30 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/
# IntelliJ related
*.iml
*.ipr
*.iws
.idea/
# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/
# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.packages
build/

10
np_async/.metadata Normal file
View file

@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.
version:
revision: 4d9e56e694b656610ab87fcf2efbcd226e0ed8cf
channel: stable
project_type: package

View file

@ -0,0 +1 @@
include: package:np_lints/np.yaml

View file

@ -0,0 +1,5 @@
library np_async;
export 'src/async_util.dart';
export 'src/compute_queue.dart';
export 'src/future_util.dart';

View file

@ -2,6 +2,10 @@ import 'dart:async';
import 'package:np_collection/np_collection.dart';
extension FutureNotNullExtension<T> on Future<T?> {
Future<T> notNull() async => (await this)!;
}
Future<List<T>> waitOr<T>(
Iterable<Future<T>> futures,
T Function(Object error, StackTrace? stackTrace) onError,

19
np_async/pubspec.yaml Normal file
View file

@ -0,0 +1,19 @@
name: np_async
description: A new Flutter package project.
version: 0.0.1
homepage:
publish_to: none
environment:
sdk: '>=2.19.6 <3.0.0'
flutter: ">=3.3.0"
dependencies:
flutter:
sdk: flutter
np_collection:
path: ../np_collection
dev_dependencies:
np_lints:
path: ../np_lints