Created
July 8, 2018 17:05
-
-
Save alash3al/d97b769102d5615f8192ce5b06299723 to your computer and use it in GitHub Desktop.
Tix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/** | |
* Tix | |
* | |
* A very tiny event loop that emulates node.js main loop | |
*/ | |
class Tix extends stdClass | |
{ | |
private $queue; | |
private $filename; | |
/** | |
* Constructor | |
* | |
* @param string $filename | |
*/ | |
public function __construct($filename) { | |
! is_file($filename) && die("File {$filename} isn't exists"); | |
$this->queue = new SplQueue; | |
$this->filename = $filename; | |
} | |
public function import($filename, $as) { | |
if ( ! is_file($filename) ) { | |
throw new Exception("The {$filename} isn't found!"); | |
} | |
$this->{$as} = require($filename); | |
return $this->{$as}; | |
} | |
/** | |
* Push a new function into the main loop | |
* | |
* @param Closure $fn | |
* @return void | |
*/ | |
public function push(Closure $fn): void { | |
$this->queue->enqueue($fn); | |
} | |
/** | |
* Loop | |
* | |
* @return void | |
*/ | |
public function loop(): void { | |
$filename = $this->filename; | |
((function() use($filename) { | |
require($filename); | |
return $this; | |
})->bindTo($this))(); | |
while ( true ) { | |
while (! $this->queue->isEmpty()) { | |
(($this->queue->pop())->bindTo($this))(); | |
usleep(200000); | |
} | |
usleep(200000); | |
} | |
} | |
} | |
/** | |
* Push the specified function(s) into the main loop | |
*/ | |
function tixify(Closure ...$fn): Tix { | |
global $tix; | |
foreach ( $fn as $f ) { | |
$tix->push($f); | |
} | |
return $tix; | |
} | |
/** | |
* Construct the Tix main loop | |
*/ | |
$tix = new Tix( | |
$argv[1] ?? die("empty filename specified") | |
); | |
/** | |
* Run the main loop | |
*/ | |
$tix->loop(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment