comparison Implab/PromiseReaction`1.cs @ 247:fb70574741a1 v3

working on promises
author cin
date Fri, 26 Jan 2018 18:46:27 +0300
parents 5aa9cfbe56c3
children 5cb4826c2c2a
comparison
equal deleted inserted replaced
246:5aa9cfbe56c3 247:fb70574741a1
1 using System; 1 using System;
2 2
3 namespace Implab { 3 namespace Implab {
4 public class PromiseReaction<T> : IResolvable<T> { 4 abstract class PromiseReaction<T> : IResolvable<T> {
5 IDispatcher m_dispatcher; 5 readonly IDispatcher m_dispatcher;
6 6
7 Action<T> m_onFulfilledJob; 7 protected PromiseReaction(IDispatcher dispatcher) {
8 m_dispatcher = dispatcher;
9 }
8 10
9 Action<Exception> m_onRejectedJob; 11 protected abstract bool HasFulfilHandler {
12 get;
13 }
14
15 protected abstract bool HasRejectHandler {
16 get;
17 }
10 18
11 public void Reject(Exception error) { 19 public void Reject(Exception error) {
12 if (m_dispatcher != null) 20 if (!HasRejectHandler)
13 m_dispatcher.Enqueue(() => m_onRejectedJob(error)); 21 DefaultReject(error);
22 else if (m_dispatcher != null)
23 m_dispatcher.Enqueue(() => RejectImpl(error));
14 else 24 else
15 m_onRejectedJob(error); 25 RejectImpl(error);
16 } 26 }
17 27
18 public void Resolve(T result) { 28 public void Resolve(T result) {
19 if (m_dispatcher != null) 29 if (!HasFulfilHandler)
20 m_dispatcher.Enqueue(() => m_onFulfilledJob(result)); 30 DefaultResolve(result);
31 else if (m_dispatcher != null)
32 m_dispatcher.Enqueue(() => ResolveImpl(result));
21 else 33 else
22 m_onFulfilledJob(result); 34 ResolveImpl(result);
23 } 35 }
36
37 protected abstract void ResolveImpl(T result);
38
39 protected abstract void RejectImpl(Exception reason);
40
41 protected abstract void DefaultResolve(T result);
42
43 protected abstract void DefaultReject(Exception reason);
24 } 44 }
25 } 45 }