reactphp/event-loop — pull request #16 by nrk (2014-10-27 18:12:39+00:00)
Title: [WIP] API changes for enabling/disabling stream listeners.

This PR is a WIP and not meant to be merged as-is, instead we want to start discussing this prototype (initially proposed by @igorw a while back) that aims to provide a new API (BC ahead!!) offering an easier and less cumbersome way to enable and disable stream listeners, effectively providing a nice way to toggle read or write notifications for streams without having to re-add the callbacks to the event loop.

This is a just a basic example to partially show how it works in practice:

```php
stream_set_blocking(STDOUT, 0);
stream_set_blocking(STDIN, 0);

$echo = null;
$loop = new React\EventLoop\StreamSelectLoop();

$loop->onWritable(STDOUT, function ($stream, $loop) use (&$echo) {
if ($echo) {
fwrite($stream, "JUST $echo??? SRSLY????".PHP_EOL);
}

fwrite($stream, "Write anything and press [ENTER]: ");

$loop->disableWrite($stream);
$loop->enableRead(STDIN);
});

$loop->onReadable(STDIN, function ($stream, $loop) use (&$echo) {
$echo = rtrim(fread($stream, 128), PHP_EOL);

$loop->disableRead($stream);
$loop->enableWrite(STDOUT);
});

$loop->enableWrite(STDOUT);

$loop->run();
```

The current implementation, as the time of writing, provides just the basics for testing the new design of `React\EventLoop\LoopInterface` but it is likely there are some issues (e.g. enabling notifications for streams without an associated listener should throw an exception). The test suite has been simply adapted to the new API with no added test cases.

Share your thoughts please!

__NOTE:__ this PR has been manually ported from reactphp/react#258 to include only the changes related to the event loop while updating parts of code affected by some radical changes introduced in the meanwhile in v0.4 and the current state of the master branch.