-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add async macros to common Facade methods (#89)
* ✨ Add async macros for common methods to the `Cache`, `Http`, `Storage` and `File` facades * ✨ Create an `CanAsync` trait to easily perform async tasks in the event loop
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Laracord\Concerns; | ||
|
||
use Exception; | ||
use React\Promise\Promise; | ||
|
||
trait CanAsync | ||
{ | ||
/** | ||
* Perform an asynchronous operation. | ||
*/ | ||
public static function handleAsync(callable $callback): Promise | ||
{ | ||
return new Promise(function ($resolve, $reject) use ($callback) { | ||
if (! $loop = app('bot')?->getLoop()) { | ||
throw new Exception('The Laracord event loop is not available.'); | ||
} | ||
|
||
$loop->futureTick(function () use ($callback, $resolve, $reject) { | ||
try { | ||
$result = $callback(); | ||
$resolve($result); | ||
} catch (Exception $e) { | ||
$reject($e); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Perform an asynchronous operation. | ||
*/ | ||
public function async(callable $callback): Promise | ||
{ | ||
return static::async($callback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters