annotate Implab/PromiseT.cs @ 146:e03ccec4a08d v2

minor changes
author cin
date Mon, 09 Mar 2015 17:21:20 +0300
parents 8c0b95069066
children 4d9830a9bbb8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
1 using System;
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
2 using System.Diagnostics;
144
8c0b95069066 DRAFT: refactoring
cin
parents: 138
diff changeset
3 using Implab.Parallels;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
4
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
5 namespace Implab {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
6
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
7 /// <summary>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
8 /// Класс для асинхронного получения результатов. Так называемое "обещание".
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
9 /// </summary>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
10 /// <typeparam name="T">Тип получаемого результата</typeparam>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
11 /// <remarks>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
12 /// <para>Сервис при обращении к его методу дает обещаиние о выполнении операции,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
13 /// клиент получив такое обещание может установить ряд обратных вызово для получения
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
14 /// событий выполнения обещания, тоесть завершения операции и предоставлении результатов.</para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
15 /// <para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
16 /// Обещение может быть как выполнено, так и выполнено с ошибкой. Для подписки на
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
17 /// данные события клиент должен использовать методы <c>Then</c>.
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
18 /// </para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
19 /// <para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
20 /// Сервис, в свою очередь, по окончанию выполнения операции (возможно с ошибкой),
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
21 /// использует методы <c>Resolve</c> либо <c>Reject</c> для оповещения клиетна о
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
22 /// выполнении обещания.
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
23 /// </para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
24 /// <para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
25 /// Если сервер успел выполнить обещание еще до того, как клиент на него подписался,
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
26 /// то в момент подписки клиента будут вызваны соответсвующие события в синхронном
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
27 /// режиме и клиент будет оповещен в любом случае. Иначе, обработчики добавляются в
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
28 /// список в порядке подписания и в этом же порядке они будут вызваны при выполнении
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
29 /// обещания.
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
30 /// </para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
31 /// <para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
32 /// Обрабатывая результаты обещания можно преобразовывать результаты либо инициировать
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
33 /// связанные асинхронные операции, которые также возвращают обещания. Для этого следует
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
34 /// использовать соответствующую форму методе <c>Then</c>.
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
35 /// </para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
36 /// <para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
37 /// Также хорошим правилом является то, что <c>Resolve</c> и <c>Reject</c> должен вызывать
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
38 /// только инициатор обещания иначе могут возникнуть противоречия.
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
39 /// </para>
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
40 /// </remarks>
144
8c0b95069066 DRAFT: refactoring
cin
parents: 138
diff changeset
41 public class Promise<T> : AbstractPromise<T>, IDeferred<T> {
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
42
144
8c0b95069066 DRAFT: refactoring
cin
parents: 138
diff changeset
43 public static IPromise<T> FromResult(T value) {
146
e03ccec4a08d minor changes
cin
parents: 144
diff changeset
44 return new SuccessPromise<T>(value);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
45 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
46
144
8c0b95069066 DRAFT: refactoring
cin
parents: 138
diff changeset
47 public static IPromise<T> FromException(Exception error) {
8c0b95069066 DRAFT: refactoring
cin
parents: 138
diff changeset
48 var p = new Promise<T>();
8c0b95069066 DRAFT: refactoring
cin
parents: 138
diff changeset
49 p.Reject(error);
8c0b95069066 DRAFT: refactoring
cin
parents: 138
diff changeset
50 return p;
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
51 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
52
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
53 public virtual void Resolve(T value) {
144
8c0b95069066 DRAFT: refactoring
cin
parents: 138
diff changeset
54 SetResult(value);
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
55 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
56
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
57 public void Reject(Exception error) {
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
58 SetError(error);
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
59 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
60 }
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents:
diff changeset
61 }