public void EvenNumberEventObserver()
        {
            const string EvenNumber = "Even Number";
            var          controller = new Mock <IEventController>();
            int          number     = -1;
            var          observer   = new EventObserver <int, bool>(
                x => {
                if (x)
                {
                    controller.Object.RaiseEvent(EvenNumber, number);
                }
            },
                x => { number = x;  return(number % 2 == 0); }
                );

            var list = Enumerable.Range(1, 10).ToList();

            list.ForEach(x => observer.OnEvent(x)); //notify OnEvent
            controller.Verify(c => c.RaiseEvent(It.IsAny <string>(), It.IsAny <object[]>()), Times.Exactly(5));
            controller.Verify(c => c.RaiseEvent(EvenNumber, 2), Times.Once);
            controller.Verify(c => c.RaiseEvent(EvenNumber, 4), Times.Once);
            controller.Verify(c => c.RaiseEvent(EvenNumber, 6), Times.Once);
            controller.Verify(c => c.RaiseEvent(EvenNumber, 8), Times.Once);
            controller.Verify(c => c.RaiseEvent(EvenNumber, 10), Times.Once);
        }
示例#2
0
    public void UnRegisterEvent(EventEnum eventEnum, EventObserver observer)
    {
        if (!events.ContainsKey(eventEnum))
        {
            return;
        }


        List <EventObserver> list = events[eventEnum];
        bool find = false;

        foreach (EventObserver obj in events[eventEnum])
        {
            if (obj == observer)
            {
                find = true;
                break;
            }
        }
        if (find)
        {
            list.Remove(observer);
        }
        else
        {
            Debug.Log("no Add");
        }
    }
示例#3
0
    public void Remove(EventObserver observer)
    {
        Dictionary <int, EventObserver> obs = null;

        if (observersByMsgCode.TryGetValue(observer.Key, out obs))
        {
            if (!obs.Remove(observer.Id))
            {
                Debuger.LogError(string.Format("删除了不存在的观察者2:{0}", observer.Key));
            }
        }
        else
        {
            Debuger.LogError(string.Format("无效的key:{0}", observer.Key));
        }

        if (!observersById.Remove(observer.Id))
        {
            Debuger.LogError(string.Format("删除了不存在的观察者:{0}", observer.Key));
        }

        if (onRemoveOb != null)
        {
            onRemoveOb(observer);
        }
    }
示例#4
0
 public void VisitObserver(
     EvalObserverFactoryNode factoryNode,
     EvalStateNode stateNode,
     EventObserver eventObserver)
 {
     stateNode.Transfer(services);
 }
示例#5
0
        protected override void Update()
        {
            base.Update();

            EventObserver.SearchVisibleEvent(this);
            DrawGizmo();
        }
        public void RefireThrottledEvents()
        {
            var resetevent = new AutoResetEvent(false);

            var controller = new Mock <IEventController>();
            var observer   = new EventObserver <int, int>(
                x => controller.Object.RaiseEvent(EventX, x),
                (x, y) => x + y
                ).Throttle(TimeSpan.FromMilliseconds(10));

            var list   = Enumerable.Range(1, 10);
            var result = Parallel.ForEach(list, x => observer.OnEvent(x));

            resetevent.WaitOne(250);
            Assert.IsTrue(result.IsCompleted);
            controller.Verify(c => c.RaiseEvent(EventX, It.IsAny <int>()), Times.Once);
            controller.Verify(c => c.RaiseEvent(EventX, list.Sum()), Times.Once);

            var list2 = Enumerable.Range(11, 10); //different range of values

            result = Parallel.ForEach(list2, x => observer.OnEvent(x));
            resetevent.WaitOne(250);
            resetevent.Dispose();
            Assert.IsTrue(result.IsCompleted);
            controller.Verify(c => c.RaiseEvent(EventX, It.IsAny <int>()), Times.Exactly(2));
            controller.Verify(c => c.RaiseEvent(EventX, list2.Sum()), Times.Once); //doesn't contain old values
        }
示例#7
0
        public StateMachineState(AutomatonymousStateMachine <TInstance> machine, string name,
                                 EventObserver <TInstance> observer, State <TInstance> superState = null)
        {
            _machine  = machine;
            _name     = name;
            _observer = observer;

            _behaviors     = new Dictionary <Event, ActivityBehaviorBuilder <TInstance> >();
            _ignoredEvents = new Dictionary <Event, StateEventFilter <TInstance> >();

            Enter = new TriggerEvent(name + ".Enter");
            Ignore(Enter);
            Leave = new TriggerEvent(name + ".Leave");
            Ignore(Leave);

            BeforeEnter = new DataEvent <State>(name + ".BeforeEnter");
            Ignore(BeforeEnter);
            AfterLeave = new DataEvent <State>(name + ".AfterLeave");
            Ignore(AfterLeave);

            _subStates  = new HashSet <State <TInstance> >();
            _superState = superState;

            superState?.AddSubstate(this);
        }
示例#8
0
 public void RegisterAddProductEvent(EventObserver <ProductDto> observer)
 {
     if (_onAddProductObserver == null)
     {
         _onAddProductObserver = observer;
         _onAddProductObserver.Subscribe(_onAddProduct);
     }
 }
 /// <summary>
 /// Evnet를 수신중인 함수 delegate를 삭제합니다.
 /// </summary>
 /// <param name="id">수신중인 event id 종류입니다</param>
 /// <param name="observer">수신중인 함수 delegate입니다</param>
 public static void RemoveObserver(EventID id, EventObserver observer)
 {
     lock (ObserversAddRemoveQueue)
     {
         Map3 <bool, EventID, EventObserver> newEvent = new Map3 <bool, EventID, EventObserver>(false, id, observer);
         ObserversAddRemoveQueue.Enqueue(newEvent);
     }
 }
示例#10
0
 /// <summary>
 /// 移除一个监听者
 /// </summary>
 /// <returns>The remove.</returns>
 /// <param name="removeobj">Removeobj.</param>
 public static void Remove(EventObserver removeobj)
 {
     if (EventManager.instance() == null)
     {
         return;
     }
     EventManager.instance().RemoveObj(removeobj);
 }
示例#11
0
    static int DoAddAll(int msg, int code, object onFire)
    {
        EventObserver observer = IdTypePool <EventObserver> .Get();

        observer.Init(null, msg, code, true, onFire);
        AllNotifier.Add(observer);
        m_observersById.Add(observer.Id, observer);
        return(observer.Id);
    }
示例#12
0
        public async Task SubscribeEvent(string funcName, string argJson, string subscriptionKey, Action <string> onNext, Action onCompleted)
        {
            var observableFactory = _eventMap [funcName];
            var observable        = await observableFactory.Invoke(argJson);

            var observer = new EventObserver(onNext, onCompleted);
            var handle   = observable.Subscribe(observer);

            _subscriptions[subscriptionKey] = handle;
        }
示例#13
0
        public void Fire()
        {
            GameObject smokeInsr      = PooledObjects.Instance.RequestGameObject(GameObjectPool.SmokeGrenade);
            Vector3    deployPosition = agent.transform.position;

            smokeInsr.transform.position = deployPosition;
            EventTrigger smokeBomb = new CustomEvent(deployPosition, EventSubject.SmokeBomb, GameConfig.smokeBombPriotity, GameConfig.smokeBombEnableTime, false);

            EventObserver.Subscribe(smokeBomb);
        }
示例#14
0
 public void SetObserver(EventObserver observer)
 {
     for (int i = 0; i < _rows; i++)
     {
         for (int j = 0; j < _cols; j++)
         {
             _board[i, j].GetComponent <Tile>().SetObserver(observer);
         }
     }
 }
示例#15
0
 /// <summary>
 /// 监听者在这里注册
 /// </summary>
 /// <returns>The register.</returns>
 /// <param name="newobj">Newobj.</param>
 /// <param name="eids">需要监听的事件列表.</param>
 public static void Register(EventObserver newobj, params EventID[] eids)
 {
     if (EventManager.instance() == null)
     {
         return;
     }
     foreach (EventID eid in eids)
     {
         EventManager.instance().RegisterObj(newobj, eid);
     }
 }
示例#16
0
        protected override void Init()
        {
//            Screen.lockCursor = true;
//            Cursor.visible = false;
//
            base.Init();

            InitInventory();
            ChangeState <NullAgentState>();
            EventObserver.Subscribe(this);
        }
示例#17
0
 public override void Start(MatchedEventMap beginState)
 {
     if (InstrumentationHelper.ENABLED)
     {
         InstrumentationHelper.Get().QPatternObserverStart(_evalObserverNode, beginState);
     }
     _eventObserver = _evalObserverNode.FactoryNode.ObserverFactory.MakeObserver(_evalObserverNode.Context, beginState, this, null, null, ParentEvaluator.IsFilterChildNonQuitting);
     _eventObserver.StartObserve();
     if (InstrumentationHelper.ENABLED)
     {
         InstrumentationHelper.Get().APatternObserverStart();
     }
 }
        protected virtual void Awake()
        {
            if (Controllers.ContainsKey(GetType()))
            {
                throw new Exception("Doubled controller: " + GetType());
            }

            EventObserver  = new EventObserver(this);
            MethodObserver = new MethodObserver(this);
            Controllers.Add(GetType(), this);
            OnPreInit  += OnPreInitInvoke;
            Initialized = false;
        }
示例#19
0
        public static void RegisterEventObserver(T event_id, EventObserver observer)
        {
            if (!m_EventObservers.ContainsKey(event_id))
            {
                m_EventObservers.Add(event_id, new List <EventObserver>());
            }
            List <EventObserver> list = m_EventObservers[event_id];

            if (!list.Contains(observer))
            {
                list.Add(observer);
            }
        }
示例#20
0
    public void Add(EventObserver observer)
    {
        Dictionary <int, EventObserver> obs = observersByMsgCode.GetNewIfNo(observer.Key);

        if (obs.Count > 100)
        {
            Debuger.LogError("EventMgr同一个对象的同一个消息的监听者过多,是不是泄露:{0} {1} {2}", observer.msg, observer.code, Util.GetDelegateName(observer.GetDelegate()));
        }


        obs.Add(observer.Id, observer);
        observersById.Add(observer.Id, observer);
    }
示例#21
0
        public static void UnregisterEventObserver(T event_id, EventObserver observer)
        {
            if (!m_EventObservers.ContainsKey(event_id))
            {
                return;
            }
            List <EventObserver> list = m_EventObservers[event_id];

            if (list.Contains(observer))
            {
                list.Remove(observer);
            }
        }
示例#22
0
    //取消监听
    public static void Remove(int observerId)
    {
        if (observerId == Invalid_Id)
        {
            return;
        }
        EventObserver ob = m_observersById.Get(observerId);

        if (ob == null)
        {
            return;
        }
        Remove(ob);
    }
示例#23
0
        public static void Regist(string register, string publisher, Define.ActionTypeEnum registAction, Action <object> callBackAction)
        {
            var currentRegister = RegisterList.FirstOrDefault(s => s.Register == register && s.RegistAction == registAction && s.Publisher == publisher);

            if (currentRegister == null)
            {
                currentRegister = new EventObserver(register, registAction, publisher, callBackAction);
                RegisterList.Add(currentRegister);
            }
            else
            {
                currentRegister.ActionParam = callBackAction;
            }
        }
    public virtual void RegisterObserver(EventObserver observer)
    {
        bool ret;

        observers.Add(observer);
        if (status == Status.On)
        {
            ret = true;
        }
        else
        {
            ret = false;
        }
        observer.SendNotification(ret, this);
    }
示例#25
0
 //移除监听者
 void RemoveObj(EventObserver removeobj)
 {
     foreach (KeyValuePair <EventID, List <EventObserver> > kv in observerList)
     {
         List <EventObserver> list = kv.Value;
         foreach (EventObserver obj in list)
         {
             if (obj == removeobj)
             {
                 list.Remove(obj);
                 break;
             }
         }
     }
 }
 /// <summary>
 /// Event를 수신할 함수를 추가합니다.
 /// </summary>
 /// <param name="id">수신하고자하는 event id 종류입니다</param>
 /// <param name="observer">Event를 수신하고자하는 함수 delegate 입니다</param>
 public static void AddObserver(EventID id, EventObserver observer)
 {
     lock (ObserversAddRemoveQueue)
     {
         foreach (Map3 <bool, EventID, EventObserver> set in ObserversAddRemoveQueue)
         {
             if (set.d2 == id && set.d3.Equals(observer))
             {
                 return;
             }
         }
         Map3 <bool, EventID, EventObserver> newEvent = new Map3 <bool, EventID, EventObserver>(true, id, observer);
         ObserversAddRemoveQueue.Enqueue(newEvent);
     }
 }
示例#27
0
    //监听,onFire返回false表示否决(之后的监听者不执行)
    static int DoAdd(EventNotifier notifier, int msg, int code, object onFire)
    {
        if (notifier == null || !m_notifiersById.ContainsKey(notifier.Id))
        {
            Debuger.LogError(string.Format("观察对象找不到:{0}", msg + code));
            return(Invalid_Id);
        }

        EventObserver observer = IdTypePool <EventObserver> .Get();

        observer.Init(notifier, msg, code, false, onFire);
        notifier.Add(observer);
        AllFireNotifier.Add(observer);
        m_observersById.Add(observer.Id, observer);
        return(observer.Id);
    }
        public void ParallelEventObserver()
        {
            var resetevent = new AutoResetEvent(false);

            var controller = new Mock <IEventController>();
            var observer   = new EventObserver <int, int>(
                x => controller.Object.RaiseEvent(EventX, x),
                (x, y) => x + y
                ).Throttle(TimeSpan.FromMilliseconds(10));

            var list   = Enumerable.Range(1, 10);
            var result = Parallel.ForEach(list, x => observer.OnEvent(x));

            resetevent.WaitOne(250);
            Assert.IsTrue(result.IsCompleted);
            controller.Verify(c => c.RaiseEvent(EventX, It.IsAny <int>()), Times.Once);
            controller.Verify(c => c.RaiseEvent(EventX, 55), Times.Once);
        }
示例#29
0
 //添加监听者
 void RegisterObj(EventObserver newobj, EventID eid)
 {
     if (!observerList.ContainsKey(eid))
     {
         List <EventObserver> list = new List <EventObserver>();
         list.Add(newobj);
         observerList.Add(eid, list);
     }
     else
     {
         List <EventObserver> list = observerList[eid];
         foreach (EventObserver obj in list)
         {
             if (obj == newobj)
             {
                 return;
             }
         }
         list.Add(newobj);
     }
 }
示例#30
0
        public override void Start(MatchedEventMap beginState)
        {
            var agentInstanceContext = evalObserverNode.Context.AgentInstanceContext;
            agentInstanceContext.InstrumentationProvider.QPatternObserverStart(
                evalObserverNode.factoryNode,
                beginState);
            agentInstanceContext.AuditProvider.PatternInstance(
                true,
                evalObserverNode.factoryNode,
                agentInstanceContext);

            eventObserver = evalObserverNode.FactoryNode.ObserverFactory.MakeObserver(
                Context,
                beginState,
                this,
                null,
                ParentEvaluator.IsFilterChildNonQuitting);
            eventObserver.StartObserve();

            agentInstanceContext.InstrumentationProvider.APatternObserverStart();
        }
 public SomeView()
 {
     Observer = new EventObserver();
 }