annotate Implab/PromiseAwaiter`1.cs @ 248:5cb4826c2c2a v3

Added awaiters to promises Added static methods to Promise Resolve, Reject, All. Updated promise helpers
author cin
date Tue, 30 Jan 2018 01:37:17 +0300
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
1 using System;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
2 using System.Runtime.CompilerServices;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
3 using System.Threading;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
4 using Implab.Parallels;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
5
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
6 namespace Implab {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
7 public struct PromiseAwaiter<T> : INotifyCompletion {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
8 class PromiseEvent : IResolvable<T> {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
9 IDispatcher m_dispatcher;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
10
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
11 Action m_handler;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
12
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
13 public PromiseEvent(Action handler, IDispatcher dispatcher) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
14 m_handler = handler;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
15 m_dispatcher = dispatcher;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
16 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
17
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
18 public void Resolve(T result) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
19 m_dispatcher.Enqueue(m_handler);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
20 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
21
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
22 public void Reject(Exception error) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
23 m_dispatcher.Enqueue(m_handler);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
24 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
25 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
26
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
27 readonly IPromise<T> m_promise;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
28
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
29 readonly IDispatcher m_dispatcher;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
30
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
31 public PromiseAwaiter(IPromise<T> promise) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
32 m_promise = promise;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
33 m_dispatcher = GetDispatcher();
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
34 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
35
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
36 public PromiseAwaiter(IPromise<T> promise, IDispatcher dispatcher) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
37 m_promise = promise;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
38 m_dispatcher = dispatcher;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
39 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
40
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
41 public void OnCompleted(Action continuation) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
42 if (m_promise != null)
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
43 m_promise.Then(new PromiseEvent(continuation, GetDispatcher()));
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
44 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
45
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
46 public T GetResult() {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
47 return m_promise.Join();
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
48 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
49
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
50 static IDispatcher GetDispatcher() {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
51 if (SynchronizationContext.Current == null)
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
52 return ThreadPoolDispatcher.Instance;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
53 return new SyncContextDispatcher(SynchronizationContext.Current);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
54 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
55
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
56 public bool IsCompleted {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
57 get {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
58 return m_promise.IsResolved;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
59 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
60 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
61 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
62 }