-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using ProtoFlux.Core; | ||
using ProtoFlux.Runtimes.Execution; | ||
|
||
[NodeCategory("Flow/Async")] | ||
[NodeName("Async Wait", false)] | ||
public class AsyncWait : AsyncActionNode<ExecutionContext> | ||
{ | ||
public ValueInput<bool> Condition; | ||
public ValueInput<float> Timeout; | ||
|
||
public AsyncCall OnStarted; | ||
|
||
public Continuation OnDone; | ||
public Continuation TimedOut; | ||
|
||
protected override async Task<IOperation> RunAsync(ExecutionContext context) | ||
{ | ||
await OnStarted.ExecuteAsync(context); | ||
|
||
var timeoutInSeconds = Timeout.Evaluate(context); | ||
var startTime = DateTime.UtcNow; | ||
|
||
// Initial evaluation of the condition | ||
if (Condition.Evaluate(context, defaultValue: false)) | ||
{ | ||
return OnDone.Target; | ||
} | ||
|
||
while (!Condition.Evaluate(context, defaultValue: false)) | ||
{ | ||
if ((DateTime.UtcNow - startTime).TotalSeconds > timeoutInSeconds) | ||
{ | ||
return TimedOut.Target; | ||
} | ||
|
||
if (context.AbortExecution) | ||
{ | ||
throw new ExecutionAbortedException(base.Runtime as IExecutionRuntime, this, TimedOut.Target, isAsync: true); | ||
} | ||
} | ||
|
||
return OnDone.Target; | ||
} | ||
} |