mirror of
https://github.com/esiur/esiur-dart.git
synced 2025-09-29 18:53:19 +00:00
null-safety
This commit is contained in:
@@ -1,55 +1,62 @@
|
||||
import 'AsyncReply.dart';
|
||||
import '../Resource/Warehouse.dart';
|
||||
|
||||
// class ReplyIndex<T> {
|
||||
// int index;
|
||||
// AsyncReply<T> reply;
|
||||
// T
|
||||
// }
|
||||
|
||||
class AsyncBag<T> extends AsyncReply<List<T>> {
|
||||
List<AsyncReply<T>> _replies = <AsyncReply<T>>[];
|
||||
List<T> _results = <T>[];
|
||||
|
||||
//List<T?> _results = <T>[];
|
||||
|
||||
int _count = 0;
|
||||
bool _sealedBag = false;
|
||||
|
||||
Type arrayType;
|
||||
Type? arrayType;
|
||||
|
||||
seal() {
|
||||
//print("SEALED");
|
||||
|
||||
if (_sealedBag) return;
|
||||
|
||||
_sealedBag = true;
|
||||
|
||||
if (_results.length == 0) trigger(<T>[]);
|
||||
if (_replies.length == 0) trigger(<T>[]);
|
||||
|
||||
for (var i = 0; i < _results.length; i++) {
|
||||
var results = List<T?>.filled(_replies.length, null);
|
||||
|
||||
for (var i = 0; i < _replies.length; i++) {
|
||||
var k = _replies[i];
|
||||
var index = i;
|
||||
|
||||
k.then<dynamic>((r) {
|
||||
_results[index] = r;
|
||||
k..then((r) {
|
||||
results[index] = r;
|
||||
_count++;
|
||||
if (_count == _results.length) {
|
||||
if (_count == _replies.length) {
|
||||
if (arrayType != null) {
|
||||
var ar = Warehouse.createArray(arrayType);
|
||||
_results.forEach(ar.add);
|
||||
trigger(ar);
|
||||
var ar = Warehouse.createArray(arrayType as Type);
|
||||
results.forEach(ar.add);
|
||||
trigger(ar as List<T>);
|
||||
} else {
|
||||
trigger(_results);
|
||||
trigger(results.cast<T>());
|
||||
}
|
||||
}
|
||||
}).error((ex) {
|
||||
})..error((ex) {
|
||||
triggerError(ex);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
add(AsyncReply<T> reply) {
|
||||
void add(AsyncReply<T> reply) {
|
||||
if (!_sealedBag) {
|
||||
_results.add(null);
|
||||
//_results.add(null);
|
||||
_replies.add(reply);
|
||||
}
|
||||
}
|
||||
|
||||
addBag(AsyncBag<T> bag) {
|
||||
void addBag(AsyncBag<T> bag) {
|
||||
bag._replies.forEach((r) {
|
||||
add(r);
|
||||
});
|
||||
|
@@ -4,7 +4,7 @@ import 'ErrorType.dart';
|
||||
class AsyncException implements Exception {
|
||||
final ErrorType type;
|
||||
final int code;
|
||||
final String message;
|
||||
final String? message;
|
||||
|
||||
AsyncException(this.type, this.code, this.message) {}
|
||||
|
||||
@@ -20,7 +20,7 @@ class AsyncException implements Exception {
|
||||
": " +
|
||||
(message ?? "");
|
||||
else
|
||||
return code.toString() + ": " + message;
|
||||
return code.toString() + ": " + (message ?? '');
|
||||
}
|
||||
|
||||
@override
|
||||
|
@@ -2,8 +2,8 @@ library esiur;
|
||||
|
||||
import 'AsyncReply.dart';
|
||||
|
||||
class AsyncQueue<T> extends AsyncReply<T> {
|
||||
List<AsyncReply<T>> _list = <AsyncReply<T>>[];
|
||||
class AsyncQueue<T> extends AsyncReply<T?> {
|
||||
List<AsyncReply<T?>> _list = <AsyncReply<T?>>[];
|
||||
|
||||
// object queueLock = new object();
|
||||
|
||||
@@ -23,7 +23,7 @@ class AsyncQueue<T> extends AsyncReply<T> {
|
||||
processQueue(null);
|
||||
}
|
||||
|
||||
void processQueue(T o) {
|
||||
void processQueue(T? o) {
|
||||
//lock (queueLock)
|
||||
for (var i = 0; i < _list.length; i++)
|
||||
if (_list[i].ready) {
|
||||
|
@@ -29,9 +29,9 @@ import 'ProgressType.dart';
|
||||
class AsyncReply<T> implements Future<T> {
|
||||
List<Function(T)> _callbacks = <Function(T)>[];
|
||||
|
||||
T _result;
|
||||
late T _result;
|
||||
|
||||
List<Function(AsyncException)> _errorCallbacks = <Function(AsyncException)>[];
|
||||
List<Function> _errorCallbacks = <Function>[];
|
||||
|
||||
List<Function(ProgressType, int, int)> _progressCallbacks =
|
||||
<Function(ProgressType, int, int)>[];
|
||||
@@ -39,7 +39,7 @@ class AsyncReply<T> implements Future<T> {
|
||||
List<Function(T)> _chunkCallbacks = <Function(T)>[];
|
||||
|
||||
bool _resultReady = false;
|
||||
AsyncException _exception;
|
||||
AsyncException? _exception;
|
||||
|
||||
bool get ready {
|
||||
return _resultReady;
|
||||
@@ -49,7 +49,7 @@ class AsyncReply<T> implements Future<T> {
|
||||
_resultReady = value;
|
||||
}
|
||||
|
||||
T get result {
|
||||
T? get result {
|
||||
return _result;
|
||||
}
|
||||
|
||||
@@ -62,26 +62,20 @@ class AsyncReply<T> implements Future<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
AsyncReply<R> then<R>(FutureOr<R> onValue(T value), {Function onError}) {
|
||||
AsyncReply<R> then<R>(FutureOr<R> onValue(T value), {Function? onError}) {
|
||||
_callbacks.add(onValue);
|
||||
|
||||
if (onError != null) {
|
||||
if (onError is Function(dynamic, dynamic)) {
|
||||
_errorCallbacks.add((ex) => onError(ex, null));
|
||||
} else if (onError is Function(dynamic)) {
|
||||
_errorCallbacks.add(onError);
|
||||
} else if (onError is Function()) {
|
||||
_errorCallbacks.add((ex) => onError());
|
||||
} else if (onError is Function(Object, StackTrace)) {
|
||||
_errorCallbacks.add((ex) => onError(ex, null));
|
||||
}
|
||||
_errorCallbacks.add(onError);
|
||||
}
|
||||
|
||||
if (_resultReady) onValue(result);
|
||||
if (_resultReady) onValue(result as T);
|
||||
|
||||
if (R == Null)
|
||||
return null;
|
||||
else
|
||||
return this as AsyncReply<R>;
|
||||
// if (R == Null)
|
||||
// return null;
|
||||
//else
|
||||
//if (R == T)
|
||||
return AsyncReply<R>();
|
||||
}
|
||||
|
||||
AsyncReply<T> whenComplete(FutureOr action()) {
|
||||
@@ -90,21 +84,39 @@ class AsyncReply<T> implements Future<T> {
|
||||
}
|
||||
|
||||
Stream<T> asStream() {
|
||||
return null;
|
||||
return Stream.empty();
|
||||
//return null;
|
||||
}
|
||||
|
||||
AsyncReply<T> catchError(Function onError, {bool test(Object error)}) {
|
||||
return this.error(onError);
|
||||
}
|
||||
// Future<T> catchError(Function onError, {bool test(Object error)?});
|
||||
|
||||
AsyncReply<T> catchError(Function onError, {bool test(Object error)?}) {
|
||||
///return this.error(onError);
|
||||
|
||||
_errorCallbacks.add(onError);
|
||||
|
||||
if (_exception != null) {
|
||||
if (onError is Function(dynamic, dynamic)) {
|
||||
onError(_exception, null);
|
||||
} else if (onError is Function(dynamic)) {
|
||||
onError(_exception);
|
||||
} else if (onError is Function()) {
|
||||
onError();
|
||||
} else if (onError is Function(Object, StackTrace)) {
|
||||
onError(_exception as Object, StackTrace.current);
|
||||
}
|
||||
}
|
||||
|
||||
AsyncReply<T> timeout(Duration timeLimit, {FutureOr<T> onTimeout()}) {
|
||||
return this;
|
||||
}
|
||||
|
||||
AsyncReply<T> error(Function(dynamic) callback) {
|
||||
_errorCallbacks.add(callback);
|
||||
AsyncReply<T> timeout(Duration timeLimit, {FutureOr<T?> onTimeout()?}) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (_exception != null) callback(_exception);
|
||||
AsyncReply<T> error(callback(AsyncException ex)) {
|
||||
_errorCallbacks.add(callback);
|
||||
if (_exception != null) callback(_exception as AsyncException);
|
||||
|
||||
return this;
|
||||
}
|
||||
@@ -144,10 +156,19 @@ class AsyncReply<T> implements Future<T> {
|
||||
//{
|
||||
|
||||
if (this._errorCallbacks.length == 0)
|
||||
throw _exception;
|
||||
throw _exception as AsyncException;
|
||||
else
|
||||
_errorCallbacks.forEach((x) {
|
||||
x(_exception);
|
||||
if (x is Function(dynamic, dynamic)) {
|
||||
x(_exception, null);
|
||||
} else if (x is Function(dynamic)) {
|
||||
x(_exception);
|
||||
} else if (x is Function()) {
|
||||
x();
|
||||
} else if (x is Function(Object, StackTrace)) {
|
||||
x(_exception as Object, StackTrace.current);
|
||||
}
|
||||
//x(_exception as AsyncException);
|
||||
});
|
||||
//}
|
||||
|
||||
|
@@ -1,48 +1,38 @@
|
||||
class IEventHandler
|
||||
{
|
||||
Map<String, List<Function>> _events;
|
||||
class IEventHandler {
|
||||
Map<String, List<Function>> _events = {};
|
||||
|
||||
register(String event)
|
||||
{
|
||||
_events[event.toLowerCase()] = [];
|
||||
register(String event) {
|
||||
_events[event.toLowerCase()] = [];
|
||||
}
|
||||
|
||||
IEventHandler() {}
|
||||
|
||||
emitArgs(String event, List arguments) {
|
||||
//event = event.toLowerCase();
|
||||
|
||||
var et = _events[event.toLowerCase()];
|
||||
if (et != null) {
|
||||
for (var i = 0; i < et.length; i++)
|
||||
if (Function.apply(et[i], arguments) != null) return true;
|
||||
}
|
||||
|
||||
IEventHandler()
|
||||
{
|
||||
_events = {};
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
emitArgs(String event, List arguments)
|
||||
{
|
||||
event = event.toLowerCase();
|
||||
if (_events.containsKey(event))
|
||||
for(var i = 0; i < _events[event].length; i++)
|
||||
if (Function.apply(_events[event][i], arguments) != null)
|
||||
return true;
|
||||
on(String event, Function callback) {
|
||||
event = event.toLowerCase();
|
||||
if (_events.containsKey(event)) register(event);
|
||||
_events[event]?.add(callback);
|
||||
return this;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
on(String event, Function callback)
|
||||
{
|
||||
event = event.toLowerCase();
|
||||
// add
|
||||
if (!_events.containsKey(event))
|
||||
register(event);
|
||||
|
||||
_events[event].add(callback);
|
||||
return this;
|
||||
}
|
||||
|
||||
off(event, callback)
|
||||
{
|
||||
event = event.toString();
|
||||
if (_events.containsKey(event))
|
||||
{
|
||||
if (callback != null)
|
||||
_events[event].remove(callback);
|
||||
else
|
||||
this._events[event] = [];
|
||||
}
|
||||
off(event, callback) {
|
||||
event = event.toString();
|
||||
if (_events.containsKey(event)) {
|
||||
if (callback != null)
|
||||
_events[event]?.remove(callback);
|
||||
else
|
||||
this._events[event] = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user