mirror of
https://github.com/esiur/esiur-dart.git
synced 2026-04-04 10:18:20 +00:00
Refactor Core
This commit is contained in:
@@ -1,52 +1,46 @@
|
|||||||
import 'AsyncReply.dart';
|
import 'AsyncReply.dart';
|
||||||
|
|
||||||
class AsyncBag<T> extends AsyncReply<List<T>>
|
class AsyncBag<T> extends AsyncReply<List<T>> {
|
||||||
{
|
List<AsyncReply<T>> _replies = List<AsyncReply<T>>();
|
||||||
|
List<T> _results = List<T>();
|
||||||
|
|
||||||
List<AsyncReply<T>> _replies = new List<AsyncReply<T>>();
|
int _count = 0;
|
||||||
List<T> _results = new List<T>();
|
bool _sealedBag = false;
|
||||||
|
|
||||||
int _count = 0;
|
seal() {
|
||||||
bool _sealedBag = false;
|
if (_sealedBag) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_sealedBag = true;
|
||||||
|
|
||||||
seal()
|
if (_results.length == 0) {
|
||||||
{
|
trigger(List<T>());
|
||||||
if (_sealedBag)
|
}
|
||||||
return;
|
|
||||||
|
|
||||||
_sealedBag = true;
|
for (var i = 0; i < _results.length; i++) {
|
||||||
|
var k = _replies[i];
|
||||||
|
var index = i;
|
||||||
|
|
||||||
if (_results.length == 0)
|
k.then((r) {
|
||||||
trigger(new List<T>());
|
_results[index] = r;
|
||||||
|
_count++;
|
||||||
for (var i = 0; i < _results.length; i++)
|
if (_count == _results.length) {
|
||||||
{
|
trigger(_results);
|
||||||
var k = _replies[i];
|
|
||||||
var index = i;
|
|
||||||
|
|
||||||
k.then((r)
|
|
||||||
{
|
|
||||||
_results[index] = r;
|
|
||||||
_count++;
|
|
||||||
if (_count == _results.length)
|
|
||||||
trigger(_results);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
add(AsyncReply<T> reply)
|
add(AsyncReply<T> reply) {
|
||||||
{
|
if (!_sealedBag) {
|
||||||
if (!_sealedBag)
|
_results.add(null);
|
||||||
{
|
_replies.add(reply);
|
||||||
_results.add(null);
|
|
||||||
_replies.add(reply);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
addBag(AsyncBag<T> bag)
|
addBag(AsyncBag<T> bag) {
|
||||||
{
|
bag._replies.forEach((r) {
|
||||||
bag._replies.forEach((r) {
|
add(r);
|
||||||
add(r);
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,33 +1,27 @@
|
|||||||
|
|
||||||
import 'ExceptionCode.dart';
|
import 'ExceptionCode.dart';
|
||||||
import 'ErrorType.dart';
|
import 'ErrorType.dart';
|
||||||
|
|
||||||
class AsyncException implements Exception
|
class AsyncException implements Exception {
|
||||||
{
|
AsyncException(this.type, this.code, this.message);
|
||||||
final ErrorType type;
|
|
||||||
final int code;
|
|
||||||
final String message;
|
|
||||||
|
|
||||||
AsyncException(this.type, this.code, this.message)
|
final ErrorType type;
|
||||||
{
|
final int code;
|
||||||
|
final String message;
|
||||||
|
|
||||||
|
static toAsyncException(Exception ex) => ex is AsyncException
|
||||||
|
? ex
|
||||||
|
: new AsyncException(ErrorType.Exception, 0, ex.toString());
|
||||||
|
|
||||||
|
String errMsg() {
|
||||||
|
if (type == ErrorType.Management) {
|
||||||
|
return ExceptionCode.values.elementAt(code).toString() +
|
||||||
|
": " +
|
||||||
|
(message ?? "");
|
||||||
|
} else {
|
||||||
|
return code.toString() + ": " + message;
|
||||||
}
|
}
|
||||||
|
|
||||||
static toAsyncException(Exception ex)
|
|
||||||
{
|
|
||||||
return ex is AsyncException ? ex
|
|
||||||
: new AsyncException(ErrorType.Exception, 0, ex.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
String errMsg() {
|
|
||||||
if (type == ErrorType.Management)
|
|
||||||
return ExceptionCode.values.elementAt(code).toString() + ": " + (message ?? "");
|
|
||||||
else
|
|
||||||
return code.toString() + ": " + message;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
String toString() {
|
|
||||||
return errMsg();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() => errMsg();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,50 +2,33 @@ library esiur;
|
|||||||
|
|
||||||
import 'AsyncReply.dart';
|
import 'AsyncReply.dart';
|
||||||
|
|
||||||
class AsyncQueue<T> extends AsyncReply<T>
|
class AsyncQueue<T> extends AsyncReply<T> {
|
||||||
{
|
List<AsyncReply<T>> _list = <AsyncReply<T>>[];
|
||||||
List<AsyncReply<T>> _list = new List<AsyncReply<T>>();
|
|
||||||
|
|
||||||
// object queueLock = new object();
|
void add(AsyncReply<T> reply) {
|
||||||
|
_list.add(reply);
|
||||||
|
|
||||||
add(AsyncReply<T> reply)
|
super.setResultReady(false);
|
||||||
{
|
|
||||||
//lock (queueLock)
|
|
||||||
_list.add(reply);
|
|
||||||
|
|
||||||
//super._resultReady = false;
|
reply.then(processQueue);
|
||||||
super.setResultReady(false);
|
|
||||||
|
|
||||||
reply.then(processQueue);
|
|
||||||
}
|
|
||||||
|
|
||||||
remove(AsyncReply<T> reply)
|
|
||||||
{
|
|
||||||
//lock (queueLock)
|
|
||||||
_list.remove(reply);
|
|
||||||
processQueue(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
void processQueue(T o)
|
|
||||||
{
|
|
||||||
//lock (queueLock)
|
|
||||||
for (var i = 0; i < _list.length; i++)
|
|
||||||
if (_list[i].ready)
|
|
||||||
{
|
|
||||||
super.trigger(_list[i].result);
|
|
||||||
_list.removeAt(i);
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
|
|
||||||
|
|
||||||
//super._resultReady = (_list.length == 0);
|
|
||||||
super.setResultReady(_list.length == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
AsyncQueue()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void remove(AsyncReply<T> reply) {
|
||||||
|
_list.remove(reply);
|
||||||
|
processQueue();
|
||||||
|
}
|
||||||
|
|
||||||
|
void processQueue([T o = null]) {
|
||||||
|
for (var i = 0; i < _list.length; i++) {
|
||||||
|
if (_list[i].ready) {
|
||||||
|
super.trigger(_list[i].result);
|
||||||
|
_list.removeAt(i);
|
||||||
|
i--;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
super.setResultReady(_list.length == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -26,195 +26,127 @@ import 'dart:core';
|
|||||||
import 'AsyncException.dart';
|
import 'AsyncException.dart';
|
||||||
import 'ProgressType.dart';
|
import 'ProgressType.dart';
|
||||||
|
|
||||||
class AsyncReply<T> implements Future<T>
|
class AsyncReply<T> implements Future<T> {
|
||||||
{
|
AsyncReply();
|
||||||
|
|
||||||
List<Function(T)> _callbacks = new List<Function(T)>();
|
List<Function(T)> _callbacks = <Function(T)>[];
|
||||||
|
|
||||||
T _result;
|
T _result;
|
||||||
|
|
||||||
List<Function(AsyncException)> _errorCallbacks = new List<Function(AsyncException)>();
|
final _errorCallbacks = <Function(AsyncException)>[];
|
||||||
|
|
||||||
List<Function(ProgressType, int, int)> _progressCallbacks = new List<Function(ProgressType, int, int)>();
|
final _progressCallbacks = <Function(ProgressType, int, int)>[];
|
||||||
|
|
||||||
List<Function(T)> _chunkCallbacks = new List<Function(T)>();
|
final _chunkCallbacks = <Function(T)>[];
|
||||||
|
|
||||||
|
bool _resultReady = false;
|
||||||
|
AsyncException _exception;
|
||||||
|
|
||||||
|
bool get ready => _resultReady;
|
||||||
|
|
||||||
bool _resultReady = false;
|
T get result => _result;
|
||||||
AsyncException _exception;
|
|
||||||
|
|
||||||
|
setResultReady(bool val) => _resultReady = val;
|
||||||
|
|
||||||
bool get ready
|
AsyncReply<R> then<R>(FutureOr<R> onValue(T value), {Function onError}) {
|
||||||
{
|
_callbacks.add(onValue);
|
||||||
return _resultReady;
|
|
||||||
}
|
|
||||||
|
|
||||||
T get result
|
if (onError != null) {
|
||||||
{
|
if (onError is Function(dynamic, dynamic)) {
|
||||||
return _result;
|
_errorCallbacks.add((ex) => onError(ex, null));
|
||||||
}
|
} else if (onError is Function(dynamic)) {
|
||||||
|
_errorCallbacks.add(onError);
|
||||||
setResultReady(bool val)
|
} else if (onError is Function()) {
|
||||||
{
|
_errorCallbacks.add((ex) => onError());
|
||||||
_resultReady = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_resultReady) onValue(result);
|
||||||
|
|
||||||
if (_resultReady)
|
return this as AsyncReply<R>;
|
||||||
onValue(result);
|
|
||||||
|
|
||||||
return this as AsyncReply<R>;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AsyncReply<T> whenComplete(FutureOr action())
|
AsyncReply<T> whenComplete(FutureOr action()) => this;
|
||||||
{
|
|
||||||
return this;
|
|
||||||
//_callbacks.add(action);
|
|
||||||
}
|
|
||||||
|
|
||||||
Stream<T> asStream()
|
Stream<T> asStream() => null;
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
AsyncReply<T> catchError(Function onError, {bool test(Object error)})
|
AsyncReply<T> catchError(Function onError, {bool test(Object error)}) =>
|
||||||
{
|
this.error(onError);
|
||||||
return this.error(onError);
|
|
||||||
}
|
AsyncReply<T> timeout(Duration timeLimit, {FutureOr<T> onTimeout()}) => this;
|
||||||
|
|
||||||
|
@deprecated
|
||||||
|
AsyncReply<T> _then_old(Function(T) callback) {
|
||||||
|
_callbacks.add(callback);
|
||||||
|
|
||||||
|
if (_resultReady) callback(result);
|
||||||
|
|
||||||
AsyncReply<T> timeout(Duration timeLimit, {FutureOr<T> onTimeout()})
|
|
||||||
{
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
AsyncReply<T> _then_old(Function(T) callback)
|
AsyncReply<T> error(Function(dynamic) callback) {
|
||||||
{
|
_errorCallbacks.add(callback);
|
||||||
_callbacks.add(callback);
|
|
||||||
|
|
||||||
if (_resultReady)
|
if (_exception != null) callback(_exception);
|
||||||
callback(result);
|
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
AsyncReply<T> progress(Function(ProgressType, int, int) callback) {
|
||||||
|
_progressCallbacks.add(callback);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
AsyncReply<T> chunk(Function(T) callback) {
|
||||||
|
_chunkCallbacks.add(callback);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void trigger(T result) {
|
||||||
|
if (_resultReady) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_result = result;
|
||||||
|
_resultReady = true;
|
||||||
|
|
||||||
AsyncReply<T> error(Function(dynamic) callback)
|
_callbacks.forEach((x) {
|
||||||
{
|
x(result);
|
||||||
_errorCallbacks.add(callback);
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (_exception != null)
|
triggerError(Exception exception) {
|
||||||
callback(_exception);
|
if (_resultReady) {
|
||||||
|
return;
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AsyncReply<T> progress(Function(ProgressType, int, int) callback)
|
_exception = AsyncException.toAsyncException(exception);
|
||||||
{
|
|
||||||
_progressCallbacks.add(callback);
|
_errorCallbacks.forEach((x) {
|
||||||
return this;
|
x(_exception);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
triggerProgress(ProgressType type, int value, int max) {
|
||||||
|
if (_resultReady) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_progressCallbacks.forEach((x) {
|
||||||
|
x(type, value, max);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
AsyncReply<T> chunk(Function(T) callback)
|
triggerChunk(T value) {
|
||||||
{
|
if (_resultReady) return;
|
||||||
_chunkCallbacks.add(callback);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void trigger(T result)
|
_chunkCallbacks.forEach((x) {
|
||||||
{
|
x(value);
|
||||||
|
});
|
||||||
// lock (callbacksLock)
|
}
|
||||||
// {
|
|
||||||
if (_resultReady)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_result = result;
|
|
||||||
_resultReady = true;
|
|
||||||
|
|
||||||
_callbacks.forEach((x) {
|
|
||||||
x(result);
|
|
||||||
});
|
|
||||||
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
triggerError(Exception exception)
|
|
||||||
{
|
|
||||||
if (_resultReady)
|
|
||||||
return;
|
|
||||||
|
|
||||||
_exception = AsyncException.toAsyncException(exception);
|
|
||||||
|
|
||||||
///lock (callbacksLock)
|
|
||||||
//{
|
|
||||||
_errorCallbacks.forEach((x) {
|
|
||||||
x(_exception);
|
|
||||||
});
|
|
||||||
//}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
triggerProgress(ProgressType type, int value, int max)
|
|
||||||
{
|
|
||||||
if (_resultReady)
|
|
||||||
return;
|
|
||||||
|
|
||||||
//lock (callbacksLock)
|
|
||||||
//{
|
|
||||||
_progressCallbacks.forEach((x) {
|
|
||||||
x(type, value, max);
|
|
||||||
});
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
triggerChunk(T value)
|
|
||||||
{
|
|
||||||
if (_resultReady)
|
|
||||||
return;
|
|
||||||
|
|
||||||
//lock (callbacksLock)
|
|
||||||
//{
|
|
||||||
_chunkCallbacks.forEach((x) {
|
|
||||||
x(value);
|
|
||||||
});
|
|
||||||
|
|
||||||
//}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
AsyncReply.ready(T result)
|
|
||||||
{
|
|
||||||
_resultReady = true;
|
|
||||||
_result = result;
|
|
||||||
}
|
|
||||||
|
|
||||||
AsyncReply()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
AsyncReply.ready(T result) {
|
||||||
|
_resultReady = true;
|
||||||
|
_result = result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1 @@
|
|||||||
enum ErrorType
|
enum ErrorType { Management, Exception }
|
||||||
{
|
|
||||||
Management,
|
|
||||||
Exception
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,31 +1,29 @@
|
|||||||
|
enum ExceptionCode {
|
||||||
enum ExceptionCode
|
HostNotReachable,
|
||||||
{
|
AccessDenied,
|
||||||
HostNotReachable,
|
ResourceNotFound,
|
||||||
AccessDenied,
|
AttachDenied,
|
||||||
ResourceNotFound,
|
InvalidMethod,
|
||||||
AttachDenied,
|
InvokeDenied,
|
||||||
InvalidMethod,
|
CreateDenied,
|
||||||
InvokeDenied,
|
AddParentDenied,
|
||||||
CreateDenied,
|
AddChildDenied,
|
||||||
AddParentDenied,
|
ViewAttributeDenied,
|
||||||
AddChildDenied,
|
UpdateAttributeDenied,
|
||||||
ViewAttributeDenied,
|
StoreNotFound,
|
||||||
UpdateAttributeDenied,
|
ParentNotFound,
|
||||||
StoreNotFound,
|
ChildNotFound,
|
||||||
ParentNotFound,
|
ResourceIsNotStore,
|
||||||
ChildNotFound,
|
DeleteDenied,
|
||||||
ResourceIsNotStore,
|
DeleteFailed,
|
||||||
DeleteDenied,
|
UpdateAttributeFailed,
|
||||||
DeleteFailed,
|
GetAttributesFailed,
|
||||||
UpdateAttributeFailed,
|
ClearAttributesFailed,
|
||||||
GetAttributesFailed,
|
TemplateNotFound,
|
||||||
ClearAttributesFailed,
|
RenameDenied,
|
||||||
TemplateNotFound,
|
ClassNotFound,
|
||||||
RenameDenied,
|
MethodNotFound,
|
||||||
ClassNotFound,
|
PropertyNotFound,
|
||||||
MethodNotFound,
|
SetPropertyDenied,
|
||||||
PropertyNotFound,
|
ReadOnlyProperty
|
||||||
SetPropertyDenied,
|
|
||||||
ReadOnlyProperty
|
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,7 @@
|
|||||||
// library esiur;
|
|
||||||
|
|
||||||
import 'IEventHandler.dart';
|
import 'IEventHandler.dart';
|
||||||
|
|
||||||
typedef DestroyedEvent(sender);
|
typedef DestroyedEvent(sender);
|
||||||
|
|
||||||
abstract class IDestructible extends IEventHandler
|
abstract class IDestructible extends IEventHandler {
|
||||||
{
|
void destroy();
|
||||||
void destroy();
|
|
||||||
}
|
}
|
||||||
@@ -1,48 +1,45 @@
|
|||||||
class IEventHandler
|
class IEventHandler {
|
||||||
{
|
Map<String, List<Function>> _events;
|
||||||
Map<String, List<Function>> _events;
|
|
||||||
|
|
||||||
register(String event)
|
register(String event) {
|
||||||
{
|
_events[event.toLowerCase()] = [];
|
||||||
_events[event.toLowerCase()] = [];
|
}
|
||||||
}
|
|
||||||
|
|
||||||
IEventHandler()
|
IEventHandler() {
|
||||||
{
|
_events = {};
|
||||||
_events = {};
|
}
|
||||||
}
|
|
||||||
|
|
||||||
emitArgs(String event, List arguments)
|
emitArgs(String event, List arguments) {
|
||||||
{
|
event = event.toLowerCase();
|
||||||
event = event.toLowerCase();
|
if (_events.containsKey(event)) {
|
||||||
if (_events.containsKey(event))
|
for (var i = 0; i < _events[event].length; i++) {
|
||||||
for(var i = 0; i < _events[event].length; i++)
|
if (Function.apply(_events[event][i], arguments) != null) {
|
||||||
if (Function.apply(_events[event][i], arguments) != null)
|
return true;
|
||||||
return true;
|
|
||||||
|
|
||||||
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] = [];
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
on(String event, Function callback) {
|
||||||
|
event = event.toLowerCase();
|
||||||
|
|
||||||
|
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] = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
enum ProgressType
|
enum ProgressType {
|
||||||
{
|
Execution,
|
||||||
Execution,
|
Network,
|
||||||
Network,
|
}
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# Generated by pub
|
# Generated by pub
|
||||||
# See https://www.dartlang.org/tools/pub/glossary#lockfile
|
# See https://dart.dev/tools/pub/glossary#lockfile
|
||||||
packages:
|
packages:
|
||||||
analyzer:
|
analyzer:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
|
|||||||
Reference in New Issue
Block a user