Skip to content

Commit

Permalink
Extract GarbageCollectionDriver into a named class
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Jul 29, 2018
1 parent 8576c4a commit e132fcc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
50 changes: 15 additions & 35 deletions lib/Loop.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Amp\Loop\Driver;
use Amp\Loop\DriverFactory;
use Amp\Loop\GarbageCollectionDriver;
use Amp\Loop\InvalidWatcherError;
use Amp\Loop\UnsupportedFeatureException;
use Amp\Loop\Watcher;

/**
* Accessor to allow global access to the event loop.
Expand All @@ -33,30 +33,10 @@ private function __construct()
*
* @param Driver $driver
*/
public static function set(Driver $driver)
public static function set(Driver $driver): void
{
try {
self::$driver = new class extends Driver {
protected function activate(array $watchers): void
{
throw new \Error("Can't activate watcher during garbage collection.");
}

protected function dispatch(bool $blocking): void
{
throw new \Error("Can't dispatch during garbage collection.");
}

protected function deactivate(Watcher $watcher): void
{
// do nothing
}

public function getHandle()
{
return null;
}
};
self::$driver = new GarbageCollectionDriver;

\gc_collect_cycles();
} finally {
Expand All @@ -75,7 +55,7 @@ public function getHandle()
*
* @return void
*/
public static function run(callable $callback = null)
public static function run(callable $callback = null): void
{
if ($callback) {
self::$driver->defer($callback);
Expand All @@ -92,7 +72,7 @@ public static function run(callable $callback = null)
*
* @return void
*/
public static function stop()
public static function stop(): void
{
self::$driver->stop();
}
Expand Down Expand Up @@ -244,7 +224,7 @@ public static function onSignal(int $signo, callable $callback, $data = null): s
*
* @throws InvalidWatcherError If the watcher identifier is invalid.
*/
public static function enable(string $watcherId)
public static function enable(string $watcherId): void
{
self::$driver->enable($watcherId);
}
Expand All @@ -262,9 +242,9 @@ public static function enable(string $watcherId)
*
* @return void
*/
public static function disable(string $watcherId)
public static function disable(string $watcherId): void
{
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
if (\PHP_VERSION_ID < 70200 && self::$driver !== null) {
// Prior to PHP 7.2, self::$driver may be unset during destruct.
// See https://github.com/amphp/amp/issues/212.
return;
Expand All @@ -276,16 +256,16 @@ public static function disable(string $watcherId)
/**
* Cancel a watcher.
*
* This will detatch the event loop from all resources that are associated to the watcher. After this operation the
* This will detach the event loop from all resources that are associated to the watcher. After this operation the
* watcher is permanently invalid. Calling this function MUST NOT fail, even if passed an invalid watcher.
*
* @param string $watcherId The watcher identifier.
*
* @return void
*/
public static function cancel(string $watcherId)
public static function cancel(string $watcherId): void
{
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
if (\PHP_VERSION_ID < 70200 && self::$driver !== null) {
// Prior to PHP 7.2, self::$driver may be unset during destruct.
// See https://github.com/amphp/amp/issues/212.
return;
Expand All @@ -306,7 +286,7 @@ public static function cancel(string $watcherId)
*
* @throws InvalidWatcherError If the watcher identifier is invalid.
*/
public static function reference(string $watcherId)
public static function reference(string $watcherId): void
{
self::$driver->reference($watcherId);
}
Expand All @@ -321,7 +301,7 @@ public static function reference(string $watcherId)
*
* @return void
*/
public static function unreference(string $watcherId)
public static function unreference(string $watcherId): void
{
if (\PHP_VERSION_ID < 70200 && !isset(self::$driver)) {
// Prior to PHP 7.2, self::$driver may be unset during destruct.
Expand All @@ -346,7 +326,7 @@ public static function unreference(string $watcherId)
*
* @return void
*/
public static function setState(string $key, $value)
public static function setState(string $key, $value): void
{
self::$driver->setState($key, $value);
}
Expand Down Expand Up @@ -383,7 +363,7 @@ public static function getState(string $key)
*
* @return callable(\Throwable $error)|null The previous handler, `null` if there was none.
*/
public static function setErrorHandler(callable $callback = null)
public static function setErrorHandler(callable $callback = null): ?callable
{
return self::$driver->setErrorHandler($callback);
}
Expand Down
27 changes: 27 additions & 0 deletions lib/Loop/GarbageCollectionDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php


namespace Amp\Loop;

class GarbageCollectionDriver extends Driver
{
protected function activate(array $watchers): void
{
throw new \Error("Can't activate watcher during garbage collection.");
}

protected function dispatch(bool $blocking): void
{
throw new \Error("Can't dispatch during garbage collection.");
}

protected function deactivate(Watcher $watcher): void
{
// do nothing
}

public function getHandle()
{
return null;
}
}

0 comments on commit e132fcc

Please sign in to comment.