Skip to content

Commit

Permalink
State rename to AsyncReadOnlyReactiveProperty and removed setter and …
Browse files Browse the repository at this point in the history
…implicit conversion.
  • Loading branch information
neuecc committed May 22, 2020
1 parent 0e25122 commit c3d2296
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 96 deletions.
60 changes: 30 additions & 30 deletions src/UniTask.NetCoreTests/AsyncReactivePropertyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,44 +52,44 @@ public async Task WithoutCurrent()
ar.Should().BeEquivalentTo(new[] { 100, 100, 100, 131, 191 });
}

[Fact]
public async Task StateIteration()
{
var rp = new State<int>(99);
var setter = rp.GetSetter();
//[Fact]
//public async Task StateIteration()
//{
// var rp = new ReadOnlyAsyncReactiveProperty<int>(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<int>(99);
var setter = rp.GetSetter();
//[Fact]
//public async Task StateWithoutCurrent()
//{
// var rp = new ReadOnlyAsyncReactiveProperty<int>(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 });
//}



Expand All @@ -98,7 +98,7 @@ public void StateFromEnumeration()
{
var rp = new AsyncReactiveProperty<int>(10);

var state = rp.ToState(CancellationToken.None);
var state = rp.ToReadOnlyAsyncReactiveProperty(CancellationToken.None);

rp.Value = 10;
state.Value.Should().Be(10);
Expand Down
65 changes: 16 additions & 49 deletions src/UniTask/Assets/Plugins/UniTask/Runtime/AsyncReactiveProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,11 @@ static void CancellationCallback(object state)
}
}

public class State<T> : IReadOnlyAsyncReactiveProperty<T>, IDisposable
public class ReadOnlyAsyncReactiveProperty<T> : IReadOnlyAsyncReactiveProperty<T>, IDisposable
{
TriggerEvent<T> triggerEvent;

T latestValue;

Action<T> setter;
IUniTaskAsyncEnumerator<T> enumerator;

public T Value
Expand All @@ -192,19 +190,13 @@ public T Value
}
}

public State(T value)
{
this.latestValue = value;
this.triggerEvent = default;
}

public State(T initialValue, IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
public ReadOnlyAsyncReactiveProperty(T initialValue, IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
{
latestValue = initialValue;
ConsumeEnumerator(source, cancellationToken).Forget();
}

public State(IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
public ReadOnlyAsyncReactiveProperty(IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
{
ConsumeEnumerator(source, cancellationToken).Forget();
}
Expand All @@ -216,7 +208,9 @@ async UniTaskVoid ConsumeEnumerator(IUniTaskAsyncEnumerable<T> source, Cancellat
{
while (await enumerator.MoveNextAsync())
{
SetValue(enumerator.Current);
var value = enumerator.Current;
this.latestValue = value;
triggerEvent.SetResult(value);
}
}
finally
Expand All @@ -226,28 +220,6 @@ async UniTaskVoid ConsumeEnumerator(IUniTaskAsyncEnumerable<T> source, Cancellat
}
}

public Action<T> 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<T> WithoutCurrent()
{
return new WithoutCurrentEnumerable(this);
Expand All @@ -268,12 +240,7 @@ public void Dispose()
triggerEvent.SetCompleted();
}

public static implicit operator State<T>(T value)
{
return new State<T>(value);
}

public static implicit operator T(State<T> value)
public static implicit operator T(ReadOnlyAsyncReactiveProperty<T> value)
{
return value.Value;
}
Expand All @@ -286,16 +253,16 @@ public override string ToString()

static bool isValueType;

static State()
static ReadOnlyAsyncReactiveProperty()
{
isValueType = typeof(T).IsValueType;
}

class WithoutCurrentEnumerable : IUniTaskAsyncEnumerable<T>
{
readonly State<T> parent;
readonly ReadOnlyAsyncReactiveProperty<T> parent;

public WithoutCurrentEnumerable(State<T> parent)
public WithoutCurrentEnumerable(ReadOnlyAsyncReactiveProperty<T> parent)
{
this.parent = parent;
}
Expand All @@ -310,14 +277,14 @@ sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<T>, ITriggerHa
{
static Action<object> cancellationCallback = CancellationCallback;

readonly State<T> parent;
readonly ReadOnlyAsyncReactiveProperty<T> parent;
readonly CancellationToken cancellationToken;
readonly CancellationTokenRegistration cancellationTokenRegistration;
T value;
bool isDisposed;
bool firstCall;

public Enumerator(State<T> parent, CancellationToken cancellationToken, bool publishCurrentValue)
public Enumerator(ReadOnlyAsyncReactiveProperty<T> parent, CancellationToken cancellationToken, bool publishCurrentValue)
{
this.parent = parent;
this.cancellationToken = cancellationToken;
Expand Down Expand Up @@ -391,14 +358,14 @@ static void CancellationCallback(object state)

public static class StateExtensions
{
public static State<T> ToState<T>(this IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
public static ReadOnlyAsyncReactiveProperty<T> ToReadOnlyAsyncReactiveProperty<T>(this IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
{
return new State<T>(source, cancellationToken);
return new ReadOnlyAsyncReactiveProperty<T>(source, cancellationToken);
}

public static State<T> ToState<T>(this IUniTaskAsyncEnumerable<T> source, T initialValue, CancellationToken cancellationToken)
public static ReadOnlyAsyncReactiveProperty<T> ToReadOnlyAsyncReactiveProperty<T>(this IUniTaskAsyncEnumerable<T> source, T initialValue, CancellationToken cancellationToken)
{
return new State<T>(initialValue, source, cancellationToken);
return new ReadOnlyAsyncReactiveProperty<T>(initialValue, source, cancellationToken);
}
}
}
19 changes: 2 additions & 17 deletions src/UniTask/Assets/Scenes/SandboxMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ public class Model
{
// State<int> Hp { get; }

AsyncReactiveProperty<int> hp;
IReadOnlyAsyncReactiveProperty<int> Hp => hp;




public Model()
Expand Down Expand Up @@ -172,23 +170,10 @@ void Increment(int value)
public Text text;
public Button button;

[SerializeField]
State<int> 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);
});

}


Expand Down

0 comments on commit c3d2296

Please sign in to comment.