From 47b701eed044a935bf69a4bb5e11c34d5beae5b6 Mon Sep 17 00:00:00 2001 From: Js Date: Fri, 2 Aug 2019 13:56:48 +0900 Subject: [PATCH 1/2] fix #30 OnCompleted won't be called on UniTask.ToObservable() when UniTask.IsCompleted == true --- Assets/UniRx.Async/UniTaskObservableExtensions.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Assets/UniRx.Async/UniTaskObservableExtensions.cs b/Assets/UniRx.Async/UniTaskObservableExtensions.cs index a089093..2c5dd49 100644 --- a/Assets/UniRx.Async/UniTaskObservableExtensions.cs +++ b/Assets/UniRx.Async/UniTaskObservableExtensions.cs @@ -257,6 +257,7 @@ namespace UniRx.Async public IDisposable Subscribe(IObserver observer) { observer.OnNext(value); + observer.OnCompleted(); return EmptyDisposable.Instance; } } From 85eeeb3dd508cdec9ac6512e2e74a3e099ef3cfd Mon Sep 17 00:00:00 2001 From: Js Date: Fri, 2 Aug 2019 13:58:29 +0900 Subject: [PATCH 2/2] UnitTest for UniTask.ToObservable() --- Assets/Tests/AsyncTest.cs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Assets/Tests/AsyncTest.cs b/Assets/Tests/AsyncTest.cs index b6f1833..248ebad 100644 --- a/Assets/Tests/AsyncTest.cs +++ b/Assets/Tests/AsyncTest.cs @@ -395,6 +395,40 @@ namespace UniRx.AsyncTests yield return new WaitForSeconds(3); } + [UnityTest] + public IEnumerator ToObservable() => UniTask.ToCoroutine(async () => + { + var completedTaskObserver = new ToObservableObserver(); + completedTaskObserver.OnNextCalled.Should().BeFalse(); + completedTaskObserver.OnCompletedCalled.Should().BeFalse(); + completedTaskObserver.OnErrorCalled.Should().BeFalse(); + UniTask.CompletedTask.ToObservable().Subscribe(completedTaskObserver); + completedTaskObserver.OnNextCalled.Should().BeTrue(); + completedTaskObserver.OnCompletedCalled.Should().BeTrue(); + completedTaskObserver.OnErrorCalled.Should().BeFalse(); + + var delayFrameTaskObserver = new ToObservableObserver(); + UniTask.DelayFrame(1).ToObservable().Subscribe(delayFrameTaskObserver); + delayFrameTaskObserver.OnNextCalled.Should().BeFalse(); + delayFrameTaskObserver.OnCompletedCalled.Should().BeFalse(); + delayFrameTaskObserver.OnErrorCalled.Should().BeFalse(); + await UniTask.DelayFrame(1); + delayFrameTaskObserver.OnNextCalled.Should().BeTrue(); + delayFrameTaskObserver.OnCompletedCalled.Should().BeTrue(); + delayFrameTaskObserver.OnErrorCalled.Should().BeFalse(); + }); + + class ToObservableObserver : IObserver + { + public bool OnNextCalled { get; private set; } + public bool OnCompletedCalled { get; private set; } + public bool OnErrorCalled { get; private set; } + + public void OnNext(T value) => OnNextCalled = true; + public void OnCompleted() => OnCompletedCalled = true; + public void OnError(Exception error) => OnErrorCalled = true; + } + #endif #endif }