-
Notifications
You must be signed in to change notification settings - Fork 1
Release 0.15.0 #26
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
Merged
Merged
Release 0.15.0 #26
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| using System; | ||
| using System.Collections; | ||
| using UnityEngine; | ||
| using Action = System.Action; | ||
| using Object = UnityEngine.Object; | ||
|
|
||
| // ReSharper disable once CheckNamespace | ||
|
|
@@ -13,6 +14,10 @@ namespace GameLovers.Services | |
| /// </summary> | ||
| public interface IAsyncCoroutine | ||
| { | ||
| /// <summary> | ||
| /// Get the execution status of the coroutine | ||
| /// </summary> | ||
| bool IsRunning { get; } | ||
| /// <summary> | ||
| /// Get the complete operation status of the coroutine | ||
| /// </summary> | ||
|
|
@@ -21,30 +26,34 @@ public interface IAsyncCoroutine | |
| /// Get the current coroutine being executed | ||
| /// </summary> | ||
| Coroutine Coroutine { get; } | ||
| /// <summary> | ||
| /// The Unity time the coroutine started | ||
| /// </summary> | ||
| float StartTime { get; } | ||
|
|
||
| /// <summary> | ||
| /// Sets the action <paramref name="onComplete"/> callback to be invoked when the coroutine is completed | ||
| /// </summary> | ||
| void OnComplete(Action onComplete); | ||
| /// <summary> | ||
| /// Stops the execution of this coroutine | ||
| /// </summary> | ||
| void StopCoroutine(bool triggerOnComplete = false); | ||
| } | ||
|
|
||
| /// <inheritdoc cref="IAsyncCoroutine"/> | ||
| public interface IAsyncCoroutine<T> | ||
| /// <inheritdoc /> | ||
| public interface IAsyncCoroutine<T> : IAsyncCoroutine | ||
| { | ||
| /// <inheritdoc cref="IAsyncCoroutine.IsCompleted"/> | ||
| bool IsCompleted { get; } | ||
| /// <inheritdoc cref="IAsyncCoroutine.Coroutine"/> | ||
| Coroutine Coroutine { get; } | ||
| /// <summary> | ||
| /// The data to be returned on the coroutine completion | ||
| /// </summary> | ||
| T Data { get; } | ||
| T Data { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Sets the action <paramref name="onComplete"/> callback to be invoked when the coroutine is completed with the | ||
| /// <paramref name="data"/> reference in the callback | ||
| /// <seealso cref="Data"/> reference in the callback | ||
| /// </summary> | ||
| void OnComplete(T data, Action<T> onComplete); | ||
| void OnComplete(Action<T> onComplete); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -70,9 +79,20 @@ public interface ICoroutineService : IDisposable | |
| IAsyncCoroutine StartAsyncCoroutine(IEnumerator routine); | ||
| /// <summary> | ||
| /// Follows the same principle and execution of <see cref="MonoBehaviour.StartCoroutine(IEnumerator)"/> but returns | ||
| /// a <see cref="IAsyncCoroutine{T}"/> to provide a callback on complete of the coroutine with given <typeparamref name="T"/> type | ||
| /// a <see cref="IAsyncCoroutine{T}"/> to provide a callback on complete of the coroutine with given <paramref name="data"/> | ||
| /// </summary> | ||
| IAsyncCoroutine<T> StartAsyncCoroutine<T>(IEnumerator routine); | ||
| IAsyncCoroutine<T> StartAsyncCoroutine<T>(IEnumerator routine, T data); | ||
| /// <summary> | ||
| /// Executes <paramref name="call"/> in a <see cref="StartAsyncCoroutine"/> with the given <paramref name="delay"/>. | ||
| /// Useful for delay callbacks | ||
| /// </summary> | ||
| IAsyncCoroutine StartDelayCall(Action call, float delay); | ||
| /// <summary> | ||
| /// Executes <paramref name="call"/> in a <see cref="StartAsyncCoroutine"/> with the given <paramref name="delay"/> | ||
| /// and <paramref name="data"/> data type. | ||
| /// Useful for delay callbacks | ||
| /// </summary> | ||
| IAsyncCoroutine<T> StartDelayCall<T>(Action<T> call, T data,float delay); | ||
| /// <inheritdoc cref="MonoBehaviour.StopCoroutine(Coroutine)"/> | ||
| void StopCoroutine(Coroutine coroutine); | ||
| /// <inheritdoc cref="MonoBehaviour.StopAllCoroutines"/> | ||
|
|
@@ -86,7 +106,7 @@ public class CoroutineService : ICoroutineService | |
|
|
||
| public CoroutineService() | ||
| { | ||
| var gameObject = new GameObject(typeof(CoroutineServiceMonoBehaviour).Name); | ||
| var gameObject = new GameObject(nameof(CoroutineServiceMonoBehaviour)); | ||
|
|
||
| _serviceObject = gameObject.AddComponent<CoroutineServiceMonoBehaviour>(); | ||
|
|
||
|
|
@@ -120,23 +140,45 @@ public Coroutine StartCoroutine(IEnumerator routine) | |
| /// <inheritdoc /> | ||
| public IAsyncCoroutine StartAsyncCoroutine(IEnumerator routine) | ||
| { | ||
| var asyncCoroutine = new AsyncCoroutine(); | ||
| var asyncCoroutine = new AsyncCoroutine(this); | ||
|
|
||
| asyncCoroutine.SetCoroutine(_serviceObject.ExternalStartCoroutine(InternalCoroutine(routine, asyncCoroutine))); | ||
|
|
||
| return asyncCoroutine; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public IAsyncCoroutine<T> StartAsyncCoroutine<T>(IEnumerator routine) | ||
| public IAsyncCoroutine<T> StartAsyncCoroutine<T>(IEnumerator routine, T data) | ||
| { | ||
| var asyncCoroutine = new AsyncCoroutine<T>(); | ||
| var asyncCoroutine = new AsyncCoroutine<T>(this, data); | ||
|
|
||
| asyncCoroutine.SetCoroutine(_serviceObject.ExternalStartCoroutine(InternalCoroutine(routine, asyncCoroutine))); | ||
|
|
||
| return asyncCoroutine; | ||
| } | ||
|
|
||
|
|
||
| /// <inheritdoc /> | ||
| public IAsyncCoroutine StartDelayCall(Action call, float delay) | ||
| { | ||
| var asyncCoroutine = new AsyncCoroutine(this); | ||
|
|
||
| asyncCoroutine.OnComplete(call); | ||
| asyncCoroutine.SetCoroutine(_serviceObject.ExternalStartCoroutine(InternalDelayCoroutine(delay, asyncCoroutine))); | ||
|
|
||
| return asyncCoroutine; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public IAsyncCoroutine<T> StartDelayCall<T>(Action<T> call, T data, float delay) | ||
| { | ||
| var asyncCoroutine = new AsyncCoroutine<T>(this, data); | ||
|
|
||
| asyncCoroutine.OnComplete(call); | ||
| asyncCoroutine.SetCoroutine(_serviceObject.ExternalStartCoroutine(InternalDelayCoroutine(delay, asyncCoroutine))); | ||
|
|
||
| return asyncCoroutine; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void StopCoroutine(Coroutine coroutine) | ||
| { | ||
|
|
@@ -165,22 +207,38 @@ private static IEnumerator InternalCoroutine(IEnumerator routine, ICompleteCorou | |
|
|
||
| completed.Completed(); | ||
| } | ||
|
|
||
| private static IEnumerator InternalDelayCoroutine(float delayInSeconds, ICompleteCoroutine completed) | ||
| { | ||
| yield return new WaitForSeconds(delayInSeconds); | ||
|
|
||
| completed.Completed(); | ||
| } | ||
|
|
||
| #region Private Interfaces | ||
|
|
||
| private interface ICompleteCoroutine | ||
| { | ||
| void Completed(); | ||
|
|
||
| void SetCoroutine(Coroutine coroutine); | ||
| } | ||
|
|
||
| private class AsyncCoroutine : IAsyncCoroutine, ICompleteCoroutine | ||
| { | ||
| private readonly ICoroutineService _coroutineService; | ||
|
|
||
| private Action _onComplete; | ||
|
|
||
| public bool IsRunning => Coroutine != null; | ||
| public bool IsCompleted { get; private set; } | ||
| public Coroutine Coroutine { get; private set; } | ||
| public float StartTime { get; } = Time.time; | ||
|
|
||
| private AsyncCoroutine() {} | ||
|
|
||
| public AsyncCoroutine(ICoroutineService coroutineService) | ||
| { | ||
| _coroutineService = coroutineService; | ||
| } | ||
|
|
||
| public void SetCoroutine(Coroutine coroutine) | ||
| { | ||
|
|
@@ -192,6 +250,13 @@ public void OnComplete(Action onComplete) | |
| _onComplete = onComplete; | ||
| } | ||
|
|
||
| public void StopCoroutine(bool triggerOnComplete = false) | ||
| { | ||
| _coroutineService.StopCoroutine(Coroutine); | ||
|
|
||
| OnCompleteTrigger(); | ||
| } | ||
|
|
||
|
Comment on lines
+253
to
+259
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion
|
||
| public void Completed() | ||
| { | ||
| if (IsCompleted) | ||
|
|
@@ -201,41 +266,35 @@ public void Completed() | |
|
|
||
| IsCompleted = true; | ||
| Coroutine = null; | ||
|
|
||
|
|
||
| OnCompleteTrigger(); | ||
| } | ||
|
|
||
| protected virtual void OnCompleteTrigger() | ||
| { | ||
| _onComplete?.Invoke(); | ||
| } | ||
| } | ||
|
|
||
| private class AsyncCoroutine<T> : IAsyncCoroutine<T>, ICompleteCoroutine | ||
| private class AsyncCoroutine<T> : AsyncCoroutine, IAsyncCoroutine<T> | ||
| { | ||
| private Action<T> _onComplete; | ||
|
|
||
| public bool IsCompleted { get; private set; } | ||
| public Coroutine Coroutine { get; private set; } | ||
| public T Data { get; private set; } | ||
|
|
||
| public T Data { get; set; } | ||
|
|
||
| public void SetCoroutine(Coroutine coroutine) | ||
| public AsyncCoroutine(ICoroutineService coroutineService, T data) : base(coroutineService) | ||
| { | ||
| Coroutine = coroutine; | ||
| Data = data; | ||
| } | ||
|
|
||
| public void OnComplete(T data, Action<T> onComplete) | ||
| public void OnComplete(Action<T> onComplete) | ||
| { | ||
| _onComplete = onComplete; | ||
|
|
||
| Data = data; | ||
| } | ||
|
|
||
| public void Completed() | ||
| protected override void OnCompleteTrigger() | ||
| { | ||
| if (IsCompleted) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| IsCompleted = true; | ||
| Coroutine = null; | ||
|
|
||
| base.OnCompleteTrigger(); | ||
| _onComplete?.Invoke(Data); | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code changes in the
CHANGELOG.mdfile look good. They accurately reflect the new features and additions in the release.Overall, the changes in this pull request seem to be focused on adding new methods and functionalities related to coroutines and object pooling. The changes are well-documented in the
CHANGELOG.mdfile, which is great for transparency and communication with other developers.Since the provided code snippets are incomplete, it's difficult to provide a comprehensive review. However, based on the information provided, it seems that the changes are reasonable and aligned with the stated goals.
If you have any specific concerns or questions about particular code sections, please provide the complete code snippets, and I'll be happy to review them in more detail.