Split AsyncHandlerListManager

this allows further code deduplication at the expense of needing 2 calls to unregister all handlers
This commit is contained in:
Dylan K. Taylor 2024-11-13 22:08:28 +00:00
parent 972a9fb201
commit 667656b1c6
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
9 changed files with 219 additions and 177 deletions

View File

@ -38,6 +38,7 @@ use pocketmine\crash\CrashDump;
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;
@ -1485,6 +1486,7 @@ class Server{
$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");

View File

@ -60,7 +60,7 @@ abstract class AsyncEvent{
/** @phpstan-var PromiseResolver<static> $globalResolver */
$globalResolver = new PromiseResolver();
$this->processRemainingHandlers(HandlerListManager::global()->getAsyncHandlersFor(static::class), $globalResolver);
$this->processRemainingHandlers(AsyncHandlerListManager::global()->getHandlersFor(static::class), $globalResolver);
return $globalResolver->getPromise();
}finally{

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -23,182 +23,21 @@ declare(strict_types=1);
namespace pocketmine\event;
use pocketmine\plugin\Plugin;
use pocketmine\utils\Utils;
class HandlerListManager{
/**
* @phpstan-extends BaseHandlerListManager<Event, RegisteredListener>
*/
class HandlerListManager extends BaseHandlerListManager{
private static ?self $globalInstance = null;
public static function global() : self{
return self::$globalInstance ?? (self::$globalInstance = new self());
}
/** @var HandlerList[] classname => HandlerList */
private array $allSyncLists = [];
/**
* @var RegisteredListenerCache[] event class name => cache
* @phpstan-var array<class-string<Event>, RegisteredListenerCache<RegisteredListener>>
*/
private array $syncHandlerCaches = [];
/** @var AsyncHandlerList[] classname => AsyncHandlerList */
private array $allAsyncLists = [];
/**
* @var RegisteredListenerCache[] event class name => cache
* @phpstan-var array<class-string<AsyncEvent>, RegisteredListenerCache<AsyncRegisteredListener>>
*/
private array $asyncHandlerCaches = [];
/**
* Unregisters all the listeners
* If a Plugin or Listener is passed, all the listeners with that object will be removed
*/
public function unregisterAll(RegisteredListener|AsyncRegisteredListener|Plugin|Listener|null $object = null) : void{
if($object !== null){
if(!$object instanceof AsyncRegisteredListener){
foreach($this->allSyncLists as $h){
$h->unregister($object);
}
}
if(!$object instanceof RegisteredListener){
foreach($this->allAsyncLists as $h){
$h->unregister($object);
}
}
}else{
foreach($this->allSyncLists as $h){
$h->clear();
}
foreach($this->allAsyncLists as $h){
$h->clear();
}
}
protected function getBaseEventClass() : string{
return Event::class;
}
/**
* @phpstan-param \ReflectionClass<Event|AsyncEvent> $class
*/
private static function isValidClass(\ReflectionClass $class) : bool{
$tags = Utils::parseDocComment((string) $class->getDocComment());
return !$class->isAbstract() || isset($tags["allowHandle"]);
}
/**
* @phpstan-template TEvent of Event|AsyncEvent
* @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;
}
/**
* 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 Event> $event
*
* @throws \ReflectionException
* @throws \InvalidArgumentException
*/
public function getListFor(string $event) : HandlerList{
if(isset($this->allSyncLists[$event])){
return $this->allSyncLists[$event];
}
$class = new \ReflectionClass($event);
if(!$class->isSubclassOf(Event::class)){
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<RegisteredListener> $cache */
$cache = new RegisteredListenerCache();
$this->syncHandlerCaches[$event] = $cache;
return $this->allSyncLists[$event] = new HandlerList(
$event,
parentList: $parent !== null ? $this->getListFor($parent->getName()) : null,
handlerCache: $cache
);
}
/**
* Returns the HandlerList for async listeners that explicitly handle this event.
*
* @phpstan-param class-string<covariant AsyncEvent> $event
*
* @throws \ReflectionException
* @throws \InvalidArgumentException
*/
public function getAsyncListFor(string $event) : AsyncHandlerList{
if(isset($this->allAsyncLists[$event])){
return $this->allAsyncLists[$event];
}
$class = new \ReflectionClass($event);
if(!$class->isSubclassOf(AsyncEvent::class)){
throw new \InvalidArgumentException("Cannot get async handler list for sync 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<AsyncRegisteredListener> $cache */
$cache = new RegisteredListenerCache();
$this->asyncHandlerCaches[$event] = $cache;
return $this->allAsyncLists[$event] = new AsyncHandlerList(
$event,
parentList: $parent !== null ? $this->getAsyncListFor($parent->getName()) : null,
handlerCache: $cache
);
}
/**
* @phpstan-param class-string<covariant Event> $event
*
* @return RegisteredListener[]
*/
public function getHandlersFor(string $event) : array{
$cache = $this->syncHandlerCaches[$event] ?? null;
//getListFor() will populate the cache for the next call
return $cache?->list ?? $this->getListFor($event)->getListenerList();
}
/**
* @phpstan-param class-string<covariant AsyncEvent> $event
*
* @return AsyncRegisteredListener[]
*/
public function getAsyncHandlersFor(string $event) : array{
$cache = $this->asyncHandlerCaches[$event] ?? null;
//getListFor() will populate the cache for the next call
return $cache?->list ?? $this->getAsyncListFor($event)->getListenerList();
}
/**
* @return HandlerList[]
*/
public function getAll() : array{
return $this->allSyncLists;
}
/**
* @return AsyncHandlerList[]
*/
public function getAllAsync() : array{
return $this->allAsyncLists;
protected function createHandlerList(string $event, ?BaseHandlerList $parentList, RegisteredListenerCache $handlerCache) : BaseHandlerList{
return new HandlerList($event, $parentList, $handlerCache);
}
}

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\plugin;
use pocketmine\event\AsyncEvent;
use pocketmine\event\AsyncHandlerListManager;
use pocketmine\event\AsyncRegisteredListener;
use pocketmine\event\Cancellable;
use pocketmine\event\Event;
@ -525,6 +526,7 @@ class PluginManager{
$plugin->onEnableStateChange(false);
$plugin->getScheduler()->shutdown();
HandlerListManager::global()->unregisterAll($plugin);
AsyncHandlerListManager::global()->unregisterAll($plugin);
}
}
@ -721,7 +723,7 @@ class PluginManager{
$timings = Timings::getEventHandlerTimings($event, $handlerName, $plugin->getDescription()->getFullName());
$registeredListener = new AsyncRegisteredListener($handler, $priority, $plugin, $handleCancelled, $exclusiveCall, $timings);
HandlerListManager::global()->getAsyncListFor($event)->register($registeredListener);
AsyncHandlerListManager::global()->getListFor($event)->register($registeredListener);
return $registeredListener;
}

View File

@ -24,8 +24,8 @@ declare(strict_types=1);
namespace pmmp\TesterPlugin;
use pmmp\TesterPlugin\event\GrandchildAsyncEvent;
use pocketmine\event\AsyncHandlerListManager;
use pocketmine\event\EventPriority;
use pocketmine\event\HandlerListManager;
use pocketmine\promise\Promise;
use pocketmine\promise\PromiseResolver;
@ -50,7 +50,7 @@ final class AsyncEventConcurrencyTest extends Test{
}
public function run() : void{
HandlerListManager::global()->unregisterAll();
AsyncHandlerListManager::global()->unregisterAll();
$main = $this->getPlugin();
$pluginManager = $main->getServer()->getPluginManager();

View File

@ -27,8 +27,8 @@ use pmmp\TesterPlugin\event\ChildAsyncEvent;
use pmmp\TesterPlugin\event\GrandchildAsyncEvent;
use pmmp\TesterPlugin\event\ParentAsyncEvent;
use pocketmine\event\AsyncEvent;
use pocketmine\event\AsyncHandlerListManager;
use pocketmine\event\EventPriority;
use pocketmine\event\HandlerListManager;
use pocketmine\promise\Promise;
use function implode;
use function shuffle;
@ -51,7 +51,7 @@ final class AsyncEventInheritanceTest extends Test{
}
public function run() : void{
HandlerListManager::global()->unregisterAll();
AsyncHandlerListManager::global()->unregisterAll();
$plugin = $this->getPlugin();
$classes = self::EXPECTED_ORDER;

View File

@ -24,8 +24,8 @@ declare(strict_types=1);
namespace pmmp\TesterPlugin;
use pmmp\TesterPlugin\event\GrandchildAsyncEvent;
use pocketmine\event\AsyncHandlerListManager;
use pocketmine\event\EventPriority;
use pocketmine\event\HandlerListManager;
use pocketmine\promise\Promise;
use pocketmine\promise\PromiseResolver;
@ -47,7 +47,7 @@ final class AsyncEventPriorityTest extends Test{
}
public function run() : void{
HandlerListManager::global()->unregisterAll();
AsyncHandlerListManager::global()->unregisterAll();
$main = $this->getPlugin();
$pluginManager = $main->getServer()->getPluginManager();