From 704adf70cfa7cd34172203bf6abae226323a77d1 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Fri, 23 Jun 2017 16:33:13 +0200 Subject: [PATCH] Add reference and unreference methods to ResourceInputStream --- lib/ResourceInputStream.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/ResourceInputStream.php b/lib/ResourceInputStream.php index a09fea2..540627e 100644 --- a/lib/ResourceInputStream.php +++ b/lib/ResourceInputStream.php @@ -135,6 +135,32 @@ public function getResource() { return $this->resource; } + /** + * References the read watcher, so the loop keeps running in case there's an active read. + * + * @see Loop::reference() + */ + public function reference() { + if (!$this->resource) { + throw new \Error("Resource has already been freed"); + } + + Loop::reference($this->watcher); + } + + /** + * Unreferences the read watcher, so the loop doesn't keep running even if there are active reads. + * + * @see Loop::unreference() + */ + public function unreference() { + if (!$this->resource) { + throw new \Error("Resource has already been freed"); + } + + Loop::unreference($this->watcher); + } + public function __destruct() { if ($this->resource !== null) { $this->free();