123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace UniRx.InternalUtil
- {
- public class ListObserver<T> : IObserver<T>
- {
- private readonly ImmutableList<IObserver<T>> _observers;
- public ListObserver(ImmutableList<IObserver<T>> observers)
- {
- _observers = observers;
- }
- public void OnCompleted()
- {
- var targetObservers = _observers.Data;
- for (int i = 0; i < targetObservers.Length; i++)
- {
- targetObservers[i].OnCompleted();
- }
- }
- public void OnError(Exception error)
- {
- var targetObservers = _observers.Data;
- for (int i = 0; i < targetObservers.Length; i++)
- {
- targetObservers[i].OnError(error);
- }
- }
- public void OnNext(T value)
- {
- var targetObservers = _observers.Data;
- for (int i = 0; i < targetObservers.Length; i++)
- {
- targetObservers[i].OnNext(value);
- }
- }
- internal IObserver<T> Add(IObserver<T> observer)
- {
- return new ListObserver<T>(_observers.Add(observer));
- }
- internal IObserver<T> Remove(IObserver<T> observer)
- {
- var i = Array.IndexOf(_observers.Data, observer);
- if (i < 0)
- return this;
- if (_observers.Data.Length == 2)
- {
- return _observers.Data[1 - i];
- }
- else
- {
- return new ListObserver<T>(_observers.Remove(observer));
- }
- }
- }
- public class EmptyObserver<T> : IObserver<T>
- {
- public static readonly EmptyObserver<T> Instance = new EmptyObserver<T>();
- EmptyObserver()
- {
- }
- public void OnCompleted()
- {
- }
- public void OnError(Exception error)
- {
- }
- public void OnNext(T value)
- {
- }
- }
- public class ThrowObserver<T> : IObserver<T>
- {
- public static readonly ThrowObserver<T> Instance = new ThrowObserver<T>();
- ThrowObserver()
- {
- }
- public void OnCompleted()
- {
- }
- public void OnError(Exception error)
- {
- error.Throw();
- }
- public void OnNext(T value)
- {
- }
- }
- public class DisposedObserver<T> : IObserver<T>
- {
- public static readonly DisposedObserver<T> Instance = new DisposedObserver<T>();
- DisposedObserver()
- {
- }
- public void OnCompleted()
- {
- throw new ObjectDisposedException("");
- }
- public void OnError(Exception error)
- {
- throw new ObjectDisposedException("");
- }
- public void OnNext(T value)
- {
- throw new ObjectDisposedException("");
- }
- }
- }
|