Skip to content

Commit

Permalink
ForceInvalidate experiments (see #21)
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil Korostelev committed Jun 19, 2023
1 parent 71c0e3b commit c51283b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
50 changes: 25 additions & 25 deletions playground/Playground.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@
using System.Diagnostics;
using System.Threading.Tasks;
using TinkState;
using TinkState.Internal;

class MutableObject
{
public string Field = "initial";
}

class Playground
{
static async Task Main()
static void Main()
{
var stateA = Observable.State("hello");
var stateB = Observable.State("world");

var o = Observable.Auto(async () =>
{
Console.WriteLine("computing");
var a = stateA.Value;
await Task.Delay(1000);
var b = stateB.Value;
return a + " " + b;
});

o.Bind(result => Console.WriteLine(result.Status switch
{
AsyncComputeStatus.Loading => "Loading...",
AsyncComputeStatus.Done => "Done: " + result.Result,
AsyncComputeStatus.Failed => "Failed: " + result.Exception,
}));

await Task.Delay(1500);

stateB.Value = "Dan";

Process.GetCurrentProcess().WaitForExit();

var mutableObject = new MutableObject();
var state = Observable.State(mutableObject, NeverEqualityComparer<MutableObject>.Instance);

void Mutate(string newValue)
{
mutableObject.Field = newValue;
state.ForceInvalidate();
}

var auto = Observable.Auto(() => state.Value.Field.ToUpperInvariant());

state.Bind(o => Console.WriteLine("Field value: " + o.Field));
auto.Bind(value => Console.WriteLine("Field value: " + value));

Mutate("hello");

// Process.GetCurrentProcess().WaitForExit();
}
}
2 changes: 1 addition & 1 deletion src/TinkState/Runtime/Internal/NeverEqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace TinkState.Internal
{
class NeverEqualityComparer<T> : IEqualityComparer<T>
public class NeverEqualityComparer<T> : IEqualityComparer<T>
{
public static readonly NeverEqualityComparer<T> Instance = new NeverEqualityComparer<T>();

Expand Down
5 changes: 5 additions & 0 deletions src/TinkState/Runtime/Internal/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public T Value
}
}

public void ForceInvalidate()
{
Fire();
}

public IDisposable Bind(Action<T> callback, IEqualityComparer<T> comparer = null, Scheduler scheduler = null)
{
return new Binding<T>(this, callback, comparer, scheduler);
Expand Down
3 changes: 3 additions & 0 deletions src/TinkState/Runtime/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ public interface State<T> : Observable<T>
/// </remarks>
/// <value>Current value of this observable object.</value>
new T Value { get; set; }

// TODO: better name
void ForceInvalidate();
}
}

0 comments on commit c51283b

Please sign in to comment.