public static IDisposable SubscribeDisposableRender <T>(this IObservableExceptional <T> o, Func <T, IDisposable> fn, IModelDoc2 doc, Action <Exception> errHandler) { var d = new SerialDisposable(); var s = o .ObserveOnSolidworksThread() .Subscribe(onNext: v => { using (OpenGlRenderer.DeferRedraw(doc)) { try { d.Disposable = Disposable.Empty; d.Disposable = fn(v) ?? Disposable.Empty; } catch (Exception e) { d.Disposable = Disposable.Empty; errHandler(e); } } }, onError: e => { d.Disposable = Disposable.Empty; errHandler(e); }); return(new CompositeDisposable(s, d)); }
/// <summary> /// Converts None to a NoneException /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="U"></typeparam> /// <param name="o"></param> /// <param name="sel"></param> /// <returns></returns> public static IObservableExceptional <T> Select <T, U>(this IObservableExceptional <U> o, Func <U, Option <T> > sel) { return(o .Select(v => sel(v).ToExceptional()) .Observable .Select(v => v.SelectMany(e0 => e0)) .ToObservableExceptional()); }
/// <summary> /// Subscribes to the observable sequence and manages the disposables with a serial disposable. That /// is before the function is called again the previous disposable is disposed. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="o"></param> /// <param name="disposableYielder"></param> /// <returns></returns> public static IDisposable SubscribeDisposable <T>(this IObservableExceptional <T> o, Action <T, Action <IDisposable> > disposableYielder, Action <Exception> errHandler) { Func <T, IDisposable> fn2 = v => { var c = new CompositeDisposable(); disposableYielder(v, c.Add); return(c); }; return(SubscribeDisposable(o, fn2, errHandler)); }
/// <summary> /// Subscribes to the observable sequence and manages the disposables with a serial disposable. That /// is before the function is called again the previous disposable is disposed. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="o"></param> /// <param name="fn"></param> /// <returns></returns> public static IDisposable SubscribeDisposable <T>(this IObservableExceptional <T> o, Func <T, IDisposable> fn, Action <Exception> errHandler) { var d = new SerialDisposable(); var s = o.Subscribe(v => { d.Disposable = Disposable.Empty; d.Disposable = fn(v) ?? Disposable.Empty; }, onError: errHandler); return(new CompositeDisposable(s, d)); }
/// <summary> /// <para>Observe on UIDispatcherScheduler.</para> /// <para>UIDIspatcherScheduler is created when access to UIDispatcher.Default first in the whole application.</para> /// <para>If you want to explicitly initialize, call UIDispatcherScheduler.Initialize() in App.xaml.cs.</para> /// If a new value arrives before the selector is finished calculated the old one then it is canceled. /// /// This will also raise a message box if an exception is thrown downstream. /// </summary> public static IObservableExceptional <U> ObserveOnSolidworksThread <T, U>(this IObservableExceptional <T> source, Func <T, CancellationToken, U> selector) { return(source .StartWith(default(T)) .Select(s => new { s, cts = new CancellationTokenSource() }) .Buffer(2, 1).Where(b => b.Count == 2) .Select (b => { b[0].cts.Cancel(); return b[1]; }) .ObserveOn(UiDispatcherScheduler.Default) .Select(b => selector(b.s, b.cts.Token))); }
/// <summary> /// This behaves like the normal sample except that it will always generate /// an event when the sampler is triggered even if the input stream has not changed. /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="U"></typeparam> /// <param name="sampled"></param> /// <param name="sampler"></param> /// <returns></returns> public static IObservableExceptional <T> SampleAlways <T, U>(this IObservableExceptional <T> sampled, IObservable <U> sampler) { return(ObservableExceptional.Create <T>(observer => { var started = false; var value = default(T); var d0 = sampled.Subscribe(v => { value = v; started = true; }); var d1 = sampler.Subscribe(s => { if (started) { observer.OnNext(value); } }); return new CompositeDisposable(d0, d1); })); }
public static IObservableExceptional <Task> ObserveOnSolidworksThread <T>(this IObservableExceptional <T> source, Func <T, CancellationToken, Task> selector) { return(source .StartWith(default(T)) .Select(s => new { s, cts = new CancellationTokenSource() }) .Buffer(2, 1).Where(b => b.Count == 2) .Select(b => { b[0].cts.Cancel(); return b[1]; }) .ObserveOn(UiDispatcherScheduler.Default) .Select(async b => { try { await selector(b.s, b.cts.Token); } catch (OperationCanceledException) { Console.WriteLine("Operation cancelled"); } })); }
public static IObservableExceptional <T> ObserveOnSolidworksThread <T>(this IObservableExceptional <T> source) => source .ObserveOn(UiDispatcherScheduler.Default);
public static IDisposable SubscribeAndReportExceptions <T>(this IObservableExceptional <T> @this) => @this.Subscribe(onNext: v => {}, onError: e => e.Show());
/// <summary> /// Using a selector bind the property to the observable. /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="S"></typeparam> /// <param name="obs"></param> /// <param name="target"></param> /// <param name="selector"></param> /// <param name="handler"></param> /// <returns></returns> public static IDisposable AssignTo <T, S>(this IObservableExceptional <T> obs, S target, Expression <Func <S, T> > selector, Action <Exception> handler) { var proxy = selector.GetProxy(target); return(obs.Subscribe(u => proxy.Value = u, onError: handler)); }
public static IObservableExceptional <T> WhereIsSome <T>(this IObservableExceptional <Option <T> > q) { return(q.SelectMany(o => o.Match(ObservableExceptional.Return, ObservableExceptional.Empty <T>))); }