Skip to content

Commit

Permalink
Add a flag autoReleaseWhenCancelled to addressable extensionhs
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Jan 25, 2024
1 parent ee12dd9 commit eceb320
Showing 1 changed file with 20 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,12 @@ public static UniTask.Awaiter GetAwaiter(this AsyncOperationHandle handle)
return ToUniTask(handle).GetAwaiter();
}

public static UniTask WithCancellation(this AsyncOperationHandle handle, CancellationToken cancellationToken)
public static UniTask WithCancellation(this AsyncOperationHandle handle, CancellationToken cancellationToken, bool cancelImmediately = false, bool autoReleaseWhenCancelled = false)
{
return ToUniTask(handle, cancellationToken: cancellationToken);
return ToUniTask(handle, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately, autoReleaseWhenCancelled: autoReleaseWhenCancelled);
}

public static UniTask WithCancellation(this AsyncOperationHandle handle, CancellationToken cancellationToken, bool cancelImmediately)
{
return ToUniTask(handle, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately);
}

public static UniTask ToUniTask(this AsyncOperationHandle handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)
public static UniTask ToUniTask(this AsyncOperationHandle handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false, bool autoReleaseWhenCancelled = false)
{
if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled(cancellationToken);

Expand All @@ -50,7 +45,7 @@ public static UniTask WithCancellation(this AsyncOperationHandle handle, Cancell
return UniTask.CompletedTask;
}

return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, timing, progress, cancellationToken, cancelImmediately, out var token), token);
return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, timing, progress, cancellationToken, cancelImmediately, autoReleaseWhenCancelled, out var token), token);
}

public struct AsyncOperationHandleAwaiter : ICriticalNotifyCompletion
Expand Down Expand Up @@ -114,6 +109,7 @@ static AsyncOperationHandleConfiguredSource()
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
IProgress<float> progress;
bool autoReleaseWhenCancelled;
bool completed;

UniTaskCompletionSourceCore<AsyncUnit> core;
Expand All @@ -123,7 +119,7 @@ static AsyncOperationHandleConfiguredSource()
completedCallback = HandleCompleted;
}

public static IUniTaskSource Create(AsyncOperationHandle handle, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, bool cancelImmediately, out short token)
public static IUniTaskSource Create(AsyncOperationHandle handle, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, bool cancelImmediately, bool autoReleaseWhenCancelled, out short token)
{
if (cancellationToken.IsCancellationRequested)
{
Expand All @@ -139,13 +135,14 @@ public static IUniTaskSource Create(AsyncOperationHandle handle, PlayerLoopTimin
result.progress = progress;
result.cancellationToken = cancellationToken;
result.completed = false;
result.autoReleaseWhenCancelled = autoReleaseWhenCancelled;

if (cancelImmediately && cancellationToken.CanBeCanceled)
{
result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
{
var promise = (AsyncOperationHandleConfiguredSource)state;
if (promise.handle.IsValid())
if (autoReleaseWhenCancelled && promise.handle.IsValid())
{
Addressables.Release(promise.handle);
}
Expand Down Expand Up @@ -179,7 +176,7 @@ void HandleCompleted(AsyncOperationHandle _)
completed = true;
if (cancellationToken.IsCancellationRequested)
{
if (handle.IsValid())
if (autoReleaseWhenCancelled && handle.IsValid())
{
Addressables.Release(handle);
}
Expand Down Expand Up @@ -227,7 +224,7 @@ public bool MoveNext()
if (cancellationToken.IsCancellationRequested)
{
completed = true;
if (handle.IsValid())
if (autoReleaseWhenCancelled && handle.IsValid())
{
Addressables.Release(handle);
}
Expand Down Expand Up @@ -264,17 +261,12 @@ public static UniTask<T>.Awaiter GetAwaiter<T>(this AsyncOperationHandle<T> hand
return ToUniTask(handle).GetAwaiter();
}

public static UniTask<T> WithCancellation<T>(this AsyncOperationHandle<T> handle, CancellationToken cancellationToken)
{
return ToUniTask(handle, cancellationToken: cancellationToken);
}

public static UniTask<T> WithCancellation<T>(this AsyncOperationHandle<T> handle, CancellationToken cancellationToken, bool cancelImmediately)
public static UniTask<T> WithCancellation<T>(this AsyncOperationHandle<T> handle, CancellationToken cancellationToken, bool cancelImmediately = false, bool autoReleaseWhenCancelled = false)
{
return ToUniTask(handle, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately);
return ToUniTask(handle, cancellationToken: cancellationToken, cancelImmediately: cancelImmediately, autoReleaseWhenCancelled: autoReleaseWhenCancelled);
}

public static UniTask<T> ToUniTask<T>(this AsyncOperationHandle<T> handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false)
public static UniTask<T> ToUniTask<T>(this AsyncOperationHandle<T> handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken), bool cancelImmediately = false, bool autoReleaseWhenCancelled = false)
{
if (cancellationToken.IsCancellationRequested) return UniTask.FromCanceled<T>(cancellationToken);

Expand All @@ -292,7 +284,7 @@ public static UniTask<T> WithCancellation<T>(this AsyncOperationHandle<T> handle
return UniTask.FromResult(handle.Result);
}

return new UniTask<T>(AsyncOperationHandleConfiguredSource<T>.Create(handle, timing, progress, cancellationToken, cancelImmediately, out var token), token);
return new UniTask<T>(AsyncOperationHandleConfiguredSource<T>.Create(handle, timing, progress, cancellationToken, cancelImmediately, autoReleaseWhenCancelled, out var token), token);
}

sealed class AsyncOperationHandleConfiguredSource<T> : IUniTaskSource<T>, IPlayerLoopItem, ITaskPoolNode<AsyncOperationHandleConfiguredSource<T>>
Expand All @@ -311,6 +303,7 @@ static AsyncOperationHandleConfiguredSource()
CancellationToken cancellationToken;
CancellationTokenRegistration cancellationTokenRegistration;
IProgress<float> progress;
bool autoReleaseWhenCancelled;
bool completed;

UniTaskCompletionSourceCore<T> core;
Expand All @@ -320,7 +313,7 @@ static AsyncOperationHandleConfiguredSource()
completedCallback = HandleCompleted;
}

public static IUniTaskSource<T> Create(AsyncOperationHandle<T> handle, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, bool cancelImmediately, out short token)
public static IUniTaskSource<T> Create(AsyncOperationHandle<T> handle, PlayerLoopTiming timing, IProgress<float> progress, CancellationToken cancellationToken, bool cancelImmediately, bool autoReleaseWhenCancelled, out short token)
{
if (cancellationToken.IsCancellationRequested)
{
Expand All @@ -336,13 +329,14 @@ public static IUniTaskSource<T> Create(AsyncOperationHandle<T> handle, PlayerLoo
result.cancellationToken = cancellationToken;
result.completed = false;
result.progress = progress;
result.autoReleaseWhenCancelled = autoReleaseWhenCancelled;

if (cancelImmediately && cancellationToken.CanBeCanceled)
{
result.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
{
var promise = (AsyncOperationHandleConfiguredSource<T>)state;
if (promise.handle.IsValid())
if (promise.autoReleaseWhenCancelled && promise.handle.IsValid())
{
Addressables.Release(promise.handle);
}
Expand Down Expand Up @@ -376,7 +370,7 @@ void HandleCompleted(AsyncOperationHandle<T> argHandle)
completed = true;
if (cancellationToken.IsCancellationRequested)
{
if (handle.IsValid())
if (autoReleaseWhenCancelled && handle.IsValid())
{
Addressables.Release(handle);
}
Expand Down Expand Up @@ -429,7 +423,7 @@ public bool MoveNext()
if (cancellationToken.IsCancellationRequested)
{
completed = true;
if (handle.IsValid())
if (autoReleaseWhenCancelled && handle.IsValid())
{
Addressables.Release(handle);
}
Expand Down

0 comments on commit eceb320

Please sign in to comment.