using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace UniRx.Operators { // FromEvent, FromEventPattern internal class FromEventPatternObservable : OperatorObservableBase> where TEventArgs : EventArgs { readonly Func, TDelegate> conversion; readonly Action addHandler; readonly Action removeHandler; public FromEventPatternObservable(Func, TDelegate> conversion, Action addHandler, Action removeHandler) : base(false) { this.conversion = conversion; this.addHandler = addHandler; this.removeHandler = removeHandler; } protected override IDisposable SubscribeCore(IObserver> observer, IDisposable cancel) { var fe = new FromEventPattern(this, observer); return fe.Register() ? fe : Disposable.Empty; } class FromEventPattern : IDisposable { readonly FromEventPatternObservable parent; readonly IObserver> observer; TDelegate handler; public FromEventPattern(FromEventPatternObservable parent, IObserver> observer) { this.parent = parent; this.observer = observer; } public bool Register() { handler = parent.conversion(OnNext); try { parent.addHandler(handler); } catch (Exception ex) { observer.OnError(ex); return false; } return true; } void OnNext(object sender, TEventArgs eventArgs) { observer.OnNext(new EventPattern(sender, eventArgs)); } public void Dispose() { if (handler != null) { parent.removeHandler(handler); handler = default(TDelegate); } } } } internal class FromEventObservable : OperatorObservableBase { readonly Func conversion; readonly Action addHandler; readonly Action removeHandler; public FromEventObservable(Func conversion, Action addHandler, Action removeHandler) : base(false) { this.conversion = conversion; this.addHandler = addHandler; this.removeHandler = removeHandler; } protected override IDisposable SubscribeCore(IObserver observer, IDisposable cancel) { var fe = new FromEvent(this, observer); return fe.Register() ? fe : Disposable.Empty; } class FromEvent : IDisposable { readonly FromEventObservable parent; readonly IObserver observer; TDelegate handler; public FromEvent(FromEventObservable parent, IObserver observer) { this.parent = parent; this.observer = observer; } public bool Register() { handler = parent.conversion(OnNext); try { parent.addHandler(handler); } catch (Exception ex) { observer.OnError(ex); return false; } return true; } void OnNext() { observer.OnNext(Unit.Default); } public void Dispose() { if (handler != null) { parent.removeHandler(handler); handler = default(TDelegate); } } } } internal class FromEventObservable : OperatorObservableBase { readonly Func, TDelegate> conversion; readonly Action addHandler; readonly Action removeHandler; public FromEventObservable(Func, TDelegate> conversion, Action addHandler, Action removeHandler) : base(false) { this.conversion = conversion; this.addHandler = addHandler; this.removeHandler = removeHandler; } protected override IDisposable SubscribeCore(IObserver observer, IDisposable cancel) { var fe = new FromEvent(this, observer); return fe.Register() ? fe : Disposable.Empty; } class FromEvent : IDisposable { readonly FromEventObservable parent; readonly IObserver observer; TDelegate handler; public FromEvent(FromEventObservable parent, IObserver observer) { this.parent = parent; this.observer = observer; } public bool Register() { handler = parent.conversion(OnNext); try { parent.addHandler(handler); } catch (Exception ex) { observer.OnError(ex); return false; } return true; } void OnNext(TEventArgs args) { observer.OnNext(args); } public void Dispose() { if (handler != null) { parent.removeHandler(handler); handler = default(TDelegate); } } } } internal class FromEventObservable : OperatorObservableBase { readonly Action addHandler; readonly Action removeHandler; public FromEventObservable(Action addHandler, Action removeHandler) : base(false) { this.addHandler = addHandler; this.removeHandler = removeHandler; } protected override IDisposable SubscribeCore(IObserver observer, IDisposable cancel) { var fe = new FromEvent(this, observer); return fe.Register() ? fe : Disposable.Empty; } class FromEvent : IDisposable { readonly FromEventObservable parent; readonly IObserver observer; Action handler; public FromEvent(FromEventObservable parent, IObserver observer) { this.parent = parent; this.observer = observer; this.handler = OnNext; } public bool Register() { try { parent.addHandler(handler); } catch (Exception ex) { observer.OnError(ex); return false; } return true; } void OnNext() { observer.OnNext(Unit.Default); } public void Dispose() { if (handler != null) { parent.removeHandler(handler); handler = null; } } } } internal class FromEventObservable_ : OperatorObservableBase { readonly Action> addHandler; readonly Action> removeHandler; public FromEventObservable_(Action> addHandler, Action> removeHandler) : base(false) { this.addHandler = addHandler; this.removeHandler = removeHandler; } protected override IDisposable SubscribeCore(IObserver observer, IDisposable cancel) { var fe = new FromEvent(this, observer); return fe.Register() ? fe : Disposable.Empty; } class FromEvent : IDisposable { readonly FromEventObservable_ parent; readonly IObserver observer; Action handler; public FromEvent(FromEventObservable_ parent, IObserver observer) { this.parent = parent; this.observer = observer; this.handler = OnNext; } public bool Register() { try { parent.addHandler(handler); } catch (Exception ex) { observer.OnError(ex); return false; } return true; } void OnNext(T value) { observer.OnNext(value); } public void Dispose() { if (handler != null) { parent.removeHandler(handler); handler = null; } } } } }