internal static ReadOnlyListSubject <TResult> CollectInternal <TResult>( this FileSystemWatcher watcher, bool composited, Func <IObservable <CollectionNotification <string> >, IObservable <CollectionModification <TResult> > > selector, IScheduler scheduler) { Contract.Requires(watcher != null); Contract.Requires(selector != null); Contract.Requires(scheduler != null); Contract.Ensures(Contract.Result <ReadOnlyListSubject <TResult> >() != null); var existing = Directory .EnumerateFiles( watcher.Path, watcher.Filter, watcher.IncludeSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly); var changes = watcher .Watch(WatcherChangeTypes.Created | WatcherChangeTypes.Deleted | WatcherChangeTypes.Renamed) .SelectMany(notification => notification.Change == WatcherChangeTypes.Created ? Observable.Return(CollectionModification.CreateAdd(notification.FullPath)) : notification.Change == WatcherChangeTypes.Deleted ? Observable.Return(CollectionModification.CreateRemove(notification.FullPath)) : Observable.Return(CollectionModification.CreateRemove(notification.OldFullPath)).Concat(Observable.Return(CollectionModification.CreateAdd(notification.FullPath)))); if (composited) { changes = changes.Finally(watcher.Dispose); } return(existing.ToObservable(scheduler).Collect(changes.ObserveOn(scheduler), selector, StringComparer.OrdinalIgnoreCase)); }
/// <summary> /// Converts <see cref="INotifyCollectionChanged.CollectionChanged"/> events into an observable sequence of <see cref="CollectionModification{T}"/>. /// </summary> /// <typeparam name="T">The object that provides notification information.</typeparam> /// <param name="source">An implementation of <see cref="INotifyCollectionChanged"/> that raises events when a collection changes.</param> /// <remarks> /// An <see cref="NotifyCollectionChangedAction.Add"/> event is projected into zero or more <see cref="CollectionModificationKind.Add"/> modifications. /// A <see cref="NotifyCollectionChangedAction.Remove"/> event is projected into zero or more <see cref="CollectionModificationKind.Remove"/> modifications. /// A <see cref="NotifyCollectionChangedAction.Move"/> event is ignored. /// A <see cref="NotifyCollectionChangedAction.Replace"/> event is projected into zero or more sequential /// <see cref="CollectionModificationKind.Remove"/> and <see cref="CollectionModificationKind.Add"/> modifications. /// A <see cref="NotifyCollectionChangedAction.Reset"/> event is projected into a single <see cref="CollectionModificationKind.Clear"/> modification. /// </remarks> /// <returns>An observable sequence of <see cref="CollectionModification{T}"/> objects corresponding to raised events.</returns> #else /// <summary> /// Converts <see cref="INotifyCollectionChanged.CollectionChanged"/> events into an observable sequence of <see cref="CollectionModification{T}"/>. /// </summary> /// <typeparam name="T">The object that provides notification information.</typeparam> /// <param name="source">An implementation of <see cref="INotifyCollectionChanged"/> that raises events when a collection changes.</param> /// <remarks> /// An <see cref="NotifyCollectionChangedAction.Add"/> event is projected into zero or more <see cref="CollectionModificationKind.Add"/> modifications. /// A <see cref="NotifyCollectionChangedAction.Remove"/> event is projected into zero or more <see cref="CollectionModificationKind.Remove"/> modifications. /// A <see cref="NotifyCollectionChangedAction.Replace"/> event is projected into zero or more sequential /// <see cref="CollectionModificationKind.Remove"/> and <see cref="CollectionModificationKind.Add"/> modifications. /// A <see cref="NotifyCollectionChangedAction.Reset"/> event is projected into a single <see cref="CollectionModificationKind.Clear"/> modification. /// </remarks> /// <returns>An observable sequence of <see cref="CollectionModification{T}"/> objects corresponding to raised events.</returns> #endif public static IObservable <CollectionModification <T> > AsCollectionModifications <T>(this INotifyCollectionChanged source) { Contract.Requires(source != null); Contract.Ensures(Contract.Result <IObservable <CollectionModification <T> > >() != null); return(Observable.FromEventPattern <NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>( eh => source.CollectionChanged += eh, eh => source.CollectionChanged -= eh) .SelectMany(e => { var args = e.EventArgs; switch (args.Action) { case NotifyCollectionChangedAction.Add: return Observable.Return(CollectionModification.CreateAdd(EnsureList <T>(args.NewItems))); case NotifyCollectionChangedAction.Remove: return Observable.Return(CollectionModification.CreateRemove(EnsureList <T>(args.OldItems))); #if !SILVERLIGHT case NotifyCollectionChangedAction.Move: return Observable.Empty <CollectionModification <T> >(); #endif case NotifyCollectionChangedAction.Replace: return Observable.Return(CollectionModification.CreateRemove(EnsureList <T>(args.OldItems))) .Concat(Observable.Return(CollectionModification.CreateAdd(EnsureList <T>(args.NewItems)))); case NotifyCollectionChangedAction.Reset: return Observable.Return(CollectionModification.CreateClear <T>()); default: throw new InvalidOperationException(); } })); }