mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-12 14:35:35 +00:00
handlerListe: reduce code complexity
This commit is contained in:
parent
243a3035ba
commit
aaa37baf2e
@ -24,11 +24,10 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\event;
|
namespace pocketmine\event;
|
||||||
|
|
||||||
use pocketmine\plugin\Plugin;
|
use pocketmine\plugin\Plugin;
|
||||||
use function array_filter;
|
|
||||||
use function array_merge;
|
use function array_merge;
|
||||||
|
use function array_merge_recursive;
|
||||||
use function krsort;
|
use function krsort;
|
||||||
use function spl_object_id;
|
use function spl_object_id;
|
||||||
use function usort;
|
|
||||||
use const SORT_NUMERIC;
|
use const SORT_NUMERIC;
|
||||||
|
|
||||||
class HandlerList{
|
class HandlerList{
|
||||||
@ -129,24 +128,23 @@ class HandlerList{
|
|||||||
$handlerLists[] = $currentList;
|
$handlerLists[] = $currentList;
|
||||||
}
|
}
|
||||||
|
|
||||||
$listenersByPriority = [];
|
$listeners = [];
|
||||||
$asyncListenersByPriority = [];
|
$asyncListeners = [];
|
||||||
|
$exclusiveAsyncListeners = [];
|
||||||
foreach($handlerLists as $currentList){
|
foreach($handlerLists as $currentList){
|
||||||
foreach($currentList->handlerSlots as $priority => $listeners){
|
foreach($currentList->handlerSlots as $priority => $listenersToSort){
|
||||||
$syncListeners = array_filter($listeners, static function(RegisteredListener $listener) : bool{ return !($listener instanceof RegisteredAsyncListener); });
|
foreach($listenersToSort as $listener){
|
||||||
$listenersByPriority[$priority] = array_merge($listenersByPriority[$priority] ?? [], $syncListeners);
|
if(!$listener instanceof RegisteredAsyncListener){
|
||||||
|
$listeners[$priority][] = $listener;
|
||||||
$asyncListeners = array_filter($listeners, static function(RegisteredListener $listener) : bool{ return $listener instanceof RegisteredAsyncListener; });
|
}elseif(!$listener->canBeCalledConcurrently()){
|
||||||
$asyncListenersByPriority[$priority] = array_merge($asyncListenersByPriority[$priority] ?? [], $asyncListeners);
|
$asyncListeners[$priority][] = $listener;
|
||||||
|
}else{
|
||||||
|
$exclusiveAsyncListeners[$priority][] = $listener;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach($asyncListenersByPriority as $priority => $asyncListeners){
|
$listenersByPriority = array_merge_recursive($listeners, $asyncListeners, $exclusiveAsyncListeners);
|
||||||
usort($asyncListeners, static function(RegisteredAsyncListener $a, RegisteredAsyncListener $b) : int{
|
|
||||||
// concurrent listeners are sorted to the end of the list
|
|
||||||
return $b->canBeCalledConcurrently() <=> $a->canBeCalledConcurrently();
|
|
||||||
});
|
|
||||||
$listenersByPriority[$priority] = array_merge($listenersByPriority[$priority] ?? [], $asyncListeners);
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: why on earth do the priorities have higher values for lower priority?
|
//TODO: why on earth do the priorities have higher values for lower priority?
|
||||||
krsort($listenersByPriority, SORT_NUMERIC);
|
krsort($listenersByPriority, SORT_NUMERIC);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user