From 66205df49631860dd8f7c3314cb518b54c944d30 Mon Sep 17 00:00:00 2001 From: neuecc Date: Mon, 1 Jul 2019 21:06:28 +0900 Subject: [PATCH] fix await behaviour of ReactivePropery/ReactiveCommand #419 --- .../UnityEngineBridge/ReactiveCommand.cs | 10 ++++-- .../UnityEngineBridge/ReactiveProperty.cs | 31 +++++++++++++++++-- Assets/Scripts/Sandbox/SandboxScene.cs | 18 +++++++++-- .../UnityTests/Rx/ReactivePropertyTest.cs | 3 +- ProjectSettings/ProjectSettings.asset | 10 ++---- 5 files changed, 58 insertions(+), 14 deletions(-) diff --git a/Assets/Plugins/UniRx/Scripts/UnityEngineBridge/ReactiveCommand.cs b/Assets/Plugins/UniRx/Scripts/UnityEngineBridge/ReactiveCommand.cs index 17499040..18471bf7 100644 --- a/Assets/Plugins/UniRx/Scripts/UnityEngineBridge/ReactiveCommand.cs +++ b/Assets/Plugins/UniRx/Scripts/UnityEngineBridge/ReactiveCommand.cs @@ -337,8 +337,14 @@ static void CancelCallback(object state) { var tcs = new CancellableTaskCompletionSource(); - var subscription = source.Subscribe(x => tcs.TrySetResult(x), ex => tcs.TrySetException(ex), () => tcs.TrySetCanceled()); - cancellationToken.Register(Callback, Tuple.Create(tcs, subscription), false); + var disposable = new SingleAssignmentDisposable(); + disposable.Disposable = source.Subscribe(x => + { + disposable.Dispose(); // finish subscription. + tcs.TrySetResult(x); + }, ex => tcs.TrySetException(ex), () => tcs.TrySetCanceled()); + + cancellationToken.Register(Callback, Tuple.Create(tcs, disposable.Disposable), false); return tcs.Task; } diff --git a/Assets/Plugins/UniRx/Scripts/UnityEngineBridge/ReactiveProperty.cs b/Assets/Plugins/UniRx/Scripts/UnityEngineBridge/ReactiveProperty.cs index 201d9466..d67a81e6 100644 --- a/Assets/Plugins/UniRx/Scripts/UnityEngineBridge/ReactiveProperty.cs +++ b/Assets/Plugins/UniRx/Scripts/UnityEngineBridge/ReactiveProperty.cs @@ -506,8 +506,35 @@ static void CancelCallback(object state) { var tcs = new CancellableTaskCompletionSource(); - var subscription = source.Subscribe(x => tcs.TrySetResult(x), ex => tcs.TrySetException(ex), () => tcs.TrySetCanceled()); - cancellationToken.Register(Callback, Tuple.Create(tcs, subscription), false); + var disposable = new SingleAssignmentDisposable(); + if (source.HasValue) + { + // Skip first value + var isFirstValue = true; + disposable.Disposable = source.Subscribe(x => + { + if (isFirstValue) + { + isFirstValue = false; + return; + } + else + { + disposable.Dispose(); // finish subscription. + tcs.TrySetResult(x); + } + }, ex => tcs.TrySetException(ex), () => tcs.TrySetCanceled()); + } + else + { + disposable.Disposable = source.Subscribe(x => + { + disposable.Dispose(); // finish subscription. + tcs.TrySetResult(x); + }, ex => tcs.TrySetException(ex), () => tcs.TrySetCanceled()); + } + + cancellationToken.Register(Callback, Tuple.Create(tcs, disposable.Disposable), false); return tcs.Task; } diff --git a/Assets/Scripts/Sandbox/SandboxScene.cs b/Assets/Scripts/Sandbox/SandboxScene.cs index bf150fdd..0f215f5b 100644 --- a/Assets/Scripts/Sandbox/SandboxScene.cs +++ b/Assets/Scripts/Sandbox/SandboxScene.cs @@ -12,9 +12,23 @@ public class SandboxScene : MonoBehaviour public Button buttonA; public Button buttonB; // MyMyClass mc; - ReactiveProperty rp = new ReactiveProperty(); + //ReactiveProperty rp = new ReactiveProperty(); + + + //public async void Start() + //{ + // rp.Value = 10; + + // buttonA.onClick.AddListener(() => + // { + // rp.Value = 99; + // }); + + // Debug.Log("Begin:" + rp.Value); + // var v= await rp; + // Debug.Log("End:" + v); + //} - } public class MyMyClass diff --git a/Assets/Scripts/UnityTests/Rx/ReactivePropertyTest.cs b/Assets/Scripts/UnityTests/Rx/ReactivePropertyTest.cs index d4e9fea0..adf2537a 100644 --- a/Assets/Scripts/UnityTests/Rx/ReactivePropertyTest.cs +++ b/Assets/Scripts/UnityTests/Rx/ReactivePropertyTest.cs @@ -3,10 +3,11 @@ using System.Linq; using System.Text; using NUnit.Framework; +using UnityEngine.TestTools; namespace UniRx.Tests { - + public class ReactivePropertyTest { [Test] diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index cd0586b4..9cc3a8d1 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -3,7 +3,7 @@ --- !u!129 &1 PlayerSettings: m_ObjectHideFlags: 0 - serializedVersion: 17 + serializedVersion: 16 productGUID: b286933f5059e50428b251ea31eb0f32 AndroidProfiler: 0 AndroidFilterTouchesWhenObscured: 0 @@ -65,7 +65,6 @@ PlayerSettings: disableDepthAndStencilBuffers: 0 androidStartInFullscreen: 1 androidRenderOutsideSafeArea: 1 - androidUseSwappy: 0 androidBlitType: 0 defaultIsNativeResolution: 1 macRetinaSupport: 1 @@ -149,7 +148,6 @@ PlayerSettings: oculus: sharedDepthBuffer: 0 dashSupport: 0 - lowOverheadMode: 0 enable360StereoCapture: 0 isWsaHolographicRemotingEnabled: 0 protectGraphicsMemory: 0 @@ -274,8 +272,6 @@ PlayerSettings: height: 180 banner: {fileID: 0} androidGamepadSupportLevel: 0 - AndroidValidateAppBundleSize: 1 - AndroidAppBundleSizeToValidate: 100 resolutionDialogBanner: {fileID: 0} m_BuildTargetIcons: - m_BuildTarget: @@ -515,7 +511,6 @@ PlayerSettings: monoEnv: splashScreenBackgroundSourceLandscape: {fileID: 0} splashScreenBackgroundSourcePortrait: {fileID: 0} - blurSplashScreenBackground: 1 spritePackerPolicy: webGLMemorySize: 256 webGLExceptionSupport: 0 @@ -536,7 +531,7 @@ PlayerSettings: platformArchitecture: iPhone: 2 scriptingBackend: - Standalone: 1 + Standalone: 0 WP8: 2 WebGL: 1 Windows Store Apps: 2 @@ -603,6 +598,7 @@ PlayerSettings: XboxOneAllowedProductIds: [] XboxOnePersistentLocalStorageSize: 0 XboxOneXTitleMemory: 8 + xboxOneScriptCompiler: 1 XboxOneOverrideIdentityName: vrEditorSettings: daydream: