annotate Implab/PromiseHandler.cs @ 255:b00441e04738 v3

Adde workaround to the behaviour of the logical operations stack in conjuction with async/await methods
author cin
date Wed, 04 Apr 2018 15:38:48 +0300
parents d82909310094
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.Diagnostics;
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
3
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
4 namespace Implab {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
5 class PromiseHandler {
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
6 public static Action<T, Deferred> Create<T>(Action<T> handler) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
7 Debug.Assert(handler != null);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
8
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
9 return (v, next) => {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
10 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
11 handler(v);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
12 next.Resolve();
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
13 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
14 next.Reject(err);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
15 }
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
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
19 public static Action<T, Deferred> Create<T>(Func<T, IPromise> handler) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
20 Debug.Assert(handler != null);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
21
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
22 return (v, next) => {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
23 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
24 next.Resolve(handler(v));
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
25 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
26 next.Reject(err);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
27 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
28 };
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
29 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
30
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
31 public static Action<T, Deferred<T2>> Create<T, T2>(Func<T, T2> handler) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
32 Debug.Assert(handler != null);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
33
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
34 return (v, next) => {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
35 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
36 next.Resolve(handler(v));
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
37 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
38 next.Reject(err);
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 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
42
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
43 public static Action<T, Deferred<T2>> Create<T, T2>(Func<T, IPromise<T2>> handler) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
44 Debug.Assert(handler != null);
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
45 return (v, next) => {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
46 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
47 next.Resolve(handler(v));
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
48 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
49 next.Reject(err);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
50 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
51 };
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
52 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
53
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
54 public static Action<Deferred> Create(Action handler) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
55 Debug.Assert(handler != null);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
56
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
57 return (next) => {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
58 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
59 handler();
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
60 next.Resolve();
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
61 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
62 next.Reject(err);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
63 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
64 };
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
65 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
66
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
67 public static Action<Deferred> Create(Func<IPromise> handler) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
68 Debug.Assert(handler != null);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
69
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
70 return (next) => {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
71 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
72 next.Resolve(handler());
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
73 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
74 next.Reject(err);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
75 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
76 };
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
77 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
78
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
79 public static Action<Deferred<T2>> Create<T2>(Func<T2> handler) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
80 Debug.Assert(handler != null);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
81
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
82 return (next) => {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
83 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
84 next.Resolve(handler());
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
85 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
86 next.Reject(err);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
87 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
88 };
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
89 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
90
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
91 public static Action<Deferred<T2>> Create<T2>(Func<IPromise<T2>> handler) {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
92 Debug.Assert(handler != null);
249
d82909310094 Implab.Test moved to xunit
cin
parents: 248
diff changeset
93 return (next) => {
248
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
94 try {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
95 next.Resolve(handler());
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
96 } catch (Exception err) {
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
97 next.Reject(err);
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
98 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
99 };
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
100 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
101 }
5cb4826c2c2a Added awaiters to promises
cin
parents:
diff changeset
102 }