public async Task SetUp() { var project = Substitute.For <IProject>(); project.Classes.Returns(Observable.Return(new IElement[] { })); var context = Substitute.For <IContext>(); _currentSelection = new BehaviorSubject <IElement>(Element.Empty); _buttonClicked = false; context.CurrentSelection.Returns(_currentSelection.Switch()); _model = new ExtractClassButtonViewModel(project, context, dialogViewModel => { _buttonClicked = true; _resultingViewModel = dialogViewModel; }, new Shell(), Substitute.For <IClassExtractor>()); _root = CreateTree("<App><JavaScript /><Include /><Panel /><Circle ux:Class=\"MyClass\" /></App>"); _classElement = await GetTreeChild("Circle"); _instanceElement = await GetTreeChild("Panel"); _javaScriptElement = await GetTreeChild("JavaScript"); _includeElement = await GetTreeChild("Include"); project.Classes.Returns(Observable.Return(new[] { _classElement })); }
public void OnOuterError() { var lbs1 = new ListBehaviorSubject <int>(); lbs1.OnAdd(1); lbs1.OnAdd(2); var source = new BehaviorSubject <IObservableList <int> >(lbs1); var list = new List <int>(); var completed = false; Exception error = null; var sub = source .Switch() .Subscribe( Observer.Create <ListChange <int> >( change => change.Apply(list), e => error = e, () => completed = true)); var ex = new Exception("AAAAAA"); source.OnError(ex); Assert.AreEqual(ex, error); Assert.IsFalse(completed); sub.Dispose(); }
public void Basics() { var lbs1 = new ListBehaviorSubject <int>(); var lbs2 = new ListBehaviorSubject <int>(); lbs1.OnAdd(1); lbs1.OnAdd(2); lbs2.OnAdd(4); lbs2.OnAdd(5); var source = new BehaviorSubject <IObservableList <int> >(lbs1); var list = new List <int>(); var sub = source.Switch().Subscribe(Observer.Create <ListChange <int> >(change => change.Apply(list))); CollectionAssert.AreEqual(new[] { 1, 2 }, list); lbs1.OnAdd(3); lbs1.OnAdd(6); CollectionAssert.AreEqual(new[] { 1, 2, 3, 6 }, list); source.OnNext(lbs2); CollectionAssert.AreEqual(new[] { 4, 5 }, list); source.OnNext(lbs2); CollectionAssert.AreEqual(new[] { 4, 5 }, list); lbs2.OnRemove(0); CollectionAssert.AreEqual(new[] { 5 }, list); lbs2.OnReplace(0, 41); CollectionAssert.AreEqual(new[] { 41 }, list); lbs2.OnClear(); CollectionAssert.IsEmpty(list); lbs2.OnAdd(4); lbs2.OnAdd(5); sub.Dispose(); lbs1.OnAdd(100); lbs2.OnAdd(200); CollectionAssert.AreEqual(new[] { 4, 5 }, list); }
public static IControl Lazy(Func <IControl> content) { var resultObs = new BehaviorSubject <IControl>(Empty); var result = resultObs.Switch(); result .IsRooted .Where(b => b) .Take(1) .Subscribe(_ => resultObs.OnNext(content())); return(result); }
public void Button_is_unsubscribed_after() { RunInMainThread(() => { var command = new BehaviorSubject <Command>(Command.Enabled(action: () => { })); var button = Button.Create(command.Switch()); Assert.That(command.HasObservers, Is.False); button.MountRoot(); Assert.That(command.HasObservers, Is.True); button.Mount(MountLocation.Unmounted); Assert.That(command.HasObservers, Is.False); }); }
public void OnComplete() { var lbs1 = new ListBehaviorSubject <int>(); var lbs2 = new ListBehaviorSubject <int>(); lbs1.OnAdd(1); lbs1.OnAdd(2); lbs2.OnAdd(3); lbs2.OnAdd(4); var source = new BehaviorSubject <IObservableList <int> >(lbs1); var list = new List <int>(); var completed = false; var sub = source .Switch() .Subscribe( Observer.Create <ListChange <int> >( change => change.Apply(list), () => completed = true)); lbs1.OnCompleted(); // Inner completion doesn't imply completion until outer is complete Assert.IsFalse(completed); CollectionAssert.AreEqual(new[] { 1, 2 }, list); source.OnNext(lbs2); CollectionAssert.AreEqual(new[] { 3, 4 }, list); source.OnCompleted(); // Outer completion implies completion when last inner is complete Assert.IsFalse(completed); lbs2.OnCompleted(); Assert.IsTrue(completed); CollectionAssert.AreEqual(new[] { 3, 4 }, list); sub.Dispose(); }
static IControl CreateLogView(IObservable <string> mainLogStream, IObservable <IBinaryMessage> deviceMessages, ISubject <Unit> changed) { // Keep a buffer of 10 tweets from the time before this control was mounted for the first time var bufferedStream = mainLogStream.Replay(10); //10 * 140); bufferedStream.Connect(); bufferedStream.Select(_ => Unit.Default).Subscribe(changed); var clearLog = new Subject <Unit>(); var doClear = Command.Enabled(() => clearLog.OnNext(Unit.Default)); var mainLogView = Fusion.LogView.Create(stream: bufferedStream, color: Theme.DefaultText, clear: clearLog, darkTheme: Theme.IsDark) .WithPadding(new Thickness <Points>(5)); var deviceLogs = new List <DeviceLog>(); var activeLogView = new BehaviorSubject <IControl>(mainLogView); var activeChanged = new Subject <Unit>(); var mainLogButton = CreateLogSelectButton("All Output", mainLogView, activeLogView, activeChanged, true); var clearButton = Label.Create("Clear", color: Theme.Active) .OnMouse(pressed: doClear); var buttons = new BehaviorSubject <IEnumerable <IControl> >(CreateButtonList(mainLogButton, deviceLogs)); deviceMessages.TryParse(DebugLog.MessageType, DebugLog.ReadDataFrom).Subscribe( l => { var device = deviceLogs.FirstOrDefault(d => (d.DeviceId == l.DeviceId) || (d.DeviceName == l.DeviceName && l.DeviceName == "Viewport")); if (device == null) { device = new DeviceLog(l.DeviceId, l.DeviceName, activeLogView, activeChanged, clearLog); deviceLogs.Add(device); buttons.OnNext(CreateButtonList(mainLogButton, deviceLogs)); } device.Write(l.Message); }); return(Layout.Dock() .Top(Layout.Dock() .Right(clearButton) .Left(buttons.StackFromLeft()) .Fill() .WithPadding(new Thickness <Points>(10))) .Fill(activeLogView.Switch())); }
public void Rectangle_is_unsubscribed_after() { RunInMainThread(() => { var stroke = new BehaviorSubject <Stroke>(Stroke.Empty); var brush = new BehaviorSubject <Brush>(Brush.Transparent); var cornerRadius = new BehaviorSubject <CornerRadius>(CornerRadius.None); var rectangle = Shapes.Rectangle(stroke.Switch(), brush.Switch(), cornerRadius); Assert.That(stroke.HasObservers, Is.False); Assert.That(brush.HasObservers, Is.False); Assert.That(cornerRadius.HasObservers, Is.False); rectangle.MountRoot(); Assert.That(stroke.HasObservers, Is.True); Assert.That(brush.HasObservers, Is.True); Assert.That(cornerRadius.HasObservers, Is.True); rectangle.Mount(MountLocation.Unmounted); Assert.That(brush.HasObservers, Is.False); Assert.That(stroke.HasObservers, Is.False); Assert.That(cornerRadius.HasObservers, Is.False); }); }
public static DockBuilder Panel( this DockBuilder dock, RectangleEdge dockEdge, IProperty <Points> size, IObservable <bool> isExpanded, IControl control, Points minSize, bool resizable = true) { var availableSize = new BehaviorSubject <Size <IObservable <Points> > >( new Size <IObservable <Points> >(Observable.Return <Points>(double.MaxValue), Observable.Return <Points>(double.MaxValue))); var maxWidth = availableSize.Switch()[dockEdge.NormalAxis()]; control = control .WithBackground(Theme.PanelBackground) .WithFrame(f => f, a => a.WithAxis(dockEdge.NormalAxis(), s => size.Min(maxWidth))) .WithDimension(dockEdge.NormalAxis(), size.Min(maxWidth)); control = Layout.Dock() .Dock(edge: dockEdge, control: control) .Dock(edge: dockEdge, control: Separator.Medium) .Fill(); if (resizable) { control = control.MakeResizable(dockEdge.Opposite(), size, minSize: minSize); } control = control.MakeCollapsable(dockEdge.Opposite(), isExpanded, lazy: false); control = control.WithFrame( frame => frame, availSize => { availableSize.OnNext(availSize); return(availSize); }); return(dock.Dock(edge: dockEdge, control: control)); }
public async Task SetUp() { var project = Substitute.For <IProject>(); project.Classes.Returns(Observable.Return(new IElement[] { })); project.RootDirectory.Returns(Observable.Return(AbsoluteDirectoryPath.Parse("/project"))); var context = Substitute.For <IContext>(); _root = CreateTree(); _fileSystem = Substitute.For <IFileSystem>(); _fileSystem.Exists(Arg.Any <AbsoluteFilePath>()) .Returns( callInfo => { var absoluteFilePath = callInfo.Arg <AbsoluteFilePath>(); var result = absoluteFilePath == AbsoluteFilePath.Parse("/project/MainView.ux"); Console.WriteLine("FileSystem.Exists({0}) -> {1}", absoluteFilePath, result); return(result); }); var panel = await GetTreeChild("Panel"); _currentSelection = new BehaviorSubject <IElement>(panel); context.CurrentSelection.Returns(_currentSelection.Switch()); _classExtractor = new MockedClassExtractor(); _model = new ExtractClassViewModel( context: context, suggestedName: "MyPanel", allClassNames: Observable.Return(new HashSet <string>(new[] { "MyCircle" })), classExtractor: _classExtractor, fileSystem: _fileSystem, project: project); project.Classes.Returns(Observable.Return(new[] { await GetTreeChild("Circle") })); }
IObservable <T> ElementSwitch <T>(Func <ILiveElement, IObservable <T> > selector, T defaultValue) { return(_element.Switch(y => y.Select(selector).FirstOr(Observable.Return(defaultValue)))); }
public IDisposable Subscribe(IObserver <T> observer) { return(valvesSubject.Switch().Subscribe(observer)); }
public FeedHandler() { _combinedPriceFeed = _combinedPriceFeedChange.Switch().Buffer(TimeSpan.FromMilliseconds(_throttleFrequency)).SelectMany(buffer => buffer.GroupBy(x => x.InstrumentId, (key, result) => result.First())); _subscriber = _combinedPriceFeed.Subscribe(NotifyClient); }
public static void Initialize(Dispatcher dispatcher) { Button.Implementation.Factory = (command, contentFactory, text, isDefault) => { if (contentFactory != null) { var states = new BehaviorSubject <ButtonStates>(ButtonStates.Unrooted); var content = contentFactory(states.Switch()); return(Control.Create(self => { Action action = () => { }; var button = new CustomButton(); button.Click += (s, a) => action(); states.OnNext(new ButtonStates( isPressed: button.ObserveDependencyProperty(instance => instance.IsPressed, ButtonBase.IsPressedProperty), isHovered: button.ObserveDependencyProperty(instance => instance.IsMouseOver, ButtonBase.IsMouseOverProperty), isEnabled: button.ObserveDependencyProperty(instance => instance.IsEnabled, ButtonBase.IsEnabledProperty))); content.Mount( new MountLocation.Mutable { NativeFrame = ObservableMath.RectangleWithSize(self.NativeFrame.Size), AvailableSize = self.AvailableSize, IsRooted = self.IsRooted, }); var child = content.NativeHandle as UIElement; if (child != null) { var grid = new Grid(); grid.Children.Add( new System.Windows.Shapes.Rectangle() { Fill = new SolidColorBrush(Colors.Transparent) }); grid.Children.Add(child); button.Content = grid; } ; self.BindNativeProperty(dispatcher, "command", command.Action, cmd => { button.IsEnabled = cmd.HasValue; action = () => cmd.Do(x => x()); }); self.BindNativeDefaults(button, dispatcher); return button; }) .WithSize(content.DesiredSize)); } else { var width = new ReplaySubject <Points>(1); return(Control.Create(self => { Action action = () => { }; var button = new System.Windows.Controls.Button(); button.Click += (s, a) => action(); command .Action .CombineLatest(text) .Take(1) .ObserveOn(Fusion.Application.MainThread) .Subscribe( cmdText => { UpdateButtonFields(button, cmdText.Item1.HasValue, cmdText.Item2, width); }); self.BindNativeProperty(dispatcher, "command", command.Action.CombineLatest(text), cmdText => { UpdateButtonFields(button, cmdText.Item1.HasValue, cmdText.Item2, width); action = () => cmdText.Item1.Do(x => x()); }); self.BindNativeDefaults(button, dispatcher); return button; }) .WithHeight(20) .WithWidth(width) .WithPadding(new Thickness <Points>(6))); } }; }
public static void Initialize(IScheduler dispatcher) { Button.Implementation.Factory = (command, contentFactory, text, isDefault) => { if (contentFactory != null) { var states = new BehaviorSubject <ButtonStates>(ButtonStates.Unrooted); var content = contentFactory(states.Switch()); var hittableContent = content.MakeHittable(); states.OnNext(new ButtonStates( isPressed: hittableContent.IsPressed(), isHovered: hittableContent.IsHovering(), isEnabled: command.IsEnabled)); hittableContent.Pressed.WithLatestFromBuffered(command.Execute, (evt, c) => c) .ConnectWhile(content.IsRooted) .Subscribe(action => dispatcher.Schedule(action)); return(hittableContent.Control); } else { var width = new ReplaySubject <Points>(1); return(Control.Create(self => { Action action = () => { }; var b = new ObservableButton() { BezelStyle = NSBezelStyle.Rounded, }; b.Activated += (s, a) => action(); if (isDefault) { b.WindowObservable .Where(window => window != null) .Subscribe(window => Fusion.Application.MainThread.InvokeAsync(() => window.DefaultButtonCell = b.Cell)); } command.Action .CombineLatest(text) .Take(1) .ObserveOn(dispatcher) .Subscribe( cmdText => { UpdateButtonFields(b, cmdText.Item1.HasValue, cmdText.Item2, width); }); self.BindNativeProperty( dispatcher, "command", command.Action.CombineLatest(text), cmdText => { UpdateButtonFields(b, cmdText.Item1.HasValue, cmdText.Item2, width); action = () => cmdText.Item1.Do(x => x()); }); self.BindNativeDefaults(b, dispatcher); return b; }) .WithHeight(32) .WithWidth(width)); } }; }
public BindControl(Func <IMountLocation, IControl> content) { _control = content(_location.Switch()); }