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 { IsCanceled: false, IsCompleted: false, IsFaulted: false })
31 {
32 yield return BadObject.Null;
33 }
34
35 if (t.IsCompletedSuccessfully())
36 {
37 // ReSharper disable once AccessToModifiedClosure
38 runnable!.SetReturn(onComplete(t.Result));
39 }
40 else
41 {
42 throw new BadRuntimeException("Task Failed");
43 }
44 }
45 }
46
47
51 public static BadInteropRunnable WaitForTask(System.Threading.Tasks.Task t)
52 {
53 BadInteropRunnable? runnable = null;
54
55 runnable = new BadInteropRunnable(InnerWaitForTask());
56
57 return runnable;
58
59 IEnumerator<BadObject> InnerWaitForTask()
60 {
61 while (t is { IsCanceled: false, IsCompleted: false, IsFaulted: false })
62 {
63 yield return BadObject.Null;
64 }
65
66 if (t.IsCompletedSuccessfully())
67 {
68 // ReSharper disable once AccessToModifiedClosure
69 runnable!.SetReturn(BadObject.Null);
70 }
71 else
72 {
73 throw new BadRuntimeException("Task Failed");
74 }
75 }
76 }
77
85 public static BadInteropRunnable WaitForTask<T>(Task<T> t)
86 {
87 BadInteropRunnable? runnable = null;
88
89 runnable = new BadInteropRunnable(InnerWaitForTask());
90
91 return runnable;
92
93 IEnumerator<BadObject> InnerWaitForTask()
94 {
95 while (t is { IsCanceled: false, IsCompleted: false, IsFaulted: false })
96 {
97 yield return BadObject.Null;
98 }
99
100 if (t.IsCompletedSuccessfully())
101 {
102 // ReSharper disable once AccessToModifiedClosure
103 runnable!.SetReturn(BadObject.Wrap(t.Result));
104 }
105 else
106 {
107 throw new BadRuntimeException("Task Failed");
108 }
109 }
110 }
111}
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