public void TestStringRoute() { counter = 0; var msg = new Msg { Content = MagicString }; var route = "Users/NVenti"; ObjectEvents <string, Msg> .Subscribe(route, Handler); ObjectEvents <string, Msg> .Publish(route, msg); ObjectEvents.Publish(route, msg); ObjectEvents.Publish(route, msg, typeof(string), typeof(Msg)); Assert.AreEqual(counter, 3); //bad route ObjectEvents.Publish(route + route, msg, typeof(string), typeof(Msg)); ObjectEvents <string, Msg> .Unsubscribe(route, Handler); ObjectEvents <string, Msg> .Publish(route, msg); Assert.AreEqual(counter, 3); ObjectEvents <string, Msg> .Subscribe(route, Handler); ObjectEvents <string, Msg> .Unsubscribe(route); ObjectEvents <string, Msg> .Publish(route, msg); Assert.AreEqual(counter, 3); }
void Start() { TestMethod.RunAll(this, () => { counter = 0; ObjectEvents <string, Msg> .Clear(); ObjectEvents <GameObject, Msg> .Clear(); }); }
/// <summary> /// check if an entry exist in ObjectEventsCollection, if not - create it /// </summary> /// <param name = "o"></param> /// <returns></returns> internal ObjectEvents checkAndCreateObjectEvents(Object o) { ObjectEvents objectEvents = getObjectEvents(o); if (objectEvents == null) { objectEvents = new ObjectEvents(); objectEvents.Object = o; _objectEventsCollection.Add(o.GetHashCode(), objectEvents); } // increase the var ref count by one. objectEvents.VarRefCount++; return(objectEvents); }
/// <summary> adds events to 'obj'</summary> /// <param name="obj"></param> /// <param name="dnEventsNames">comma-delimited string of events that should be raised for the object</param> public void addEvents(object obj, String[] dnEventsNames) { if (obj != null) { // Even if there is no event to this object, add an entry into ObjectsEventsHashTable. // So that we know the references to this object, when we do add events to this obj ObjectEvents objectEvents = checkAndCreateObjectEvents(obj); if (dnEventsNames != null) { foreach (string item in dnEventsNames) { ReflectionServices.addHandler(item, obj, objectEvents, true); } } } }
/// <summary> remove events to 'obj'</summary> /// <param name="obj"></param> public void removeEvents(object obj) { if (obj != null) { // get the objectEvent in the ObjectsEventsHashTable ObjectEvents objectEvents = getObjectEvents(obj); if (objectEvents != null) { // decrease ref count by one objectEvents.VarRefCount--; if (objectEvents.VarRefCount <= 0) { Remove(obj); } } } }
public void TestObjectRoute() { //define out message, a class var msg = new Msg { Content = MagicString }; //define our routes (strings or game objects or something else) var route = new GameObject(); var route2 = new GameObject(); //subscribe using the object reference ObjectEvents <GameObject, Msg> .Subscribe(route, Handler); //many ways to send ObjectEvents <GameObject, Msg> .Publish(route, msg); ObjectEvents.Publish(route, msg); ObjectEvents.Publish(route, msg, typeof(GameObject), typeof(Msg)); Assert.AreEqual(counter, 3); //bad route ObjectEvents.Publish(route2, msg, typeof(GameObject), typeof(Msg)); //be sure to clean up as we are not using weak references ObjectEvents <GameObject, Msg> .Unsubscribe(route, Handler); ObjectEvents <GameObject, Msg> .Publish(route, msg); Assert.AreEqual(counter, 3); ObjectEvents <GameObject, Msg> .Subscribe(route, Handler); ObjectEvents <GameObject, Msg> .Unsubscribe(route); ObjectEvents <GameObject, Msg> .Publish(route, msg); Assert.AreEqual(counter, 3); }
/// <summary> /// remove an entry from ObjectEventsCollection /// </summary> /// <param name = "o"></param> private void Remove(Object o) { ObjectEvents objectEvents = getObjectEvents(o); if (objectEvents != null) { foreach (var item in objectEvents.Delegates) { //remove handler ReflectionServices.removeHandler(o, item.Key, item.Value); } objectEvents.Delegates.Clear(); if (objectEvents.DNControlValueChangedDelegate != null) { ReflectionServices.removeHandler(o, objectEvents.DNControlValueChangedEventName, objectEvents.DNControlValueChangedDelegate); } } if (_objectEventsCollection.ContainsKey(o.GetHashCode())) { _objectEventsCollection.Remove(o.GetHashCode()); } }
public void TestObjectRoute() { counter = 0; //define out message, a class var msg = new Msg { Content = MagicString }; //subscribe using the object reference ObjectEvents <SpaceShip, Msg> .Subscribe(route, Handler); //many ways to send ObjectEvents <SpaceShip, Msg> .Publish(route, msg); ObjectEvents.Publish(route, msg); ObjectEvents.Publish(route, msg, typeof(SpaceShip), typeof(Msg)); Assert.AreEqual(counter, 3); //bad route ObjectEvents.Publish(route2, msg, typeof(SpaceShip), typeof(Msg)); //be sure to clean up as we are not using weak references ObjectEvents <SpaceShip, Msg> .Unsubscribe(route, Handler); ObjectEvents <SpaceShip, Msg> .Publish(route, msg); Assert.AreEqual(counter, 3); ObjectEvents <SpaceShip, Msg> .Subscribe(route, Handler); ObjectEvents <SpaceShip, Msg> .Unsubscribe(route); ObjectEvents <SpaceShip, Msg> .Publish(route, msg); Assert.AreEqual(counter, 3); }
public ObjectModel(int number, int hp) { Events = new ObjectEvents(); StartHP = CurrentHP = hp; }