Skip to content

Commit

Permalink
Split AsyncHandlerListManager
Browse files Browse the repository at this point in the history
this allows further code deduplication at the expense of needing 2 calls to unregister all handlers
  • Loading branch information
dktapps committed Nov 13, 2024
1 parent 972a9fb commit 667656b
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 177 deletions.
2 changes: 2 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use pocketmine\crash\CrashDumpRenderer;
use pocketmine\entity\EntityDataHelper;
use pocketmine\entity\Location;
use pocketmine\event\AsyncHandlerListManager;
use pocketmine\event\HandlerListManager;
use pocketmine\event\player\PlayerCreationEvent;
use pocketmine\event\player\PlayerDataSaveEvent;
Expand Down Expand Up @@ -1485,6 +1486,7 @@ public function forceShutdown() : void{

$this->logger->debug("Removing event handlers");
HandlerListManager::global()->unregisterAll();
AsyncHandlerListManager::global()->unregisterAll();

if(isset($this->asyncPool)){
$this->logger->debug("Shutting down async task worker pool");
Expand Down
2 changes: 1 addition & 1 deletion src/event/AsyncEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ final public function call() : Promise{
/** @phpstan-var PromiseResolver<static> $globalResolver */
$globalResolver = new PromiseResolver();

$this->processRemainingHandlers(HandlerListManager::global()->getAsyncHandlersFor(static::class), $globalResolver);
$this->processRemainingHandlers(AsyncHandlerListManager::global()->getHandlersFor(static::class), $globalResolver);

Check failure on line 63 in src/event/AsyncEvent.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Parameter #2 $globalResolver of method pocketmine\event\AsyncEvent::processRemainingHandlers() expects pocketmine\promise\PromiseResolver<pocketmine\event\AsyncEvent>, pocketmine\promise\PromiseResolver<static(pocketmine\event\AsyncEvent)> given.

Check failure on line 63 in src/event/AsyncEvent.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Parameter #2 $globalResolver of method pocketmine\event\AsyncEvent::processRemainingHandlers() expects pocketmine\promise\PromiseResolver<pocketmine\event\AsyncEvent>, pocketmine\promise\PromiseResolver<static(pocketmine\event\AsyncEvent)> given.

Check failure on line 63 in src/event/AsyncEvent.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Parameter #2 $globalResolver of method pocketmine\event\AsyncEvent::processRemainingHandlers() expects pocketmine\promise\PromiseResolver<pocketmine\event\AsyncEvent>, pocketmine\promise\PromiseResolver<static(pocketmine\event\AsyncEvent)> given.

Check failure on line 63 in src/event/AsyncEvent.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 / PHPStan analysis

Parameter #2 $globalResolver of method pocketmine\event\AsyncEvent::processRemainingHandlers() expects pocketmine\promise\PromiseResolver<pocketmine\event\AsyncEvent>, pocketmine\promise\PromiseResolver<static(pocketmine\event\AsyncEvent)> given.

Check failure on line 63 in src/event/AsyncEvent.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 / PHPStan analysis

Parameter #2 $globalResolver of method pocketmine\event\AsyncEvent::processRemainingHandlers() expects pocketmine\promise\PromiseResolver<pocketmine\event\AsyncEvent>, pocketmine\promise\PromiseResolver<static(pocketmine\event\AsyncEvent)> given.

Check failure on line 63 in src/event/AsyncEvent.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 / PHPStan analysis

Parameter #2 $globalResolver of method pocketmine\event\AsyncEvent::processRemainingHandlers() expects pocketmine\promise\PromiseResolver<pocketmine\event\AsyncEvent>, pocketmine\promise\PromiseResolver<static(pocketmine\event\AsyncEvent)> given.

return $globalResolver->getPromise();
}finally{
Expand Down
43 changes: 43 additions & 0 deletions src/event/AsyncHandlerListManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/

declare(strict_types=1);

namespace pocketmine\event;

/**
* @phpstan-extends BaseHandlerListManager<AsyncEvent, AsyncRegisteredListener>
*/
final class AsyncHandlerListManager extends BaseHandlerListManager{
private static ?self $globalInstance = null;

public static function global() : self{
return self::$globalInstance ?? (self::$globalInstance = new self());
}

protected function getBaseEventClass() : string{
return AsyncEvent::class;
}

protected function createHandlerList(string $event, ?BaseHandlerList $parentList, RegisteredListenerCache $handlerCache) : BaseHandlerList{
return new AsyncHandlerList($event, $parentList, $handlerCache);
}
}
156 changes: 156 additions & 0 deletions src/event/BaseHandlerListManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/

declare(strict_types=1);

namespace pocketmine\event;

use pocketmine\plugin\Plugin;
use pocketmine\utils\Utils;

/**
* @phpstan-template TEvent of Event|AsyncEvent
* @phpstan-template TRegisteredListener of BaseRegisteredListener
*
* @phpstan-type THandlerList BaseHandlerList<TRegisteredListener, TEvent>
*/
abstract class BaseHandlerListManager{
/**
* @var BaseHandlerList[] classname => BaseHandlerList
* @phpstan-var array<class-string<covariant TEvent>, THandlerList>
*/
private array $allLists = [];
/**
* @var RegisteredListenerCache[] event class name => cache
* @phpstan-var array<class-string<TEvent>, RegisteredListenerCache<TRegisteredListener>>
*/
private array $handlerCaches = [];

/**
* Unregisters all the listeners
* If a Plugin or Listener is passed, all the listeners with that object will be removed
*
* @phpstan-param TRegisteredListener|Plugin|Listener|null $object
*/
public function unregisterAll(BaseRegisteredListener|Plugin|Listener|null $object = null) : void{
if($object !== null){
foreach($this->allLists as $h){
$h->unregister($object);
}
}else{
foreach($this->allLists as $h){
$h->clear();
}
}
}

/**
* @phpstan-param \ReflectionClass<TEvent> $class
*/
private static function isValidClass(\ReflectionClass $class) : bool{
$tags = Utils::parseDocComment((string) $class->getDocComment());
return !$class->isAbstract() || isset($tags["allowHandle"]);
}

/**
* @phpstan-param \ReflectionClass<TEvent> $class
*
* @phpstan-return \ReflectionClass<TEvent>|null
*/
private static function resolveNearestHandleableParent(\ReflectionClass $class) : ?\ReflectionClass{
for($parent = $class->getParentClass(); $parent !== false; $parent = $parent->getParentClass()){
if(self::isValidClass($parent)){
return $parent;
}
//NOOP
}
return null;
}

/**
* @phpstan-return class-string<TEvent>
*/
abstract protected function getBaseEventClass() : string;

/**
* @phpstan-param class-string<covariant TEvent> $event
* @phpstan-param THandlerList|null $parentList
* @phpstan-param RegisteredListenerCache<TRegisteredListener> $handlerCache
*
* @phpstan-return THandlerList
*/
abstract protected function createHandlerList(string $event, ?BaseHandlerList $parentList, RegisteredListenerCache $handlerCache) : BaseHandlerList;

/**
* Returns the HandlerList for listeners that explicitly handle this event.
*
* Calling this method also lazily initializes the $classMap inheritance tree of handler lists.
*
* @phpstan-param class-string<covariant TEvent> $event
* @phpstan-return THandlerList
*
* @throws \ReflectionException
* @throws \InvalidArgumentException
*/
public function getListFor(string $event) : BaseHandlerList{
if(isset($this->allLists[$event])){
return $this->allLists[$event];
}

$class = new \ReflectionClass($event);
if(!$class->isSubclassOf($this->getBaseEventClass())){
throw new \InvalidArgumentException("Cannot get sync handler list for async event");
}
if(!self::isValidClass($class)){
throw new \InvalidArgumentException("Event must be non-abstract or have the @allowHandle annotation");
}

$parent = self::resolveNearestHandleableParent($class);
/** @phpstan-var RegisteredListenerCache<TRegisteredListener> $cache */
$cache = new RegisteredListenerCache();
$this->handlerCaches[$event] = $cache;
return $this->allLists[$event] = $this->createHandlerList(
$event,
parentList: $parent !== null ? $this->getListFor($parent->getName()) : null,
handlerCache: $cache
);
}

/**
* @phpstan-param class-string<covariant TEvent> $event
*
* @return RegisteredListener[]
* @phpstan-return list<TRegisteredListener>
*/
public function getHandlersFor(string $event) : array{
$cache = $this->handlerCaches[$event] ?? null;
//getListFor() will populate the cache for the next call
return $cache?->list ?? $this->getListFor($event)->getListenerList();
}

/**
* @return HandlerList[]
* @phpstan-return array<class-string<covariant TEvent>, THandlerList>
*/
public function getAll() : array{
return $this->allLists;
}
}
Loading

0 comments on commit 667656b

Please sign in to comment.