PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Reactive/EventStream/Combinators/Each.php

https://bitbucket.org/mkjpryor/reactive
PHP | 38 lines | 14 code | 7 blank | 17 comment | 0 complexity | c80b0eb6ebb3f2689e2ad9336862211d MD5 | raw file
  1. <?php
  2. namespace Reactive\EventStream\Combinators;
  3. use Reactive\Core\Reactor;
  4. use Reactive\EventStream\EventStream;
  5. class Each implements Reactor {
  6. /**
  7. * The callback function
  8. *
  9. * @var callable
  10. */
  11. protected $callback = null;
  12. /**
  13. * Creates a new Each object that reacts to events from $stream by calling $f
  14. *
  15. * @param \Reactive\EventStream\EventStream $stream
  16. * @param callable $f
  17. */
  18. public function __construct(EventStream $stream, callable $f) {
  19. // Store the callback to use later
  20. $this->callback = $f;
  21. // Subscribe to events from the stream
  22. $stream->subscribe($this);
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function react(\Reactive\Core\Emitter $source, $value) {
  28. // We react by simply calling our callback with the value
  29. call_user_func($this->callback, $value);
  30. }
  31. }