Interop\Async\Loop::onWritable PHP Method

onWritable() public static method

Warning: Closing resources locally, e.g. with fclose, might not invoke the callback. Be sure to cancel the watcher when closing the resource locally. Drivers MAY choose to notify the user if there are watchers on invalid resources, but are not required to, due to the high performance impact. Watchers on closed resources are therefore undefined behavior. Multiple watchers on the same stream MAY be executed in any order.
public static onWritable ( resource $stream, callable $callback, mixed $data = null ) : string
$stream resource The stream to monitor.
$callback callable
$data mixed Arbitrary data given to the callback function as the `$data` parameter.
return string An unique identifier that can be used to cancel, enable or disable the watcher.
    public static function onWritable($stream, callable $callback, $data = null)
    {
        $driver = self::$driver ?: self::get();
        return $driver->onWritable($stream, $callback, $data);
    }

Usage Example

Beispiel #1
0
 public function write(string $bytes) : Awaitable
 {
     if ($bytes === '') {
         if (!\is_resource($this->socket)) {
             return new Failure(new StreamClosedException('Socket resource unavailable'));
         }
         return new Success(0);
     }
     $len = \strlen($bytes);
     if (!$this->writerEnabled) {
         try {
             $bytes = $this->writeBytes($bytes);
         } catch (\Throwable $e) {
             return new Failure($e);
         }
         if ($bytes === '') {
             return new Success($len);
         }
         $this->writerEnabled = true;
         if ($this->pendingWrites === null) {
             $this->pendingWrites = new \SplQueue();
         }
         if ($this->writeWatcher === null) {
             $this->writeWatcher = Loop::onWritable($this->socket, $this->createWriteWatcher());
         } else {
             try {
                 Loop::enable($this->writeWatcher);
             } catch (InvalidWatcherException $e) {
                 $this->writeWatcher = Loop::onWritable($this->socket, $this->createWriteWatcher());
             }
         }
     }
     $this->pendingWrites->enqueue($write = new PendingWrite($bytes, $len, function () {
         foreach ($this->pendingWrites as $write) {
             if (!$write->disabled) {
                 return;
             }
         }
         $this->writerEnabled = false;
         Loop::disable($this->writeWatcher);
     }));
     return $write;
 }
All Usage Examples Of Interop\Async\Loop::onWritable