diff --git a/src/UniTask.NetCoreTests/AsyncReactivePropertyTest.cs b/src/UniTask.NetCoreTests/AsyncReactivePropertyTest.cs index d289cb9..21d1ef1 100644 --- a/src/UniTask.NetCoreTests/AsyncReactivePropertyTest.cs +++ b/src/UniTask.NetCoreTests/AsyncReactivePropertyTest.cs @@ -52,44 +52,44 @@ namespace NetCoreTests ar.Should().BeEquivalentTo(new[] { 100, 100, 100, 131, 191 }); } - [Fact] - public async Task StateIteration() - { - var rp = new State(99); - var setter = rp.GetSetter(); + //[Fact] + //public async Task StateIteration() + //{ + // var rp = new ReadOnlyAsyncReactiveProperty(99); + // var setter = rp.GetSetter(); - var f = await rp.FirstAsync(); - f.Should().Be(99); + // var f = await rp.FirstAsync(); + // f.Should().Be(99); - var array = rp.Take(5).ToArrayAsync(); + // var array = rp.Take(5).ToArrayAsync(); - setter(100); - setter(100); - setter(100); - setter(131); + // setter(100); + // setter(100); + // setter(100); + // setter(131); - var ar = await array; + // var ar = await array; - ar.Should().BeEquivalentTo(new[] { 99, 100, 100, 100, 131 }); - } + // ar.Should().BeEquivalentTo(new[] { 99, 100, 100, 100, 131 }); + //} - [Fact] - public async Task StateWithoutCurrent() - { - var rp = new State(99); - var setter = rp.GetSetter(); + //[Fact] + //public async Task StateWithoutCurrent() + //{ + // var rp = new ReadOnlyAsyncReactiveProperty(99); + // var setter = rp.GetSetter(); - var array = rp.WithoutCurrent().Take(5).ToArrayAsync(); - setter(100); - setter(100); - setter(100); - setter(131); - setter(191); + // var array = rp.WithoutCurrent().Take(5).ToArrayAsync(); + // setter(100); + // setter(100); + // setter(100); + // setter(131); + // setter(191); - var ar = await array; + // var ar = await array; - ar.Should().BeEquivalentTo(new[] { 100, 100, 100, 131, 191 }); - } + // ar.Should().BeEquivalentTo(new[] { 100, 100, 100, 131, 191 }); + //} @@ -98,7 +98,7 @@ namespace NetCoreTests { var rp = new AsyncReactiveProperty(10); - var state = rp.ToState(CancellationToken.None); + var state = rp.ToReadOnlyAsyncReactiveProperty(CancellationToken.None); rp.Value = 10; state.Value.Should().Be(10); diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/AsyncReactiveProperty.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/AsyncReactiveProperty.cs index 6d3587d..1c289fc 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/AsyncReactiveProperty.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/AsyncReactiveProperty.cs @@ -175,13 +175,11 @@ namespace Cysharp.Threading.Tasks } } - public class State : IReadOnlyAsyncReactiveProperty, IDisposable + public class ReadOnlyAsyncReactiveProperty : IReadOnlyAsyncReactiveProperty, IDisposable { TriggerEvent triggerEvent; T latestValue; - - Action setter; IUniTaskAsyncEnumerator enumerator; public T Value @@ -192,19 +190,13 @@ namespace Cysharp.Threading.Tasks } } - public State(T value) - { - this.latestValue = value; - this.triggerEvent = default; - } - - public State(T initialValue, IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + public ReadOnlyAsyncReactiveProperty(T initialValue, IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) { latestValue = initialValue; ConsumeEnumerator(source, cancellationToken).Forget(); } - public State(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + public ReadOnlyAsyncReactiveProperty(IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) { ConsumeEnumerator(source, cancellationToken).Forget(); } @@ -216,7 +208,9 @@ namespace Cysharp.Threading.Tasks { while (await enumerator.MoveNextAsync()) { - SetValue(enumerator.Current); + var value = enumerator.Current; + this.latestValue = value; + triggerEvent.SetResult(value); } } finally @@ -226,28 +220,6 @@ namespace Cysharp.Threading.Tasks } } - public Action GetSetter() - { - if (enumerator != null) - { - throw new InvalidOperationException("Can not get setter when create from IUniTaskAsyncEnumerable source."); - } - - if (setter != null) - { - throw new InvalidOperationException("GetSetter can only call once."); - } - - setter = SetValue; - return setter; - } - - void SetValue(T value) - { - this.latestValue = value; - triggerEvent.SetResult(value); - } - public IUniTaskAsyncEnumerable WithoutCurrent() { return new WithoutCurrentEnumerable(this); @@ -268,12 +240,7 @@ namespace Cysharp.Threading.Tasks triggerEvent.SetCompleted(); } - public static implicit operator State(T value) - { - return new State(value); - } - - public static implicit operator T(State value) + public static implicit operator T(ReadOnlyAsyncReactiveProperty value) { return value.Value; } @@ -286,16 +253,16 @@ namespace Cysharp.Threading.Tasks static bool isValueType; - static State() + static ReadOnlyAsyncReactiveProperty() { isValueType = typeof(T).IsValueType; } class WithoutCurrentEnumerable : IUniTaskAsyncEnumerable { - readonly State parent; + readonly ReadOnlyAsyncReactiveProperty parent; - public WithoutCurrentEnumerable(State parent) + public WithoutCurrentEnumerable(ReadOnlyAsyncReactiveProperty parent) { this.parent = parent; } @@ -310,14 +277,14 @@ namespace Cysharp.Threading.Tasks { static Action cancellationCallback = CancellationCallback; - readonly State parent; + readonly ReadOnlyAsyncReactiveProperty parent; readonly CancellationToken cancellationToken; readonly CancellationTokenRegistration cancellationTokenRegistration; T value; bool isDisposed; bool firstCall; - public Enumerator(State parent, CancellationToken cancellationToken, bool publishCurrentValue) + public Enumerator(ReadOnlyAsyncReactiveProperty parent, CancellationToken cancellationToken, bool publishCurrentValue) { this.parent = parent; this.cancellationToken = cancellationToken; @@ -391,14 +358,14 @@ namespace Cysharp.Threading.Tasks public static class StateExtensions { - public static State ToState(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) + public static ReadOnlyAsyncReactiveProperty ToReadOnlyAsyncReactiveProperty(this IUniTaskAsyncEnumerable source, CancellationToken cancellationToken) { - return new State(source, cancellationToken); + return new ReadOnlyAsyncReactiveProperty(source, cancellationToken); } - public static State ToState(this IUniTaskAsyncEnumerable source, T initialValue, CancellationToken cancellationToken) + public static ReadOnlyAsyncReactiveProperty ToReadOnlyAsyncReactiveProperty(this IUniTaskAsyncEnumerable source, T initialValue, CancellationToken cancellationToken) { - return new State(initialValue, source, cancellationToken); + return new ReadOnlyAsyncReactiveProperty(initialValue, source, cancellationToken); } } } \ No newline at end of file diff --git a/src/UniTask/Assets/Scenes/SandboxMain.cs b/src/UniTask/Assets/Scenes/SandboxMain.cs index 622da24..9898ae7 100644 --- a/src/UniTask/Assets/Scenes/SandboxMain.cs +++ b/src/UniTask/Assets/Scenes/SandboxMain.cs @@ -142,9 +142,7 @@ public class SandboxMain : MonoBehaviour { // State Hp { get; } - AsyncReactiveProperty hp; - IReadOnlyAsyncReactiveProperty Hp => hp; - + public Model() @@ -172,23 +170,10 @@ public class SandboxMain : MonoBehaviour public Text text; public Button button; - [SerializeField] - State count; void Start2() { - count = 10; - - var countS = count.GetSetter(); - - count.BindTo(text); - button.OnClickAsAsyncEnumerable().ForEachAsync(_ => - { - // int foo = countS; - //countS.Set(countS += 10); - - // setter.SetValue(count.Value + 10); - }); + }