HandlerList: improve listener list development to make way for #5678

This commit is contained in:
Dylan K. Taylor 2023-04-07 22:55:27 +01:00
parent d6c923b525
commit 3ea8d27a3b
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 14 additions and 10 deletions

View File

@ -32,6 +32,8 @@ use function mb_strtoupper;
* LOWEST -> LOW -> NORMAL -> HIGH -> HIGHEST -> MONITOR * LOWEST -> LOW -> NORMAL -> HIGH -> HIGHEST -> MONITOR
* *
* MONITOR events should not change the event outcome or contents * MONITOR events should not change the event outcome or contents
*
* WARNING: If these values are changed, handler sorting in HandlerList::getListenerList() may need to be updated.
*/ */
final class EventPriority{ final class EventPriority{

View File

@ -24,8 +24,10 @@ declare(strict_types=1);
namespace pocketmine\event; namespace pocketmine\event;
use pocketmine\plugin\Plugin; use pocketmine\plugin\Plugin;
use function array_fill_keys; use function array_merge;
use function krsort;
use function spl_object_id; use function spl_object_id;
use const SORT_NUMERIC;
class HandlerList{ class HandlerList{
/** @var RegisteredListener[][] */ /** @var RegisteredListener[][] */
@ -48,8 +50,6 @@ class HandlerList{
for($list = $this; $list !== null; $list = $list->parentList){ for($list = $this; $list !== null; $list = $list->parentList){
$list->affectedHandlerCaches[spl_object_id($this->handlerCache)] = $this->handlerCache; $list->affectedHandlerCaches[spl_object_id($this->handlerCache)] = $this->handlerCache;
} }
$this->handlerSlots = array_fill_keys(EventPriority::ALL, []);
} }
/** /**
@ -94,7 +94,7 @@ class HandlerList{
} }
public function clear() : void{ public function clear() : void{
$this->handlerSlots = array_fill_keys(EventPriority::ALL, []); $this->handlerSlots = [];
$this->invalidateAffectedCaches(); $this->invalidateAffectedCaches();
} }
@ -132,15 +132,17 @@ class HandlerList{
$handlerLists[] = $currentList; $handlerLists[] = $currentList;
} }
$listeners = []; $listenersByPriority = [];
foreach(EventPriority::ALL as $priority){
foreach($handlerLists as $currentList){ foreach($handlerLists as $currentList){
foreach($currentList->getListenersByPriority($priority) as $registration){ foreach($currentList->handlerSlots as $priority => $listeners){
$listeners[] = $registration; $listenersByPriority[$priority] = array_merge($listenersByPriority[$priority] ?? [], $listeners);
}
} }
} }
//TODO: why on earth do the priorities have higher values for lower priority?
krsort($listenersByPriority, SORT_NUMERIC);
$listeners = array_merge(...$listenersByPriority);
return $this->handlerCache->list = $listeners; return $this->handlerCache->list = $listeners;
} }
} }