Skip to content

Commit

Permalink
Merge pull request #538 from kochounoyume/argument-allowed-factory
Browse files Browse the repository at this point in the history
Factory method allowed argument
  • Loading branch information
neuecc authored Apr 5, 2024
2 parents d057074 + 2886764 commit 0c8057c
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/UniTask/Assets/Plugins/UniTask/Runtime/UniTask.Factory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ public static UniTask Create(Func<UniTask> factory)
return factory();
}

public static UniTask Create(Func<CancellationToken, UniTask> factory, CancellationToken cancellationToken)
{
return factory(cancellationToken);
}

public static UniTask Create<T>(T state, Func<T, UniTask> factory)
{
return factory(state);
}

public static UniTask<T> Create<T>(Func<UniTask<T>> factory)
{
return factory();
Expand Down Expand Up @@ -137,11 +147,19 @@ public static Action Action(Func<CancellationToken, UniTaskVoid> asyncAction, Ca
return () => asyncAction(cancellationToken).Forget();
}

/// <summary>
/// helper of create add UniTaskVoid to delegate.
/// </summary>
public static Action Action<T>(T state, Func<T, UniTaskVoid> asyncAction)
{
return () => asyncAction(state).Forget();
}

#if UNITY_2018_3_OR_NEWER

/// <summary>
/// Create async void(UniTaskVoid) UnityAction.
/// For exampe: onClick.AddListener(UniTask.UnityAction(async () => { /* */ } ))
/// For example: onClick.AddListener(UniTask.UnityAction(async () => { /* */ } ))
/// </summary>
public static UnityEngine.Events.UnityAction UnityAction(Func<UniTaskVoid> asyncAction)
{
Expand All @@ -150,13 +168,22 @@ public static UnityEngine.Events.UnityAction UnityAction(Func<UniTaskVoid> async

/// <summary>
/// Create async void(UniTaskVoid) UnityAction.
/// For exampe: onClick.AddListener(UniTask.UnityAction(FooAsync, this.GetCancellationTokenOnDestroy()))
/// For example: onClick.AddListener(UniTask.UnityAction(FooAsync, this.GetCancellationTokenOnDestroy()))
/// </summary>
public static UnityEngine.Events.UnityAction UnityAction(Func<CancellationToken, UniTaskVoid> asyncAction, CancellationToken cancellationToken)
{
return () => asyncAction(cancellationToken).Forget();
}

/// <summary>
/// Create async void(UniTaskVoid) UnityAction.
/// For example: onClick.AddListener(UniTask.UnityAction(FooAsync, Argument))
/// </summary>
public static UnityEngine.Events.UnityAction UnityAction<T>(T state, Func<T, UniTaskVoid> asyncAction)
{
return () => asyncAction(state).Forget();
}

#endif

/// <summary>
Expand Down

0 comments on commit 0c8057c

Please sign in to comment.