public void WatchFolder_Typical( DirectoryPath existingDir, FilePath existingFile, [Frozen] MockFileSystemWatcher mockFileWatcher, [Frozen] MockFileSystem fs) { FilePath fileB = Path.Combine(existingDir.Path, "FileB"); fs.File.WriteAllText(existingFile, string.Empty); var live = ObservableExt.WatchFolderContents(existingDir.Path, fileSystem: fs) .RemoveKey(); var list = live.AsObservableList(); list.Count.Should().Be(1); list.Items.ToExtendedList()[0].Should().Be(existingFile); fs.File.WriteAllText(fileB, string.Empty); mockFileWatcher.MarkCreated(fileB); list = live.AsObservableList(); list.Count.Should().Be(2); list.Items.ToExtendedList()[0].Should().Be(existingFile); list.Items.ToExtendedList()[1].Should().Be(fileB); fs.File.Delete(existingFile); mockFileWatcher.MarkDeleted(existingFile); list = live.AsObservableList(); list.Count.Should().Be(1); list.Items.ToExtendedList()[0].Should().Be(fileB); }
public IObservable <IChangeSet <IModListingGetter> > Get(out IObservable <ErrorResponse> state) { var path = ListingsPathProvider.Path; if (path == null) { state = Observable.Return(ErrorResponse.Success); return(Observable.Empty <IChangeSet <IModListingGetter> >()); } var raw = ObservableExt.WatchFile(path.Value, fileWatcherFactory: _fileSystem.FileSystemWatcher, throwIfInvalidPath: true) .StartWith(Unit.Default) .Select(_ => { try { return(GetResponse <IObservable <IChangeSet <IModListingGetter> > > .Succeed( ListingsReader.Read(_fileSystem.File.OpenRead(path.Value)) .AsObservableChangeSet())); } catch (Exception ex) { return(GetResponse <IObservable <IChangeSet <IModListingGetter> > > .Fail(ex)); } }); state = raw .Select(r => (ErrorResponse)r); return(raw .Select(r => { return r.Value ?? Observable.Empty <IChangeSet <IModListingGetter> >(); }) .Switch()); }
public IObservable <IChangeSet <IModListingGetter> > Get(out IObservable <ErrorResponse> state, IScheduler?scheduler = null) { var results = ObservableExt.WatchFile(_pluginListingsFilePath.Path, fileWatcherFactory: _fileSystem.FileSystemWatcher) .StartWith(Unit.Default) .Select(_ => { try { return(GetResponse <IObservable <IChangeSet <IModListingGetter> > > .Succeed( _listingsProvider.Get() .AsObservableChangeSet())); } catch (Exception ex) { return(GetResponse <IObservable <IChangeSet <IModListingGetter> > > .Fail(ex)); } }) .Replay(1) .RefCount(); state = results .Select(r => (ErrorResponse)r); return(results .Select(r => { return r.Value ?? Observable.Empty <IChangeSet <IModListingGetter> >(); }) .Switch() .ObserveOnIfApplicable(scheduler)); }
public static IObservable <IChangeSet <LoadOrderListing> > GetLiveLoadOrder( FilePath cccFilePath, DirectoryPath dataFolderPath, out IObservable <ErrorResponse> state, bool orderListings = true) { var raw = ObservableExt.WatchFile(cccFilePath.Path) .StartWith(Unit.Default) .Select(_ => { try { return(GetResponse <IObservable <IChangeSet <ModKey> > > .Succeed( File.ReadAllLines(cccFilePath.Path) .Select(x => ModKey.FromNameAndExtension(x)) .AsObservableChangeSet())); } catch (Exception ex) { return(GetResponse <IObservable <IChangeSet <ModKey> > > .Fail(ex)); } }) .Replay(1) .RefCount(); state = raw .Select(r => (ErrorResponse)r); var ret = ObservableListEx.And( raw .Select(r => { return(r.Value ?? Observable.Empty <IChangeSet <ModKey> >()); }) .Switch(), ObservableExt.WatchFolderContents(dataFolderPath.Path) .Transform(x => { if (ModKey.TryFromNameAndExtension(Path.GetFileName(x), out var modKey)) { return(TryGet <ModKey> .Succeed(modKey)); } return(TryGet <ModKey> .Failure); }) .Filter(x => x.Succeeded) .Transform(x => x.Value) .RemoveKey()) .Transform(x => new LoadOrderListing(x, true)); if (orderListings) { ret = ret.OrderListings(); } return(ret); }
public bool Show(Color inputColor, TimeSpan visibleTime) { var timer = ObservableExt.TimerMaxTick(1, TimeSpan.Zero, visibleTime); var color = ProcessColor(inputColor); var colors = new[] { color, Color.Black }.ToObservable(); colors.Zip(timer, (c, t) => new { Color = c, Count = t }) .Subscribe(item => commandBus.SendCommand(new SetColorCommand(item.Color))); return(true); }
public void WatchFile_Typical( [Frozen] FilePath path, [Frozen] MockFileSystemWatcher mockFileWatcher, [Frozen] MockFileSystem fs) { int count = 0; using var sub = ObservableExt.WatchFile(path, fileWatcherFactory: fs.FileSystemWatcher) .Subscribe(x => count++); count.Should().Be(0); fs.File.WriteAllText(path, string.Empty); mockFileWatcher.MarkCreated(path); count.Should().Be(1); }
public void WatchFile_MovedOut( FilePath path, FileName name, [Frozen] MockFileSystemWatcher mockFileWatcher, [Frozen] MockFileSystem fs) { int count = 0; using var sub = ObservableExt.WatchFile(path, fileWatcherFactory: fs.FileSystemWatcher) .Subscribe(x => count++); count.Should().Be(0); fs.File.WriteAllText(path, string.Empty); mockFileWatcher.MarkRenamed(path, name); count.Should().Be(1); }
public void WatchFolder_ATypicalSeparator( DirectoryPath someDirectory, [Frozen] FilePath file, [Frozen] MockFileSystemWatcher mockFileWatcher, [Frozen] MockFileSystem fs) { fs.Directory.CreateDirectory(someDirectory); var live = ObservableExt.WatchFolderContents(someDirectory.Path.Replace('\\', '/'), fileSystem: fs) .RemoveKey(); var list = live.AsObservableList(); list.Count.Should().Be(0); fs.File.WriteAllText(file, string.Empty); mockFileWatcher.MarkCreated(file); list.Count.Should().Be(1); list.Items.ToExtendedList()[0].Should().Be(file); }
public IObservable <IChangeSet <ModKey, ModKey> > Get() { if (!_fileSystem.Directory.Exists(DataDirectory.Path)) { return(Observable.Empty <IChangeSet <ModKey, ModKey> >()); } return(ObservableExt .WatchFolderContents(DataDirectory.Path, fileSystem: _fileSystem) .Transform(x => { if (ModKey.TryFromNameAndExtension(Path.GetFileName(x), out var modKey)) { return TryGet <ModKey> .Succeed(modKey); } return TryGet <ModKey> .Failure; }) .Filter(x => x.Succeeded) .Transform(x => x.Value) .ChangeKey(x => x) .Catch <IChangeSet <ModKey, ModKey>, DirectoryNotFoundException>(_ => Observable.Empty <IChangeSet <ModKey, ModKey> >())); }
public void WatchFolder_OnlySubfolder( [Frozen] DirectoryPath existingDir, [Frozen] MockFileSystemWatcher mockFileWatcher, [Frozen] MockFileSystem fs) { FilePath fileA = Path.Combine(existingDir.Path, "SomeFolder", "FileA"); FilePath fileB = Path.Combine(existingDir.Path, "FileB"); fs.Directory.CreateDirectory(Path.GetDirectoryName(fileA) !); fs.File.WriteAllText(fileA, string.Empty); var live = ObservableExt.WatchFolderContents(Path.Combine(existingDir.Path, "SomeFolder"), fileSystem: fs) .RemoveKey(); var list = live.AsObservableList(); list.Count.Should().Be(1); list.Items.ToExtendedList()[0].Should().Be(fileA); fs.File.WriteAllText(fileB, string.Empty); mockFileWatcher.MarkCreated(fileB); list = live.AsObservableList(); list.Count.Should().Be(1); list.Items.ToExtendedList()[0].Should().Be(fileA); }
/// <inheritdoc cref="IPluginLiveLoadOrderProvider"/> public static IObservable <Unit> GetLoadOrderChanged(GameRelease game) { return(ObservableExt.WatchFile( new PluginListingsPathProvider( new GameReleaseInjection(game)).Path)); }
/// <inheritdoc cref="IPluginLiveLoadOrderProvider"/> public static IObservable <Unit> GetLoadOrderChanged(FilePath loadOrderFilePath) { return(ObservableExt.WatchFile(loadOrderFilePath.Path)); }
public IObservable <Unit> Watch(DirectoryPath path, bool throwIfInvalidPath = false) { return(ObservableExt.WatchFolder(path, throwIfInvalidPath, _FileSystem.FileSystemWatcher)); }