Skip to content

Commit

Permalink
Added Disposed event
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanMurzak committed May 13, 2024
1 parent 488b7d1 commit 484a151
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Assets/Samples/SampleLifecycle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ void Start()
.Completed(isLoaded => Debug.Log($"Completed, isLoaded={isLoaded}")) // if completed (failed, loaded or canceled)
.Then(sprite => Debug.Log("Loaded")) // if loaded
.ThenSet(image) // if loaded set sprite into image
.Canceled(() => Debug.Log("Canceled")) // if cancelled
.Canceled(() => Debug.Log("Canceled")) // if canceled
.Disposed(() => Debug.Log("Disposed")) // if disposed
.Forget();
}
}
3 changes: 2 additions & 1 deletion Assets/_PackageRoot/Documentation~/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ public class SampleLifecycle : MonoBehaviour
.Completed(isLoaded => Debug.Log($"Completed, isLoaded={isLoaded}")) // if completed (failed, loaded or canceled)
.Then(sprite => Debug.Log("Loaded")) // if loaded
.ThenSet(image) // if loaded set sprite into image
.Canceled(() => Debug.Log("Canceled")) // if cancelled
.Canceled(() => Debug.Log("Canceled")) // if canceled
.Disposed(() => Debug.Log("Disposed")) // if disposed
.Forget();
}
}
Expand Down
3 changes: 2 additions & 1 deletion Assets/_PackageRoot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ public class SampleLifecycle : MonoBehaviour
.Completed(isLoaded => Debug.Log($"Completed, isLoaded={isLoaded}")) // if completed (failed, loaded or canceled)
.Then(sprite => Debug.Log("Loaded")) // if loaded
.ThenSet(image) // if loaded set sprite into image
.Canceled(() => Debug.Log("Canceled")) // if cancelled
.Canceled(() => Debug.Log("Canceled")) // if canceled
.Disposed(() => Debug.Log("Disposed")) // if disposed
.Forget();
}
}
Expand Down
19 changes: 17 additions & 2 deletions Assets/_PackageRoot/Runtime/Future/Future.API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,18 @@ public Future<T> Canceled(Action action)
action();
return this;
}
OnCancelled += action;
OnCanceled += action;
return this;
}

/// <summary>
/// When the Future is going to be disposed
/// </summary>
/// <param name="action">action to execute on the event</param>
/// <returns>Returns the Future instance</returns>
public Future<T> Disposed(Action action)
{
OnDispose += action;
return this;
}

Expand All @@ -156,7 +167,7 @@ public void Cancel()
Debug.Log($"[ImageLoader] Cancel: {Url}");
Status = FutureStatus.Canceled;
cts.Cancel();
OnCancelled?.Invoke();
OnCanceled?.Invoke();
Clear();
}

Expand All @@ -167,8 +178,12 @@ public void Dispose()
{
Clear();
Status = FutureStatus.Disposed;
OnDispose?.Invoke();
OnDispose = null;

if (value is IDisposable disposable)
disposable?.Dispose();

value = default;
exception = default;
cts.Cancel();
Expand Down
7 changes: 5 additions & 2 deletions Assets/_PackageRoot/Runtime/Future/Future.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public partial class Future<T> : IFuture, IDisposable
private event Action<T> OnLoaded;
private event Action<Exception> OnFailedToLoad;
private event Action<bool> OnCompleted;
private event Action OnCancelled;
private event Action OnCanceled;
private event Action OnDispose;

private readonly CancellationTokenSource cts;
private readonly bool muteLogs;
Expand Down Expand Up @@ -70,6 +71,8 @@ internal Future<T> PassEvents(Future<T> to, bool passCancelled = true)
if (passCancelled)
Canceled(to.Cancel);

Disposed(to.Dispose);

return this;
}
internal void Loading(FutureLoadingFrom loadingFrom)
Expand Down Expand Up @@ -148,7 +151,7 @@ private void Clear()
OnLoaded = null;
OnFailedToLoad = null;
OnCompleted = null;
OnCancelled = null;
OnCanceled = null;
}

public override string ToString() => Url;
Expand Down
2 changes: 1 addition & 1 deletion Assets/_PackageRoot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Ivan Murzak",
"url": "https://github.com/IvanMurzak"
},
"version": "5.2.0",
"version": "5.3.0",
"unity": "2019.2",
"description": "Asynchronous image loading from remote or local destination. It has two layers of configurable Memory and Disk cache systems.",
"dependencies": {
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ public class SampleLifecycle : MonoBehaviour
.Completed(isLoaded => Debug.Log($"Completed, isLoaded={isLoaded}")) // if completed (failed, loaded or canceled)
.Then(sprite => Debug.Log("Loaded")) // if loaded
.ThenSet(image) // if loaded set sprite into image
.Canceled(() => Debug.Log("Canceled")) // if cancelled
.Canceled(() => Debug.Log("Canceled")) // if canceled
.Disposed(() => Debug.Log("Disposed")) // if disposed
.Forget();
}
}
Expand Down

0 comments on commit 484a151

Please sign in to comment.