public async Task TestListenAsync() { DiscreteCellSink <int> a = DiscreteCell.CreateSink(1); DiscreteCell <int> a1 = a.Map(x => x + 1); DiscreteCell <int> a2 = a.Map(x => x * 2); ValueTuple <List <int>, DiscreteCellLoop <int>, IListener> resultsAndCalled = Transaction.Run(() => { DiscreteCell <int> result = a1.Lift(a2, (x, y) => x + y); Stream <Unit> incrementStream = Operational.Value(result.Cell).MapTo(Unit.Value); StreamSink <Unit> decrementStream = Stream.CreateSink <Unit>(); DiscreteCellLoop <int> calledLoop = DiscreteCell.CreateLoop <int>(); calledLoop.Loop(incrementStream.MapTo(1).Merge(decrementStream.MapTo(-1), (x, y) => x + y).Snapshot(calledLoop.Cell, (u, c) => c + u).Hold(0)); List <int> r = new List <int>(); IListener l = result.Listen(v => { Task.Run(async() => { await Task.Delay(900); r.Add(v); decrementStream.Send(Unit.Value); }); }); return(ValueTuple.Create(r, calledLoop, l)); }); // ReSharper disable once UnusedVariable List <int> results = resultsAndCalled.Item1; DiscreteCell <int> called = resultsAndCalled.Item2; List <int> calledResults = new List <int>(); IListener l2 = called.Listen(calledResults.Add); await Task.Delay(500); a.Send(2); await Task.Delay(500); a.Send(3); await Task.Delay(2500); l2.Unlisten(); resultsAndCalled.Item3.Unlisten(); }
public async Task TestListenAsync() { CellSink <int> a = Cell.CreateSink(1); Cell <int> a1 = a.Map(x => x + 1); Cell <int> a2 = a.Map(x => x * 2); (List <int> results, CellLoop <int> called, IListener l) = Transaction.Run(() => { Cell <int> result = a1.Lift(a2, (x, y) => x + y); Stream <Unit> incrementStream = result.Values().MapTo(Unit.Value); StreamSink <Unit> decrementStream = Stream.CreateSink <Unit>(); CellLoop <int> calledLoop = Cell.CreateLoop <int>(); calledLoop.Loop(incrementStream.MapTo(1).Merge(decrementStream.MapTo(-1), (x, y) => x + y).Snapshot(calledLoop, (u, c) => c + u).Hold(0)); List <int> r = new List <int>(); IListener lLocal = result.Listen(v => { Task.Run(async() => { await Task.Delay(900); r.Add(v); decrementStream.Send(Unit.Value); }); }); return(r, calledLoop, lLocal); }); // ReSharper disable once UnusedVariable List <int> calledResults = new List <int>(); IListener l2 = called.Listen(calledResults.Add); await Task.Delay(500); a.Send(2); await Task.Delay(500); a.Send(3); await Task.Delay(2500); l2.Unlisten(); l.Unlisten(); }