Coffeehouse Post

Single Post Permalink

View Thread: Asynchrony, Progress Reporting and Cancellation
  • exoteric

    @Richard.Hein: There are pros and cons to both styles.

    The examples are a little unbalanced however.

    The first example is unnecessarily verbose:

    • uses explicit variable typing instead of type-inference
    • uses pointless class instead of fully parameterized extension method

    The second example is too simple:

    • no use of using
    • no use of exception handling
    • no use of throttling

    I realize this was just to show a point about a fluent style of writing Rx of course.

    I prefer your style as it has a certain simplicity about it and it appears completely lazy.

    Since we do in fact have some level of syntactic support for Rx, namely LINQ, we could sugar up your example a little bit:

    var observable =
        from _ in Observable.Return(0)
        let watcher =
            new FileSystemWatcher(@"D:\Documents")
            {
                EnableRaisingEvents = true,
                IncludeSubdirectories = true,
            }
        let created =
            from e in Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>
                            (h => watcher.Created += h, h => watcher.Created -= h)
            select new { e.EventArgs.ChangeType, e.EventArgs.FullPath, e.EventArgs.Name }
        let deleted =
            from e in Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>
                            (h => watcher.Deleted += h, h => watcher.Deleted -= h)
            select new { e.EventArgs.ChangeType, e.EventArgs.FullPath, e.EventArgs.Name }
        let changed =
            from e in Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>
                            (h => watcher.Changed += h, h => watcher.Changed -= h)
            select new { e.EventArgs.ChangeType, e.EventArgs.FullPath, e.EventArgs.Name }
        let renamed =
            from e in Observable.FromEventPattern<RenamedEventHandler, RenamedEventArgs>
                            (h => watcher.Renamed += h, h => watcher.Renamed -= h)
            select new
            {
                e.EventArgs.ChangeType,
                e.EventArgs.FullPath,
                Name = e.EventArgs.OldName + " renamed to " + e.EventArgs.Name
            }
        select Observable.Merge
        (
            created, deleted, changed, renamed
        );
    

    This is more verbose and unfortunately less homogeneous but I suspect more readable.