3
3
namespace Rcalicdan \FiberAsync \Async \Interfaces ;
4
4
5
5
use Rcalicdan \FiberAsync \Promise \Interfaces \PromiseInterface ;
6
+ use Throwable ;
6
7
7
8
/**
8
9
* Provides core asynchronous operations for fiber-based async programming.
@@ -15,23 +16,24 @@ interface AsyncOperationsInterface
15
16
/**
16
17
* Checks if the current execution context is within a fiber.
17
18
*
18
- * @return bool True if executing within a fiber, false otherwise
19
+ * @return bool True if executing within a fiber, false otherwise.
19
20
*/
20
21
public function inFiber (): bool ;
21
22
22
23
/**
23
24
* Creates a resolved promise with the given value.
24
25
*
25
- * @param mixed $value The value to resolve the promise with
26
- * @return PromiseInterface A promise resolved with the provided value
26
+ * @template T
27
+ * @param T $value The value to resolve the promise with.
28
+ * @return PromiseInterface<T> A promise resolved with the provided value.
27
29
*/
28
30
public function resolve (mixed $ value ): PromiseInterface ;
29
31
30
32
/**
31
33
* Creates a rejected promise with the given reason.
32
34
*
33
- * @param mixed $reason The reason for rejection (typically an exception or error message)
34
- * @return PromiseInterface A promise rejected with the provided reason
35
+ * @param mixed $reason The reason for rejection (typically an exception or error message).
36
+ * @return PromiseInterface<mixed> A promise rejected with the provided reason.
35
37
*/
36
38
public function reject (mixed $ reason ): PromiseInterface ;
37
39
@@ -41,8 +43,8 @@ public function reject(mixed $reason): PromiseInterface;
41
43
* The returned callable will execute the original function within a fiber
42
44
* and return a promise that resolves with the function's result.
43
45
*
44
- * @param callable $asyncFunction The function to wrap asynchronously
45
- * @return callable A callable that returns a PromiseInterface
46
+ * @param callable $asyncFunction The function to wrap asynchronously.
47
+ * @return callable(): PromiseInterface<mixed> A callable that returns a PromiseInterface.
46
48
*/
47
49
public function async (callable $ asyncFunction ): callable ;
48
50
@@ -52,18 +54,18 @@ public function async(callable $asyncFunction): callable;
52
54
* This method should only be called within a fiber context.
53
55
* It will yield control back to the event loop until the promise settles.
54
56
*
55
- * @param PromiseInterface $promise The promise to await
56
- * @return mixed The resolved value of the promise
57
- *
58
- * @throws mixed The rejection reason if the promise is rejected
57
+ * @template T
58
+ * @param PromiseInterface<T> $promise The promise to await.
59
+ * @return T The resolved value of the promise.
60
+ * @throws Throwable The rejection reason if the promise is rejected.
59
61
*/
60
62
public function await (PromiseInterface $ promise ): mixed ;
61
63
62
64
/**
63
- * Creates a promise that resolves after the specified delay.
65
+ * Creates a promise that resolves with null after the specified delay.
64
66
*
65
- * @param float $seconds The delay in seconds (supports fractions for milliseconds)
66
- * @return PromiseInterface A promise that resolves after the delay
67
+ * @param float $seconds The delay in seconds (supports fractions for milliseconds).
68
+ * @return PromiseInterface<null> A promise that resolves with null after the delay.
67
69
*/
68
70
public function delay (float $ seconds ): PromiseInterface ;
69
71
@@ -73,8 +75,8 @@ public function delay(float $seconds): PromiseInterface;
73
75
* Returns a promise that resolves with an array of all resolved values
74
76
* in the same order as the input promises, or rejects with the first rejection.
75
77
*
76
- * @param array $promises Array of PromiseInterface instances
77
- * @return PromiseInterface A promise that resolves with an array of results
78
+ * @param array<PromiseInterface<mixed>> $promises Array of PromiseInterface instances.
79
+ * @return PromiseInterface<array<mixed>> A promise that resolves with an array of results.
78
80
*/
79
81
public function all (array $ promises ): PromiseInterface ;
80
82
@@ -84,8 +86,8 @@ public function all(array $promises): PromiseInterface;
84
86
* The returned promise will resolve or reject with the value/reason
85
87
* of whichever input promise settles first.
86
88
*
87
- * @param array $promises Array of PromiseInterface instances
88
- * @return PromiseInterface A promise that settles with the first settled promise
89
+ * @param array<PromiseInterface<mixed>> $promises Array of PromiseInterface instances.
90
+ * @return PromiseInterface<mixed> A promise that settles with the first settled promise.
89
91
*/
90
92
public function race (array $ promises ): PromiseInterface ;
91
93
@@ -95,9 +97,9 @@ public function race(array $promises): PromiseInterface;
95
97
* Limits the number of simultaneously executing tasks while ensuring
96
98
* all tasks eventually complete.
97
99
*
98
- * @param array $tasks Array of callable tasks that return promises
99
- * @param int $concurrency Maximum number of concurrent executions (default: 10)
100
- * @return PromiseInterface A promise that resolves when all tasks complete
100
+ * @param array<callable(): PromiseInterface<mixed>> $tasks Array of callable tasks that return promises.
101
+ * @param int $concurrency Maximum number of concurrent executions (default: 10).
102
+ * @return PromiseInterface<array<mixed>> A promise that resolves with an array of all results when all tasks complete.
101
103
*/
102
104
public function concurrent (array $ tasks , int $ concurrency = 10 ): PromiseInterface ;
103
105
@@ -107,10 +109,10 @@ public function concurrent(array $tasks, int $concurrency = 10): PromiseInterfac
107
109
* This method processes tasks in smaller batches, allowing for
108
110
* controlled concurrency and resource management.
109
111
*
110
- * @param array $tasks Array of tasks (callables or promises) to execute
111
- * @param int $batchSize Size of each batch to process concurrently
112
- * @param int $concurrency Maximum number of concurrent executions per batch
113
- * @return PromiseInterface A promise that resolves with all results
112
+ * @param array<callable(): PromiseInterface<mixed>> $tasks Array of tasks (callables that return promises) to execute.
113
+ * @param int $batchSize Size of each batch to process concurrently.
114
+ * @param int|null $concurrency Maximum number of concurrent executions per batch.
115
+ * @return PromiseInterface<array<mixed>> A promise that resolves with all results.
114
116
*/
115
117
public function batch (array $ tasks , int $ batchSize = 10 , ?int $ concurrency = null ): PromiseInterface ;
116
- }
118
+ }
0 commit comments