Exemple #1
0
        void IInjector.Dispose()
        {
            var count = _disposed.Count;

            for (var index = 0; index < count; index++)
            {
                var target = _disposed[index];
                if (target != null)
                {
#if !STRANGE_ANALYSIS
                    try
                    {
                        target.Dispose();
                    }
                    catch (Exception exception)
                    {
                        Debug.LogException(exception);
                    }
#else
                    StrangeDebugger.Measure(() => target.Dispose(), null, target, "DisposeInfo");
#endif
                }
            }

            _disposed.Clear();
        }
Exemple #2
0
        public object Inject(object target, bool isAvoidDestroy = false)
        {
            failIf(binder == null, "Attempt to inject into Injector without a Binder",
                   InjectionExceptionType.NO_BINDER);
            failIf(reflector == null, "Attempt to inject without a reflector", InjectionExceptionType.NO_REFLECTOR);
            failIf(target == null, "Attempt to inject into null instance", InjectionExceptionType.NULL_TARGET);

            //Some things can't be injected into. Bail out.
            var t = target.GetType();

            if (t.IsPrimitive || t == typeof(decimal) || t == typeof(string))
            {
                return(target);
            }

            ReflectedClass reflection = default;

#if !STRANGE_ANALYSIS
            reflection = reflector.Get(t);
#else
            StrangeDebugger.Measure(() => reflection = reflector.Get(t), null, target, "GetReflectedInfo");
#endif

            failIf(
                reflection == null, "Attempt to PostConstruct without a reflection",
                InjectionExceptionType.NULL_REFLECTION);

            if (reflection.Setters.Length > 0)
            {
#if !STRANGE_ANALYSIS
                performSetterInjection(target, reflection);
#else
                StrangeDebugger.Measure(() => performSetterInjection(target, reflection), null, target, "SetterInjection");
#endif
            }

            if (reflection.PostConstructor != null)
            {
#if !STRANGE_ANALYSIS
                postInject(target, reflection);
#else
                StrangeDebugger.Measure(() => postInject(target, reflection), null, target, "PostConstruct");
#endif
            }

            if (reflection.DeConstructor != null && !isAvoidDestroy)
            {
                _deconstructed.Add(new DeconstructBucket {
                    Target = target, Deconstruct = reflection.DeConstructor
                });
            }

            //exclude context to avoid dead loop. Dispose on context must be called manually
            if (target is IDisposable && !(target is IContext) && !isAvoidDestroy)
            {
                _disposed.Add((IDisposable)target);
            }

            return(target);
        }
Exemple #3
0
        void IInjector.Deconstruct()
        {
            var count = _deconstructed.Count;

            for (var index = 0; index < count; index++)
            {
                var bucket = _deconstructed[index];
                if (bucket.Deconstruct != null && bucket.Target != null)
                {
#if !STRANGE_ANALYSIS
                    try
                    {
                        bucket.Deconstruct.Invoke(bucket.Target, null);
                    }
                    catch (Exception exception)
                    {
                        Debug.LogException(exception);
                    }
#else
                    StrangeDebugger.Measure(() => bucket.Deconstruct.Invoke(bucket.Target, null), null, bucket.Target, "DeconstructInfo");
#endif
                }
            }

            _deconstructed.Clear();
        }
Exemple #4
0
        public virtual void Retain()
        {
            retain = true;
#if STRANGE_ANALYSIS
            StrangeDebugger.CommandRetain(this);
#endif
        }
Exemple #5
0
        /// Use/override this method to clean up the Command for recycling
        public virtual void Restore()
        {
#if STRANGE_ANALYSIS
            StrangeDebugger.CommandRestore(this);
#endif
            injectionBinder.injector.Uninject(this);
            IsClean = true;
        }
Exemple #6
0
        public void Cancel()
        {
            cancelled = true;

#if STRANGE_ANALYSIS
            StrangeDebugger.CommandCancel(this);
#endif
        }
Exemple #7
0
        /// A MonoBehaviour OnEnable handler
        /// The View will inform the Context that it was enabled
        protected virtual void OnEnable()
        {
#if STRANGE_ANALYSIS
            StrangeDebugger.Measure(() => bubbleToContext(this, BubbleType.Enable, false), null, name + "_OnEnable", "bubbleToContext");
#else
            bubbleToContext(this, BubbleType.Enable, false);
#endif
        }
Exemple #8
0
        public virtual void Fail()
        {
#if STRANGE_ANALYSIS
            StrangeDebugger.CommandFail(this);
#endif
            if (commandBinder != null)
            {
                commandBinder.Stop(this);
            }
        }
Exemple #9
0
        public virtual void Release()
        {
            retain = false;
#if STRANGE_ANALYSIS
            StrangeDebugger.CommandRelease(this);
#endif
            if (commandBinder != null)
            {
                commandBinder.ReleaseCommand(this);
            }
        }
Exemple #10
0
        /// A MonoBehaviour Awake handler.
        /// The View will attempt to connect to the Context at this moment.
        protected virtual void Awake()
        {
            if (autoRegisterWithContext && !registeredWithContext)
            {
#if STRANGE_ANALYSIS
                StrangeDebugger.Measure(() => bubbleToContext(this, BubbleType.Add, false), null, name + "_Awake", "bubbleToContext");
#else
                bubbleToContext(this, BubbleType.Add, false);
#endif
            }
        }
Exemple #11
0
        public T Instantiate <T>(Type type)
        {
            var reflection = reflector.Get(type);

            failIf(
                reflection.Constructor == null, "Attempt to construction inject a null constructor",
                InjectionExceptionType.NULL_CONSTRUCTOR);

#if STRANGE_ANALYSIS
            T value = default;
            StrangeDebugger.Measure(
                () =>
            {
                var args2 = GetArgs(reflection);
                value     = (T)reflection.Constructor.Invoke(args2);
            }, null, type.Name, "ConstructorInjection");
            return(value);
#endif
            var args = GetArgs(reflection);
            return((T)reflection.Constructor.Invoke(args));
        }