Exemplo n.º 1
0
        public ISubscriptionGen AddSubscription(ISubscriptionKeyGen subscriptionRequest, out bool wasAdded)
        {
            if (subscriptionRequest.HasBeenUsed)
            {
                throw new ApplicationException("Its alread been used.");
            }

            SubscriberCollection sc = GetSubscriptions((SimpleExKey)subscriptionRequest.SourcePropRef);

            bool internalWasAdded = false;

            ISubscriptionGen result = sc.GetOrAdd
                                      (
                subscriptionRequest,
                (
                    x => subscriptionRequest.CreateSubscription()
                )
                                      );

            if (subscriptionRequest.HasBeenUsed)
            {
                System.Diagnostics.Debug.WriteLine($"Created a new Subscription for Property:" +
                                                   $" {subscriptionRequest.SourcePropRef} / Event: {result.SubscriptionKind}.");
            }
            else
            {
                System.Diagnostics.Debug.WriteLine($"The subscription for for Property:" +
                                                   $" {subscriptionRequest.SourcePropRef} / Event: {result.SubscriptionKind} was not added.");
            }

            wasAdded = internalWasAdded;
            return(result);
        }
Exemplo n.º 2
0
        private ISubscription CreateSubscriptionGen(ISubscriptionKeyGen subscriptionRequestGen, IProvideHandlerDispatchDelegateCaches handlerDispatchDelegateCacheProvider)
        {
            ISubscription result = new SubscriptionGen(subscriptionRequestGen, handlerDispatchDelegateCacheProvider);

            subscriptionRequestGen.MarkAsUsed();
            return(result);
        }
Exemplo n.º 3
0
        private bool BindingIsForRequest(ISubscription binding, ISubscriptionKeyGen bindingRequest)
        {
            bool result =
                binding.OwnerPropId == bindingRequest.OwnerPropId &&
                binding.BindingInfo == bindingRequest.BindingInfo;

            return(result);
        }
Exemplo n.º 4
0
        public bool TryGetBinding(ISubscriptionKeyGen bindingRequest, out ISubscription binding)
        {
            lock (_sync)
            {
                binding = _bindings.FirstOrDefault(x => BindingIsForRequest(x, bindingRequest));
            }
            bool result = binding != null;

            return(result);
        }
Exemplo n.º 5
0
        public bool TryGetSubscription(ISubscriptionKeyGen request, out ISubscription subscription)
        {
            lock (_sync)
            {
                subscription = _subs.FirstOrDefault(x => SubscriptionIsForRequest(x, request));
            }

            bool result = subscription != null;

            return(result);
        }
Exemplo n.º 6
0
        public bool RemoveSubscription(ISubscriptionKeyGen subscriptionRequest)
        {
            SubscriberCollection sc = GetSubscriptions(subscriptionRequest.SourcePropRef);
            bool result             = sc.RemoveSubscription(subscriptionRequest);

            if (result)
            {
                System.Diagnostics.Debug.WriteLine($"Removed the subscription for {subscriptionRequest.SourcePropRef}.");
            }

            return(result);
        }
Exemplo n.º 7
0
        private bool SubscriptionIsForRequest(ISubscription subscription, ISubscriptionKeyGen subscriptionRequest)
        {
            // Helpful for debugging.
            //bool t1 = subscription.OwnerPropId.Equals(subscriptionRequest.OwnerPropId);
            //bool t2 = subscription.MethodName == subscriptionRequest.Method.Name;
            //bool t3 = ReferenceEquals(subscription.Target.Target, subscriptionRequest.Target);

            bool result =
                subscription.OwnerPropId.Equals(subscriptionRequest.OwnerPropId) &&
                subscription.MethodName == subscriptionRequest.Method.Name &&
                //ReferenceEquals(subscription.Target_Wrk.Target, subscriptionRequest.Target);
                subscription.Target_Wrk == subscriptionRequest.Target_Wrk;

            return(result);
        }
Exemplo n.º 8
0
        //public bool AddBinding(ISubscriptionGen binding)
        //{
        //    lock (_sync)
        //    {
        //        if (ContainsBinding(binding))
        //        {
        //            return false;
        //        }
        //        else
        //        {
        //            _bindings.Add(binding);
        //            return true;
        //        }
        //    }
        //}

        public bool TryRemoveBinding(ISubscriptionKeyGen bindingRequest, out ISubscription binding)
        {
            lock (_sync)
            {
                if (TryGetBinding(bindingRequest, out binding))
                {
                    _bindings.Remove(binding);
                    return(true);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine($"Could not find the binding for {bindingRequest.OwnerPropId} <= {bindingRequest.BindingInfo.PropertyPath.Path} when trying to remove it.");
                    return(false);
                }
            }
        }
Exemplo n.º 9
0
        public ISubscription GetOrAdd(ISubscriptionKeyGen bindingRequest, Func <ISubscriptionKeyGen, ISubscription> factory)
        {
            lock (_sync)
            {
                if (TryGetBinding(bindingRequest, out ISubscription binding))
                {
                    System.Diagnostics.Debug.WriteLine($"The binding for {bindingRequest.OwnerPropId} has aleady been created.");
                    return(binding);
                }
                else
                {
                    binding = factory(bindingRequest);
                    _bindings.Add(binding);

                    return(binding);
                }
            }
        }
Exemplo n.º 10
0
        public bool TryRemoveSubscription(ISubscriptionKeyGen request)
        {
            ISubscription subscription;

            lock (_sync)
            {
                if (null != (subscription = _subs.FirstOrDefault(x => SubscriptionIsForRequest(x, request))))
                {
                    _subs.Remove(subscription);
                    _serial++;
                    return(true);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine($"Could not find the subscription for {request.OwnerPropId} when trying to remove it.");
                    return(false);
                }
            }
        }
Exemplo n.º 11
0
        public ISubscription GetOrAdd(ISubscriptionKeyGen request, Func <ISubscriptionKeyGen, ISubscription> factory)
        {
            ISubscription subscription;

            lock (_sync)
            {
                if (null != (subscription = _subs.FirstOrDefault(x => SubscriptionIsForRequest(x, request))))
                {
                    System.Diagnostics.Debug.WriteLine($"The subscription for {request.OwnerPropId} has aleady been created.");
                    return(subscription);
                }
                else
                {
                    subscription = factory(request);
                    AddSubscription(subscription);
                    _serial++;
                    return(subscription);
                }
            }
        }
Exemplo n.º 12
0
 private static ISubscription CreateSubscriptionGen(ISubscriptionKeyGen subscriptionRequestGen, IProvideHandlerDispatchDelegateCaches handlerDispatchDelegateCacheProvider)
 {
     return((ISubscription)CreateSubscription((ISubscriptionKey <T>)subscriptionRequestGen, handlerDispatchDelegateCacheProvider));
 }
Exemplo n.º 13
0
 public static ISubscription CreateBindingGen(ISubscriptionKeyGen bindingRequestGen, PSAccessServiceInterface propStoreAccessService)
 {
     return((ISubscription)CreateBinding((IBindingSubscriptionKey <T>)bindingRequestGen, propStoreAccessService));
 }
Exemplo n.º 14
0
        public bool ContainsBinding(ISubscriptionKeyGen bindingRequest)
        {
            bool result = _bindings.Exists(x => BindingIsForRequest(x, bindingRequest));

            return(result);
        }
Exemplo n.º 15
0
        public SubscriptionGen(ISubscriptionKeyGen subRequestKey, IProvideHandlerDispatchDelegateCaches handlerDispatchDelegateCacheProvider)
        {
            if (subRequestKey.HasBeenUsed)
            {
                throw new InvalidOperationException("The Key has already been used.");
            }

            OwnerPropId  = subRequestKey.OwnerPropId;
            PropertyType = subRequestKey.PropertyType;

            SubscriptionKind          = subRequestKey.SubscriptionKind;
            SubscriptionPriorityGroup = subRequestKey.SubscriptionPriorityGroup;
            //SubscriptionTargetKind = sKey.SubscriptionTargetKind;

            switch (SubscriptionKind)
            {
            case SubscriptionKind.TypedHandler:
            {
                Target_Wrk = subRequestKey.Target_Wrk;         // new WeakRefKey(subRequestKey.Target);

                Delegate proxyDelegate = handlerDispatchDelegateCacheProvider.DelegateProxyCache.GetOrAdd(new MethodSubscriptionKind(subRequestKey.Method, subRequestKey.SubscriptionKind));
                HandlerProxy = proxyDelegate;

                Type targetType = subRequestKey.Target.GetType();

                TypePair tp = new TypePair(targetType, PropertyType);

                CallPcTypedEventSubscriberDelegate callTheListenerDel =
                    handlerDispatchDelegateCacheProvider.CallPcTypedEventSubsCache.GetOrAdd(tp);

                PcTypedHandlerDispatcher = callTheListenerDel;

                MethodName = HandlerProxy.Method.Name;
                break;
            }

            case SubscriptionKind.GenHandler:
            {
                Target_Wrk = subRequestKey.Target_Wrk;         // new WeakRefKey(subRequestKey.Target);

                Delegate proxyDelegate = handlerDispatchDelegateCacheProvider.DelegateProxyCache.GetOrAdd(new MethodSubscriptionKind(subRequestKey.Method, subRequestKey.SubscriptionKind));
                HandlerProxy = proxyDelegate;

                //Type targetType = sKey.GenHandler.Target.GetType();
                Type targetType = subRequestKey.Target.GetType();

                PcGenHandlerDispatcher = handlerDispatchDelegateCacheProvider.CallPcGenEventSubsCache.GetOrAdd(targetType);

                MethodName = HandlerProxy.Method.Name;
                break;
            }

            case SubscriptionKind.ObjHandler:
            {
                Target_Wrk = subRequestKey.Target_Wrk;         // new WeakRefKey(subRequestKey.Target);

                Delegate proxyDelegate = handlerDispatchDelegateCacheProvider.DelegateProxyCache.GetOrAdd(new MethodSubscriptionKind(subRequestKey.Method, subRequestKey.SubscriptionKind));
                HandlerProxy = proxyDelegate;

                //Type targetType = sKey.ObjHandler.Target.GetType();
                Type targetType = subRequestKey.Target.GetType();

                PcObjHandlerDispatcher = handlerDispatchDelegateCacheProvider.CallPcObjEventSubsCache.GetOrAdd(targetType);

                MethodName = HandlerProxy.Method.Name;
                break;
            }

            case SubscriptionKind.StandardHandler:
            {
                Target_Wrk = subRequestKey.Target_Wrk;         // new WeakRefKey(subRequestKey.Target);

                Delegate proxyDelegate = handlerDispatchDelegateCacheProvider.DelegateProxyCache.GetOrAdd(new MethodSubscriptionKind(subRequestKey.Method, subRequestKey.SubscriptionKind));
                HandlerProxy = proxyDelegate;

                //Type targetType = sKey.StandardHandler.Target.GetType();
                Type targetType = subRequestKey.Target.GetType();

                PcStandardHandlerDispatcher = handlerDispatchDelegateCacheProvider.CallPcStEventSubsCache.GetOrAdd(targetType);

                MethodName = HandlerProxy.Method.Name;
                break;
            }

            case SubscriptionKind.ChangingHandler:
            {
                Target_Wrk = subRequestKey.Target_Wrk;         // new WeakRefKey(subRequestKey.Target);

                Delegate proxyDelegate = handlerDispatchDelegateCacheProvider.DelegateProxyCache.GetOrAdd(new MethodSubscriptionKind(subRequestKey.Method, subRequestKey.SubscriptionKind));
                HandlerProxy = proxyDelegate;

                //Type targetType = sKey.ChangingHandler.Target.GetType();
                Type targetType = subRequestKey.Target.GetType();

                PChangingHandlerDispatcher = handlerDispatchDelegateCacheProvider.CallPChangingEventSubsCache.GetOrAdd(targetType);

                MethodName = HandlerProxy.Method.Name;
                break;
            }

            //case SubscriptionKind.TypedAction:
            //    break;
            //case SubscriptionKind.ObjectAction:
            //    break;
            //case SubscriptionKind.ActionNoParams:
            //    break;
            //case SubscriptionKind.LocalBinding:
            //    break;
            default:
                throw new InvalidOperationException($"The SubscriptionKind: {SubscriptionKind} is not recognized or is not supported.");
            }

            //GenDoWhenChanged = sKey.GenDoWhenChanged;
            //Action = sKey.Action;
        }
Exemplo n.º 16
0
 public Unsubscriber(WeakReference <PSAccessServiceInterface> wr_us, ISubscriptionKeyGen request)
 {
     _propStoreAccessService_Wr = wr_us;
     _request = request;
 }