예제 #1
0
        /// <summary>
        /// Associate a property with an object
        /// </summary>
        /// <param name="target">The target object</param>
        /// <param name="property">The name of the property. Usually is a string</param>
        /// <param name="value">The value to be associated with 'target' as 'property'</param>
        public static void set(object target, object property, object value)
        {
            if (!ObjectsStates.ContainsKey(target))
            {
                ObjectsStates[target] = new Dictionary <object, VarValue>();
            }

            if (!ObjectsStates[target].ContainsKey(property))
            {
                ObjectsStates[target][property] = new VarValue();
            }

            ObjectsStates[target][property].AsRaw = value;

            __notifyObservers(target, property, ObjectsStates[target][property]);
        }
예제 #2
0
        private static void __notifyObservers(object target, object property, VarValue newValue)
        {
            if (ObjectsStates.ContainsKey(target))
            {
                if (ObjectsStates[target].ContainsKey("__observers__"))
                {
                    Dictionary <object, List <SMObjserver> > observers = (Dictionary <object, List <SMObjserver> >)ObjectsStates[target]["__observers__"].AsRaw;

                    if (observers.ContainsKey(property))
                    {
                        for (int c = 0; c < observers[property].Count; c++)
                        {
                            observers[property][c](newValue);
                        }
                    }
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Observe an associated property with an object.
        /// </summary>
        /// <param name="target">The target object</param>
        /// <param name="property">The name of the property. Usually is a string</param>
        /// <param name="action">The action to be executed every time that 'property' is changed</param>
        public static void observe(object target, object property, SMObjserver action)
        {
            if (!ObjectsStates.ContainsKey(target))
            {
                ObjectsStates[target] = new Dictionary <object, VarValue>();
            }

            if (!ObjectsStates[target].ContainsKey("__observers__"))
            {
                ObjectsStates[target]["__observers__"] = new VarValue
                {
                    AsRaw = new Dictionary <object, List <SMObjserver> >()
                };
            }

            Dictionary <object, List <SMObjserver> > observers = (Dictionary <object, List <SMObjserver> >)ObjectsStates[target]["__observers__"].AsRaw;

            if (!observers.ContainsKey(property))
            {
                observers[property] = new List <SMObjserver>();
            }

            observers[property].Add(action);
        }