2323
2424namespace vennv \vapm ;
2525
26+ use Generator ;
2627use SplObjectStorage ;
2728use Throwable ;
2829use function count ;
@@ -44,9 +45,9 @@ public static function isQueue(int $id): bool;
4445 public static function getQueue (int $ id ): ?Promise ;
4546
4647 /**
47- * @return SplObjectStorage
48+ * @return Generator
4849 */
49- public static function getQueues (): SplObjectStorage ;
50+ public static function getQueues (): Generator ;
5051
5152 public static function addReturn (Promise $ promise ): void ;
5253
@@ -57,9 +58,9 @@ public static function isReturn(int $id): bool;
5758 public static function getReturn (int $ id ): ?Promise ;
5859
5960 /**
60- * @return SplObjectStorage
61+ * @return Generator
6162 */
62- public static function getReturns (): SplObjectStorage ;
63+ public static function getReturns (): Generator ;
6364
6465}
6566
@@ -74,14 +75,14 @@ class EventLoop implements EventLoopInterface
7475 protected static SplObjectStorage $ queues ;
7576
7677 /**
77- * @var SplObjectStorage
78+ * @var array<int, Promise>
7879 */
79- protected static SplObjectStorage $ returns ;
80+ protected static array $ returns = [];
81+ protected static bool $ isCleaningGarbage = false ;
8082
8183 public static function init (): void
8284 {
8385 if (!isset (self ::$ queues )) self ::$ queues = new SplObjectStorage ();
84- if (!isset (self ::$ returns )) self ::$ returns = new SplObjectStorage ();
8586 }
8687
8788 public static function generateId (): int
@@ -97,11 +98,8 @@ public static function addQueue(Promise $promise): void
9798
9899 public static function removeQueue (int $ id ): void
99100 {
100- /**
101- * @var Promise $promise
102- */
103101 foreach (self ::$ queues as $ promise ) {
104- if ($ promise ->getId () === $ id ) {
102+ if ($ promise instanceof Promise && $ promise ->getId () === $ id ) {
105103 self ::$ queues ->offsetUnset ($ promise );
106104 break ;
107105 }
@@ -123,57 +121,51 @@ public static function getQueue(int $id): ?Promise
123121 }
124122
125123 /**
126- * @return SplObjectStorage
124+ * @return Generator
127125 */
128- public static function getQueues (): SplObjectStorage
126+ public static function getQueues (): Generator
129127 {
130- return self ::$ queues ;
128+ foreach (self ::$ queues as $ promise ) {
129+ yield $ promise ;
130+ }
131131 }
132132
133133 public static function addReturn (Promise $ promise ): void
134134 {
135- if (!self ::getReturn ( $ promise ->getId ())) self ::$ returns-> offsetSet ( $ promise) ;
135+ if (!isset ( self ::$ returns [ $ promise ->getId ()] )) self ::$ returns[ $ promise -> getId ()] = $ promise ;
136136 }
137137
138138 public static function isReturn (int $ id ): bool
139139 {
140- /* @var Promise $promise */
141- foreach (self ::$ returns as $ promise ) if ($ promise instanceof Promise && $ promise ->getId () === $ id ) return true ;
142- return false ;
140+ return isset (self ::$ returns [$ id ]);
143141 }
144142
145143 public static function removeReturn (int $ id ): void
146144 {
147- /**
148- * @var Promise $promise
149- */
150- foreach (self ::$ returns as $ promise ) {
151- if ($ promise ->getId () === $ id ) {
152- self ::$ returns ->offsetUnset ($ promise );
153- break ;
154- }
155- }
145+ if (self ::isReturn ($ id )) unset(self ::$ returns [$ id ]);
156146 }
157147
158148 public static function getReturn (int $ id ): ?Promise
159149 {
160- /* @var Promise $promise */
161- foreach (self ::$ returns as $ promise ) if ($ promise instanceof Promise && $ promise ->getId () === $ id ) return $ promise ;
162- return null ;
150+ return self ::$ returns [$ id ] ?? null ;
163151 }
164152
165153 /**
166- * @return SplObjectStorage
154+ * @return Generator
167155 */
168- public static function getReturns (): SplObjectStorage
156+ public static function getReturns (): Generator
169157 {
170- return self ::$ returns ;
158+ foreach (self ::$ returns as $ id => $ promise ) {
159+ yield $ id => $ promise ;
160+ }
171161 }
172162
163+ /**
164+ * @throws Throwable
165+ */
173166 private static function clearGarbage (): void
174167 {
175- /* @var Promise $promise */
176- foreach (self ::$ returns as $ promise ) if ($ promise instanceof Promise && $ promise ->canDrop ()) self ::removeReturn ($ promise ->getId ());
168+ foreach (self ::getReturns () as $ id => $ promise ) if ($ promise instanceof Promise && $ promise ->canDrop ()) unset(self ::$ returns [$ id ]);
177169 }
178170
179171 /**
@@ -186,7 +178,7 @@ protected static function run(): void
186178 /**
187179 * @var Promise $promise
188180 */
189- foreach (self ::$ queues as $ promise ) {
181+ foreach (self ::getQueues () as $ promise ) {
190182 $ id = $ promise ->getId ();
191183 $ fiber = $ promise ->getFiber ();
192184
@@ -203,7 +195,7 @@ protected static function run(): void
203195 echo $ e ->getMessage ();
204196 }
205197 MicroTask::addTask ($ id , $ promise );
206- self ::removeQueue ( $ id );
198+ self ::$ queues -> offsetUnset ( $ promise ); // Remove from queue
207199 }
208200 }
209201
@@ -221,4 +213,4 @@ protected static function runSingle(): void
221213 while (count (self ::$ queues ) > 0 || count (MicroTask::getTasks ()) > 0 || count (MacroTask::getTasks ()) > 0 || count (GreenThread::getFibers ()) > 0 ) self ::run ();
222214 }
223215
224- }
216+ }
0 commit comments