Fibers/Extensions.cs
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license.
4 //
5 // Microsoft Bot Framework: http://botframework.com
6 //
7 // Bot Builder SDK GitHub:
8 // https://github.com/Microsoft/BotBuilder
9 //
10 // Copyright (c) Microsoft Corporation
11 // All rights reserved.
12 //
13 // MIT License:
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 
34 using System;
35 using System.Collections.Generic;
36 using System.Linq;
37 using System.Text;
38 using System.Runtime.Serialization;
39 using System.Threading.Tasks;
40 
42 
43 namespace Microsoft.Bot.Builder.Internals.Fibers
44 {
45  public static partial class Extensions
46  {
50  public static IWait<C> Wait<C, T>(this IFiber<C> fiber, Rest<C, T> resumeHandler)
51  {
52  var wait = fiber.Waits.Make<T>();
53  wait.Wait(resumeHandler);
54  fiber.Wait = wait;
55  return wait;
56  }
57 
61  public static IWait<C> Call<C, T, R>(this IFiber<C> fiber, Rest<C, T> invokeHandler, T item, Rest<C, R> returnHandler)
62  {
63  // tell the leaf frame of the stack to wait for the return value
64  var wait = fiber.Wait(returnHandler);
65 
66  // call the child
67  return fiber.Call<C, T>(invokeHandler, item);
68  }
69 
77  public static IWait<C> Call<C, T>(this IFiber<C> fiber, Rest<C, T> invokeHandler, T item)
78  {
79  // make a frame on the stack for calling the method
80  fiber.Push();
81 
82  // initiate and immediately complete a wait for calling the child
83  var wait = fiber.Wait(invokeHandler);
84  wait.Post(item);
85  return wait;
86  }
87 
91  public static IWait<C> Done<C, T>(this IFiber<C> fiber, T item)
92  {
93  // pop the stack
94  fiber.Done();
95 
96  // complete the caller's wait for the return value
97  fiber.Wait.Post(item);
98  return fiber.Wait;
99  }
100 
101  public static void Reset<C>(this IFiber<C> fiber)
102  {
103  while (fiber.Frames.Count > 0)
104  {
105  fiber.Done();
106  }
107  }
108 
109  public static IWait<C> Post<C, T>(this IFiber<C> fiber, T item)
110  {
111  fiber.Wait.Post(item);
112  return fiber.Wait;
113  }
114 
115  public static IWait<C> Fail<C>(this IFiber<C> fiber, Exception error)
116  {
117  // pop the stack
118  fiber.Done();
119 
120  // complete the caller's wait with an exception
121  fiber.Wait.Fail(error);
122  return fiber.Wait;
123  }
124 
125  public static void ValidateNeed(this IWait wait, Need need)
126  {
127  if (need != wait.Need)
128  {
129  throw new InvalidNeedException(wait, need);
130  }
131  }
132 
133  public static IWait<C> CloneTyped<C>(this IWait<C> wait)
134  {
135  return (IWait<C>)wait.Clone();
136  }
137 
138  public static Task<T> ToTask<T>(this IAwaitable<T> item)
139  {
140  var source = new TaskCompletionSource<T>();
141  try
142  {
143  var result = item.GetAwaiter().GetResult();
144  source.SetResult(result);
145  }
146  catch (Exception error)
147  {
148  source.SetException(error);
149  }
150 
151  return source.Task;
152  }
153  }
154 }
static void ValidateNeed(this IWait wait, Need need)
Need
This is the stage of the wait, showing what the wait needs during its lifecycle.
Definition: Wait.cs:58
Core namespace for Dialogs and associated infrastructure.
Root namespace for the Microsoft Bot Builder SDK.