From 57c8fc903d097deb50e6300322edd7cb18c038d1 Mon Sep 17 00:00:00 2001 From: Mohammed Salman Date: Sat, 31 Aug 2019 16:52:05 +0300 Subject: [PATCH] Refactor Core --- lib/src/Core/AsyncBag.dart | 72 ++++----- lib/src/Core/AsyncException.dart | 44 +++--- lib/src/Core/AsyncQueue.dart | 69 ++++----- lib/src/Core/AsyncReply.dart | 248 +++++++++++-------------------- lib/src/Core/ErrorType.dart | 6 +- lib/src/Core/ExceptionCode.dart | 60 ++++---- lib/src/Core/IDestructible.dart | 9 +- lib/src/Core/IEventHandler.dart | 81 +++++----- lib/src/Core/ProgressType.dart | 9 +- pubspec.lock | 2 +- 10 files changed, 245 insertions(+), 355 deletions(-) diff --git a/lib/src/Core/AsyncBag.dart b/lib/src/Core/AsyncBag.dart index e15b012..09ec650 100644 --- a/lib/src/Core/AsyncBag.dart +++ b/lib/src/Core/AsyncBag.dart @@ -1,52 +1,46 @@ import 'AsyncReply.dart'; -class AsyncBag extends AsyncReply> -{ +class AsyncBag extends AsyncReply> { + List> _replies = List>(); + List _results = List(); - List> _replies = new List>(); - List _results = new List(); + int _count = 0; + bool _sealedBag = false; - int _count = 0; - bool _sealedBag = false; + seal() { + if (_sealedBag) { + return; + } + _sealedBag = true; - seal() - { - if (_sealedBag) - return; + if (_results.length == 0) { + trigger(List()); + } - _sealedBag = true; + for (var i = 0; i < _results.length; i++) { + var k = _replies[i]; + var index = i; - if (_results.length == 0) - trigger(new List()); - - 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); - }); + k.then((r) { + _results[index] = r; + _count++; + if (_count == _results.length) { + trigger(_results); } + }); } + } - add(AsyncReply reply) - { - if (!_sealedBag) - { - _results.add(null); - _replies.add(reply); - } + add(AsyncReply reply) { + if (!_sealedBag) { + _results.add(null); + _replies.add(reply); } + } - addBag(AsyncBag bag) - { - bag._replies.forEach((r) { - add(r); - }); - } + addBag(AsyncBag bag) { + bag._replies.forEach((r) { + add(r); + }); + } } diff --git a/lib/src/Core/AsyncException.dart b/lib/src/Core/AsyncException.dart index ec04af3..2fce138 100644 --- a/lib/src/Core/AsyncException.dart +++ b/lib/src/Core/AsyncException.dart @@ -1,33 +1,27 @@ - import 'ExceptionCode.dart'; import 'ErrorType.dart'; -class AsyncException implements Exception -{ - final ErrorType type; - final int code; - final String message; +class AsyncException implements Exception { + AsyncException(this.type, this.code, this.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(); } diff --git a/lib/src/Core/AsyncQueue.dart b/lib/src/Core/AsyncQueue.dart index c1339d7..41c5f2c 100644 --- a/lib/src/Core/AsyncQueue.dart +++ b/lib/src/Core/AsyncQueue.dart @@ -2,50 +2,33 @@ library esiur; import 'AsyncReply.dart'; -class AsyncQueue extends AsyncReply -{ - List> _list = new List>(); +class AsyncQueue extends AsyncReply { + List> _list = >[]; -// object queueLock = new object(); - - add(AsyncReply reply) - { - //lock (queueLock) - _list.add(reply); + void add(AsyncReply reply) { + _list.add(reply); - //super._resultReady = false; - super.setResultReady(false); + super.setResultReady(false); - reply.then(processQueue); - } - - remove(AsyncReply 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() - { - - } + reply.then(processQueue); } + + void remove(AsyncReply 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); + } +} diff --git a/lib/src/Core/AsyncReply.dart b/lib/src/Core/AsyncReply.dart index 2be9080..dd63ce9 100644 --- a/lib/src/Core/AsyncReply.dart +++ b/lib/src/Core/AsyncReply.dart @@ -26,195 +26,127 @@ import 'dart:core'; import 'AsyncException.dart'; import 'ProgressType.dart'; -class AsyncReply implements Future -{ +class AsyncReply implements Future { + AsyncReply(); - List _callbacks = new List(); + List _callbacks = []; - T _result; + T _result; - List _errorCallbacks = new List(); - - List _progressCallbacks = new List(); + final _errorCallbacks = []; - List _chunkCallbacks = new List(); + final _progressCallbacks = []; + final _chunkCallbacks = []; + bool _resultReady = false; + AsyncException _exception; - bool _resultReady = false; - AsyncException _exception; + bool get ready => _resultReady; + T get result => _result; - bool get ready - { - return _resultReady; - } + setResultReady(bool val) => _resultReady = val; - T get result - { - return _result; - } + AsyncReply then(FutureOr onValue(T value), {Function onError}) { + _callbacks.add(onValue); - setResultReady(bool val) - { - _resultReady = val; - } - - AsyncReply then(FutureOr 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 (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) - onValue(result); - - return this as AsyncReply; - + return this as AsyncReply; } - AsyncReply whenComplete(FutureOr action()) - { - return this; - //_callbacks.add(action); - } + AsyncReply whenComplete(FutureOr action()) => this; - Stream asStream() - { - return null; - } + Stream asStream() => null; - AsyncReply catchError(Function onError, {bool test(Object error)}) - { - return this.error(onError); - } + AsyncReply catchError(Function onError, {bool test(Object error)}) => + this.error(onError); + + AsyncReply timeout(Duration timeLimit, {FutureOr onTimeout()}) => this; + + @deprecated + AsyncReply _then_old(Function(T) callback) { + _callbacks.add(callback); + + if (_resultReady) callback(result); - AsyncReply timeout(Duration timeLimit, {FutureOr onTimeout()}) - { return this; } - AsyncReply _then_old(Function(T) callback) - { - _callbacks.add(callback); + AsyncReply error(Function(dynamic) callback) { + _errorCallbacks.add(callback); - if (_resultReady) - callback(result); + if (_exception != null) callback(_exception); - return this; + return this; + } + + AsyncReply progress(Function(ProgressType, int, int) callback) { + _progressCallbacks.add(callback); + return this; + } + + AsyncReply chunk(Function(T) callback) { + _chunkCallbacks.add(callback); + return this; + } + + void trigger(T result) { + if (_resultReady) { + return; } - - AsyncReply error(Function(dynamic) callback) - { - _errorCallbacks.add(callback); + _result = result; + _resultReady = true; - if (_exception != null) - callback(_exception); - - return this; + _callbacks.forEach((x) { + x(result); + }); + } + + triggerError(Exception exception) { + if (_resultReady) { + return; } - AsyncReply progress(Function(ProgressType, int, int) callback) - { - _progressCallbacks.add(callback); - return this; + _exception = AsyncException.toAsyncException(exception); + + _errorCallbacks.forEach((x) { + x(_exception); + }); + } + + triggerProgress(ProgressType type, int value, int max) { + if (_resultReady) { + return; } - - AsyncReply chunk(Function(T) callback) - { - _chunkCallbacks.add(callback); - return this; - } + _progressCallbacks.forEach((x) { + x(type, value, max); + }); + } - void trigger(T result) - { + triggerChunk(T value) { + if (_resultReady) return; -// lock (callbacksLock) -// { - if (_resultReady) - return; + _chunkCallbacks.forEach((x) { + x(value); + }); + } - _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() - { - - } - -} \ No newline at end of file + AsyncReply.ready(T result) { + _resultReady = true; + _result = result; + } +} diff --git a/lib/src/Core/ErrorType.dart b/lib/src/Core/ErrorType.dart index 171b6ee..fedba9d 100644 --- a/lib/src/Core/ErrorType.dart +++ b/lib/src/Core/ErrorType.dart @@ -1,5 +1 @@ -enum ErrorType -{ - Management, - Exception -} +enum ErrorType { Management, Exception } diff --git a/lib/src/Core/ExceptionCode.dart b/lib/src/Core/ExceptionCode.dart index 09f8bd2..4bc1582 100644 --- a/lib/src/Core/ExceptionCode.dart +++ b/lib/src/Core/ExceptionCode.dart @@ -1,31 +1,29 @@ - -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 -} \ No newline at end of file +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 +} diff --git a/lib/src/Core/IDestructible.dart b/lib/src/Core/IDestructible.dart index 748d135..9a83102 100644 --- a/lib/src/Core/IDestructible.dart +++ b/lib/src/Core/IDestructible.dart @@ -1,10 +1,7 @@ -// library esiur; - import 'IEventHandler.dart'; typedef DestroyedEvent(sender); -abstract class IDestructible extends IEventHandler -{ - void destroy(); -} \ No newline at end of file +abstract class IDestructible extends IEventHandler { + void destroy(); +} diff --git a/lib/src/Core/IEventHandler.dart b/lib/src/Core/IEventHandler.dart index 1d81101..c4a10b2 100644 --- a/lib/src/Core/IEventHandler.dart +++ b/lib/src/Core/IEventHandler.dart @@ -1,48 +1,45 @@ -class IEventHandler -{ - Map> _events; +class IEventHandler { + Map> _events; - register(String event) - { - _events[event.toLowerCase()] = []; - } + register(String event) { + _events[event.toLowerCase()] = []; + } - IEventHandler() - { - _events = {}; - } + 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] = []; + 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(); + + 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] = []; + } + } + } } diff --git a/lib/src/Core/ProgressType.dart b/lib/src/Core/ProgressType.dart index 76e17b6..cf24abc 100644 --- a/lib/src/Core/ProgressType.dart +++ b/lib/src/Core/ProgressType.dart @@ -1,5 +1,4 @@ - enum ProgressType - { - Execution, - Network, - } \ No newline at end of file +enum ProgressType { + Execution, + Network, +} diff --git a/pubspec.lock b/pubspec.lock index 5cf1ecb..c391e7f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,5 +1,5 @@ # Generated by pub -# See https://www.dartlang.org/tools/pub/glossary#lockfile +# See https://dart.dev/tools/pub/glossary#lockfile packages: analyzer: dependency: transitive