comparison Implab/PromiseReaction`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 fb70574741a1
children
comparison
equal deleted inserted replaced
247:fb70574741a1 248:5cb4826c2c2a
1 using System; 1 using System;
2 2
3 namespace Implab { 3 namespace Implab {
4 /// <summary>
5 /// Базовыйй класс для создания обработчиков результов выполнения обещаний.
6 /// Данный объект связывает обработчик и обешание, при этом для выполнения
7 /// обработчика будет использоваться диспетчер.
8 /// </summary>
4 abstract class PromiseReaction<T> : IResolvable<T> { 9 abstract class PromiseReaction<T> : IResolvable<T> {
5 readonly IDispatcher m_dispatcher; 10 readonly IDispatcher m_dispatcher;
6 11
7 protected PromiseReaction(IDispatcher dispatcher) { 12 protected PromiseReaction(IDispatcher dispatcher) {
8 m_dispatcher = dispatcher; 13 m_dispatcher = dispatcher;
9 } 14 }
10 15
11 protected abstract bool HasFulfilHandler { 16 protected Action<T> FulfilHandler { get; set; }
12 get;
13 }
14 17
15 protected abstract bool HasRejectHandler { 18 protected Action<Exception> RejectHandler { get; set; }
16 get;
17 }
18 19
19 public void Reject(Exception error) { 20 public void Reject(Exception error) {
20 if (!HasRejectHandler) 21 if (RejectHandler == null)
21 DefaultReject(error); 22 DefaultReject(error);
22 else if (m_dispatcher != null) 23 else if (m_dispatcher != null)
23 m_dispatcher.Enqueue(() => RejectImpl(error)); 24 m_dispatcher.Enqueue(RejectHandler, error);
24 else 25 else
25 RejectImpl(error); 26 RejectHandler(error);
26 } 27 }
27 28
28 public void Resolve(T result) { 29 public void Resolve(T result) {
29 if (!HasFulfilHandler) 30 if (FulfilHandler == null)
30 DefaultResolve(result); 31 DefaultResolve(result);
31 else if (m_dispatcher != null) 32 else if (m_dispatcher != null)
32 m_dispatcher.Enqueue(() => ResolveImpl(result)); 33 m_dispatcher.Enqueue(FulfilHandler, result);
33 else 34 else
34 ResolveImpl(result); 35 FulfilHandler(result);
35 } 36 }
36
37 protected abstract void ResolveImpl(T result);
38
39 protected abstract void RejectImpl(Exception reason);
40 37
41 protected abstract void DefaultResolve(T result); 38 protected abstract void DefaultResolve(T result);
42 39
43 protected abstract void DefaultReject(Exception reason); 40 protected abstract void DefaultReject(Exception reason);
44 } 41 }