-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathAsyncEventDelegate.php
More file actions
131 lines (112 loc) · 3.57 KB
/
AsyncEventDelegate.php
File metadata and controls
131 lines (112 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?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\promise\Promise;
use pocketmine\promise\PromiseResolver;
use pocketmine\utils\ObjectSet;
use function array_shift;
use function count;
final class AsyncEventDelegate extends Event{
/** @phpstan-var ObjectSet<Promise<null>> $promises */
private ObjectSet $promises;
public function __construct(
private AsyncEvent&Event $event
){
$this->promises = new ObjectSet();
}
/**
* @phpstan-return Promise<null>
*/
public function callAsync() : Promise{
$this->promises->clear();
return $this->callDepth($this->callAsyncDepth(...));
}
/**
* @phpstan-return Promise<null>
*/
private function callAsyncDepth() : Promise{
/** @phpstan-var PromiseResolver<null> $globalResolver */
$globalResolver = new PromiseResolver();
$priorities = EventPriority::ALL;
$testResolve = function () use (&$testResolve, &$priorities, $globalResolver){
if(count($priorities) === 0){
$globalResolver->resolve(""); // TODO: see #6110
}else{
$this->callPriority(array_shift($priorities))->onCompletion(function() use ($testResolve) : void{
$testResolve();
}, function () use ($globalResolver) {
$globalResolver->reject();
});
}
};
$testResolve();
return $globalResolver->getPromise();
}
/**
* @phpstan-return Promise<null>
*/
private function callPriority(int $priority) : Promise{
$handlers = HandlerListManager::global()->getListFor($this->event::class)->getListenersByPriority($priority);
/** @phpstan-var PromiseResolver<null> $resolver */
$resolver = new PromiseResolver();
$nonConcurrentHandlers = [];
foreach($handlers as $registration){
if($registration instanceof RegisteredAsyncListener){
if($registration->canBeCallConcurrently()){
$this->promises->add($registration->callAsync($this->event));
}else{
$nonConcurrentHandlers[] = $registration;
}
}else{
$registration->callEvent($this->event);
}
}
$testResolve = function() use (&$nonConcurrentHandlers, &$testResolve, $resolver){
if(count($nonConcurrentHandlers) === 0){
$this->waitForPromises()->onCompletion(function() use ($resolver){
$resolver->resolve(""); // TODO: see #6110
}, function() use ($resolver){
$resolver->reject();
});
}else{
$this->waitForPromises()->onCompletion(function() use (&$nonConcurrentHandlers, $testResolve){
$handler = array_shift($nonConcurrentHandlers);
if($handler instanceof RegisteredAsyncListener){
$this->promises->add($handler->callAsync($this->event));
}
$testResolve();
}, function() use ($resolver) {
$resolver->reject();
});
}
};
$testResolve();
return $resolver->getPromise();
}
/**
* @phpstan-return Promise<array<int, null>>
*/
private function waitForPromises() : Promise{
$array = $this->promises->toArray();
$this->promises->clear();
return Promise::all($array);
}
}