1919 * GNU General Public License for more details.
2020 */
2121
22- declare (strict_types = 1 );
22+ declare (strict_types= 1 );
2323
2424namespace vennv \vapm ;
2525
3030use Throwable ;
3131use function call_user_func ;
3232
33- interface CoroutineGenInterface {
33+ interface CoroutineGenInterface
34+ {
3435
3536 /**
3637 * @param mixed ...$coroutines
3738 * @return void
3839 *
3940 * This is a blocking function that runs all the coroutines passed to it.
4041 */
41- public static function runBlocking (mixed ...$ coroutines ) : void ;
42+ public static function runBlocking (mixed ...$ coroutines ): void ;
4243
4344 /**
4445 * @param callable $callback
@@ -47,40 +48,40 @@ public static function runBlocking(mixed ...$coroutines) : void;
4748 *
4849 * This is a generator that runs a callback function a specified amount of times.
4950 */
50- public static function repeat (callable $ callback , int $ times ) : Closure ;
51+ public static function repeat (callable $ callback , int $ times ): Closure ;
5152
5253 /**
5354 * @param int $milliseconds
5455 * @return Generator
5556 *
5657 * This is a generator that yields for a specified amount of milliseconds.
5758 */
58- public static function delay (int $ milliseconds ) : Generator ;
59+ public static function delay (int $ milliseconds ): Generator ;
5960
6061 /**
6162 * @param mixed ...$callback
6263 * @return CoroutineScope
6364 *
6465 * This is a generator that runs a callback function.
6566 */
66- public static function launch (mixed ...$ callback ) : CoroutineScope ;
67+ public static function launch (mixed ...$ callback ): CoroutineScope ;
6768
6869}
6970
70- final class CoroutineGen implements CoroutineGenInterface {
71+ final class CoroutineGen implements CoroutineGenInterface
72+ {
7173
7274 protected static ?SplQueue $ taskQueue = null ;
7375
7476 /**
75- * @throws Throwable
7677 * @param mixed ...$coroutines
7778 * @return void
79+ * @throws Throwable
7880 */
79- public static function runBlocking (mixed ...$ coroutines ) : void {
81+ public static function runBlocking (mixed ...$ coroutines ): void
82+ {
8083 foreach ($ coroutines as $ coroutine ) {
81- if (is_callable ($ coroutine )) {
82- $ coroutine = call_user_func ($ coroutine );
83- }
84+ if (is_callable ($ coroutine )) $ coroutine = call_user_func ($ coroutine );
8485
8586 if ($ coroutine instanceof CoroutineScope) {
8687 self ::schedule ($ coroutine );
@@ -98,85 +99,69 @@ public static function runBlocking(mixed ...$coroutines) : void {
9899 * @param mixed ...$coroutines
99100 * @return Closure
100101 */
101- private static function processCoroutine (mixed ...$ coroutines ) : Closure {
102- return function () use ($ coroutines ) : void {
102+ private static function processCoroutine (mixed ...$ coroutines ): Closure
103+ {
104+ return function () use ($ coroutines ): void {
103105 foreach ($ coroutines as $ coroutine ) {
104106 if ($ coroutine instanceof CoroutineScope) {
105107 self ::schedule ($ coroutine );
106108 } else if (is_callable ($ coroutine )) {
107109 $ coroutine = call_user_func ($ coroutine );
108110 }
109111
110- if (!$ coroutine instanceof Generator) {
111- call_user_func (fn () => $ coroutine );
112- } else {
113- self ::schedule (new ChildCoroutine ($ coroutine ));
114- }
112+ !$ coroutine instanceof Generator ? call_user_func (fn () => $ coroutine ) : self ::schedule (new ChildCoroutine ($ coroutine ));
115113 }
116114
117115 self ::run ();
118116 };
119117 }
120118
121- public static function repeat (callable $ callback , int $ times ) : Closure {
122- for ($ i = 0 ; $ i <= $ times ; $ i ++) {
123- if (call_user_func ($ callback ) instanceof Generator) {
124- $ callback = self ::processCoroutine ($ callback );
125- }
126- }
127-
119+ public static function repeat (callable $ callback , int $ times ): Closure
120+ {
121+ for ($ i = 0 ; $ i <= $ times ; $ i ++) if (call_user_func ($ callback ) instanceof Generator) $ callback = self ::processCoroutine ($ callback );
128122 return fn () => null ;
129123 }
130124
131- public static function delay (int $ milliseconds ) : Generator {
132- for ($ i = 0 ; $ i < GeneratorManager::calculateSeconds ($ milliseconds ); $ i ++) {
133- yield ;
134- }
125+ public static function delay (int $ milliseconds ): Generator
126+ {
127+ for ($ i = 0 ; $ i < GeneratorManager::calculateSeconds ($ milliseconds ); $ i ++) yield ;
135128 }
136129
137130 /**
138131 * @throws ReflectionException
139132 * @throws Throwable
140133 */
141- public static function launch (mixed ...$ callback ) : CoroutineScope {
134+ public static function launch (mixed ...$ callback ): CoroutineScope
135+ {
142136 $ coroutine = new CoroutineScope ();
143137 $ coroutine ->launch (...$ callback );
144138
145139 return $ coroutine ;
146140 }
147141
148- private static function schedule (ChildCoroutine |CoroutineScope $ childCoroutine ) : void {
149- if (self ::$ taskQueue === null ) {
150- self ::$ taskQueue = new SplQueue ();
151- }
152-
142+ private static function schedule (ChildCoroutine |CoroutineScope $ childCoroutine ): void
143+ {
144+ if (self ::$ taskQueue === null ) self ::$ taskQueue = new SplQueue ();
153145 self ::$ taskQueue ->enqueue ($ childCoroutine );
154146 }
155147
156148 /**
157149 * @throws ReflectionException
158150 * @throws Throwable
159151 */
160- private static function run () : void {
161- if (self ::$ taskQueue !== null ) {
162- while (!self ::$ taskQueue ->isEmpty ()) {
163- $ coroutine = self ::$ taskQueue ->dequeue ();
164-
165- if ($ coroutine instanceof ChildCoroutine) {
166- $ coroutine ->run ();
167-
168- if (!$ coroutine ->isFinished ()) {
169- self ::schedule ($ coroutine );
170- }
171- }
172-
173- if ($ coroutine instanceof CoroutineScope) {
174- $ coroutine ->run ();
152+ private static function run (): void
153+ {
154+ while (self ::$ taskQueue ?->isEmpty() === false ) {
155+ $ coroutine = self ::$ taskQueue ->dequeue ();
156+
157+ if ($ coroutine instanceof ChildCoroutine) {
158+ $ coroutine ->run ();
159+ if (!$ coroutine ->isFinished ()) self ::schedule ($ coroutine );
160+ }
175161
176- if (!$ coroutine ->isFinished ()) {
177- self ::schedule ($ coroutine );
178- }
179- }
162+ if ($ coroutine instanceof CoroutineScope) {
163+ $ coroutine ->run ();
164+ if (!$ coroutine ->isFinished ()) self ::schedule ($ coroutine );
180165 }
181166 }
182167 }
0 commit comments