Ejemplo n.º 1
0
        protected virtual void RegisterInternal(OperationType operation, [NotNull] object target,
                                                [NotNull] IOperationCallback callback, [NotNull] IDataContext context)
        {
            CallbackDictionary callbacks;
            var viewModel = target as IViewModel;

            if (viewModel != null && callback.IsSerializable)
            {
                lock (_locker)
                {
                    if (!viewModel.Settings.State.TryGetData(CallbackConstant, out callbacks))
                    {
                        callbacks = new CallbackDictionary();
                        viewModel.Settings.State.Add(CallbackConstant, callbacks);
                    }
                }
            }
            else
            {
                callbacks = ServiceProvider
                            .AttachedValueProvider
                            .GetOrAdd(target, CallbacksMember, (o, o1) => new CallbackDictionary(), null);
            }
            RegisterInternal(callbacks, operation.Id, callback);
        }
Ejemplo n.º 2
0
        private static void RegisterInternal(CallbackDictionary callbacks, string id, IOperationCallback callback)
        {
            //Only for debug callback
            if (AlwaysSerializeCallback && callback.IsSerializable)
            {
                ISerializer serializer;
                if (ServiceProvider.IocContainer.TryGet(out serializer))
                {
                    var stream = serializer.Serialize(callback);
                    stream.Position = 0;
                    callback        = (IOperationCallback)serializer.Deserialize(stream);
                }
            }

            lock (callbacks)
            {
                List <object> list;
                if (!callbacks.TryGetValue(id, out list))
                {
                    list          = new List <object>();
                    callbacks[id] = list;
                }
                list.Add(callback);
            }
        }
Ejemplo n.º 3
0
        private static List <string> InvokeCallbacks <T>(CallbackDictionary dictionary, T target, Func <OperationType, object, IOperationResult> getResult, Action <T> clearDictAction)
        {
            List <string> invoked = null;

            KeyValuePair <string, List <object> >[] keyValuePairs;
            lock (dictionary)
                keyValuePairs = dictionary.ToArray();
            foreach (var pair in keyValuePairs)
            {
                var op     = new OperationType(pair.Key);
                var result = getResult(op, target);
                if (result == null)
                {
                    continue;
                }

                List <IOperationCallback> list = null;
                InitializeCallbacks(dictionary, op, ref list, target, clearDictAction);
                if (list == null)
                {
                    continue;
                }
                foreach (var callback in list)
                {
                    callback.Invoke(result);
                }
                if (invoked == null)
                {
                    invoked = new List <string>();
                }
                invoked.Add(pair.Key);
            }
            return(invoked);
        }
Ejemplo n.º 4
0
        private static void InitializeCallbacks <T>(CallbackDictionary dictionary, OperationType type, ref List <IOperationCallback> list, T target, Action <T> clearDictAction)
        {
            if (dictionary == null)
            {
                return;
            }
            List <object> value;

            lock (dictionary)
            {
                if (dictionary.TryGetValue(type.Id, out value))
                {
                    dictionary.Remove(type.Id);
                }
                if (dictionary.Count == 0)
                {
                    clearDictAction(target);
                }
            }
            if (value == null)
            {
                return;
            }
            if (list == null)
            {
                list = new List <IOperationCallback>();
            }
            lock (value)
                list.AddRange(value.Cast <IOperationCallback>());
        }
Ejemplo n.º 5
0
        private void RegisterInternal(CallbackDictionary callbacks, string id, IOperationCallback callback)
        {
            //Only for debug
            if (AlwaysSerializeCallback && callback.IsSerializable)
            {
                var stream = _serializer.Serialize(callback);
                stream.Position = 0;
                callback        = (IOperationCallback)_serializer.Deserialize(stream);
            }

            List <object> list;

            lock (callbacks)
            {
                if (!callbacks.TryGetValue(id, out list))
                {
                    list          = new List <object>();
                    callbacks[id] = list;
                }
            }
            lock (list)
                list.Add(callback);
        }