<?php 
/*
Response::includeJs("event.js","1.0");
Route::isJs("event.js",function($data){
?>
<script>
	(function(){
		window.bw = window.bw || {};
		window.bw.event = {};
		window.bw.event.onstore = {};
		window.bw.event.on = function(name,callback){
			if(!window.bw.event.onstore[name]){
				window.bw.event.onstore[name] = [];
			}
			window.bw.event.onstore[name].push(callback);
		}
		window.bw.event.run = function (name,data){
			if (window.bw.event.onstore[name]) {
				for (var i = 0; i < window.bw.event.onstore[name].length; i++) {
					window.bw.event.onstore[name][i](data);
				}
			}
		}
	})();
</script>
<?
});
*/


class Event{
	static $listEvents = [];
	static function on($event,$fn){
		$event = new Event($event,$fn);
		return $event;
	}
	static function si($event,$fn){
		return self::on($event,$fn);
	}
	static function run($event,$data = null){
		if(empty(self::$listEvents[$event])) self::$listEvents[$event] = [];
		foreach (self::$listEvents[$event] as $key => $eventInstance) {
			$rep = $eventInstance->runEvent($data);
			if (!is_null($rep)) $data = $rep;
		}
		return $data;
	}
	static function fire($event,$data){
		return self::run($event,$data);
	}
	var $name = "";
	var $fn = "";
	function __construct($name,$fn){
		$this->name = $name;
		$this->fn = $fn;
		if(empty(self::$listEvents[$this->name])) self::$listEvents[$this->name] = [];
		self::$listEvents[$this->name][] = $this;
	}
	function runEvent($data){
		return $this->fn->bindTo($this)($data);
	}

}