private void CheckTimeout(Stopwatch sw, EventListenerOptions options)
 {
     if (sw.ElapsedMilliseconds > options.Timeout)
     {
         throw new TimeoutException($"The event was not fired during the timeout period ({options.Timeout}ms).");
     }
 }
        protected async Task <TArgs> ListenForEventAsync <TArgs>(Action <EventHandler <TArgs> > assigner, Action <EventListenerOptions> optionsBuilder = null)
        {
            var options = new EventListenerOptions();

            optionsBuilder?.Invoke(options);

            var   resolved  = false;
            TArgs eventArgs = default;

            EventHandler <TArgs> handler = (_, args) =>
            {
                resolved  = true;
                eventArgs = args;
            };

            assigner(handler);

            var sw = Stopwatch.StartNew();

            do
            {
                CheckTimeout(sw, options);
                await Task.Delay(options.Interval);
            }while (!resolved);

            return(eventArgs);
        }
Beispiel #3
0
 public bool removeEventListener(string eventType, EventListener listener, EventListenerOptions options)
 {
     lock (this)
     {
         if (_eventEntries.TryGetValue(eventType, out var eventEntry))
         {
             var r = eventEntry.RemoveAll(x => x.options.capture == options.capture) > 0;
             if (eventEntry.Count == 0)
             {
                 _eventEntries.Remove(eventType);
             }
             return(r);
         }
     }
     return(false);
 }
Beispiel #4
0
 public bool addEventListener(string eventType, EventListener listener, EventListenerOptions options)
 {
     lock (this)
     {
         if (_eventEntries.TryGetValue(eventType, out var eventEntry))
         {
             if (eventEntry.Any(x => x.options.capture == options.capture))
             {
                 return(false);
             }
             _eventEntries[eventType].Add(new EventEntry(listener, options));
             return(true);
         }
         _eventEntries[eventType] = new List <EventEntry> {
             new EventEntry(listener, options)
         };
         return(true);
     }
 }
        protected async Task ListenForEventAsync(Action <EventHandler> assigner, Action <EventListenerOptions> optionsBuilder = null)
        {
            var options = new EventListenerOptions();

            optionsBuilder?.Invoke(options);

            var resolved = false;

            EventHandler handler = (_, _) => resolved = true;

            assigner(handler);

            var sw = Stopwatch.StartNew();

            do
            {
                CheckTimeout(sw, options);
                await Task.Delay(options.Interval);
            }while (!resolved);
        }
Beispiel #6
0
        /// <summary>
        /// Registers new event handler.
        /// </summary>
        /// <param name="type">The type name of the event.</param>
        /// <param name="handler">The event handler.</param>
        /// <param name="options">An options object that specifies characteristics about the event listener. </param>
        public void AddEventListener(string type, Action <Event> handler, EventListenerOptions options)
        {
            if (handler == null)
            {
                return;
            }

            if (options == null)
            {
                AddEventListener(type, handler);
                return;
            }

            var listenersList = options.Capture ? GetCapturingListeners(type) : GetBubblingListeners(type);

            if (listenersList.Any(x => x.Handler == handler))            //do not add listener twice
            {
                return;
            }
            listenersList.Add(new Listener {
                Handler = handler, Options = options
            });
        }
Beispiel #7
0
 public EventEntry(EventListener l, EventListenerOptions o)
 {
     listener = l; options = o;
 }
Beispiel #8
0
 /// <summary>
 /// Removes previously registered event handler.
 /// </summary>
 /// <param name="type">The type name of event.</param>
 /// <param name="listener">The handler to be removed.</param>
 /// <param name="options">The options with which the listener was added.</param>
 public void RemoveEventListener(string type, Action <Event> listener, EventListenerOptions options) =>
 EventTarget.RemoveEventListener(type, listener, options);
Beispiel #9
0
 public void RemoveEventListener(string type, Action <Event> listener, EventListenerOptions options) =>
 RemoveEventListener(type, listener, options.Capture);