Reduce number of classes

This commit is contained in:
Dylan K. Taylor 2024-11-13 22:23:43 +00:00
parent 667656b1c6
commit edae9f26e4
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
6 changed files with 164 additions and 225 deletions

View File

@ -1,43 +0,0 @@
<?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 function uasort;
/**
* @phpstan-extends BaseHandlerList<AsyncRegisteredListener, AsyncEvent>
*/
class AsyncHandlerList extends BaseHandlerList{
protected function sortSamePriorityListeners(array $listeners) : array{
uasort($listeners, function(AsyncRegisteredListener $left, AsyncRegisteredListener $right) : int{
//While the system can handle these in any order, it's better for latency if concurrent handlers
//are processed together. It doesn't matter whether they are processed before or after exclusive handlers.
if($right->canBeCalledConcurrently()){
return $left->canBeCalledConcurrently() ? 0 : 1;
}
return -1;
});
return $listeners;
}
}

View File

@ -23,6 +23,8 @@ declare(strict_types=1);
namespace pocketmine\event;
use function uasort;
/**
* @phpstan-extends BaseHandlerListManager<AsyncEvent, AsyncRegisteredListener>
*/
@ -37,7 +39,23 @@ final class AsyncHandlerListManager extends BaseHandlerListManager{
return AsyncEvent::class;
}
protected function createHandlerList(string $event, ?BaseHandlerList $parentList, RegisteredListenerCache $handlerCache) : BaseHandlerList{
return new AsyncHandlerList($event, $parentList, $handlerCache);
/**
* @phpstan-param array<int, AsyncRegisteredListener> $listeners
* @phpstan-return array<int, AsyncRegisteredListener>
*/
private static function sortSamePriorityHandlers(array $listeners) : array{
uasort($listeners, function(AsyncRegisteredListener $left, AsyncRegisteredListener $right) : int{
//While the system can handle these in any order, it's better for latency if concurrent handlers
//are processed together. It doesn't matter whether they are processed before or after exclusive handlers.
if($right->canBeCalledConcurrently()){
return $left->canBeCalledConcurrently() ? 0 : 1;
}
return -1;
});
return $listeners;
}
protected function createHandlerList(string $event, ?HandlerList $parentList, RegisteredListenerCache $handlerCache) : HandlerList{
return new HandlerList($event, $parentList, $handlerCache, self::sortSamePriorityHandlers(...));
}
}

View File

@ -1,170 +0,0 @@
<?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 function array_merge;
use function krsort;
use function spl_object_id;
use const SORT_NUMERIC;
/**
* @phpstan-template TListener of BaseRegisteredListener
* @phpstan-template TEvent
*/
abstract class BaseHandlerList{
/**
* @var BaseRegisteredListener[][]
* @phpstan-var array<int, array<int, TListener>>
*/
private array $handlerSlots = [];
/**
* @var RegisteredListenerCache[]
* @phpstan-var array<int, RegisteredListenerCache<TListener>>
*/
private array $affectedHandlerCaches = [];
/**
* @phpstan-param class-string<covariant TEvent> $class
* @phpstan-param ?static<TListener, TEvent> $parentList
* @phpstan-param RegisteredListenerCache<TListener> $handlerCache
*/
public function __construct(
private string $class,
private ?BaseHandlerList $parentList,
private RegisteredListenerCache $handlerCache = new RegisteredListenerCache()
){
for($list = $this; $list !== null; $list = $list->parentList){
$list->affectedHandlerCaches[spl_object_id($this->handlerCache)] = $this->handlerCache;
}
}
/**
* @phpstan-param TListener $listener
*/
public function register(BaseRegisteredListener $listener) : void{
if(isset($this->handlerSlots[$listener->getPriority()][spl_object_id($listener)])){
throw new \InvalidArgumentException("This listener is already registered to priority {$listener->getPriority()} of event {$this->class}");
}
$this->handlerSlots[$listener->getPriority()][spl_object_id($listener)] = $listener;
$this->invalidateAffectedCaches();
}
/**
* @param BaseRegisteredListener[] $listeners
* @phpstan-param array<TListener> $listeners
*/
public function registerAll(array $listeners) : void{
foreach($listeners as $listener){
$this->register($listener);
}
$this->invalidateAffectedCaches();
}
/**
* @phpstan-param TListener|Plugin|Listener $object
*/
public function unregister(BaseRegisteredListener|Plugin|Listener $object) : void{
if($object instanceof Plugin || $object instanceof Listener){
foreach($this->handlerSlots as $priority => $list){
foreach($list as $hash => $listener){
if(($object instanceof Plugin && $listener->getPlugin() === $object)
|| ($object instanceof Listener && (new \ReflectionFunction($listener->getHandler()))->getClosureThis() === $object) //this doesn't even need to be a listener :D
){
unset($this->handlerSlots[$priority][$hash]);
}
}
}
}else{
unset($this->handlerSlots[$object->getPriority()][spl_object_id($object)]);
}
$this->invalidateAffectedCaches();
}
public function clear() : void{
$this->handlerSlots = [];
$this->invalidateAffectedCaches();
}
/**
* @return BaseRegisteredListener[]
* @phpstan-return array<int, TListener>
*/
public function getListenersByPriority(int $priority) : array{
return $this->handlerSlots[$priority] ?? [];
}
/**
* @phpstan-return static<TListener, TEvent>
*/
public function getParent() : ?BaseHandlerList{
return $this->parentList;
}
/**
* Invalidates all known caches which might be affected by this list's contents.
*/
private function invalidateAffectedCaches() : void{
foreach($this->affectedHandlerCaches as $cache){
$cache->list = null;
}
}
/**
* @param BaseRegisteredListener[] $listeners
* @phpstan-param array<int, TListener> $listeners
*
* @return BaseRegisteredListener[]
* @phpstan-return array<int, TListener>
*/
abstract protected function sortSamePriorityListeners(array $listeners) : array;
/**
* @return BaseRegisteredListener[]
* @phpstan-return list<TListener>
*/
public function getListenerList() : array{
if($this->handlerCache->list !== null){
return $this->handlerCache->list;
}
$handlerLists = [];
for($currentList = $this; $currentList !== null; $currentList = $currentList->parentList){
$handlerLists[] = $currentList;
}
$listenersByPriority = [];
foreach($handlerLists as $currentList){
foreach($currentList->handlerSlots as $priority => $listeners){
$listenersByPriority[$priority] = array_merge($listenersByPriority[$priority] ?? [], $this->sortSamePriorityListeners($listeners));
}
}
//TODO: why on earth do the priorities have higher values for lower priority?
krsort($listenersByPriority, SORT_NUMERIC);
return $this->handlerCache->list = array_merge(...$listenersByPriority);
}
}

View File

@ -30,11 +30,11 @@ use pocketmine\utils\Utils;
* @phpstan-template TEvent of Event|AsyncEvent
* @phpstan-template TRegisteredListener of BaseRegisteredListener
*
* @phpstan-type THandlerList BaseHandlerList<TRegisteredListener, TEvent>
* @phpstan-type THandlerList HandlerList<TRegisteredListener>
*/
abstract class BaseHandlerListManager{
/**
* @var BaseHandlerList[] classname => BaseHandlerList
* @var HandlerList[] classname => HandlerList
* @phpstan-var array<class-string<covariant TEvent>, THandlerList>
*/
private array $allLists = [];
@ -92,12 +92,12 @@ abstract class BaseHandlerListManager{
/**
* @phpstan-param class-string<covariant TEvent> $event
* @phpstan-param THandlerList|null $parentList
* @phpstan-param HandlerList<TRegisteredListener>|null $parentList
* @phpstan-param RegisteredListenerCache<TRegisteredListener> $handlerCache
*
* @phpstan-return THandlerList
*/
abstract protected function createHandlerList(string $event, ?BaseHandlerList $parentList, RegisteredListenerCache $handlerCache) : BaseHandlerList;
abstract protected function createHandlerList(string $event, ?HandlerList $parentList, RegisteredListenerCache $handlerCache) : HandlerList;
/**
* Returns the HandlerList for listeners that explicitly handle this event.
@ -110,7 +110,7 @@ abstract class BaseHandlerListManager{
* @throws \ReflectionException
* @throws \InvalidArgumentException
*/
public function getListFor(string $event) : BaseHandlerList{
public function getListFor(string $event) : HandlerList{
if(isset($this->allLists[$event])){
return $this->allLists[$event];
}

View File

@ -23,11 +23,145 @@ declare(strict_types=1);
namespace pocketmine\event;
use pocketmine\plugin\Plugin;
use function array_merge;
use function krsort;
use function spl_object_id;
use const SORT_NUMERIC;
/**
* @phpstan-extends BaseHandlerList<RegisteredListener, Event>
* @phpstan-template TListener of BaseRegisteredListener
*/
class HandlerList extends BaseHandlerList{
protected function sortSamePriorityListeners(array $listeners) : array{
return $listeners;
class HandlerList{
/**
* @var BaseRegisteredListener[][]
* @phpstan-var array<int, array<int, TListener>>
*/
private array $handlerSlots = [];
/**
* @var RegisteredListenerCache[]
* @phpstan-var array<int, RegisteredListenerCache<TListener>>
*/
private array $affectedHandlerCaches = [];
/**
* @phpstan-param class-string $class
* @phpstan-param ?static<TListener> $parentList
* @phpstan-param RegisteredListenerCache<TListener> $handlerCache
* @phpstan-param ?\Closure(array<int, TListener>) : array<int, TListener> $sortSamePriorityHandlers
*/
public function __construct(
private string $class,
private ?HandlerList $parentList,
private RegisteredListenerCache $handlerCache = new RegisteredListenerCache(),
private ?\Closure $sortSamePriorityHandlers = null
){
for($list = $this; $list !== null; $list = $list->parentList){
$list->affectedHandlerCaches[spl_object_id($this->handlerCache)] = $this->handlerCache;
}
}
/**
* @phpstan-param TListener $listener
*/
public function register(BaseRegisteredListener $listener) : void{
if(isset($this->handlerSlots[$listener->getPriority()][spl_object_id($listener)])){
throw new \InvalidArgumentException("This listener is already registered to priority {$listener->getPriority()} of event {$this->class}");
}
$this->handlerSlots[$listener->getPriority()][spl_object_id($listener)] = $listener;
$this->invalidateAffectedCaches();
}
/**
* @param BaseRegisteredListener[] $listeners
* @phpstan-param array<TListener> $listeners
*/
public function registerAll(array $listeners) : void{
foreach($listeners as $listener){
$this->register($listener);
}
$this->invalidateAffectedCaches();
}
/**
* @phpstan-param TListener|Plugin|Listener $object
*/
public function unregister(BaseRegisteredListener|Plugin|Listener $object) : void{
if($object instanceof Plugin || $object instanceof Listener){
foreach($this->handlerSlots as $priority => $list){
foreach($list as $hash => $listener){
if(($object instanceof Plugin && $listener->getPlugin() === $object)
|| ($object instanceof Listener && (new \ReflectionFunction($listener->getHandler()))->getClosureThis() === $object) //this doesn't even need to be a listener :D
){
unset($this->handlerSlots[$priority][$hash]);
}
}
}
}else{
unset($this->handlerSlots[$object->getPriority()][spl_object_id($object)]);
}
$this->invalidateAffectedCaches();
}
public function clear() : void{
$this->handlerSlots = [];
$this->invalidateAffectedCaches();
}
/**
* @return BaseRegisteredListener[]
* @phpstan-return array<int, TListener>
*/
public function getListenersByPriority(int $priority) : array{
return $this->handlerSlots[$priority] ?? [];
}
/**
* @phpstan-return static<TListener>
*/
public function getParent() : ?HandlerList{
return $this->parentList;
}
/**
* Invalidates all known caches which might be affected by this list's contents.
*/
private function invalidateAffectedCaches() : void{
foreach($this->affectedHandlerCaches as $cache){
$cache->list = null;
}
}
/**
* @return BaseRegisteredListener[]
* @phpstan-return list<TListener>
*/
public function getListenerList() : array{
if($this->handlerCache->list !== null){
return $this->handlerCache->list;
}
$handlerLists = [];
for($currentList = $this; $currentList !== null; $currentList = $currentList->parentList){
$handlerLists[] = $currentList;
}
$listenersByPriority = [];
foreach($handlerLists as $currentList){
foreach($currentList->handlerSlots as $priority => $listeners){
$listenersByPriority[$priority] = array_merge(
$listenersByPriority[$priority] ?? [],
$this->sortSamePriorityHandlers !== null ?
($this->sortSamePriorityHandlers)($listeners) :
$listeners
);
}
}
//TODO: why on earth do the priorities have higher values for lower priority?
krsort($listenersByPriority, SORT_NUMERIC);
return $this->handlerCache->list = array_merge(...$listenersByPriority);
}
}

View File

@ -37,7 +37,7 @@ class HandlerListManager extends BaseHandlerListManager{
return Event::class;
}
protected function createHandlerList(string $event, ?BaseHandlerList $parentList, RegisteredListenerCache $handlerCache) : BaseHandlerList{
protected function createHandlerList(string $event, ?HandlerList $parentList, RegisteredListenerCache $handlerCache) : HandlerList{
return new HandlerList($event, $parentList, $handlerCache);
}
}