/src/Reactive/EventStream/Combinators/Each.php
PHP | 38 lines | 14 code | 7 blank | 17 comment | 0 complexity | c80b0eb6ebb3f2689e2ad9336862211d MD5 | raw file
- <?php
- namespace Reactive\EventStream\Combinators;
- use Reactive\Core\Reactor;
- use Reactive\EventStream\EventStream;
- class Each implements Reactor {
- /**
- * The callback function
- *
- * @var callable
- */
- protected $callback = null;
-
- /**
- * Creates a new Each object that reacts to events from $stream by calling $f
- *
- * @param \Reactive\EventStream\EventStream $stream
- * @param callable $f
- */
- public function __construct(EventStream $stream, callable $f) {
- // Store the callback to use later
- $this->callback = $f;
-
- // Subscribe to events from the stream
- $stream->subscribe($this);
- }
-
- /**
- * {@inheritdoc}
- */
- public function react(\Reactive\Core\Emitter $source, $value) {
- // We react by simply calling our callback with the value
- call_user_func($this->callback, $value);
- }
- }