Skip to content

Commit 64514ca

Browse files
authored
Add files via upload
1 parent de9dd03 commit 64514ca

File tree

8 files changed

+218
-25
lines changed

8 files changed

+218
-25
lines changed

src/vennv/Error.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ final class Error
88
public const ALL_PROMISES_WERE_REJECTED = "All promises were rejected";
99

1010
public const QUEUE_IS_TIMEOUT = "Queue with %id% timed out";
11+
1112
public const QUEUE_NOT_FOUND = "Queue with %id% not found";
13+
1214
public const QUEUE_STILL_PENDING = "Queue with %id% still pending";
1315

16+
public const FAILED_TO_INITIALIZE_CURL = "Failed to initialize cURL";
17+
1418
}

src/vennv/EventQueue.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,24 @@ public static function isMaxId() : bool
5050
/**
5151
* @throws Throwable
5252
*/
53-
public static function addQueue(Fiber $fiber, bool $isPromise = false, bool $isPromiseAll = false, float $timeOut = 0.0) : int
53+
public static function addQueue(
54+
Fiber $fiber,
55+
bool $isPromise = false,
56+
bool $isPromiseAll = false,
57+
float $timeOut = 0.0
58+
) : int
5459
{
5560
$id = self::generateId();
56-
self::$queues[$id] = new Queue($id, $fiber, $timeOut, StatusQueue::PENDING, $isPromise, $isPromiseAll);
61+
62+
self::$queues[$id] = new Queue(
63+
$id,
64+
$fiber,
65+
$timeOut,
66+
StatusQueue::PENDING,
67+
$isPromise,
68+
$isPromiseAll
69+
);
70+
5771
return $id;
5872
}
5973

@@ -259,7 +273,7 @@ private static function checkStatus(int $id) : void
259273

260274
if ($queue->isPromiseAll())
261275
{
262-
if ($queue->hasCompletedAllPromise())
276+
if ($queue->hasCompletedAllPromises())
263277
{
264278
self::fulfillPromise($id);
265279
}

src/vennv/InterfaceEventQueue.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,57 @@
33
namespace vennv;
44

55
use Fiber;
6-
use Throwable;
76

87
interface InterfaceEventQueue
98
{
109

1110
/**
12-
* @deprecated This method you should not use.
11+
* This method to get the next id for the queue.
1312
*/
1413
public static function getNextId() : int;
1514

1615
/**
17-
* @deprecated This method you should not use.
16+
* This method to check if the id is the maximum.
1817
*/
1918
public static function isMaxId() : bool;
2019

2120
/**
22-
* @deprecated This method you should not use.
21+
* This method to add a queue.
2322
*/
24-
public static function addQueue(Fiber $fiber, bool $isPromise = false, bool $isPromiseAll = false, float $timeOut = 0.0) : int;
23+
public static function addQueue(
24+
Fiber $fiber,
25+
bool $isPromise = false,
26+
bool $isPromiseAll = false,
27+
float $timeOut = 0.0
28+
) : int;
2529

2630
/**
27-
* @deprecated This method you should not use.
31+
* This method to get a queue and check if it exists.
2832
*/
2933
public static function getQueue(int $id) : ?Queue;
3034

3135
/**
32-
* @deprecated This method you should not use.
36+
* This method to get result of a queue and check if it exists.
3337
*/
3438
public static function getReturn(int $id) : mixed;
3539

3640
/**
37-
* @deprecated This method you should not use.
41+
* This method to remove a result of a queue and check if it exists.
3842
*/
3943
public static function unsetReturn(int $id) : void;
4044

4145
/**
42-
* @deprecated This method you should not use.
46+
* This method to run a queue with id.
4347
*/
4448
public static function runQueue(int $id) : void;
4549

4650
/**
47-
* @deprecated This method you should not use.
51+
* This is method to reject a queue with result for id.
4852
*/
4953
public static function rejectQueue(int $id, mixed $result) : void;
5054

5155
/**
52-
* @deprecated This method you should not use.
56+
* This is method to fulfill a queue with result for id.
5357
*/
5458
public static function fulfillQueue(int $id, mixed $result) : void;
5559

src/vennv/InterfaceQueue.php

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
3+
namespace vennv;
4+
5+
use Fiber;
6+
use Throwable;
7+
8+
interface InterfaceQueue
9+
{
10+
11+
/**
12+
* This method to get the id for the queue.
13+
*/
14+
public function getId() : int;
15+
16+
/**
17+
* This method to get the fiber for the queue.
18+
*/
19+
public function getFiber() : Fiber;
20+
21+
/**
22+
* This method to get the timeout for the queue.
23+
*/
24+
public function getTimeOut() : float;
25+
26+
/**
27+
* This method to get the status for the queue.
28+
*/
29+
public function getStatus() : StatusQueue;
30+
31+
/**
32+
* This method to set the status for the queue.
33+
*/
34+
public function setStatus(StatusQueue $status) : void;
35+
36+
/**
37+
* This method to check if the queue is a promise.
38+
*/
39+
public function isPromise() : bool;
40+
41+
/**
42+
* This method to get the time start for the queue.
43+
*/
44+
public function getTimeStart() : float;
45+
46+
/**
47+
* This method to get result of a queue.
48+
*/
49+
public function getReturn() : mixed;
50+
51+
/**
52+
* This method to set result of a queue.
53+
*/
54+
public function setReturn(mixed $return) : void;
55+
56+
/**
57+
* @throws Throwable
58+
*
59+
* This method to run a callback for a queue when the queue is fulfilled.
60+
*/
61+
public function useCallableResolve(mixed $result) : void;
62+
63+
/**
64+
* This method to set a callback resolve for a queue.
65+
*/
66+
public function setCallableResolve(callable $callableResolve) : Queue;
67+
68+
/**
69+
* @throws Throwable
70+
*
71+
* This method to run a callback for a queue when the queue is rejected.
72+
*/
73+
public function useCallableReject(mixed $result) : void;
74+
75+
/**
76+
* This method to set a callback reject for a queue.
77+
*/
78+
public function setCallableReject(callable $callableReject) : Queue;
79+
80+
/**
81+
* This method to get result of a queue when the queue is resolved.
82+
*/
83+
public function getReturnResolve() : mixed;
84+
85+
/**
86+
* This method to get result of a queue when the queue is rejected.
87+
*/
88+
public function getReturnReject() : mixed;
89+
90+
/**
91+
* This method to catch result of a queue parent when the queue is resolved.
92+
*/
93+
public function thenPromise(callable $callable) : Queue;
94+
95+
/**
96+
* This method to catch result of a queue parent when the queue is rejected.
97+
*/
98+
public function catchPromise(callable $callable) : Queue;
99+
100+
/**
101+
* This method to catch result of a queue child when the queue is resolved.
102+
*/
103+
public function then(callable $callable) : Queue;
104+
105+
/**
106+
* This method to catch result of a queue child when the queue is rejected.
107+
*/
108+
public function catch(callable $callable) : Queue;
109+
110+
/**
111+
* This method to check should drop queue when the queue is resolved or rejected.
112+
*/
113+
public function canDrop() : bool;
114+
115+
/**
116+
* @return array<callable|Async|Promise>
117+
*
118+
* This method to get waiting promises.
119+
*/
120+
public function getWaitingPromises() : array;
121+
122+
/**
123+
* @param array<callable|Async|Promise> $waitingPromises
124+
*
125+
* This method to set waiting promises.
126+
*/
127+
public function setWaitingPromises(array $waitingPromises) : void;
128+
129+
/**
130+
* This method check if the queue is a promise for all.
131+
*/
132+
public function isPromiseAll() : bool;
133+
134+
/**
135+
* This method to set the queue is a promise race.
136+
*/
137+
public function setRacePromise(bool $isRacePromise) : void;
138+
139+
/**
140+
* This method to check if the queue is a promise race.
141+
*/
142+
public function isRacePromise() : bool;
143+
144+
/**
145+
* This method to set the queue is a promise any.
146+
*/
147+
public function setAnyPromise(bool $isAnyPromise) : void;
148+
149+
/**
150+
* This method to check if the queue is a promise any.
151+
*/
152+
public function isAnyPromise() : bool;
153+
154+
/**
155+
* This method to set the queue is a promise all settled.
156+
*/
157+
public function setAllSettled(bool $isAllSettled) : void;
158+
159+
/**
160+
* This method to check if the queue is a promise all settled.
161+
*/
162+
public function isAllSettled() : bool;
163+
164+
/**
165+
* @throws Throwable
166+
*
167+
* This method to check if the queue has completed all promises.
168+
*/
169+
public function hasCompletedAllPromises() : bool;
170+
171+
}

src/vennv/InterfaceSystem.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public static function setTimeout(callable $callable, int $timeout) : void;
1616
/**
1717
* @param array<int, mixed> $options
1818
*
19-
* This method is used to fetch data from a url.
19+
* This method is used to fetch data from an url.
2020
*/
2121
public static function fetch(string $url, array $options = [CURLOPT_RETURNTRANSFER => true]) : Promise;
2222

2323
/**
24-
* This method is used to fetch data from a url. But it uses file_get_contents() instead of curl.
24+
* This method is used to fetch data from an url. But it uses file_get_contents() instead of curl.
2525
*/
2626
public static function fetchJg(string $url) : Promise;
2727

src/vennv/PromiseResult.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ public function getStatus() : StatusQueue
2121
return $this->status;
2222
}
2323

24-
}
24+
}

src/vennv/Queue.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Fiber;
77
use Throwable;
88

9-
final class Queue
9+
final class Queue implements InterfaceQueue
1010
{
1111

1212
private const MAIN_QUEUE = "Main";
@@ -208,7 +208,7 @@ public function useCallableResolve(mixed $result) : void
208208
}
209209
catch (Throwable | Exception $error)
210210
{
211-
echo $error->getMessage();
211+
// TODO: Add error handler
212212
}
213213

214214
unset($this->callableResolve[self::MAIN_QUEUE]);
@@ -349,7 +349,7 @@ public function isAllSettled() : bool
349349
* @throws Throwable
350350
* @return array<PromiseResult>
351351
*/
352-
private function checkPromise(Async|Promise $promise) : array
352+
private function getResultPromise(Async|Promise $promise) : array
353353
{
354354
$results = [];
355355
$queue = EventQueue::getReturn($promise->getId());
@@ -373,7 +373,7 @@ private function checkPromise(Async|Promise $promise) : array
373373
/**
374374
* @throws Throwable
375375
*/
376-
public function hasCompletedAllPromise() : bool
376+
public function hasCompletedAllPromises() : bool
377377
{
378378
$return = false;
379379
$results = [];
@@ -401,12 +401,12 @@ public function hasCompletedAllPromise() : bool
401401

402402
if ($value instanceof Promise || $value instanceof Async)
403403
{
404-
$results = array_merge($results, $this->checkPromise($value));
404+
$results = array_merge($results, $this->getResultPromise($value));
405405
}
406406

407407
if ($result instanceof Promise || $result instanceof Async)
408408
{
409-
$results = array_merge($results, $this->checkPromise($result));
409+
$results = array_merge($results, $this->getResultPromise($result));
410410
}
411411
}
412412

@@ -509,4 +509,4 @@ public function hasCompletedAllPromise() : bool
509509
return $return;
510510
}
511511

512-
}
512+
}

src/vennv/System.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function fetch(string $url, array $options = [CURLOPT_RETURNTRANSF
2424

2525
if ($ch === false)
2626
{
27-
$reject('Failed to initialize cURL');
27+
$reject(Error::FAILED_TO_INITIALIZE_CURL);
2828
}
2929
else
3030
{

0 commit comments

Comments
 (0)