-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a613942
commit 32dec1d
Showing
6 changed files
with
433 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea/ | ||
vendor/ | ||
js/ | ||
composer.lock |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,56 @@ | ||
# emitter | ||
Event emitter component | ||
# Emitter | ||
|
||
Event emitter component. | ||
|
||
## Installation | ||
|
||
``` | ||
$ composer install socketio-php/emitter | ||
``` | ||
|
||
## Test | ||
|
||
```shell | ||
➜ emitter git:(master) ✗ php vendor/bin/phpunit tests/EmitterTest.php | ||
PHPUnit 7.5.20 by Sebastian Bergmann and contributors. | ||
|
||
.......... 10 / 10 (100%) | ||
|
||
Time: 24 ms, Memory: 4.00 MB | ||
|
||
OK (10 tests, 10 assertions) | ||
``` | ||
|
||
## API | ||
|
||
### Emitter#on(event, fn) | ||
|
||
Register an `event` handler `fn`. | ||
|
||
### Emitter#once(event, fn) | ||
|
||
Register a single-shot `event` handler `fn`, | ||
removed immediately after it is invoked the | ||
first time. | ||
|
||
### Emitter#off(event, fn) | ||
|
||
* Pass `event` and `fn` to remove a listener. | ||
* Pass `event` to remove all listeners on that event. | ||
* Pass nothing to remove all listeners on all events. | ||
|
||
### Emitter#emit(event, ...) | ||
|
||
Emit an `event` with variable option args. | ||
|
||
### Emitter#listeners(event) | ||
|
||
Return an array of callbacks, or an empty array. | ||
|
||
### Emitter#hasListeners(event) | ||
|
||
Check if this emitter has `event` handlers. | ||
|
||
## License | ||
|
||
MIT |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "socketio-php/emitter", | ||
"description": "event emitter", | ||
"type": "library", | ||
"require": { | ||
"php": ">=7.1.22" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^7.5" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Emitter\\": "src/", | ||
"EmitterTest\\": "tests/" | ||
} | ||
}, | ||
"license": "MIT" | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<phpunit bootstrap="vendor/autoload.php"> | ||
<testsuites> | ||
<testsuite name="parser"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
<?php | ||
|
||
namespace Emitter; | ||
|
||
/** | ||
* Class Emitter | ||
* | ||
* @package Emitter | ||
*/ | ||
class Emitter | ||
{ | ||
private $callbacks = []; | ||
|
||
/** | ||
* @param string $event | ||
* @param callable $fn | ||
* | ||
* @return Emitter | ||
*/ | ||
public function on(string $event, callable $fn) | ||
{ | ||
return $this->addEventListener($event, $fn); | ||
} | ||
|
||
/** | ||
* @param string $event | ||
* @param callable $fn | ||
* | ||
* @return $this | ||
*/ | ||
public function addEventListener(string $event, callable $fn) | ||
{ | ||
if (!isset($this->callbacks['$' . $event])) { | ||
$this->callbacks['$' . $event] = []; | ||
} | ||
|
||
array_push($this->callbacks['$' . $event], $fn); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $event | ||
* @param callable $fn | ||
* | ||
* @return $this | ||
*/ | ||
public function once(string $event, callable $fn) | ||
{ | ||
$on = function ($retFn = false) use ($event, $fn) { | ||
$arguments = func_get_args(); | ||
if (is_bool(current($arguments))) { | ||
$arguments = array_slice($arguments, 1); | ||
}else{ | ||
$retFn = false; | ||
} | ||
if (!$retFn) { | ||
$this->off($event, $fn); | ||
$fn(...$arguments); | ||
|
||
return null; | ||
} else { | ||
return $fn; | ||
} | ||
}; | ||
|
||
$this->on($event, $on); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $event | ||
* @param callable|null $fn | ||
* | ||
* @return Emitter | ||
*/ | ||
public function off(string $event = '', ?callable $fn = null) | ||
{ | ||
return $this->removeListener($event, $fn); | ||
} | ||
|
||
/** | ||
* @param string $event | ||
* @param callable|null $fn | ||
* | ||
* @return Emitter | ||
*/ | ||
public function removeListener(string $event = '', ?callable $fn = null) | ||
{ | ||
return $this->removeAllListeners($event, $fn); | ||
} | ||
|
||
/** | ||
* @param string $event | ||
* @param callable|null $fn | ||
* | ||
* @return Emitter | ||
*/ | ||
public function removeAllListeners(string $event = '', ?callable $fn = null) | ||
{ | ||
return $this->removeEventListener($event, $fn); | ||
} | ||
|
||
/** | ||
* @param string $event | ||
* @param callable|null $fn | ||
* | ||
* @return $this | ||
*/ | ||
public function removeEventListener(string $event = '', ?callable $fn = null) | ||
{ | ||
// all | ||
$arguments = func_get_args(); | ||
$arguments = array_filter($arguments); | ||
if (0 === count($arguments)) { | ||
$this->callbacks = []; | ||
|
||
return $this; | ||
} | ||
|
||
|
||
if (!isset($this->callbacks['$' . $event])) return $this; | ||
|
||
// specific event | ||
$callbacks = $this->callbacks['$' . $event]; | ||
|
||
// remove all handlers | ||
if (1 == count($arguments)) { | ||
unset($this->callbacks['$' . $event]); | ||
|
||
return $this; | ||
} | ||
|
||
// remove specific handler | ||
$cb = null; | ||
for ($i = 0; $i < count($callbacks); $i++) { | ||
$cb = $callbacks[$i]; | ||
if ($cb === $fn || $cb(true) === $fn) { | ||
unset($callbacks[$i]); | ||
unset($this->callbacks['$' . $event]); | ||
break; | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @param string $event | ||
*/ | ||
public function emit(string $event) | ||
{ | ||
$arguments = func_get_args(); | ||
$args = array_slice($arguments, 1); | ||
$callbacks = $this->callbacks['$' . $event]; | ||
|
||
if (!empty($callbacks)) { | ||
for ($i = 0, $len = count($callbacks); $i < $len; ++$i) { | ||
$callbacks[$i](...$args); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param string $event | ||
* | ||
* @return array|mixed | ||
*/ | ||
public function listeners(string $event) | ||
{ | ||
return $this->callbacks['$' . $event] ?? []; | ||
} | ||
|
||
/** | ||
* @param string $event | ||
* | ||
* @return bool | ||
*/ | ||
public function hasListeners(string $event) | ||
{ | ||
return count($this->listeners($event)) ? true : false; | ||
} | ||
|
||
} |
Oops, something went wrong.