From c8bec085a40f058b923677132ca31ebde00e4610 Mon Sep 17 00:00:00 2001 From: Ahmed Zamil Date: Tue, 31 Dec 2019 13:23:55 +0300 Subject: [PATCH] New awaiters --- .../Esyur.Stores.MongoDB.csproj | 10 ++++ Esyur/Core/AsyncAwaiter.cs | 11 ++-- Esyur/Core/AsyncAwaiterGeneric.cs | 53 +++++++++++++++++++ Esyur/Core/AsyncBag.cs | 4 +- Esyur/Core/AsyncBagAwaiter.cs | 53 +++++++++++++++++++ Esyur/Core/AsyncBagAwaiterGeneric.cs | 53 +++++++++++++++++++ Esyur/Core/AsyncBagGeneric.cs | 4 +- Esyur/Core/AsyncReply.cs | 4 +- 8 files changed, 181 insertions(+), 11 deletions(-) create mode 100644 Esyur/Core/AsyncAwaiterGeneric.cs create mode 100644 Esyur/Core/AsyncBagAwaiter.cs create mode 100644 Esyur/Core/AsyncBagAwaiterGeneric.cs diff --git a/Esyur.Stores.MongoDB/Esyur.Stores.MongoDB.csproj b/Esyur.Stores.MongoDB/Esyur.Stores.MongoDB.csproj index 58dc39b..89fa366 100644 --- a/Esyur.Stores.MongoDB/Esyur.Stores.MongoDB.csproj +++ b/Esyur.Stores.MongoDB/Esyur.Stores.MongoDB.csproj @@ -14,6 +14,16 @@ 1.3.0 + + + + + + + + + + diff --git a/Esyur/Core/AsyncAwaiter.cs b/Esyur/Core/AsyncAwaiter.cs index 9766ed2..4bedac1 100644 --- a/Esyur/Core/AsyncAwaiter.cs +++ b/Esyur/Core/AsyncAwaiter.cs @@ -6,20 +6,21 @@ using System.Threading.Tasks; namespace Esyur.Core { - public class AsyncAwaiter : INotifyCompletion + public class AsyncAwaiter : INotifyCompletion { Action callback = null; AsyncException exception = null; - T result; + object result; + public AsyncAwaiter(AsyncReply reply) { reply.Then(x => { this.IsCompleted = true; - this.result = (T)x; + this.result = x; this.callback?.Invoke(); }).Error(x => { @@ -29,7 +30,7 @@ namespace Esyur.Core }); } - public T GetResult() + public object GetResult() { if (exception != null) throw exception; @@ -47,6 +48,6 @@ namespace Esyur.Core callback = continuation; } - + } } diff --git a/Esyur/Core/AsyncAwaiterGeneric.cs b/Esyur/Core/AsyncAwaiterGeneric.cs new file mode 100644 index 0000000..095f9b9 --- /dev/null +++ b/Esyur/Core/AsyncAwaiterGeneric.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; + +namespace Esyur.Core +{ + public class AsyncAwaiter : INotifyCompletion + { + Action callback = null; + + AsyncException exception = null; + + T result; + + public AsyncAwaiter(AsyncReply reply) + { + reply.Then(x => + { + this.IsCompleted = true; + this.result = (T)x; + this.callback?.Invoke(); + }).Error(x => + { + exception = x; + this.IsCompleted = true; + this.callback?.Invoke(); + }); + } + + public T GetResult() + { + if (exception != null) + throw exception; + return result; + } + + public bool IsCompleted { get; private set; } + + public void OnCompleted(Action continuation) + { + if (IsCompleted) + continuation?.Invoke(); + else + // Continue.... + callback = continuation; + } + + + } +} + diff --git a/Esyur/Core/AsyncBag.cs b/Esyur/Core/AsyncBag.cs index be2ead4..8819777 100644 --- a/Esyur/Core/AsyncBag.cs +++ b/Esyur/Core/AsyncBag.cs @@ -46,9 +46,9 @@ namespace Esyur.Core return this; } - public new AsyncAwaiter GetAwaiter() + public new AsyncBagAwaiter GetAwaiter() { - return new AsyncAwaiter(this); + return new AsyncBagAwaiter(this); } public new object[] Wait() diff --git a/Esyur/Core/AsyncBagAwaiter.cs b/Esyur/Core/AsyncBagAwaiter.cs new file mode 100644 index 0000000..5b4a789 --- /dev/null +++ b/Esyur/Core/AsyncBagAwaiter.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; + +namespace Esyur.Core +{ + public class AsyncBagAwaiter : INotifyCompletion + { + Action callback = null; + + AsyncException exception = null; + + object[] result; + + public AsyncBagAwaiter(AsyncBag reply) + { + reply.Then(x => + { + this.IsCompleted = true; + this.result = x; + this.callback?.Invoke(); + }).Error(x => + { + exception = x; + this.IsCompleted = true; + this.callback?.Invoke(); + }); + } + + public object[] GetResult() + { + if (exception != null) + throw exception; + return result; + } + + public bool IsCompleted { get; private set; } + + public void OnCompleted(Action continuation) + { + if (IsCompleted) + continuation?.Invoke(); + else + // Continue.... + callback = continuation; + } + + + } +} + diff --git a/Esyur/Core/AsyncBagAwaiterGeneric.cs b/Esyur/Core/AsyncBagAwaiterGeneric.cs new file mode 100644 index 0000000..39c32a7 --- /dev/null +++ b/Esyur/Core/AsyncBagAwaiterGeneric.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; + +namespace Esyur.Core +{ + public class AsyncBagAwaiter : INotifyCompletion + { + Action callback = null; + + AsyncException exception = null; + + T[] result; + + public AsyncBagAwaiter(AsyncBag reply) + { + reply.Then(x => + { + this.IsCompleted = true; + this.result = x; + this.callback?.Invoke(); + }).Error(x => + { + exception = x; + this.IsCompleted = true; + this.callback?.Invoke(); + }); + } + + public T[] GetResult() + { + if (exception != null) + throw exception; + return result; + } + + public bool IsCompleted { get; private set; } + + public void OnCompleted(Action continuation) + { + if (IsCompleted) + continuation?.Invoke(); + else + // Continue.... + callback = continuation; + } + + + } +} + diff --git a/Esyur/Core/AsyncBagGeneric.cs b/Esyur/Core/AsyncBagGeneric.cs index 153a2d0..7a5afe4 100644 --- a/Esyur/Core/AsyncBagGeneric.cs +++ b/Esyur/Core/AsyncBagGeneric.cs @@ -51,9 +51,9 @@ namespace Esyur.Core } - public new AsyncAwaiter GetAwaiter() + public new AsyncBagAwaiter GetAwaiter() { - return new AsyncAwaiter(this); + return new AsyncBagAwaiter(this); } public new T[] Wait() diff --git a/Esyur/Core/AsyncReply.cs b/Esyur/Core/AsyncReply.cs index 790f96c..3a26d5f 100644 --- a/Esyur/Core/AsyncReply.cs +++ b/Esyur/Core/AsyncReply.cs @@ -255,9 +255,9 @@ namespace Esyur.Core //} } - public AsyncAwaiter GetAwaiter() + public AsyncAwaiter GetAwaiter() { - return new AsyncAwaiter(this); + return new AsyncAwaiter(this); }