using System; namespace UniRx { public static class SubjectExtensions { public static ISubject Synchronize(this ISubject subject) { return new AnonymousSubject((subject as IObserver).Synchronize(), subject); } public static ISubject Synchronize(this ISubject subject, object gate) { return new AnonymousSubject((subject as IObserver).Synchronize(gate), subject); } class AnonymousSubject : ISubject { readonly IObserver observer; readonly IObservable observable; public AnonymousSubject(IObserver observer, IObservable observable) { this.observer = observer; this.observable = observable; } public void OnCompleted() { observer.OnCompleted(); } public void OnError(Exception error) { if (error == null) throw new ArgumentNullException("error"); observer.OnError(error); } public void OnNext(T value) { observer.OnNext(value); } public IDisposable Subscribe(IObserver observer) { if (observer == null) throw new ArgumentNullException("observer"); return observable.Subscribe(observer); } } class AnonymousSubject : AnonymousSubject, ISubject { public AnonymousSubject(IObserver observer, IObservable observable) : base(observer, observable) { } } } }