Example #1
0
        public ObserverHandle Alloc()
        {
            var handle = new ObserverHandle();

            Add(handle);
            return(handle);
        }
Example #2
0
        public ObserverHandle Observe <T>(IObserverList <T> observable, T observer)
        {
            ObserverHandle handle = null;

            Observe(ref handle, observable, observer);
            return(handle);
        }
Example #3
0
 public void Unobserve(ObserverHandle handle)
 {
     if (handle != null)
     {
         handle.Dispose();
         handles.Remove(handle);
     }
 }
Example #4
0
        public void Add(ref ObserverHandle handle, T observer)
        {
            if (handle == null)
            {
                handle = new ObserverHandle();
            }

            DebugOnly.Check(observer != null, "Observer is null.");
            DebugOnly.Check(handle.List == null, "Handle is already in use.");

            handle.Index = list.Count;
            handle.List  = this;
            list.Add(new Item {
                handle = handle, observer = observer
            });
        }
Example #5
0
        public void Remove(ObserverHandle handle)
        {
            DebugOnly.Check(handle != null, "Handle is null.");
            DebugOnly.Check(handle.List == this, "Handle is not registered with this list.");

            int lastIndex = list.Count - 1;

            if (handle.Index != lastIndex)
            {
                var replacement = list[lastIndex];
                list[handle.Index]       = replacement;
                replacement.handle.Index = handle.Index;
            }

            handle.List = null;
            list.RemoveAt(lastIndex);
        }
Example #6
0
 void IObserverList.Add(ref ObserverHandle handle, object observer)
 {
     if (observer is T typedObserver)
     {
         Add(ref handle, typedObserver);
     }
     else
     {
         if (observer == null)
         {
             DebugOnly.Error("Observer is null.");
         }
         else
         {
             DebugOnly.Error($"Was expecting type {typeof(T)}, got type {observer.GetType().Name}.");
         }
     }
 }
Example #7
0
 public void Observe <T>(ref ObserverHandle handle, IObserverList <T> observable, T observer)
 {
     observable.Add(ref handle, observer);
     Add(handle);
 }
Example #8
0
 public void Add(ObserverHandle handle)
 {
     handles.Add(handle);
 }
 protected void Unobserve(ObserverHandle handle)
 {
     Observers.Unobserve(handle);
 }
 protected void Observe <O>(ref ObserverHandle handle, IObserverList <O> observable)
     where O : class
 {
     Observers.Observe(ref handle, observable, this as O);
 }