Add event listener to event loop.

@param $fd
@param $flag
@param $func
@param array $args
@return bool
14545
public function add($fd, $flag, $func, $args = array())
    {
        $args = (array)$args;
        switch ($flag) {
            case EventInterface::EV_READ:
                return $this->addReadStream($fd, $func);
            case EventInterface::EV_WRITE:
                return $this->addWriteStream($fd, $func);
            case EventInterface::EV_SIGNAL:
                return $this->addSignal($fd, $func);
            case EventInterface::EV_TIMER:
                $timer_id = ++$this->_timerIdIndex;
                $timer_obj = $this->addPeriodicTimer($fd, function() use ($func, $args) {
                    call_user_func_array($func, $args);
                });
                $this->_timerIdMap[$timer_id] = $timer_obj;
                return $timer_id;
            case EventInterface::EV_TIMER_ONCE:
                $timer_id = ++$this->_timerIdIndex;
                $timer_obj = $this->addTimer($fd, function() use ($func, $args, $timer_id) {
                    unset($this->_timerIdMap[$timer_id]);
                    call_user_func_array($func, $args);
                });
                $this->_timerIdMap[$timer_id] = $timer_obj;
                return $timer_id;
        }
        return false;
    }
661899
public static function trigger(string $name, ?array $args = null)
  {
    if (!isset(self::$events[$name])) return false;

    foreach (self::$events[$name] as $event => $callback) {
      if (is_callable($callback)) {
        return call_user_func_array($callback, isset($args) ? $args : []);
      }
      else if (is_array($callback)) {
        (new Events)->triggerGroup($callback, $args);
      }
      else if (is_string($callback)) {
        $call = new $callback;
        return call_user_func_array([$call, 'persist'], $args ?? []);
      }
    }
  }
20245
public function sort(callable $func = null, callable $ufunc = null, int $flags = 0): self
    {
        Arrays::sort($this->data, $func, $ufunc, $flags);

        return $this;
    }
85787
public function triggerEvent($event, $args = [])
    {
        if (!($event instanceof EventInterface)) {
            if (isset($this->events[$event])) {
                $event = $this->events[$event];
            } else {
                $event = new Event($event);
            }
        }

        $event->mergeArguments($args);

        if (isset($this->listeners[$event->getName()])) {
            foreach ($this->listeners[$event->getName()] as $listener) {
                if ($event->isStopped()) {
                    return $event;
                }

                if (is_callable($listener)) {
                    call_user_func($listener, $event);
                } else {
                    call_user_func([$listener, $event->getName()], $event);
                }
            }
        }

        return $event;
    }
949357
protected function triggerParamsMergeEvent($event, $argv = array(), $callback = null)
    {
        $eventRet = $this->triggerEvent($event, $argv, $callback);
        foreach ($eventRet as $event) {
            if (is_array($event) || $event instanceof Traversable) {
                $argv = array_merge_recursive($argv, $event);
            }
        }

        return $argv;
    }
712202
function relay($funcs,$args=NULL) {
		foreach (is_array($funcs)?$funcs:$this->split($funcs) as $func)
			$args=[$this->call($func,$args)];
		return array_shift($args);
	}
963557
public function fire($name, I_Registry $args, $fireAgain = false)
    {
        $uid = uniqid('');
        if ($this->debug && !in_array($name, self::$debugEvents)) {
            $debugArgs = new Args([
                'uid'    => $uid,
                'name'   => $name,
                'args'   => $args,
                'source' => 'core:event:debug',
                'level'  => E_NOTICE,
            ]);
            $this->fire(':onEventStart:', $debugArgs);
            unset($debugArgs);
        }
        if (isset($this->handlers[$name]) && empty($this->disabledEvents[$name])) {
            if (!$fireAgain && isset($this->firedEvents[$name])) {
                throw new RuntimeException(
                    sprintf(
                        "Event '%s' is fired already",
                        $name
                    )
                );
            } else {
                $this->firedEvents[$name] = true;
                if (!isset($this->orderedHandlers[$name])) {
                    $this->orderedHandlers[$name] = $this->handlers[$name];
                    $this->sortEvents($name);
                }
                foreach (array_keys($this->orderedHandlers[$name]) as $index) {
                    if (empty($this->orderedHandlers[$name][$index][1])) {
                        // Targeted event, not for this target
                        continue;
                    }
                    // Call handler
                    $callback = $this->orderedHandlers[$name][$index][1];
                    if (!is_callable($callback)) {
                        unset($this->firedEvents[$name]);
                        if (is_array($callback)) {
                            $callback =
                                (
                                    is_object($callback[0])
                                        ? get_class($callback[0]) . '->'
                                        : $callback[0] . '::'
                                ) .
                                $callback[1];
                        } else {
                            $callback = sprintf("function %s", $callback);
                        }

                        throw new RuntimeException(
                            sprintf(
                                "Invalid event handler %s() added to process '%s' event",
                                $callback,
                                $name
                            )
                        );
                    }

                    if ($this->debug && !in_array($name, self::$debugEvents)) {
                        $debugArgs = new Args([
                            'uid'     => $uid,
                            'name'    => $name,
                            'handler' => $this->orderedHandlers[$name][$index][1],
                            'args'    => $args,
                            'source'  => 'core:event:debug',
                            'level'   => E_NOTICE,
                        ]);
                        $this->fire(':onHandlerFound:', $debugArgs);
                    }
                    call_user_func(
                        $this->orderedHandlers[$name][$index][1],
                        $name,
                        $args
                    );
                    if (!$args->isEmpty(':break:')) {
                        break;
                    }
                }
                unset($this->firedEvents[$name]);
            }
        }
        if ($this->debug && !in_array($name, self::$debugEvents)) {
            $debugArgs = new Args([
                'uid'    => $uid,
                'name'   => $name,
                'args'   => $args,
                'source' => 'core:event:debug',
                'level'  => $args->get(':break:', false) ? E_WARNING : E_NOTICE,
            ]);
            $this->fire(':onEventEnd:', $debugArgs);
        }
    }
905480
function doOnce(fn) {
    var called = false;
    return function() {
      if (called) throw new Error("Callback already called.");
      called = true;
      fn.apply(root, arguments);
    };
  }
775866
public function trigger(EventInterface $event, array $args = []){
        return call_user_func_array($this->callback, array_merge([$event], $args));
    }
792724
function onQuickFormEvent($event, $arg, &$caller)
    {
        switch ($event) {
            case 'updateValue':
                if ($caller->getAttribute('method') == 'get') {
                    return self::raiseError('Cannot add a file upload field to a GET method form');
                }
                $this->_value = $this->_findValue();
                $caller->updateAttributes(array('enctype' => 'multipart/form-data'));
                $caller->setMaxFileSize();
                break;
            case 'addElement':
                $this->onQuickFormEvent('createElement', $arg, $caller);
                return $this->onQuickFormEvent('updateValue', null, $caller);
                break;
            case 'createElement':
                static::__construct($arg[0], $arg[1], $arg[2], $arg[3], $arg[4]);
                break;
        }
        return true;
    }
812959
public function onReceive($server, $fd, $from_id, $data) {
		$this->handle(__METHOD__, [$fd, $from_id, $data]);
	}
1313158
function onQuickFormEvent($event, $arg, &$caller)
    {
        switch ($event) {
            case 'updateValue':
                $this->_createElementsIfNotExist();
                foreach (array_keys($this->_elements) as $key) {
                    if ($this->_appendName) {
                        $elementName = $this->_elements[$key]->getName();
                        if (is_null($elementName)) {
                            $this->_elements[$key]->setName($this->getName());
                        } elseif ('' === $elementName) {
                            $this->_elements[$key]->setName($this->getName() . '[' . $key . ']');
                        } else {
                            $this->_elements[$key]->setName($this->getName() . '[' . $elementName . ']');
                        }
                    }
                    $this->_elements[$key]->onQuickFormEvent('updateValue', $arg, $caller);
                    if ($this->_appendName) {
                        $this->_elements[$key]->setName($elementName);
                    }
                }
                break;

            default:
                parent::onQuickFormEvent($event, $arg, $caller);
        }
        return true;
    }
695994
function (element, events, func) {
            for (var i = 0; i < events.length; i++) {
                element.addEventListener(events[i], func);
            }
        }
1211825
public function trigger($event_name, array $arguments = [])
    {
        // Read in our config/events file so that we have them all!
        if (! $this->have_read_from_file)
        {
            $this->readConfigFile();
        }

        foreach ($this->listeners($event_name) as $listener)
        {
            if (! is_callable($listener)) continue;

            $result = call_user_func_array($listener, $arguments);

            if ($result === false)
            {
                return false;
            }
        }

        return true;
    }
26941
public function notify($event, array $arguments = []): void
    {
        if (!isset($this->listeners[$event])) {
            return;
        }

        foreach ($this->listeners[$event] as $listener) {
            call_user_func_array($listener, $arguments);
        }
    }
320982
public function triggerEvent($event, $args = [])
    {
        if (!is_string($event) && !$event instanceof Event) {
            throw new \InvalidArgumentException(sprintf(
                '%s::%s only allow Event object or string.',
                get_called_class(),
                __FUNCTION__
            ));
        }

        if (!($event instanceof Event)) {
            if (isset($this->events[$event])) {
                $event = $this->events[$event];
            } else {
                $event = new Event($event);
            }
        }

        $event->mergeArguments($args);

        $listeners = [];

        if (isset($this->listeners[$event->getName()])) {
            foreach ($this->listeners[$event->getName()] as $listener) {
                if ($event->isStopped()) {
                    return $event;
                }

                if (!is_callable($listener)) {
                    $listener = [$listener, $event->getName()];
                }

                if (!is_callable($listener)) {
                    continue;
                }

                $listener($event);

                if ($this->debug) {
                    $listeners[] = $listener;
                }
            }
        }

        if ($this->debug) {
            $executedListeners = [];

            foreach ($listeners as $listener) {
                if ($listener instanceof \Closure) {
                    $ref = new \ReflectionFunction($listener);

                    $listener = ['{Closure}: ' . $ref->getFileName() . ' - Line: ' . $ref->getStartLine()];
                }

                if (is_object($listener[0])) {
                    $listener[0] = get_class($listener[0]);
                }

                $executedListeners[] = $listener;
            }

            $this->collector[] = [
                'event' => $event->getName(),
                'listeners' => $executedListeners,
            ];
        }

        return $event;
    }
1343894
public function addFunction(callable $fn, $args = [], $id = null)
    {
        $job = new Job($fn, $args, $id);
        $this->queueJob($job->configure($this->config));

        return $job;
    }