2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-05-06 11:32:59 +00:00

New awaiters

This commit is contained in:
Ahmed Zamil 2019-12-31 13:23:55 +03:00
parent ed34ae229d
commit c8bec085a4
8 changed files with 181 additions and 11 deletions

View File

@ -14,6 +14,16 @@
<Version>1.3.0</Version> <Version>1.3.0</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Remove="MongoDBStore.cs" />
<Compile Remove="MongoDBStoreGeneric.cs" />
</ItemGroup>
<ItemGroup>
<None Include="MongoDBStore.cs" />
<None Include="MongoDBStoreGeneric.cs" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MongoDB.Bson" Version="2.9.1" /> <PackageReference Include="MongoDB.Bson" Version="2.9.1" />
<PackageReference Include="MongoDB.Driver" Version="2.9.1" /> <PackageReference Include="MongoDB.Driver" Version="2.9.1" />

View File

@ -6,20 +6,21 @@ using System.Threading.Tasks;
namespace Esyur.Core namespace Esyur.Core
{ {
public class AsyncAwaiter<T> : INotifyCompletion public class AsyncAwaiter : INotifyCompletion
{ {
Action callback = null; Action callback = null;
AsyncException exception = null; AsyncException exception = null;
T result; object result;
public AsyncAwaiter(AsyncReply reply) public AsyncAwaiter(AsyncReply reply)
{ {
reply.Then(x => reply.Then(x =>
{ {
this.IsCompleted = true; this.IsCompleted = true;
this.result = (T)x; this.result = x;
this.callback?.Invoke(); this.callback?.Invoke();
}).Error(x => }).Error(x =>
{ {
@ -29,7 +30,7 @@ namespace Esyur.Core
}); });
} }
public T GetResult() public object GetResult()
{ {
if (exception != null) if (exception != null)
throw exception; throw exception;
@ -47,6 +48,6 @@ namespace Esyur.Core
callback = continuation; callback = continuation;
} }
} }
} }

View File

@ -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<T> : INotifyCompletion
{
Action callback = null;
AsyncException exception = null;
T result;
public AsyncAwaiter(AsyncReply<T> 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;
}
}
}

View File

@ -46,9 +46,9 @@ namespace Esyur.Core
return this; return this;
} }
public new AsyncAwaiter<object[]> GetAwaiter() public new AsyncBagAwaiter GetAwaiter()
{ {
return new AsyncAwaiter<object[]>(this); return new AsyncBagAwaiter(this);
} }
public new object[] Wait() public new object[] Wait()

View File

@ -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;
}
}
}

View File

@ -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<T> : INotifyCompletion
{
Action callback = null;
AsyncException exception = null;
T[] result;
public AsyncBagAwaiter(AsyncBag<T> 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;
}
}
}

View File

@ -51,9 +51,9 @@ namespace Esyur.Core
} }
public new AsyncAwaiter<T[]> GetAwaiter() public new AsyncBagAwaiter<T> GetAwaiter()
{ {
return new AsyncAwaiter<T[]>(this); return new AsyncBagAwaiter<T>(this);
} }
public new T[] Wait() public new T[] Wait()

View File

@ -255,9 +255,9 @@ namespace Esyur.Core
//} //}
} }
public AsyncAwaiter<object> GetAwaiter() public AsyncAwaiter GetAwaiter()
{ {
return new AsyncAwaiter<object>(this); return new AsyncAwaiter(this);
} }