Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
fix await behaviour of ReactivePropery/ReactiveCommand #419
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Jul 1, 2019
1 parent 0cb5009 commit 66205df
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,14 @@ static void CancelCallback(object state)
{
var tcs = new CancellableTaskCompletionSource<T>();

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;
}
Expand Down
31 changes: 29 additions & 2 deletions Assets/Plugins/UniRx/Scripts/UnityEngineBridge/ReactiveProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,35 @@ static void CancelCallback(object state)
{
var tcs = new CancellableTaskCompletionSource<T>();

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;
}
Expand Down
18 changes: 16 additions & 2 deletions Assets/Scripts/Sandbox/SandboxScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,23 @@ public class SandboxScene : MonoBehaviour
public Button buttonA;
public Button buttonB;
// MyMyClass mc;
ReactiveProperty<int> rp = new ReactiveProperty<int>();
//ReactiveProperty<int> rp = new ReactiveProperty<int>();


//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
Expand Down
3 changes: 2 additions & 1 deletion Assets/Scripts/UnityTests/Rx/ReactivePropertyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
using System.Linq;
using System.Text;
using NUnit.Framework;
using UnityEngine.TestTools;

namespace UniRx.Tests
{

public class ReactivePropertyTest
{
[Test]
Expand Down
10 changes: 3 additions & 7 deletions ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
--- !u!129 &1
PlayerSettings:
m_ObjectHideFlags: 0
serializedVersion: 17
serializedVersion: 16
productGUID: b286933f5059e50428b251ea31eb0f32
AndroidProfiler: 0
AndroidFilterTouchesWhenObscured: 0
Expand Down Expand Up @@ -65,7 +65,6 @@ PlayerSettings:
disableDepthAndStencilBuffers: 0
androidStartInFullscreen: 1
androidRenderOutsideSafeArea: 1
androidUseSwappy: 0
androidBlitType: 0
defaultIsNativeResolution: 1
macRetinaSupport: 1
Expand Down Expand Up @@ -149,7 +148,6 @@ PlayerSettings:
oculus:
sharedDepthBuffer: 0
dashSupport: 0
lowOverheadMode: 0
enable360StereoCapture: 0
isWsaHolographicRemotingEnabled: 0
protectGraphicsMemory: 0
Expand Down Expand Up @@ -274,8 +272,6 @@ PlayerSettings:
height: 180
banner: {fileID: 0}
androidGamepadSupportLevel: 0
AndroidValidateAppBundleSize: 1
AndroidAppBundleSizeToValidate: 100
resolutionDialogBanner: {fileID: 0}
m_BuildTargetIcons:
- m_BuildTarget:
Expand Down Expand Up @@ -515,7 +511,6 @@ PlayerSettings:
monoEnv:
splashScreenBackgroundSourceLandscape: {fileID: 0}
splashScreenBackgroundSourcePortrait: {fileID: 0}
blurSplashScreenBackground: 1
spritePackerPolicy:
webGLMemorySize: 256
webGLExceptionSupport: 0
Expand All @@ -536,7 +531,7 @@ PlayerSettings:
platformArchitecture:
iPhone: 2
scriptingBackend:
Standalone: 1
Standalone: 0
WP8: 2
WebGL: 1
Windows Store Apps: 2
Expand Down Expand Up @@ -603,6 +598,7 @@ PlayerSettings:
XboxOneAllowedProductIds: []
XboxOnePersistentLocalStorageSize: 0
XboxOneXTitleMemory: 8
xboxOneScriptCompiler: 1
XboxOneOverrideIdentityName:
vrEditorSettings:
daydream:
Expand Down

0 comments on commit 66205df

Please sign in to comment.