Example #1
0
        public virtual T GetInstance <T>() where T : new()
        {
            Instances.TryGetValue(HashCodeTypeTool.GetCachedHashCode <T>(), out var stack);

            var instance = (T)(stack != null && stack.Count > 0 && stack.Peek() != null
                ? stack.Pop()
                : CreateInstance <T>());

            GetStuff(instance);

            return(instance);
        }
Example #2
0
        public void Add <T>(IListen newListener)
        {
            var hash = HashCodeTypeTool.GetCachedHashCode <T>();

            if (_eventList.TryGetValue(hash, out var cachedListeners))
            {
                cachedListeners.Add(newListener);
                _eventList[hash] = cachedListeners;

                return;
            }

            _eventList.Add(hash, new List <IListen> {
                newListener
            });
        }
Example #3
0
        public virtual void ReturnInstance <T>(T instance)  where T : class
        {
            var hash = HashCodeTypeTool.GetCachedHashCode <T>();

            if (Instances.TryGetValue(hash, out var stack))
            {
                stack.Push(instance);
            }
            else
            {
                var newStack = new Stack <object>();

                newStack.Push(instance);

                Instances.Add(hash, newStack);
            }

            ReturnStuff(instance);
        }
Example #4
0
        public void Remove <T>(IListen listener)
        {
            if (listener == null)
            {
                return;
            }

            if (!_eventList.TryGetValue(HashCodeTypeTool.GetCachedHashCode <T>(), out var cachedListeners))
            {
                return;
            }

            // if (_isSendingInProcess)
            // {
            //     _removeQueue.Enqueue(new RemoveQueueInstance(cachedListeners, listener));
            // }
            // else
            // {
            cachedListeners.Remove(listener);
            // }
        }
Example #5
0
        public void Send <T>(T arguments = default)
        {
            if (!_eventList.TryGetValue(HashCodeTypeTool.GetCachedHashCode <T>(), out var cachedListeners))
            {
                return;
            }

            if (cachedListeners == null)
            {
                return;
            }

            for (var i = 0; i < cachedListeners.Count; i++)
            {
                _isSendingInProcess = true;

                var cachedListener = cachedListeners[i];

                if (cachedListener == null)
                {
                    continue;
                }

                if (!(cachedListener is IListen <T> listener))
                {
#if UNITY_EDITOR
                    Debug.LogError($"WRONG LISTENER <color=red>{typeof(T)}</color> in {cachedListener}");
#endif
                    continue;
                }

                listener.handleCallback(arguments);

                _isSendingInProcess = true;
            }

            ExecuteRemoveQueue();

            _isSendingInProcess = false;
        }
Example #6
0
 protected virtual int GetId <T>(T obj)
 {
     return(HashCodeTypeTool.GetCachedHashCode <T>());
 }
 protected virtual int GetId(T instance)
 {
     return(HashCodeTypeTool.GetCachedHashCode <T>());
 }