123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace UniRx.Operators
- {
- // FromEvent, FromEventPattern
- internal class FromEventPatternObservable<TDelegate, TEventArgs> : OperatorObservableBase<EventPattern<TEventArgs>>
- where TEventArgs : EventArgs
- {
- readonly Func<EventHandler<TEventArgs>, TDelegate> conversion;
- readonly Action<TDelegate> addHandler;
- readonly Action<TDelegate> removeHandler;
- public FromEventPatternObservable(Func<EventHandler<TEventArgs>, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
- : base(false)
- {
- this.conversion = conversion;
- this.addHandler = addHandler;
- this.removeHandler = removeHandler;
- }
- protected override IDisposable SubscribeCore(IObserver<EventPattern<TEventArgs>> observer, IDisposable cancel)
- {
- var fe = new FromEventPattern(this, observer);
- return fe.Register() ? fe : Disposable.Empty;
- }
- class FromEventPattern : IDisposable
- {
- readonly FromEventPatternObservable<TDelegate, TEventArgs> parent;
- readonly IObserver<EventPattern<TEventArgs>> observer;
- TDelegate handler;
- public FromEventPattern(FromEventPatternObservable<TDelegate, TEventArgs> parent, IObserver<EventPattern<TEventArgs>> 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<TEventArgs>(sender, eventArgs));
- }
- public void Dispose()
- {
- if (handler != null)
- {
- parent.removeHandler(handler);
- handler = default(TDelegate);
- }
- }
- }
- }
- internal class FromEventObservable<TDelegate> : OperatorObservableBase<Unit>
- {
- readonly Func<Action, TDelegate> conversion;
- readonly Action<TDelegate> addHandler;
- readonly Action<TDelegate> removeHandler;
- public FromEventObservable(Func<Action, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
- : base(false)
- {
- this.conversion = conversion;
- this.addHandler = addHandler;
- this.removeHandler = removeHandler;
- }
- protected override IDisposable SubscribeCore(IObserver<Unit> observer, IDisposable cancel)
- {
- var fe = new FromEvent(this, observer);
- return fe.Register() ? fe : Disposable.Empty;
- }
- class FromEvent : IDisposable
- {
- readonly FromEventObservable<TDelegate> parent;
- readonly IObserver<Unit> observer;
- TDelegate handler;
- public FromEvent(FromEventObservable<TDelegate> parent, IObserver<Unit> 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<TDelegate, TEventArgs> : OperatorObservableBase<TEventArgs>
- {
- readonly Func<Action<TEventArgs>, TDelegate> conversion;
- readonly Action<TDelegate> addHandler;
- readonly Action<TDelegate> removeHandler;
- public FromEventObservable(Func<Action<TEventArgs>, TDelegate> conversion, Action<TDelegate> addHandler, Action<TDelegate> removeHandler)
- : base(false)
- {
- this.conversion = conversion;
- this.addHandler = addHandler;
- this.removeHandler = removeHandler;
- }
- protected override IDisposable SubscribeCore(IObserver<TEventArgs> observer, IDisposable cancel)
- {
- var fe = new FromEvent(this, observer);
- return fe.Register() ? fe : Disposable.Empty;
- }
- class FromEvent : IDisposable
- {
- readonly FromEventObservable<TDelegate, TEventArgs> parent;
- readonly IObserver<TEventArgs> observer;
- TDelegate handler;
- public FromEvent(FromEventObservable<TDelegate, TEventArgs> parent, IObserver<TEventArgs> 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<Unit>
- {
- readonly Action<Action> addHandler;
- readonly Action<Action> removeHandler;
- public FromEventObservable(Action<Action> addHandler, Action<Action> removeHandler)
- : base(false)
- {
- this.addHandler = addHandler;
- this.removeHandler = removeHandler;
- }
- protected override IDisposable SubscribeCore(IObserver<Unit> observer, IDisposable cancel)
- {
- var fe = new FromEvent(this, observer);
- return fe.Register() ? fe : Disposable.Empty;
- }
- class FromEvent : IDisposable
- {
- readonly FromEventObservable parent;
- readonly IObserver<Unit> observer;
- Action handler;
- public FromEvent(FromEventObservable parent, IObserver<Unit> 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_<T> : OperatorObservableBase<T>
- {
- readonly Action<Action<T>> addHandler;
- readonly Action<Action<T>> removeHandler;
- public FromEventObservable_(Action<Action<T>> addHandler, Action<Action<T>> removeHandler)
- : base(false)
- {
- this.addHandler = addHandler;
- this.removeHandler = removeHandler;
- }
- protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
- {
- var fe = new FromEvent(this, observer);
- return fe.Register() ? fe : Disposable.Empty;
- }
- class FromEvent : IDisposable
- {
- readonly FromEventObservable_<T> parent;
- readonly IObserver<T> observer;
- Action<T> handler;
- public FromEvent(FromEventObservable_<T> parent, IObserver<T> 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;
- }
- }
- }
- }
- }
|