public static void Add(object target, IStateTreeNode node)
 {
     if (target != null)
     {
         cache.Add(target, node);
     }
 }
 public static IStateTreeNode GetRoot(this IStateTreeNode target)
 {
     // check all arguments
     if (!target.IsStateTreeNode())
     {
         throw new Exception($"expected first argument to be a mobx-state-tree node, got {target} instead");
     }
     return(target.GetStateTreeNode().Root.StoredValue.GetStateTree());
 }
示例#3
0
        public static IDisposable AddMiddleware(IStateTreeNode target, Action <IMiddlewareEvent, Action <IMiddlewareEvent, Func <object, object> >, Action <object> > handler, bool includeHooks = true)
        {
            var node = target.GetStateTreeNode();

            if (!node.IsProtectionEnabled)
            {
                Console.WriteLine("It is recommended to protect the state tree before attaching action middleware, as otherwise it cannot be guaranteed that all changes are passed through middleware. See `protect`");
            }

            return(node.AddMiddleware(handler, includeHooks));
        }
示例#4
0
        public static List <string> GetTargetTypePath(IStateTreeNode node)
        {
            var            names   = new List <string>();
            IStateTreeNode current = node;

            while (current != null)
            {
                names.Insert(0, GetTypeName(current));
                current = current.HasParent() ? current.GetParent <IStateTreeNode>() : null;
            }
            return(names);
        }
示例#5
0
        public static Func <object[], object> CreateHookInvoker(object target, string name, Func <object[], object> action)
        {
            return((object[] arguments) =>
            {
                IStateTreeNode node = target.GetStateTree();

                if (node != null)
                {
                    return Actions.RunInAction(name, action, new object[] { target }.Concat(arguments).ToArray());
                }

                throw new Exception($"Target does have associated node {target}");
            });
        }
示例#6
0
        public static void Walk(IStateTreeNode target, Action <IStateTreeNode> processor)
        {
            var node = target.GetStateTreeNode();

            foreach (var child in node.GetChildren())
            {
                if (child.StoredValue.IsStateTreeNode())
                {
                    Walk(child.StoredValue.GetStateTree(), processor);
                }
            }

            processor(node.StoredValue.GetStateTree());
        }
示例#7
0
        public static Func <object[], object> CreateActionInvoker(object target, string name, Func <object[], object> action)
        {
            return((object[] arguments) =>
            {
                IStateTreeNode node = target.GetStateTree();

                if (node != null)
                {
                    var id = GetNextActionId();

                    var currentActionContext = CurrentActionContext.IsValueCreated ? CurrentActionContext.Value : null;

                    var context = new MiddlewareEvent
                    {
                        Type = MiddlewareEventType.Action,

                        Name = name,

                        Id = id,

                        Arguments = arguments,

                        Context = node,

                        Target = target,

                        Tree = node.GetRoot(),

                        RootId = currentActionContext?.RootId ?? id,

                        ParentId = currentActionContext?.Id ?? 0
                    };

                    return RunWithActionContext(context, action);
                }

                throw new Exception($"Target does have associated node {target}");
            });
        }
        protected T CreateNewInstance(IMap <string, INode> childNodes, IStateTreeNode meta)
        {
            var observables = Mutables.Select(mutable => new ObservableProperty {
                Type = mutable.Kind, Name = mutable.Name, Default = mutable.Default
            }).ToList();

            var volatiles = Volatiles.Select(xvolatile => new Observable.VolatileProperty {
                Type = xvolatile.Kind, Name = xvolatile.Name, Default = xvolatile.Default
            }).ToList();

            var computeds = Views.Select(view => new ComputedProperty {
                Type = view.Kind, Name = view.Name, Compute = view.View
            }).ToList();

            // var actions = Actions.Select(action => new ActionMethod { Name = action.Name, Action = action.Action });

            ObservableTypeDef typeDef = new ObservableTypeDef(observables, volatiles, computeds);

            var instance = ObservableObject <T, INode> .FromAs(typeDef, Proxify, Name, this, meta);

            return(instance);
        }
 public static bool TryGetValue(object target, out IStateTreeNode node)
 {
     return(cache.TryGetValue(target, out node));
 }
示例#10
0
 public static string GetTypeName(IStateTreeNode node)
 {
     return(node.GetType().Name ?? "(UnnamedType)");
 }
 public static ObjectNode GetStateTreeNode(this IStateTreeNode node, bool throwing = true)
 {
     return(node.TreeNode as ObjectNode);
 }
 public static IType <S, T> GetChildType <S, T>(this IStateTreeNode node, string child)
 {
     return(node.GetStateTreeNode().GetChildType <S, T>(child));
 }
        //public static IType<S, T> GetNodeType<S, T>(this IStateTreeNode node)
        //{
        //    return (IType<S, T>)node.GetStateTreeNode().Type;
        //}

        //public static IType<S, T> GetNodeType<S, T>(this object node)
        //{
        //    return (IType<S, T>)node.GetStateTreeNode().Type;
        //}

        public static IType GetNodeType(this IStateTreeNode node)
        {
            return(node.GetStateTreeNode().Type);
        }
 private IObservableList <INode, T> CreateNewInstance(IMap <string, INode> childNodes, IStateTreeNode meta)
 {
     return(ObservableList <INode, T> .FromIn(ConvertChildNodesToList(childNodes), null, this, meta));
 }
        private IObservableMap <I, INode, T> CreateNewInstance(IMap <string, INode> childNodes, IStateTreeNode meta)
        {
            return(ObservableMap <I, INode, T> .FromIn(childNodes.Aggregate(new Map <I, INode>(), (acc, pair) =>
            {
                acc[Converter(pair.Key)] = pair.Value;
                return acc;
            }), null, this, meta));

            // addHiddenFinalProp(map, "put", put)
        }