Beispiel #1
0
        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();
        }
Beispiel #2
0
        public void Count_reflects_changes()
        {
            var lbs = new ListBehaviorSubject <int>();

            lbs.OnAdd(1);
            lbs.OnAdd(2);
            lbs.OnAdd(3);

            var count = 0;

            var sub = lbs.Count().Subscribe(Observer.Create <int>(newCount => count = newCount));

            Assert.AreEqual(3, count);

            lbs.OnRemove(0);

            Assert.AreEqual(2, count);

            lbs[0] = 5;

            Assert.AreEqual(2, count);

            lbs.OnClear();

            Assert.AreEqual(0, count);

            sub.Dispose();

            lbs.OnAdd(41);

            Assert.AreEqual(0, count);
        }
Beispiel #3
0
        public void ListBehaviorSubject_Subscribe_reflects_changes()
        {
            var lbs = new ListBehaviorSubject <int>();

            lbs.OnAdd(1);
            lbs.OnAdd(2);
            lbs.OnAdd(3);

            var list = new List <int>();
            var sub  = lbs.Subscribe(Observer.Create <ListChange <int> >(change => change.Apply(list)));

            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, list);

            lbs.OnAdd(4);

            CollectionAssert.AreEqual(new[] { 1, 2, 3, 4 }, list);

            lbs.OnInsert(0, 0);

            CollectionAssert.AreEqual(new[] { 0, 1, 2, 3, 4 }, list);

            lbs.OnRemove(1);

            CollectionAssert.AreEqual(new[] { 0, 2, 3, 4 }, list);

            lbs.OnClear();

            CollectionAssert.AreEqual(new int[] {}, list);

            sub.Dispose();

            lbs.OnAdd(41);

            CollectionAssert.AreEqual(new int[] { }, list);
        }
Beispiel #4
0
        public void OnCompleteInnerFirst()
        {
            var src1 = new Subject <int>();
            var src2 = new Subject <int>();
            var src3 = new Subject <int>();

            var src = new ListBehaviorSubject <IObservable <int> >();

            var list = new List <int>();

            bool completed = false;

            var sub = src.Switch().Subscribe(change => change.Apply(list), () => completed = true);

            src.OnAdd(src1);
            src.OnAdd(src2);
            src.OnAdd(src3);

            src1.OnCompleted();
            Assert.IsFalse(completed);
            src3.OnCompleted();
            Assert.IsFalse(completed);
            src2.OnCompleted();
            Assert.IsFalse(completed);
            src.OnCompleted();
            Assert.IsTrue(completed);

            sub.Dispose();
        }
Beispiel #5
0
        public void Replay_handles_multiple_subscriptions()
        {
            var lbs = new ListBehaviorSubject <int>();

            lbs.OnAdd(1);

            var replay = lbs.Replay();

            lbs.OnAdd(2);

            var list  = new List <int>();
            var list2 = new List <int>();

            var sub  = replay.Subscribe(Observer.Create <ListChange <int> >(change => change.Apply(list)));
            var sub2 = replay.Subscribe(Observer.Create <ListChange <int> >(change => change.Apply(list2)));

            CollectionAssert.AreEqual(new int[] { }, list);
            CollectionAssert.AreEqual(new int[] { }, list2);

            var conn = replay.Connect();

            CollectionAssert.AreEqual(new[] { 1, 2 }, list);
            CollectionAssert.AreEqual(new[] { 1, 2 }, list2);

            lbs.OnAdd(3);

            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, list);
            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, list2);

            sub.Dispose();
            sub2.Dispose();
            conn.Dispose();
        }
Beispiel #6
0
        public void Replay_doesnt_emit_until_connected()
        {
            var lbs = new ListBehaviorSubject <int>();

            lbs.OnAdd(1);

            var replay = lbs.Replay();

            lbs.OnAdd(2);

            var list = new List <int>();

            var sub = replay.Subscribe(Observer.Create <ListChange <int> >(change => change.Apply(list)));

            CollectionAssert.AreEqual(new int[] { }, list);

            var conn = replay.Connect();

            CollectionAssert.AreEqual(new[] { 1, 2 }, list);

            lbs.OnAdd(3);

            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, list);

            sub.Dispose();
            conn.Dispose();
        }
Beispiel #7
0
        public void OnInnerError()
        {
            var src1 = new Subject <int>();
            var src2 = new Subject <int>();
            var src3 = new Subject <int>();

            var src = new ListBehaviorSubject <IObservable <int> >();

            var list = new List <int>();

            Exception error = null;

            var sub = src.Switch().Subscribe(change => change.Apply(list), e => error = e);

            CollectionAssert.IsEmpty(list);

            src.OnAdd(src1);
            src.OnAdd(src2);
            src.OnAdd(src3);

            var ex = new Exception("AAAAAAAaa");

            src2.OnError(ex);

            Assert.AreEqual(ex, error);

            sub.Dispose();
        }
Beispiel #8
0
        public void Replace()
        {
            var left  = new ListBehaviorSubject <int>();
            var right = new ListBehaviorSubject <int>();

            left.OnAdd(1);
            left.OnAdd(2);
            left.OnAdd(3);

            right.OnAdd(4);
            right.OnAdd(5);
            right.OnAdd(6);

            var list = new List <int>();

            var sub = left.Concat(right).Subscribe(Observer.Create <ListChange <int> >(change => change.Apply(list)));

            CollectionAssert.AreEqual(new[] { 1, 2, 3, 4, 5, 6 }, list);

            left.OnReplace(0, 100);
            right.OnReplace(0, 200);

            CollectionAssert.AreEqual(new[] { 100, 2, 3, 200, 5, 6 }, list);

            left.OnReplace(2, 0);
            right.OnReplace(2, 0);

            CollectionAssert.AreEqual(new[] { 100, 2, 0, 200, 5, 0 }, list);

            sub.Dispose();
        }
Beispiel #9
0
        public void Basics()
        {
            var src1 = new Subject <int>();
            var src2 = new Subject <int>();
            var src3 = new Subject <int>();

            var src = new ListBehaviorSubject <IObservable <int> >();

            var list = new List <int>();

            var sub = src.Switch().Subscribe(change => change.Apply(list));

            CollectionAssert.IsEmpty(list);

            src.OnAdd(src1);
            src.OnAdd(src2);
            src.OnAdd(src3);

            CollectionAssert.IsEmpty(list);

            src1.OnNext(1);

            CollectionAssert.AreEqual(new[] { 1 }, list);

            src3.OnNext(3);

            CollectionAssert.AreEqual(new[] { 1, 3 }, list);

            src2.OnNext(2);

            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, list);

            src1.OnNext(11);
            src2.OnNext(22);
            src3.OnNext(33);

            CollectionAssert.AreEqual(new[] { 11, 22, 33 }, list);

            src.OnRemove(1);

            CollectionAssert.AreEqual(new[] { 11, 33 }, list);

            src.OnReplace(1, src2);

            CollectionAssert.AreEqual(new[] { 11 }, list);

            src2.OnNext(22);

            CollectionAssert.AreEqual(new[] { 11, 22 }, list);

            src.OnClear();

            CollectionAssert.IsEmpty(list);

            sub.Dispose();
        }
Beispiel #10
0
        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);
        }
Beispiel #11
0
        public void DisposeElements_disposes_elements()
        {
            var lbs = new ListBehaviorSubject <MyDisposable>();

            var d1 = new MyDisposable();
            var d2 = new MyDisposable();
            var d3 = new MyDisposable();
            var d4 = new MyDisposable();

            lbs.OnAdd(d1);
            lbs.OnAdd(d2);
            lbs.OnAdd(d3);

            var list = new List <MyDisposable>();

            var sub = lbs.DisposeElements().Subscribe(change => change.Apply(list));

            CollectionAssert.AreEqual(new[] { d1, d2, d3 }, list);

            Assert.AreEqual(0, d1.DisposeCount);
            Assert.AreEqual(0, d2.DisposeCount);
            Assert.AreEqual(0, d3.DisposeCount);
            Assert.AreEqual(0, d4.DisposeCount);

            lbs.OnRemove(1);

            CollectionAssert.AreEqual(new [] { d1, d3 }, list);

            Assert.AreEqual(0, d1.DisposeCount);
            Assert.AreEqual(1, d2.DisposeCount);
            Assert.AreEqual(0, d3.DisposeCount);
            Assert.AreEqual(0, d4.DisposeCount);

            lbs.OnReplace(1, d4);

            CollectionAssert.AreEqual(new [] { d1, d4 }, list);

            Assert.AreEqual(0, d1.DisposeCount);
            Assert.AreEqual(1, d2.DisposeCount);
            Assert.AreEqual(1, d3.DisposeCount);
            Assert.AreEqual(0, d4.DisposeCount);

            lbs.OnClear();

            CollectionAssert.AreEqual(new MyDisposable[] { }, list);

            Assert.AreEqual(1, d1.DisposeCount);
            Assert.AreEqual(1, d2.DisposeCount);
            Assert.AreEqual(1, d3.DisposeCount);
            Assert.AreEqual(1, d4.DisposeCount);

            sub.Dispose();
        }
Beispiel #12
0
        public void Basics()
        {
            var src = new ListBehaviorSubject <int>();

            var list = new List <int>();

            var sub = src.Where(i => i % 2 == 0).Subscribe(change => change.Apply(list));

            CollectionAssert.IsEmpty(list);

            for (var i = 0; i < 6; ++i)
            {
                src.OnAdd(i);
            }

            CollectionAssert.AreEqual(new[] { 0, 2, 4 }, list);

            src.OnRemove(1);

            CollectionAssert.AreEqual(new[] { 0, 2, 4 }, list);

            src.OnReplace(1, 4);

            CollectionAssert.AreEqual(new[] { 0, 4, 4 }, list);

            src.OnInsert(0, -2);

            CollectionAssert.AreEqual(new[] { -2, 0, 4, 4 }, list);

            src.OnClear();

            CollectionAssert.IsEmpty(list);

            sub.Dispose();
        }
Beispiel #13
0
        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();
        }
Beispiel #14
0
        public void Basics()
        {
            var src = new ListBehaviorSubject <int>();

            var list = new List <int>();

            var sub = src.SeparateBy(() => - 1).Subscribe(change => change.Apply(list));

            CollectionAssert.IsEmpty(list);

            for (var i = 0; i < 3; ++i)
            {
                src.OnAdd(i);
            }

            CollectionAssert.AreEqual(new[] { 0, -1, 1, -1, 2 }, list);

            src.OnRemove(0);

            CollectionAssert.AreEqual(new[] { 1, -1, 2 }, list);

            src.OnRemove(1);

            CollectionAssert.AreEqual(new[] { 1 }, list);

            src.OnInsert(0, 4);

            CollectionAssert.AreEqual(new[] { 4, -1, 1 }, list);

            src.OnAdd(5);

            CollectionAssert.AreEqual(new[] { 4, -1, 1, -1, 5 }, list);

            src.OnReplace(1, 11);

            CollectionAssert.AreEqual(new[] { 4, -1, 11, -1, 5 }, list);

            src.OnClear();

            CollectionAssert.IsEmpty(list);

            sub.Dispose();
        }
Beispiel #15
0
        public void Replay_handles_errors()
        {
            var lbs = new ListBehaviorSubject <int>();

            lbs.OnAdd(1);

            var replay = lbs.Replay();

            lbs.OnAdd(2);

            var       list  = new List <int>();
            Exception error = null;
            var       sub   = replay.Subscribe(Observer.Create <ListChange <int> >(change => change.Apply(list), e => error = e));

            CollectionAssert.AreEqual(new int[] { }, list);

            var conn = replay.Connect();

            CollectionAssert.AreEqual(new[] { 1, 2 }, list);

            Assert.IsNull(error);

            var sourceError = new Exception("Actual");

            lbs.OnError(sourceError);

            Assert.AreEqual(sourceError, error);

            var       list2  = new List <int>();
            Exception error2 = null;
            var       sub2   = replay.Subscribe(Observer.Create <ListChange <int> >(change => change.Apply(list2), e => error2 = e));

            Assert.AreEqual(sourceError, error2);

            sub2.Dispose();
            sub.Dispose();
            conn.Dispose();
        }
Beispiel #16
0
        public void Replay_handles_completion()
        {
            var lbs = new ListBehaviorSubject <int>();

            lbs.OnAdd(1);

            var replay = lbs.Replay();

            lbs.OnAdd(2);

            var list      = new List <int>();
            var completed = false;
            var sub       = replay.Subscribe(Observer.Create <ListChange <int> >(change => change.Apply(list), () => completed = true));

            CollectionAssert.AreEqual(new int[] { }, list);

            var conn = replay.Connect();

            CollectionAssert.AreEqual(new[] { 1, 2 }, list);

            Assert.IsFalse(completed);

            lbs.OnCompleted();

            Assert.IsTrue(completed);

            var list2      = new List <int>();
            var completed2 = false;
            var sub2       = replay.Subscribe(Observer.Create <ListChange <int> >(change => change.Apply(list2), () => completed2 = true));

            Assert.IsTrue(completed2);

            sub2.Dispose();
            sub.Dispose();
            conn.Dispose();
        }
Beispiel #17
0
            public static IDisposable ListenForBuildErrors(IObservable <Started> buildStarted, IObservable <BuildIssueDetected> issuesDetected, out IObservableList <BuildIssueDetected> issues)
            {
                var errors = new ListBehaviorSubject <BuildIssueDetected>();

                issues = errors;

                return(Disposable.Combine(
                           buildStarted.Select(b => BinaryMessage.TryParse(b.Command, BuildProject.MessageType, BuildProject.ReadDataFrom))
                           .NotNone()
                           .Subscribe(_ => errors.OnClear()),

                           buildStarted.Select(b => BinaryMessage.TryParse(b.Command, GenerateBytecode.MessageType, GenerateBytecode.ReadDataFrom))
                           .NotNone()
                           .Subscribe(_ => errors.OnClear()),

                           issuesDetected.Subscribe(
                               issue =>
                {
                    errors.OnAdd(issue);
                })));
            }
Beispiel #18
0
        public IControl View(IProject project, IDialog <object> dialog)
        {
            var newFile = Property.Create("");
            var files   = new ListBehaviorSubject <IFilePath>();

            project.FilePath.Select(SketchImportUtils.SketchListFilePath)
            .Where(path => File.Exists(path.NativePath))
            .Select(SketchFilePaths).Do(paths =>
            {
                foreach (var absoluteFilePath in paths)
                {
                    files.OnAdd(absoluteFilePath);
                }
            })
            .Subscribe();

            var addNewSketchFileCommand =
                Command.Create(newFile
                               .CombineLatest(project.RootDirectory,
                                              (pathString, rootDir) =>
            {
                if (!string.IsNullOrWhiteSpace(pathString))
                {
                    var path = FilePath.Parse(pathString);

                    if (_fileSystem.Exists((path as IAbsolutePath) ?? rootDir.Combine((RelativeFilePath)path)))
                    {
                        return(Optional.Some(path));
                    }
                }
                return(Optional.None());
            })
                               .WherePerElement(path => SketchImportUtils.IsSketchFile(path) && !files.Value.Contains(path))
                               .SelectPerElement(
                                   path => (Action)(() =>
            {
                _logger.Info("Adding sketch file to watch: " + path);
                files.OnAdd(path);
                newFile.Write("");
            })));

            var content = Layout.Dock()
                          .Top(
                Layout.StackFromLeft(
                    Label.Create(
                        "Add sketch files to import symbols from",
                        font: Theme.DefaultFont,
                        textAlignment: TextAlignment.Left,
                        color: Theme.DescriptorText))
                .CenterHorizontally())
                          .Top(Spacer.Medium)
                          .Bottom(
                Layout.Dock()
                .Right(
                    AddButton(addNewSketchFileCommand)
                    .SetToolTip("Import sketch symbols from this file")
                    .Center())
                .Right(Spacer.Small)
                .Fill(
                    FilePathControl.Create(
                        newFile,
                        project.RootDirectory,
                        new[] { new FileFilter("Sketch files", SketchImportUtils.SketchExtention) },
                        "Add sketch file",
                        "Enter path of new sketch file to add here, or click browse button",
                        "Open")))
                          .Fill(
                files
                .ToObservableImmutableList()
                .SelectPerElement(
                    filePath =>
            {
                var highlightSketchFileControl = new BehaviorSubject <bool>(false);
                var fileExists = project.RootDirectory.Select(
                    root => File.Exists(FilePath.ParseAndMakeAbsolute(filePath, root).NativePath));

                return(highlightSketchFileControl
                       .DistinctUntilChanged()
                       .CombineLatest(fileExists,
                                      (highlight, exists) =>
                {
                    return Layout.Dock()
                    .Bottom(Spacer.Smaller)
                    .Right(
                        RemoveButton(Command.Enabled(() => { files.OnRemove(files.Value.IndexOf(filePath)); }))
                        .SetToolTip("Remove sketch file from watch list"))
                    .Right(Spacer.Small)
                    .Fill(
                        Label.Create(
                            Observable.Return(filePath.ToString()).AsText(),
                            color: exists?Theme.DefaultText: Theme.ErrorColor,
                            font: Theme.DefaultFont)
                        .CenterVertically()).SetToolTip(exists ? default(Text) : "Can't find file " + filePath)
                    .OnMouse(
                        entered: Command.Enabled(() => { highlightSketchFileControl.OnNext(true); }),
                        exited: Command.Enabled(() => highlightSketchFileControl.OnNext(false)))
                    .WithPadding(new Points(2))
                    .WithBackground(highlight ? Theme.FaintBackground : Theme.PanelBackground);
                })
                       .Switch());
            })
                .StackFromTop()
                .MakeScrollable());

            return
                (ConfirmCancelControl.Create(
                     close: Command.Enabled(() => { _isVisible.OnNext(false); }),
                     confirm: Command.Enabled(
                         () => { project.FilePath.Select(SketchImportUtils.SketchListFilePath).Do(path => { WriteSketchFilePaths(path, files.Value); }).Subscribe(); }),
                     fill: content.WithMediumPadding(), cancel: null, confirmText: null, cancelText: null, confirmTooltip: null)
                 .WithMacWindowStyleCompensation()                        // order of window style compensation and with background is important
                 .WithBackground(Theme.PanelBackground));
        }