public void TestLoopCell() { CellSink <int> ca = Cell.CreateSink(22); (CellLoop <int> cb, Cell <int> cb2, Cell <int> cc) = Transaction.Run(() => { CellLoop <int> cbLocal = Cell.CreateLoop <int>(); Cell <int> ccLocal = ca.Map(x => x % 10).Lift(cbLocal, (x, y) => x * y); Cell <int> cbOut = ca.Map(x => x / 10); cbLocal.Loop(cbOut); return(cbLocal, cbOut, ccLocal); }); List <int> @out = new List <int>(); List <int> out2 = new List <int>(); List <int> out3 = new List <int>(); IListener l = cb.Listen(@out.Add); IListener l2 = cb2.Listen(out2.Add); IListener l3 = cc.Listen(out3.Add); ca.Send(2); ca.Send(52); l3.Unlisten(); l2.Unlisten(); l.Unlisten(); CollectionAssert.AreEqual(new[] { 2, 0, 5 }, @out.ToArray()); CollectionAssert.AreEqual(new[] { 2, 0, 5 }, out2.ToArray()); CollectionAssert.AreEqual(new[] { 4, 0, 10 }, out3.ToArray()); }
public void TestSwitchCSimultaneous() { Sc2 sc1 = new Sc2(0); CellSink <Sc2> csc = new CellSink <Sc2>(sc1); Cell <int> co = csc.Map <Cell <int> >(b => b.C).SwitchC(); List <int> @out = new List <int>(); using (co.Listen(@out.Add)) { Sc2 sc2 = new Sc2(3); Sc2 sc3 = new Sc2(4); Sc2 sc4 = new Sc2(7); sc1.C.Send(1); sc1.C.Send(2); csc.Send(sc2); sc1.C.Send(3); sc2.C.Send(4); sc3.C.Send(5); csc.Send(sc3); sc3.C.Send(6); sc3.C.Send(7); Transaction.RunVoid(() => { sc3.C.Send(2); csc.Send(sc4); sc4.C.Send(8); }); sc4.C.Send(9); } CollectionAssert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, @out); }
public void TestSwitchEarlySSimultaneous() { Ss2 ss1 = new Ss2(); CellSink <Ss2> css = Cell.CreateSink(ss1); Stream <int> so = css.Map <Stream <int> >(b => b.S).SwitchEarlyS(); List <int> @out = new List <int>(); IListener l = so.Listen(@out.Add); Ss2 ss2 = new Ss2(); Ss2 ss3 = new Ss2(); Ss2 ss4 = new Ss2(); ss1.S.Send(0); ss1.S.Send(1); ss1.S.Send(2); css.Send(ss2); ss1.S.Send(7); ss2.S.Send(3); ss2.S.Send(4); ss3.S.Send(2); css.Send(ss3); ss3.S.Send(5); ss3.S.Send(6); ss3.S.Send(7); Transaction.RunVoid(() => { ss4.S.Send(8); css.Send(ss4); ss3.S.Send(2); }); ss4.S.Send(9); l.Unlisten(); CollectionAssert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, @out); }
public async Task TestListenOnceAsync() { CellSink <int> c = Cell.CreateSink(9); int result = await Transaction.Run(() => Operational.Value(c).ListenOnceAsync()); c.Send(2); c.Send(7); Assert.AreEqual(9, result); }
public void Run() { CellSink <int> x = Cell.CreateSink(0); IListener l = x.Listen(Console.WriteLine); x.Send(10); x.Send(20); x.Send(30); l.Unlisten(); }
public void Run() { CellSink<int> x = new CellSink<int>(0); using (x.Listen(Console.WriteLine)) { x.Send(10); x.Send(20); x.Send(30); } }
public void TestListenOnce() { CellSink <int> c = Cell.CreateSink(9); List <int> @out = new List <int>(); IListener l = Transaction.Run(() => Operational.Value(c).ListenOnce(@out.Add)); c.Send(2); c.Send(7); l.Unlisten(); CollectionAssert.AreEqual(new[] { 9 }, @out); }
public void TestUpdates() { CellSink <int> c = Cell.CreateSink(9); List <int> @out = new List <int>(); IListener l = Operational.Updates(c).Listen(@out.Add); c.Send(2); c.Send(7); l.Unlisten(); CollectionAssert.AreEqual(new[] { 2, 7 }, @out); }
public void Run() { CellSink <int> x = new CellSink <int>(0); x.Send(1); IListener l = x.Values.Listen(Console.WriteLine); x.Send(2); x.Send(3); l.Unlisten(); }
public void Run() { CellSink <int> x = Cell.CreateSink(0); x.Send(1); IListener l = Transaction.Run(() => x.Values().Listen(Console.WriteLine)); x.Send(2); x.Send(3); l.Unlisten(); }
public void TestListenOnce() { CellSink<int> c = new CellSink<int>(9); List<int> @out = new List<int>(); using (Transaction.Run(() => Operational.Value(c).ListenOnce(@out.Add))) { c.Send(2); c.Send(7); } CollectionAssert.AreEqual(new[] { 9 }, @out); }
public void Run() { CellSink <int> x = new CellSink <int>(0); using (x.Listen(Console.WriteLine)) { x.Send(10); x.Send(20); x.Send(30); } }
public void Run() { CellSink <int> x = new CellSink <int>(0); x.Send(1); using (Sodium.Operational.Updates(x).Listen(Console.WriteLine)) { x.Send(2); x.Send(3); } }
public void Run() { CellSink <int> x = new CellSink <int>(0); x.Send(1); using (Transaction.Run(() => Sodium.Operational.Value(x).Listen(Console.WriteLine))) { x.Send(2); x.Send(3); } }
public void TestListen() { CellSink<int> c = new CellSink<int>(9); List<int> @out = new List<int>(); using (c.Listen(@out.Add)) { c.Send(2); c.Send(7); } CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out); }
public void TestValues() { CellSink <int> c = Cell.CreateSink(9); List <int> @out = new List <int>(); IListener l = Transaction.Run(() => c.Values().Listen(@out.Add)); c.Send(2); c.Send(7); l.Unlisten(); CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out); }
public void Run() { CellSink <int> x = new CellSink <int>(0); x.Send(1); IListener l = Transaction.Run(() => Sodium.Operational.Value(x).Listen(Console.WriteLine)); x.Send(2); x.Send(3); l.Unlisten(); }
public void TestValueThenMap() { CellSink <int> c = Cell.CreateSink(9); List <int> @out = new List <int>(); IListener l = Transaction.Run(() => Operational.Value(c).Map(x => x + 100).Listen(@out.Add)); c.Send(2); c.Send(7); l.Unlisten(); CollectionAssert.AreEqual(new[] { 109, 102, 107 }, @out); }
public void TestListen() { CellSink <int> c = new CellSink <int>(9); List <int> @out = new List <int>(); using (c.Listen(@out.Add)) { c.Send(2); c.Send(7); } CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out); }
public void TestListenOnce() { CellSink <int> c = new CellSink <int>(9); List <int> @out = new List <int>(); using (Transaction.Run(() => Operational.Value(c).ListenOnce(@out.Add))) { c.Send(2); c.Send(7); } CollectionAssert.AreEqual(new[] { 9 }, @out); }
public void TestValue() { CellSink <int> b = new CellSink <int>(9); List <int> @out = new List <int>(); using (Transaction.Run(() => Operational.Value(b).Listen(@out.Add))) { b.Send(2); b.Send(7); } CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out); }
public void TestLiftCellsInSwitchC() { List <int> @out = new List <int>(); CellSink <int> s = new CellSink <int>(0); Cell <Cell <int> > c = Cell.Constant(Cell.Constant(1)); Cell <Cell <int> > r = c.Map(c2 => c2.Lift(s, (v1, v2) => v1 + v2)); r.SwitchC().Listen(@out.Add); s.Send(2); s.Send(4); CollectionAssert.AreEqual(new[] { 1, 3, 5 }, @out); }
public void TestSendNull() { CellSink <string> c = new CellSink <string>(string.Empty); List <string> @out = new List <string>(); IListener l = c.Listen(@out.Add); c.Send("0"); c.Send(null); c.Send("1"); l.Unlisten(); CollectionAssert.AreEqual(new[] { string.Empty, "0", null, "1" }, @out); }
public void TestUpdates() { CellSink <int> c = new CellSink <int>(9); List <int> @out = new List <int>(); using (Operational.Updates(c).Listen(@out.Add)) { c.Send(2); c.Send(7); } CollectionAssert.AreEqual(new[] { 2, 7 }, @out); }
public void TestValueThenMap() { CellSink <int> c = new CellSink <int>(9); List <int> @out = new List <int>(); using (Transaction.Run(() => Operational.Value(c).Map(x => x + 100).Listen(@out.Add))) { c.Send(2); c.Send(7); } CollectionAssert.AreEqual(new[] { 109, 102, 107 }, @out); }
public void TestValueThenFilter() { CellSink <int> c = new CellSink <int>(9); List <int> @out = new List <int>(); using (Transaction.Run(() => Operational.Value(c).Filter(x => x % 2 != 0).Listen(@out.Add))) { c.Send(2); c.Send(7); } CollectionAssert.AreEqual(new[] { 9, 7 }, @out); }
public void TestValueThenLateListen() { CellSink <int> b = new CellSink <int>(9); List <int> @out = new List <int>(); Stream <int> value = Operational.Value(b); b.Send(8); using (value.Listen(@out.Add)) { b.Send(2); } CollectionAssert.AreEqual(new[] { 2 }, @out); }
public void TestMapLateListen() { CellSink <int> c = new CellSink <int>(6); List <string> @out = new List <string>(); Cell <string> cm = c.Map(x => x.ToString()); c.Send(2); using (cm.Listen(@out.Add)) { c.Send(8); } CollectionAssert.AreEqual(new[] { "2", "8" }, @out); }
public void TestLiftSimultaneousUpdates() { List <int> @out = new List <int>(); CellSink <int> cellSink = Cell.CreateSink(1); Cell <int> cell = cellSink.Map(v => 2 * v); IListener l = cellSink.Lift(cell, (x, y) => x + y).Updates().Listen(@out.Add); cellSink.Send(2); cellSink.Send(7); l.Unlisten(); CollectionAssert.AreEqual(new[] { 6, 21 }, @out); }
public void TestValueThenLateListen() { CellSink <int> c = Cell.CreateSink(9); List <int> @out = new List <int>(); Stream <int> value = Operational.Value(c); c.Send(8); IListener l = value.Listen(@out.Add); c.Send(2); c.Send(7); l.Unlisten(); CollectionAssert.AreEqual(new[] { 2, 7 }, @out); }
public void TestGate() { StreamSink <char?> sc = Stream.CreateSink <char?>(); CellSink <bool> cGate = Cell.CreateSink(true); List <char?> @out = new List <char?>(); IListener l = sc.Gate(cGate).Listen(@out.Add); sc.Send('H'); cGate.Send(false); sc.Send('O'); cGate.Send(true); sc.Send('I'); l.Unlisten(); CollectionAssert.AreEqual(new[] { 'H', 'I' }, @out); }
public void TestSnapshot() { CellSink<int> c = new CellSink<int>(0); StreamSink<long> trigger = new StreamSink<long>(); List<string> @out = new List<string>(); using (trigger.Snapshot(c, (x, y) => x + " " + y).Listen(@out.Add)) { trigger.Send(100L); c.Send(2); trigger.Send(200L); c.Send(9); c.Send(1); trigger.Send(300L); } CollectionAssert.AreEqual(new[] { "100 0", "200 2", "300 1" }, @out); }
public void FunctionalCellLoop() { CellSink <int> s = Cell.CreateSink(0); Cell <int> result = Cell.Loop <int>().WithoutCaptures(l => s.Updates().Snapshot(l, (n, o) => n + o).Hold(0)); List <int> @out = new List <int>(); using (Transaction.Run(() => result.Listen(@out.Add))) { s.Send(1); s.Send(2); s.Send(3); } CollectionAssert.AreEqual(new[] { 0, 1, 3, 6 }, @out); }
public void TestSnapshot() { CellSink <int> c = Cell.CreateSink(0); StreamSink <long> trigger = Stream.CreateSink <long>(); List <string> @out = new List <string>(); IListener l = trigger.Snapshot(c, (x, y) => x + " " + y).Listen(@out.Add); trigger.Send(100L); c.Send(2); trigger.Send(200L); c.Send(9); c.Send(1); trigger.Send(300L); l.Unlisten(); CollectionAssert.AreEqual(new[] { "100 0", "200 2", "300 1" }, @out); }
public STextField(string initialText) { base.Text = initialText; CellSink<string> text = new CellSink<string>(initialText); this.TextChanged += async (sender, args) => { string t = base.Text; await Task.Run(() => text.Send(t)); }; this.Text = text; }
public void TestApply() { CellSink<Func<long, string>> bf = new CellSink<Func<long, string>>(b => "1 " + b); CellSink<long> ba = new CellSink<long>(5L); List<string> @out = new List<string>(); using (ba.Apply(bf).Listen(@out.Add)) { bf.Send(b => "12 " + b); ba.Send(6L); } CollectionAssert.AreEqual(new[] { "1 5", "12 5", "12 6" }, @out); }
public void TestLift() { CellSink<int> b1 = new CellSink<int>(1); CellSink<long> b2 = new CellSink<long>(5L); List<string> @out = new List<string>(); using (b1.Lift(b2, (x, y) => x + " " + y).Listen(@out.Add)) { b1.Send(12); b2.Send(6L); } CollectionAssert.AreEqual(new[] { "1 5", "12 5", "12 6" }, @out); }
public FrView(Window window, Fridget fr, IListener l) { StreamSink<MouseEvent> sMouse = new StreamSink<MouseEvent>(); StreamSink<KeyEvent> sKey = new StreamSink<KeyEvent>(); this.MouseDown += (sender, args) => sMouse.Send(new MouseEvent(args, () => args.GetPosition(this))); this.MouseUp += (sender, args) => sMouse.Send(new MouseEvent(args, () => args.GetPosition(this))); this.MouseMove += (sender, args) => sMouse.Send(new MouseEvent(args, () => args.GetPosition(this))); CellSink<IMaybe<Size>> size = new CellSink<IMaybe<Size>>(Maybe.Nothing<Size>()); this.SizeChanged += (sender, args) => size.Send(Maybe.Just(args.NewSize)); window.KeyDown += (sender, args) => { Key key = args.Key == Key.System ? args.SystemKey : args.Key; if (key == Key.Back) { sKey.Send(new BackspaceKeyEvent()); } }; window.TextInput += (sender, args) => sKey.Send(new StringKeyEvent(args.Text)); CellLoop<long> focus = new CellLoop<long>(); Fridget.Output fo = fr.Reify(size, sMouse, sKey, focus, new Supply()); focus.Loop(fo.SChangeFocus.Hold(-1)); this.drawable = fo.Drawable; this.l = new ImmutableCompositeListener(new[] { l, Operational.Updates(this.drawable).Listen(d => this.InvalidateVisual()) }); }
public void TestMapLateListen() { CellSink<int> c = new CellSink<int>(6); List<string> @out = new List<string>(); Cell<string> cm = c.Map(x => x.ToString()); c.Send(2); using (cm.Listen(@out.Add)) { c.Send(8); } CollectionAssert.AreEqual(new[] { "2", "8" }, @out); }
public void TestMap() { CellSink<int> b = new CellSink<int>(6); List<string> @out = new List<string>(); using (b.Map(x => x.ToString()).Listen(@out.Add)) { b.Send(8); } CollectionAssert.AreEqual(new[] { "6", "8" }, @out); }
public void TestUpdates() { CellSink<int> c = new CellSink<int>(9); List<int> @out = new List<int>(); using (Operational.Updates(c).Listen(@out.Add)) { c.Send(2); c.Send(7); } CollectionAssert.AreEqual(new[] { 2, 7 }, @out); }
public void TestValueThenFilter() { CellSink<int> b = new CellSink<int>(9); List<int> @out = new List<int>(); using (Transaction.Run(() => Operational.Value(b).Filter(_ => true).Listen(@out.Add))) { b.Send(2); b.Send(7); } CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out); }
public void TestValueThenMap() { CellSink<int> c = new CellSink<int>(9); List<int> @out = new List<int>(); using (Transaction.Run(() => Operational.Value(c).Map(x => x + 100).Listen(@out.Add))) { c.Send(2); c.Send(7); } CollectionAssert.AreEqual(new[] { 109, 102, 107 }, @out); }
public async Task TestListenOnceTask() { CellSink<int> c = new CellSink<int>(9); int result = await Transaction.Run(() => Operational.Value(c).ListenOnce()); c.Send(2); c.Send(7); Assert.AreEqual(9, result); }
public void TestGate() { StreamSink<char?> sc = new StreamSink<char?>(); CellSink<bool> cGate = new CellSink<bool>(true); List<char?> @out = new List<char?>(); using (sc.Gate(cGate).Listen(@out.Add)) { sc.Send('H'); cGate.Send(false); sc.Send('O'); cGate.Send(true); sc.Send('I'); } CollectionAssert.AreEqual(new[] { 'H', 'I' }, @out); }
public void TestCalm2() { CellSink<int> c = new CellSink<int>(2); List<int> @out = new List<int>(); using (Transaction.Run(() => c.Calm().Listen(@out.Add))) { c.Send(4); c.Send(2); c.Send(4); c.Send(4); c.Send(2); c.Send(2); } CollectionAssert.AreEqual(new[] { 2, 4, 2, 4, 2 }, @out); }
public void TestLiftGlitch() { CellSink<int> b1 = new CellSink<int>(1); Cell<int> b3 = b1.Map(x => x * 3); Cell<int> b5 = b1.Map(x => x * 5); Cell<string> b = b3.Lift(b5, (x, y) => x + " " + y); List<string> @out = new List<string>(); using (b.Listen(@out.Add)) { b1.Send(2); } CollectionAssert.AreEqual(new[] { "3 5", "6 10" }, @out); }
public void TestApply() { CellSink<Func<long, string>> cf = new CellSink<Func<long, string>>(x => "1 " + x); CellSink<long> ca = new CellSink<long>(5L); List<string> @out = new List<string>(); using (ca.Apply(cf).Listen(@out.Add)) { cf.Send(x => "12 " + x); ca.Send(6L); } CollectionAssert.AreEqual(new[] { "1 5", "12 5", "12 6" }, @out); }
public void TestLiftGlitch() { CellSink<int> c1 = new CellSink<int>(1); Cell<int> c3 = c1.Map(x => x * 3); Cell<int> c5 = c1.Map(x => x * 5); Cell<string> c = c3.Lift(c5, (x, y) => x + " " + y); List<string> @out = new List<string>(); using (c.Listen(@out.Add)) { c1.Send(2); } CollectionAssert.AreEqual(new[] { "3 5", "6 10" }, @out); }
public void TestLiftFromSimultaneous() { Tuple<CellSink<int>, CellSink<int>> t = Transaction.Run(() => { CellSink<int> localC1 = new CellSink<int>(3); CellSink<int> localC2 = new CellSink<int>(5); localC2.Send(7); return Tuple.Create(localC1, localC2); }); CellSink<int> c1 = t.Item1; CellSink<int> c2 = t.Item2; List<int> @out = new List<int>(); using (c1.Lift(c2, (x, y) => x + y).Listen(@out.Add)) { } CollectionAssert.AreEqual(new[] { 10 }, @out); }
public void TestSwitchCSimultaneous() { Sc2 sc1 = new Sc2(0); CellSink<Sc2> csc = new CellSink<Sc2>(sc1); Cell<int> co = csc.Map<Cell<int>>(b => b.C).SwitchC(); List<int> @out = new List<int>(); using (co.Listen(@out.Add)) { Sc2 sc2 = new Sc2(3); Sc2 sc3 = new Sc2(4); Sc2 sc4 = new Sc2(7); sc1.C.Send(1); sc1.C.Send(2); csc.Send(sc2); sc1.C.Send(3); sc2.C.Send(4); sc3.C.Send(5); csc.Send(sc3); sc3.C.Send(6); sc3.C.Send(7); Transaction.RunVoid(() => { sc3.C.Send(2); csc.Send(sc4); sc4.C.Send(8); }); sc4.C.Send(9); } CollectionAssert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, @out); }
public void Run() { CellSink<int> x = new CellSink<int>(0); x.Send(1); using (Transaction.Run(() => Sodium.Operational.Value(x).Listen(Console.WriteLine))) { x.Send(2); x.Send(3); } }
public void TestValueThenLateListen() { CellSink<int> c = new CellSink<int>(9); List<int> @out = new List<int>(); Stream<int> value = Operational.Value(c); c.Send(8); using (value.Listen(@out.Add)) { c.Send(2); c.Send(7); } CollectionAssert.AreEqual(new[] { 2, 7 }, @out); }
public void TestValueThenFilter() { CellSink<int> c = new CellSink<int>(9); List<int> @out = new List<int>(); using (Transaction.Run(() => Operational.Value(c).Filter(x => x % 2 != 0).Listen(@out.Add))) { c.Send(2); c.Send(7); } CollectionAssert.AreEqual(new[] { 9, 7 }, @out); }
public Implementation(PetrolPumpWindow petrolPump) { SComboBox<IPump> logic = new SComboBox<IPump>( new IPump[] { new LifeCyclePump(), new AccumulatePulsesPump(), new ShowDollarsPump(), new ClearSalePump(), new KeypadPump(), new PresetAmountPump() }, p => p.GetType().FullName); petrolPump.LogicComboBoxPlaceholder.Children.Add(logic); STextField textPrice1 = new STextField("2.149") { Width = 100 }; petrolPump.Price1Placeholder.Children.Add(textPrice1); STextField textPrice2 = new STextField("2.341") { Width = 100 }; petrolPump.Price2Placeholder.Children.Add(textPrice2); STextField textPrice3 = new STextField("1.499") { Width = 100 }; petrolPump.Price3Placeholder.Children.Add(textPrice3); Func<string, double> parseDoubleSafe = s => { double n; if (double.TryParse(s, out n)) { return n; } return 0.0; }; StreamSink<Key> sKey = new StreamSink<Key>(); Dictionary<Key, FrameworkElement> containersByKey = new Dictionary<Key, FrameworkElement> { { Key.One, petrolPump.Keypad1Button }, { Key.Two, petrolPump.Keypad2Button }, { Key.Three, petrolPump.Keypad3Button }, { Key.Four, petrolPump.Keypad4Button }, { Key.Five, petrolPump.Keypad5Button }, { Key.Six, petrolPump.Keypad6Button }, { Key.Seven, petrolPump.Keypad7Button }, { Key.Eight, petrolPump.Keypad8Button }, { Key.Nine, petrolPump.Keypad9Button }, { Key.Zero, petrolPump.Keypad0Button }, { Key.Clear, petrolPump.KeypadClearButton } }; foreach (KeyValuePair<Key, FrameworkElement> containerAndKey in containersByKey) { containerAndKey.Value.MouseDown += async (sender, args) => { if (args.LeftButton == MouseButtonState.Pressed) { await Task.Run(() => sKey.Send(containerAndKey.Key)); } }; } CellLoop<UpDown> nozzle1 = new CellLoop<UpDown>(); CellLoop<UpDown> nozzle2 = new CellLoop<UpDown>(); CellLoop<UpDown> nozzle3 = new CellLoop<UpDown>(); Cell<double> calibration = Cell.Constant(0.001); Cell<double> price1 = textPrice1.Text.Map(parseDoubleSafe); Cell<double> price2 = textPrice2.Text.Map(parseDoubleSafe); Cell<double> price3 = textPrice3.Text.Map(parseDoubleSafe); CellSink<Stream<Unit>> csClearSale = new CellSink<Stream<Unit>>(Sodium.Stream.Never<Unit>()); Stream<Unit> sClearSale = csClearSale.SwitchS(); StreamSink<int> sFuelPulses = new StreamSink<int>(); Cell<Outputs> outputs = logic.SelectedItem.Map( pump => pump.Create(new Inputs( Operational.Updates(nozzle1), Operational.Updates(nozzle2), Operational.Updates(nozzle3), sKey, sFuelPulses, calibration, price1, price2, price3, sClearSale))); Cell<Delivery> delivery = outputs.Map(o => o.Delivery).SwitchC(); Cell<string> presetLcd = outputs.Map(o => o.PresetLcd).SwitchC(); Cell<string> saleCostLcd = outputs.Map(o => o.SaleCostLcd).SwitchC(); Cell<string> saleQuantityLcd = outputs.Map(o => o.SaleQuantityLcd).SwitchC(); Cell<string> priceLcd1 = outputs.Map(o => o.PriceLcd1).SwitchC(); Cell<string> priceLcd2 = outputs.Map(o => o.PriceLcd2).SwitchC(); Cell<string> priceLcd3 = outputs.Map(o => o.PriceLcd3).SwitchC(); Stream<Unit> sBeep = outputs.Map(o => o.SBeep).SwitchS(); Stream<Sale> sSaleComplete = outputs.Map(o => o.SSaleComplete).SwitchS(); SoundPlayer beepPlayer = new SoundPlayer(GetResourceStream(@"sounds\beep.wav")); this.listeners.Add(sBeep.Listen(_ => new Thread(() => beepPlayer.PlaySync()) { IsBackground = true }.Start())); SoundPlayer fastRumblePlayer = new SoundPlayer(GetResourceStream(@"sounds\fast.wav")); Action stopFast = () => { }; Action playFast = () => { ManualResetEvent mre = new ManualResetEvent(false); new Thread(() => { fastRumblePlayer.PlayLooping(); mre.WaitOne(); fastRumblePlayer.Stop(); }) { IsBackground = true }.Start(); stopFast = () => mre.Set(); }; SoundPlayer slowRumblePlayer = new SoundPlayer(GetResourceStream(@"sounds\slow.wav")); Action stopSlow = () => { }; Action playSlow = () => { ManualResetEvent mre = new ManualResetEvent(false); new Thread(() => { slowRumblePlayer.PlayLooping(); mre.WaitOne(); slowRumblePlayer.Stop(); }) { IsBackground = true }.Start(); stopSlow = () => mre.Set(); }; this.listeners.Add(delivery.Changes().Listen(d => { petrolPump.Dispatcher.InvokeIfNecessary(() => { if (d == Delivery.Fast1 || d == Delivery.Fast2 || d == Delivery.Fast3) { playFast(); } else { stopFast(); } if (d == Delivery.Slow1 || d == Delivery.Slow2 || d == Delivery.Slow3) { playSlow(); } else { stopSlow(); } }); })); StackPanel presetLcdStackPanel = new StackPanel { Orientation = Orientation.Horizontal }; petrolPump.PresetPlaceholder.Children.Add(presetLcdStackPanel); this.listeners.Add(presetLcd.Listen(t => petrolPump.Dispatcher.InvokeIfNecessary(() => petrolPump.SetLcdDigits(presetLcdStackPanel, t, 5, true)))); StackPanel saleCostStackPanel = new StackPanel { Orientation = Orientation.Horizontal }; petrolPump.DollarsPlaceholder.Children.Add(saleCostStackPanel); this.listeners.Add(saleCostLcd.Listen(t => petrolPump.Dispatcher.InvokeIfNecessary(() => petrolPump.SetLcdDigits(saleCostStackPanel, t, 5, true)))); StackPanel saleQuantityLcdStackPanel = new StackPanel { Orientation = Orientation.Horizontal }; petrolPump.LitersPlaceholder.Children.Add(saleQuantityLcdStackPanel); this.listeners.Add(saleQuantityLcd.Listen(t => petrolPump.Dispatcher.InvokeIfNecessary(() => petrolPump.SetLcdDigits(saleQuantityLcdStackPanel, t, 5, true)))); StackPanel priceLcd1StackPanel = new StackPanel { Orientation = Orientation.Horizontal }; petrolPump.Fuel1Placeholder.Children.Add(priceLcd1StackPanel); this.listeners.Add(priceLcd1.Listen(t => petrolPump.Dispatcher.InvokeIfNecessary(() => petrolPump.SetLcdDigits(priceLcd1StackPanel, t, 5, false)))); StackPanel priceLcd2StackPanel = new StackPanel { Orientation = Orientation.Horizontal }; petrolPump.Fuel2Placeholder.Children.Add(priceLcd2StackPanel); this.listeners.Add(priceLcd2.Listen(t => petrolPump.Dispatcher.InvokeIfNecessary(() => petrolPump.SetLcdDigits(priceLcd2StackPanel, t, 5, false)))); StackPanel priceLcd3StackPanel = new StackPanel { Orientation = Orientation.Horizontal }; petrolPump.Fuel3Placeholder.Children.Add(priceLcd3StackPanel); this.listeners.Add(priceLcd3.Listen(t => petrolPump.Dispatcher.InvokeIfNecessary(() => petrolPump.SetLcdDigits(priceLcd3StackPanel, t, 5, false)))); Dictionary<CellLoop<UpDown>, Image> nozzles = new Dictionary<CellLoop<UpDown>, Image> { { nozzle1, petrolPump.Nozzle1Image }, { nozzle2, petrolPump.Nozzle2Image }, { nozzle3, petrolPump.Nozzle3Image } }; this.listeners.AddRange(nozzles.Select(nozzle => nozzle.Key.Listen(p => petrolPump.Dispatcher.InvokeIfNecessary(() => nozzle.Value.Margin = p == UpDown.Up ? new Thickness(0, 0, 0, 0) : new Thickness(0, 30, 0, 0))))); foreach (KeyValuePair<CellLoop<UpDown>, Image> nozzle in nozzles) { StreamSink<Unit> nozzleClicks = new StreamSink<Unit>(); nozzle.Value.MouseDown += async (sender, args) => { if (args.LeftButton == MouseButtonState.Pressed) { await Task.Run(() => nozzleClicks.Send(Unit.Value)); } }; nozzle.Key.Loop(nozzleClicks.Snapshot(nozzle.Key, (_, n) => n == UpDown.Down ? UpDown.Up : UpDown.Down).Hold(UpDown.Down)); } this.listeners.Add(sSaleComplete.Listen(sale => { Task.Run(() => { petrolPump.Dispatcher.InvokeIfNecessary(() => { SaleCompleteDialog dialog = new SaleCompleteDialog( sale.Fuel.ToString(), Formatters.FormatPrice(sale.Price, null), Formatters.FormatSaleCost(sale.Cost), Formatters.FormatSaleQuantity(sale.Quantity)); dialog.Owner = petrolPump; csClearSale.Send(dialog.SOkClicked); dialog.Show(); IListener l = null; // ReSharper disable once RedundantAssignment l = dialog.SOkClicked.Listen(_ => { petrolPump.Dispatcher.InvokeIfNecessary(() => dialog.Close()); // ReSharper disable once AccessToModifiedClosure using (l) { } }); }); }); })); Task.Run(async () => { while (true) { Transaction.RunVoid(() => { switch (delivery.Sample()) { case Delivery.Fast1: case Delivery.Fast2: case Delivery.Fast3: sFuelPulses.Send(40); break; case Delivery.Slow1: case Delivery.Slow2: case Delivery.Slow3: sFuelPulses.Send(2); break; } }); await Task.Delay(200).ConfigureAwait(false); } // ReSharper disable once FunctionNeverReturns }); }
public void TestValueThenMerge() { CellSink<int> c1 = new CellSink<int>(9); CellSink<int> c2 = new CellSink<int>(2); List<int> @out = new List<int>(); using (Transaction.Run(() => Operational.Value(c1).Merge(Operational.Value(c2), (x, y) => x + y).Listen(@out.Add))) { c1.Send(1); c2.Send(4); } CollectionAssert.AreEqual(new[] { 11, 1, 4 }, @out); }
public void Run() { CellSink<int> x = new CellSink<int>(0); x.Send(1); using (Sodium.Operational.Updates(x).Listen(Console.WriteLine)) { x.Send(2); x.Send(3); } }
public void TestSwitchSSimultaneous() { Ss2 ss1 = new Ss2(); CellSink<Ss2> css = new CellSink<Ss2>(ss1); Stream<int> so = css.Map<Stream<int>>(b => b.S).SwitchS(); List<int> @out = new List<int>(); using (so.Listen(@out.Add)) { Ss2 ss2 = new Ss2(); Ss2 ss3 = new Ss2(); Ss2 ss4 = new Ss2(); ss1.S.Send(0); ss1.S.Send(1); ss1.S.Send(2); css.Send(ss2); ss1.S.Send(7); ss2.S.Send(3); ss2.S.Send(4); ss3.S.Send(2); css.Send(ss3); ss3.S.Send(5); ss3.S.Send(6); ss3.S.Send(7); Transaction.RunVoid(() => { ss3.S.Send(8); css.Send(ss4); ss4.S.Send(2); }); ss4.S.Send(9); } CollectionAssert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, @out); }