Skip to content

Commit

Permalink
Create AsyncWait.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Xlinka committed Jul 2, 2024
1 parent ddf172b commit 5b6102e
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions ProjectObsidian/ProtoFlux/Flow/AsyncWait.cs
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;
}
}

0 comments on commit 5b6102e

Please sign in to comment.