BadScript 2
Loading...
Searching...
No Matches
BadTaskUtils.cs
Go to the documentation of this file.
4
6
10public static class BadTaskUtils
11{
20 public static BadInteropRunnable WaitForTask<T>(Task<T> t, Func<T, BadObject> onComplete)
21 {
22 BadInteropRunnable? runnable = null;
23
24 runnable = new BadInteropRunnable(InnerWaitForTask());
25
26 return runnable;
27
28 IEnumerator<BadObject> InnerWaitForTask()
29 {
30 while (t is
31 {
32 IsCanceled: false, IsCompleted: false, IsFaulted: false,
33 })
34 {
35 yield return BadObject.Null;
36 }
37
38 if (t.IsCompletedSuccessfully())
39 {
40 // ReSharper disable once AccessToModifiedClosure
41 runnable!.SetReturn(onComplete(t.Result));
42 }
43 else
44 {
45 throw new BadRuntimeException("Task Failed");
46 }
47 }
48 }
49
50
54 public static BadInteropRunnable WaitForTask(System.Threading.Tasks.Task t)
55 {
56 BadInteropRunnable? runnable = null;
57
58 runnable = new BadInteropRunnable(InnerWaitForTask());
59
60 return runnable;
61
62 IEnumerator<BadObject> InnerWaitForTask()
63 {
64 while (t is
65 {
66 IsCanceled: false, IsCompleted: false, IsFaulted: false,
67 })
68 {
69 yield return BadObject.Null;
70 }
71
72 if (t.IsCompletedSuccessfully())
73 {
74 // ReSharper disable once AccessToModifiedClosure
75 runnable!.SetReturn(BadObject.Null);
76 }
77 else
78 {
79 throw new BadRuntimeException("Task Failed");
80 }
81 }
82 }
83
91 public static BadInteropRunnable WaitForTask<T>(Task<T> t)
92 {
93 BadInteropRunnable? runnable = null;
94
95 runnable = new BadInteropRunnable(InnerWaitForTask());
96
97 return runnable;
98
99 IEnumerator<BadObject> InnerWaitForTask()
100 {
101 while (t is
102 {
103 IsCanceled: false, IsCompleted: false, IsFaulted: false,
104 })
105 {
106 yield return BadObject.Null;
107 }
108
109 if (t.IsCompletedSuccessfully())
110 {
111 // ReSharper disable once AccessToModifiedClosure
112 runnable!.SetReturn(BadObject.Wrap(t.Result));
113 }
114 else
115 {
116 throw new BadRuntimeException("Task Failed");
117 }
118 }
119 }
120}
Implements a Runnable that can return a value.
void SetReturn(BadObject obj)
Sets the Return Value.
static BadInteropRunnable WaitForTask(System.Threading.Tasks.Task t)
static BadInteropRunnable WaitForTask< T >(Task< T > t, Func< T, BadObject > onComplete)
Waits for a C# Task to complete and returns the result as a BadObject.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Contains task/async Extensions and Integrations for the BadScript2 Runtime.
Contains the Error Objects for the BadScript2 Language.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains Utility Functions and Classes.
Definition BadEnum.cs:5