Skip to content

Commit

Permalink
Improve readme examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Atlinx committed Feb 11, 2023
1 parent 1576fb9 commit 081d20f
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
57 changes: 45 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,51 @@ Based on code from [Cysharp's UniTask library for Unity](https://github.com/Cysh
```CSharp
using Fractural.Tasks;

async GDTask<string> DemoAsync()
public Test : Node
{
await GDTask.DelayFrame(100);

await UniTask.Delay(TimeSpan.FromSeconds(10));

await GDTask.Yield();
await GDTask.NextFrame();

await GDTask.WaitForEndOfFrame();
await GDTask.WaitForPhysicsProcess();

return "final value";
public override _Ready()
{
// Running a task from a non-async method
Run().Forget();
}

public async GDTaskVoid Run()
{
await GDTask.DelayFrame(100);

// waiting some amount of time
await GDTask.Delay(TimeSpan.FromSeconds(10));

// Waiting a single frame
await GDTask.Yield();
await GDTask.NextFrame();
await GDTask.WaitForEndOfFrame();

// Waiting for specific lifetime call
await GDTask.WaitForPhysicsProcess();

// Cancellation
var cts = new CancellationTokenSource();
CancellableReallyLongTask(cts.Token).Forget();
await GDTask.Delay(TimeSpan.FromSeconds(3));
cts.Cancel();

// Async await with return value
string result = await RunWithResult();
return result + " with additional text";
}

public async GDTask<string> RunWithResult()
{
await GDTask.Delay(TimeSpan.FromSeconds(3));
return "A result string";
}

public async GDTaskVoid ReallyLongTask(CancellationToken cancellationToken)
{
GD.Print("Starting long task.");
await GDTask.Delay(TimeSpan.FromSeconds(1000000), cancellationToken: cancellationToken);
GD.Print("Finished long task.");
}
}
```
22 changes: 22 additions & 0 deletions tests/manual/Test.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Fractural.Tasks;
using Godot;
using System;
using System.Threading;

namespace Tests.Manual
{
Expand Down Expand Up @@ -36,6 +37,15 @@ private async GDTaskVoid Run()
string result = await RunWithResult();
GD.Print($"Post got result: {result}");

GD.Print("LongTask started");
var cts = new CancellationTokenSource();

CancellableReallyLongTask(cts.Token).Forget();

await GDTask.Delay(TimeSpan.FromSeconds(3));
cts.Cancel();
GD.Print("LongTask cancelled");

await GDTask.WaitForEndOfFrame();
GD.Print("WaitForEndOfFrame");
await GDTask.WaitForPhysicsProcess();
Expand All @@ -51,5 +61,17 @@ private async GDTask<string> RunWithResult()
await GDTask.Delay(TimeSpan.FromSeconds(2));
return "Hello";
}

private async GDTaskVoid CancellableReallyLongTask(CancellationToken cancellationToken)
{
int seconds = 10;
GD.Print($"Starting long task ({seconds} seconds long).");
for (int i = 0; i < seconds; i++)
{
GD.Print($"Working on long task for {i} seconds...");
await GDTask.Delay(TimeSpan.FromSeconds(1), cancellationToken: cancellationToken);
}
GD.Print("Finished long task.");
}
}
}

0 comments on commit 081d20f

Please sign in to comment.