public void FinalizeBinding(DiContainer container)
        {
            if (BindInfo.ContractTypes.IsEmpty())
            {
                // We could assert her instead but it is nice when used with things like
                // BindInterfaces() (and there aren't any interfaces) to allow
                // interfaces to be added later
                return;
            }

            OnFinalizeBinding(container);

            if (BindInfo.NonLazy)
            {
                // Note that we can't simply use container.BindRootResolve here because
                // binding finalizers must only use RegisterProvider to allow cloning / bind
                // inheritance to work properly
                var bindingId = new BindingId(
                    typeof(object), DiContainer.DependencyRootIdentifier);

                foreach (var contractType in BindInfo.ContractTypes)
                {
                    container.RegisterProvider(
                        bindingId, null, new ResolveProvider(
                            contractType, container, BindInfo.Identifier, false));
                }
            }
        }
Ejemplo n.º 2
0
        void Construct(SignalManager manager, SignalSettings settings, BindInfo bindInfo)
        {
            _manager = manager;

            SignalId = new BindingId(this.GetType(), bindInfo.Identifier);
            Settings = settings;
        }
Ejemplo n.º 3
0
 public InstanceMethodSignalHandler(
     BindingId signalId, SignalManager manager, InjectContext lookupContext,
     Func<THandler, Action> methodGetter)
     : base(signalId, manager, lookupContext)
 {
     _methodGetter = methodGetter;
 }
Ejemplo n.º 4
0
 public StaticMethodWithInstanceSignalHandler(
     BindingId signalId, SignalManager manager, InjectContext lookupContext,
     Action <THandler> method)
     : base(signalId, manager, lookupContext)
 {
     _method = method;
 }
 public InstanceMethodSignalHandler(
     BindingId signalId, SignalManager manager, Lazy <THandler> handler,
     Func <THandler, Action> methodGetter)
     : base(signalId, manager, handler)
 {
     _methodGetter = methodGetter;
 }
 public InstanceMethodSignalHandlerBase(
     BindingId signalId, SignalManager manager,
     Lazy <THandler> handler)
     : base(signalId, manager)
 {
     _handler = handler;
 }
Ejemplo n.º 7
0
        public InstanceMethodSignalHandlerBase(
            BindingId signalId, SignalManager manager,
            InjectContext lookupContext)
            : base(signalId, manager)
        {
            Assert.IsEqual(lookupContext.MemberType, typeof(THandler));

            _lookupContext = lookupContext;
        }
Ejemplo n.º 8
0
        List <ComponentInfo> GetBoundInfosWithId(BindingId bindingId)
        {
            List <ComponentInfo> result;

            if (!_bindings.TryGetValue(bindingId, out result))
            {
                result = new List <ComponentInfo>();
            }

            return(result);
        }
Ejemplo n.º 9
0
        public void OnSpawned(
            Action <object> callback, SignalDeclaration declaration)
        {
            Assert.IsNull(_callback);
            _callback    = callback;
            _declaration = declaration;
            // Cache this in case OnDeclarationDespawned is called
            _signalId = declaration.BindingId;

            declaration.Add(this);
        }
        public SignalDeclaration(
            SignalDeclarationBindInfo bindInfo,
            [InjectOptional] ZenjectSettings zenjectSettings)
        {
            zenjectSettings = zenjectSettings ?? ZenjectSettings.Default;
            _settings       = zenjectSettings.Signals ?? ZenjectSettings.SignalSettings.Default;

            _bindingId = new BindingId(bindInfo.SignalType, bindInfo.Identifier);
            _missingHandlerResponses = bindInfo.MissingHandlerResponse;
            _isAsync     = bindInfo.RunAsync;
            TickPriority = bindInfo.TickPriority;
        }
Ejemplo n.º 11
0
        private List <ISignalHandler> GetList(BindingId signalType)
        {
            List <ISignalHandler> handlers;

            if (!_signalHandlers.TryGetValue(signalType, out handlers))
            {
                handlers = new List <ISignalHandler>();
                _signalHandlers.Add(signalType, handlers);
            }

            return(handlers);
        }
Ejemplo n.º 12
0
 public InjectContext(
     DiContainer container, Type memberType, string identifier, bool optional,
     Type objectType, object objectInstance, string memberName, InjectContext parentContext)
 {
     ObjectType     = objectType;
     ObjectInstance = objectInstance;
     Identifier     = identifier;
     MemberName     = memberName;
     MemberType     = memberType;
     Optional       = optional;
     Container      = container;
     BindingId      = new BindingId(memberType, identifier);
     ParentContext  = parentContext;
 }
Ejemplo n.º 13
0
 public InjectContext(
     DiContainer container, Type memberType, string identifier, bool optional,
     Type parentType, object parentInstance, string memberName, List <Type> parentTypes)
 {
     ParentType     = parentType;
     ParentInstance = parentInstance;
     Identifier     = identifier;
     MemberName     = memberName;
     MemberType     = memberType;
     ParentTypes    = parentTypes;
     Optional       = optional;
     Container      = container;
     BindingId      = new BindingId(memberType, identifier);
 }
Ejemplo n.º 14
0
        SignalDeclaration GetDeclaration(BindingId signalId)
        {
            SignalDeclaration handler;

            if (_localDeclarationMap.TryGetValue(signalId, out handler))
            {
                return(handler);
            }

            if (_parentBus != null)
            {
                return(_parentBus.GetDeclaration(signalId));
            }

            return(null);
        }
Ejemplo n.º 15
0
        internal IEnumerable <ProviderBase> GetProvidersForContract(BindingId bindingId)
        {
            List <ProviderBase> providers;

            if (_providers.TryGetValue(bindingId, out providers))
            {
                return(providers);
            }

            // If we are asking for a List<int>, we should also match for any providers that are bound to the open generic type List<>
            // Currently it only matches one and not the other - not totally sure if this is better than returning both
            if (bindingId.Type.IsGenericType && _providers.TryGetValue(new BindingId(bindingId.Type.GetGenericTypeDefinition(), bindingId.Identifier), out providers))
            {
                return(providers);
            }

            return(Enumerable.Empty <ProviderBase>());
        }
Ejemplo n.º 16
0
        void InstallSingleBinding(Type type, string identifier, ComponentInfo componentInfo)
        {
            var bindingId = new BindingId(type, identifier);

            List <ComponentInfo> infoList;

            if (!_bindings.TryGetValue(bindingId, out infoList))
            {
                infoList = new List <ComponentInfo>();

                _bindings.Add(bindingId, infoList);

                // Note: We only want to register for each unique BindingId once
                // since we return multiple matches in GetAllInstancesWithInjectSplit
                _container.RegisterProvider(bindingId, null, this);
            }

            infoList.Add(componentInfo);
        }
Ejemplo n.º 17
0
 public InjectContext(
     DiContainer container, Type memberType, string identifier, bool optional,
     Type objectType, object objectInstance, string memberName,
     InjectContext parentContext, string concreteIdentifier,
     object fallBackValue, InjectSources sourceType)
 {
     ObjectType         = objectType;
     ObjectInstance     = objectInstance;
     Identifier         = identifier;
     ConcreteIdentifier = concreteIdentifier;
     MemberName         = memberName;
     MemberType         = memberType;
     Optional           = optional;
     FallBackValue      = fallBackValue;
     Container          = container;
     BindingId          = new BindingId(memberType, identifier);
     ParentContext      = parentContext;
     SourceType         = sourceType;
 }
Ejemplo n.º 18
0
        // Returns true the signal was handled
        public bool Trigger(BindingId signalType, object[] args)
        {
            var handlers = GetList(signalType);

            if (handlers.Count == 0)
            {
                return(false);
            }

#if UNITY_EDITOR && ZEN_PROFILING_ENABLED
            using (ProfileBlock.Start("Signal '{0}'", signalType))
#endif
            {
                foreach (var handler in handlers)
                {
                    handler.Execute(args);
                }
            }

            return(true);
        }
Ejemplo n.º 19
0
        SignalDeclaration GetDeclaration(BindingId signalId, bool requireDeclaration)
        {
            SignalDeclaration handler;

            if (_localDeclarationMap.TryGetValue(signalId, out handler))
            {
                return(handler);
            }

            if (_parentBus != null)
            {
                return(_parentBus.GetDeclaration(signalId, requireDeclaration));
            }

            if (requireDeclaration)
            {
                throw Assert.CreateException("Fired undeclared signal '{0}'!", signalId);
            }

            return(null);
        }
Ejemplo n.º 20
0
        // Returns true the signal was handled
        public bool Trigger(BindingId signalType, object[] args)
        {
            var handlers = GetList(signalType);

            if (handlers.IsEmpty())
            {
                return(false);
            }

#if UNITY_EDITOR
            using (ProfileBlock.Start("Signal '{0}'", signalType))
#endif
            {
                foreach (var handler in handlers)
                {
                    handler.Execute(args);
                }
            }

            return(true);
        }
Ejemplo n.º 21
0
        public void RegisterProvider(
            ProviderBase provider, BindingId bindingId)
        {
            if (_providers.ContainsKey(bindingId))
            {
                // Prevent duplicate singleton bindings:
                if (_providers[bindingId].Find(item => ReferenceEquals(item, provider)) != null)
                {
                    throw new ZenjectBindException(
                              "Found duplicate singleton binding for contract '{0}' and id '{1}'".Fmt(bindingId.Type, bindingId.Identifier));
                }

                _providers[bindingId].Add(provider);
            }
            else
            {
                _providers.Add(bindingId, new List <ProviderBase> {
                    provider
                });
            }
        }
Ejemplo n.º 22
0
        public bool Unbind <TContract>(string identifier)
        {
            List <ProviderBase> providersToRemove;
            var bindingId = new BindingId(typeof(TContract), identifier);

            if (_providers.TryGetValue(bindingId, out providersToRemove))
            {
                _providers.Remove(bindingId);

                // Only dispose if the provider is not bound to another type
                foreach (var provider in providersToRemove)
                {
                    if (_providers.Where(x => x.Value.Contains(provider)).IsEmpty())
                    {
                        provider.Dispose();
                    }
                }

                return(true);
            }

            return(false);
        }
Ejemplo n.º 23
0
        private void InternalFire(Type signalType, object signal, object identifier, bool requireDeclaration)
        {
            var signalId = new BindingId(signalType, identifier);

            // Do this before creating the signal so that it throws if the signal was not declared
            var declaration = GetDeclaration(signalId);

            if (declaration == null)
            {
                if (requireDeclaration)
                {
                    throw Assert.CreateException("Fired undeclared signal '{0}'!", signalId);
                }
            }
            else
            {
                if (signal == null)
                {
                    signal = Activator.CreateInstance(signalType);
                }

                declaration.Fire(signal);
            }
        }
Ejemplo n.º 24
0
 public void Unregister(BindingId signalType, ISignalHandler handler)
 {
     GetList(signalType).RemoveWithConfirm(handler);
 }
Ejemplo n.º 25
0
 void SetDefaults()
 {
     _callback    = null;
     _declaration = null;
     _signalId    = new BindingId();
 }
Ejemplo n.º 26
0
 public SignalHandlerBase(
     BindingId signalId, SignalManager manager)
 {
     _manager  = manager;
     _signalId = signalId;
 }
Ejemplo n.º 27
0
        public bool IsSignalDeclared(Type signalType, object identifier)
        {
            var signalId = new BindingId(signalType, identifier);

            return(GetDeclaration(signalId) != null);
        }
Ejemplo n.º 28
0
 public StaticMethodSignalHandler(
     BindingId signalId, SignalManager manager, Action method)
     : base(signalId, manager)
 {
     _method = method;
 }
Ejemplo n.º 29
0
 void UnsubscribeInternal(BindingId signalId, object token, bool throwIfMissing)
 {
     UnsubscribeInternal(
         new SignalSubscriptionId(signalId, token), throwIfMissing);
 }
Ejemplo n.º 30
0
 void SubscribeInternal(BindingId signalId, object token, Action <object> callback)
 {
     SubscribeInternal(
         new SignalSubscriptionId(signalId, token), callback);
 }