mirror of
https://github.com/esiur/esiur-dart.git
synced 2025-06-27 14:53:11 +00:00
1.0
This commit is contained in:
58
bin/Core/AsyncBag.dart
Normal file
58
bin/Core/AsyncBag.dart
Normal file
@ -0,0 +1,58 @@
|
||||
import 'AsyncReply.dart';
|
||||
|
||||
class AsyncBag<T> extends AsyncReply<List<T>>
|
||||
{
|
||||
|
||||
List<AsyncReply<T>> _replies = new List<AsyncReply<T>>();
|
||||
List<T> _results = new List<T>();
|
||||
|
||||
int _count = 0;
|
||||
bool _sealedBag = false;
|
||||
|
||||
seal()
|
||||
{
|
||||
if (_sealedBag)
|
||||
return;
|
||||
|
||||
_sealedBag = true;
|
||||
|
||||
if (_results.length == 0)
|
||||
trigger(new List<T>());
|
||||
|
||||
for (var i = 0; i < _results.length; i++)
|
||||
{
|
||||
var k = _replies[i];
|
||||
var index = i;
|
||||
|
||||
k.then((r)
|
||||
{
|
||||
_results[index] = r;
|
||||
_count++;
|
||||
if (_count == _results.length)
|
||||
trigger(_results);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
add(AsyncReply<T> reply)
|
||||
{
|
||||
if (!_sealedBag)
|
||||
{
|
||||
_results.add(null);
|
||||
_replies.add(reply);
|
||||
}
|
||||
}
|
||||
|
||||
addBag(AsyncBag<T> bag)
|
||||
{
|
||||
bag._replies.forEach((r) {
|
||||
add(r);
|
||||
});
|
||||
}
|
||||
|
||||
AsyncBag()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
33
bin/Core/AsyncException.dart
Normal file
33
bin/Core/AsyncException.dart
Normal file
@ -0,0 +1,33 @@
|
||||
|
||||
import 'ExceptionCode.dart';
|
||||
import 'ErrorType.dart';
|
||||
|
||||
class AsyncException implements Exception
|
||||
{
|
||||
final ErrorType type;
|
||||
final int code;
|
||||
final String message;
|
||||
|
||||
AsyncException(this.type, this.code, this.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();
|
||||
}
|
||||
}
|
51
bin/Core/AsyncQueue.dart
Normal file
51
bin/Core/AsyncQueue.dart
Normal file
@ -0,0 +1,51 @@
|
||||
library esiur;
|
||||
|
||||
import 'AsyncReply.dart';
|
||||
|
||||
class AsyncQueue<T> extends AsyncReply<T>
|
||||
{
|
||||
List<AsyncReply<T>> _list = new List<AsyncReply<T>>();
|
||||
|
||||
// object queueLock = new object();
|
||||
|
||||
add(AsyncReply<T> reply)
|
||||
{
|
||||
//lock (queueLock)
|
||||
_list.add(reply);
|
||||
|
||||
//super._resultReady = false;
|
||||
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()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
220
bin/Core/AsyncReply.dart
Normal file
220
bin/Core/AsyncReply.dart
Normal file
@ -0,0 +1,220 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2019 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
import 'dart:async';
|
||||
import 'dart:core';
|
||||
import 'AsyncException.dart';
|
||||
import 'ProgressType.dart';
|
||||
|
||||
class AsyncReply<T> implements Future<T>
|
||||
{
|
||||
|
||||
List<Function(T)> _callbacks = new List<Function(T)>();
|
||||
|
||||
T _result;
|
||||
|
||||
List<Function(AsyncException)> _errorCallbacks = new List<Function(AsyncException)>();
|
||||
|
||||
List<Function(ProgressType, int, int)> _progressCallbacks = new List<Function(ProgressType, int, int)>();
|
||||
|
||||
List<Function(T)> _chunkCallbacks = new List<Function(T)>();
|
||||
|
||||
|
||||
|
||||
bool _resultReady = false;
|
||||
AsyncException _exception;
|
||||
|
||||
|
||||
bool get ready
|
||||
{
|
||||
return _resultReady;
|
||||
}
|
||||
|
||||
T get result
|
||||
{
|
||||
return _result;
|
||||
}
|
||||
|
||||
setResultReady(bool val)
|
||||
{
|
||||
_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);
|
||||
|
||||
return this as AsyncReply<R>;
|
||||
|
||||
}
|
||||
|
||||
AsyncReply<T> whenComplete(FutureOr action())
|
||||
{
|
||||
return this;
|
||||
//_callbacks.add(action);
|
||||
}
|
||||
|
||||
Stream<T> asStream()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
AsyncReply<T> catchError(Function onError, {bool test(Object error)})
|
||||
{
|
||||
return this.error(onError);
|
||||
}
|
||||
|
||||
AsyncReply<T> timeout(Duration timeLimit, {FutureOr<T> onTimeout()})
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
AsyncReply<T> _then_old(Function(T) callback)
|
||||
{
|
||||
_callbacks.add(callback);
|
||||
|
||||
if (_resultReady)
|
||||
callback(result);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
AsyncReply<T> error(Function(dynamic) callback)
|
||||
{
|
||||
_errorCallbacks.add(callback);
|
||||
|
||||
if (_exception != null)
|
||||
callback(_exception);
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
// 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()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
5
bin/Core/ErrorType.dart
Normal file
5
bin/Core/ErrorType.dart
Normal file
@ -0,0 +1,5 @@
|
||||
enum ErrorType
|
||||
{
|
||||
Management,
|
||||
Exception
|
||||
}
|
31
bin/Core/ExceptionCode.dart
Normal file
31
bin/Core/ExceptionCode.dart
Normal file
@ -0,0 +1,31 @@
|
||||
|
||||
enum ExceptionCode
|
||||
{
|
||||
HostNotReachable,
|
||||
AccessDenied,
|
||||
ResourceNotFound,
|
||||
AttachDenied,
|
||||
InvalidMethod,
|
||||
InvokeDenied,
|
||||
CreateDenied,
|
||||
AddParentDenied,
|
||||
AddChildDenied,
|
||||
ViewAttributeDenied,
|
||||
UpdateAttributeDenied,
|
||||
StoreNotFound,
|
||||
ParentNotFound,
|
||||
ChildNotFound,
|
||||
ResourceIsNotStore,
|
||||
DeleteDenied,
|
||||
DeleteFailed,
|
||||
UpdateAttributeFailed,
|
||||
GetAttributesFailed,
|
||||
ClearAttributesFailed,
|
||||
TemplateNotFound,
|
||||
RenameDenied,
|
||||
ClassNotFound,
|
||||
MethodNotFound,
|
||||
PropertyNotFound,
|
||||
SetPropertyDenied,
|
||||
ReadOnlyProperty
|
||||
}
|
10
bin/Core/IDestructible.dart
Normal file
10
bin/Core/IDestructible.dart
Normal file
@ -0,0 +1,10 @@
|
||||
library esiur;
|
||||
|
||||
import 'IEventHandler.dart';
|
||||
|
||||
typedef DestroyedEvent(sender);
|
||||
|
||||
abstract class IDestructible extends IEventHandler
|
||||
{
|
||||
void destroy();
|
||||
}
|
48
bin/Core/IEventHandler.dart
Normal file
48
bin/Core/IEventHandler.dart
Normal file
@ -0,0 +1,48 @@
|
||||
class IEventHandler
|
||||
{
|
||||
Map<String, List<Function>> _events;
|
||||
|
||||
register(String event)
|
||||
{
|
||||
_events[event.toLowerCase()] = [];
|
||||
}
|
||||
|
||||
IEventHandler()
|
||||
{
|
||||
_events = {};
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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] = [];
|
||||
}
|
||||
}
|
||||
}
|
5
bin/Core/ProgressType.dart
Normal file
5
bin/Core/ProgressType.dart
Normal file
@ -0,0 +1,5 @@
|
||||
enum ProgressType
|
||||
{
|
||||
Execution,
|
||||
Network,
|
||||
}
|
Reference in New Issue
Block a user