-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daniil Korostelev
committed
Jun 21, 2024
1 parent
6f769d6
commit 939395e
Showing
4 changed files
with
152 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,61 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Timers; | ||
using TinkState; | ||
using Timer = System.Timers.Timer; | ||
|
||
class Playground | ||
{ | ||
static async Task Main() | ||
static void Main() | ||
{ | ||
var stateA = Observable.State("hello"); | ||
var stateB = Observable.State("world"); | ||
var time = CreateTimeObservable(); | ||
var o = Observable.Auto(() => (time.Value, time.Value.ToShortTimeString())); | ||
// var o = time; | ||
|
||
var o = Observable.Auto(async () => | ||
Console.WriteLine(o.Value); | ||
Thread.Sleep(2000); | ||
Console.WriteLine(o.Value); | ||
// | ||
// var counter = 0; | ||
// IDisposable binding = null; | ||
// binding = o.Bind(v => | ||
// { | ||
// Console.WriteLine($"Current time is: {v}"); | ||
// counter++; | ||
// if (counter >= 5) binding.Dispose(); | ||
// }); | ||
|
||
Process.GetCurrentProcess().WaitForExit(); | ||
} | ||
|
||
private static Observable<DateTime> CreateTimeObservable() | ||
{ | ||
var time = Observable.External(() => DateTime.UtcNow); | ||
|
||
var timer = new Timer(1000); | ||
|
||
var timerElapsedHandler = new ElapsedEventHandler((_, _) => | ||
{ | ||
Console.WriteLine("computing"); | ||
var a = stateA.Value; | ||
await Task.Delay(1000); | ||
var b = stateB.Value; | ||
return a + " " + b; | ||
Console.WriteLine("tick"); | ||
time.Invalidate(); | ||
}); | ||
|
||
o.Bind(result => Console.WriteLine(result.Status switch | ||
time.Subscribed += () => | ||
{ | ||
AsyncComputeStatus.Loading => "Loading...", | ||
AsyncComputeStatus.Done => "Done: " + result.Result, | ||
AsyncComputeStatus.Failed => "Failed: " + result.Exception, | ||
})); | ||
|
||
await Task.Delay(1500); | ||
Console.WriteLine("wakeup"); | ||
timer.Elapsed += timerElapsedHandler; | ||
timer.Start(); | ||
}; | ||
|
||
stateB.Value = "Dan"; | ||
time.Unsubscribed += () => | ||
{ | ||
Console.WriteLine("sleep"); | ||
timer.Elapsed -= timerElapsedHandler; | ||
timer.Stop(); | ||
}; | ||
|
||
Process.GetCurrentProcess().WaitForExit(); | ||
return time; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace TinkState | ||
{ | ||
public interface ExternalObservableSource<out T> : Observable<T> | ||
{ | ||
event Action Subscribed; | ||
event Action Unsubscribed; | ||
void Invalidate(); | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/TinkState/Runtime/Internal/ExternalObservableSource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
|
||
namespace TinkState.Internal | ||
{ | ||
class ExternalObservableSource<T> : Dispatcher, TinkState.ExternalObservableSource<T>, DispatchingObservable<T> | ||
{ | ||
public event Action Subscribed; | ||
public event Action Unsubscribed; | ||
|
||
readonly Func<T> getter; | ||
readonly IEqualityComparer<T> comparer; | ||
|
||
bool isSubscribedTo; | ||
bool valid; | ||
T last; | ||
|
||
public ExternalObservableSource(Func<T> getter, IEqualityComparer<T> comparer) | ||
{ | ||
this.getter = getter; | ||
this.comparer = comparer ?? EqualityComparer<T>.Default; | ||
} | ||
|
||
public IDisposable Bind(Action<T> callback, IEqualityComparer<T> comparer = null, Scheduler scheduler = null) | ||
{ | ||
return new Binding<T>(this, callback, comparer, scheduler); | ||
} | ||
|
||
public Observable<TOut> Map<TOut>(Func<T, TOut> transform, IEqualityComparer<TOut> comparer = null) | ||
{ | ||
return new TransformObservable<T, TOut>(this, transform, comparer); | ||
} | ||
|
||
public IEqualityComparer<T> GetComparer() | ||
{ | ||
return comparer; | ||
} | ||
|
||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | ||
public T Value => AutoObservable.Track(this); | ||
|
||
public void Invalidate() | ||
{ | ||
if (valid) | ||
{ | ||
valid = false; | ||
Fire(); | ||
} | ||
} | ||
|
||
public T GetCurrentValue() | ||
{ | ||
if (!valid || !isSubscribedTo) | ||
{ | ||
Calculate(); | ||
} | ||
return last; | ||
} | ||
|
||
public long GetRevision() | ||
{ | ||
// TODO: auto-observables rely on this so what should we do? | ||
return revision; | ||
} | ||
|
||
void Calculate() | ||
{ | ||
last = getter(); | ||
valid = true; | ||
} | ||
|
||
protected override void OnStatusChange(bool active) | ||
{ | ||
if (active) WakeUp(); else Sleep(); | ||
} | ||
|
||
void WakeUp() | ||
{ | ||
isSubscribedTo = true; | ||
Calculate(); | ||
Subscribed?.Invoke(); | ||
} | ||
|
||
void Sleep() | ||
{ | ||
isSubscribedTo = false; | ||
Unsubscribed?.Invoke(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters