Skip to content

Exception and fallback for WaitForBackgroundThread on WebGL #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,30 @@

public class WaitForBackgroundThread
{
private readonly bool canFallbackToMainThread;
private readonly bool platformSupportsMultithread;

public WaitForBackgroundThread() : this(false) { }

public WaitForBackgroundThread(bool canFallbackToMainThread)
{
this.canFallbackToMainThread = canFallbackToMainThread;
this.platformSupportsMultithread = Application.platform != RuntimePlatform.WebGLPlayer;
}

public ConfiguredTaskAwaitable.ConfiguredTaskAwaiter GetAwaiter()
{
return Task.Run(() => {}).ConfigureAwait(false).GetAwaiter();
if (!platformSupportsMultithread && !canFallbackToMainThread)
{
throw new InvalidOperationException($"{Application.platform} does not support background threads.");
}

return Empty().ConfigureAwait(!platformSupportsMultithread).GetAwaiter();
}

private static async Task Empty()
{
// Task.Run does not run on WebGL, so we use this async method instead.
await Task.Yield();
}
}