This commit is contained in:
problematicconsumer
2023-12-01 12:56:24 +03:30
parent 9c165e178b
commit ed614988a2
181 changed files with 3092 additions and 2341 deletions

View File

@@ -0,0 +1,48 @@
import 'package:fpdart/fpdart.dart';
import 'package:hiddify/utils/utils.dart';
import 'package:rxdart/rxdart.dart';
mixin ExceptionHandler implements LoggerMixin {
TaskEither<F, R> exceptionHandler<F, R>(
Future<Either<F, R>> Function() run,
F Function(Object error, StackTrace stackTrace) onError,
) {
return TaskEither(
() async {
try {
return await run();
} catch (error, stackTrace) {
return Left(onError(error, stackTrace));
}
},
);
}
}
extension StreamExceptionHandler<R extends Object?> on Stream<R> {
Stream<Either<F, R>> handleExceptions<F>(
F Function(Object error, StackTrace stackTrace) onError,
) {
return map(right<F, R>).onErrorReturnWith(
(error, stackTrace) {
return Left(onError(error, stackTrace));
},
);
}
}
extension TaskEitherExceptionHandler<F, R> on TaskEither<F, R> {
TaskEither<F, R> handleExceptions(
F Function(Object error, StackTrace stackTrace) onError,
) {
return TaskEither(
() async {
try {
return await run();
} catch (error, stackTrace) {
return Left(onError(error, stackTrace));
}
},
);
}
}

View File

@@ -0,0 +1,15 @@
import 'dart:ffi';
import 'package:ffi/ffi.dart';
R withMemory<R, T extends NativeType>(
int size,
R Function(Pointer<T> memory) action,
) {
final memory = calloc<Int8>(size);
try {
return action(memory.cast());
} finally {
calloc.free(memory);
}
}

View File

@@ -0,0 +1,11 @@
import 'package:freezed_annotation/freezed_annotation.dart';
class IntervalInSecondsConverter implements JsonConverter<Duration, int> {
const IntervalInSecondsConverter();
@override
Duration fromJson(int json) => Duration(seconds: json);
@override
int toJson(Duration object) => object.inSeconds;
}