annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
1 using System;
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
2
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
3 namespace Implab {
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
4 /// <summary>
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
5 /// Базовыйй класс для создания обработчиков результов выполнения обещаний.
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
6 /// Данный объект связывает обработчик и обешание, при этом для выполнения
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
7 /// обработчика будет использоваться диспетчер.
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
8 /// </summary>
247
fb70574741a1 working on promises
cin
parents: 246
diff changeset
9 abstract class PromiseReaction<T> : IResolvable<T> {
fb70574741a1 working on promises
cin
parents: 246
diff changeset
10 readonly IDispatcher m_dispatcher;
fb70574741a1 working on promises
cin
parents: 246
diff changeset
11
fb70574741a1 working on promises
cin
parents: 246
diff changeset
12 protected PromiseReaction(IDispatcher dispatcher) {
fb70574741a1 working on promises
cin
parents: 246
diff changeset
13 m_dispatcher = dispatcher;
fb70574741a1 working on promises
cin
parents: 246
diff changeset
14 }
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
15
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
16 protected Action<T> FulfilHandler { get; set; }
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
17
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
18 protected Action<Exception> RejectHandler { get; set; }
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
19
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
20 public void Reject(Exception error) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
21 if (RejectHandler == null)
247
fb70574741a1 working on promises
cin
parents: 246
diff changeset
22 DefaultReject(error);
fb70574741a1 working on promises
cin
parents: 246
diff changeset
23 else if (m_dispatcher != null)
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
24 m_dispatcher.Enqueue(RejectHandler, error);
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
25 else
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
26 RejectHandler(error);
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
27 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
28
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
29 public void Resolve(T result) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
30 if (FulfilHandler == null)
247
fb70574741a1 working on promises
cin
parents: 246
diff changeset
31 DefaultResolve(result);
fb70574741a1 working on promises
cin
parents: 246
diff changeset
32 else if (m_dispatcher != null)
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
33 m_dispatcher.Enqueue(FulfilHandler, result);
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
34 else
248
5cb4826c2c2a Added awaiters to promises
cin
parents: 247
diff changeset
35 FulfilHandler(result);
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
36 }
247
fb70574741a1 working on promises
cin
parents: 246
diff changeset
37
fb70574741a1 working on promises
cin
parents: 246
diff changeset
38 protected abstract void DefaultResolve(T result);
fb70574741a1 working on promises
cin
parents: 246
diff changeset
39
fb70574741a1 working on promises
cin
parents: 246
diff changeset
40 protected abstract void DefaultReject(Exception reason);
246
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
41 }
5aa9cfbe56c3 missing files
cin
parents:
diff changeset
42 }