Skip to content

Commit

Permalink
Update the code & add version name
Browse files Browse the repository at this point in the history
  • Loading branch information
Mcbeany committed Jun 25, 2022
1 parent c4dac32 commit 29a0c9c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 62 deletions.
31 changes: 16 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"name": "mcbeany/libasync",
"description": "Stupid async implementation using await-generator",
"type": "library",
"license": "GPL-3.0-only",
"require": {
"sof3/await-generator": "^3.4"
},
"require-dev": {
"pocketmine/pocketmine-mp": "^4.5"
},
"autoload": {
"psr-0": {
"Mcbeany\\libAsync": "src/"
}
}
"name": "mcbeany/libasync",
"version": "0.0.1",
"description": "Stupid async implementation using await-generator",
"type": "library",
"license": "GPL-3.0-only",
"require": {
"sof3/await-generator": "^3.4"
},
"require-dev": {
"pocketmine/pocketmine-mp": "^4.5"
},
"autoload": {
"psr-0": {
"Mcbeany\\libAsync": "src/"
}
}
}
94 changes: 47 additions & 47 deletions src/Mcbeany/libAsync/libAsync.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,53 @@
use Throwable;

final class libAsync {
/**
* Call a function asynchronously.
*
* @template T
* @param Closure(): T @executor
*
* @return Generator<mixed, mixed, mixed, T>
*/
public static function doAsync(Closure $executor): Generator {
$closure = function (Closure $resolve, Closure $reject) use ($executor): void {
$task = new class($executor, $resolve, $reject) extends AsyncTask {
private bool $rejected = false;
public function __construct(
private Closure $executor,
Closure $resolve,
Closure $reject
) {
$this->storeLocal('resolve', $resolve);
$this->storeLocal('reject', $reject);
}
/**
* Call a function asynchronously.
*
* @template T
* @param Closure(): T $executor
*
* @return Generator<mixed, mixed, mixed, T>
*/
public static function doAsync(Closure $executor): Generator {
$resolve = yield Await::RESOLVE;
$reject = yield Await::REJECT;
$task = new class($executor, $resolve, $reject) extends AsyncTask {
private bool $rejected = false;
public function __construct(
private Closure $executor,
Closure $resolve,
Closure $reject
) {
$this->storeLocal('resolve', $resolve);
$this->storeLocal('reject', $reject);
}

public function onRun(): void {
try {
$result = ($this->executor)();
} catch (Throwable $e) {
$this->rejected = true;
$result = $e;
}
$this->setResult($result);
}
public function onRun(): void {
try {
$result = ($this->executor)();
} catch (Throwable $e) {
$this->rejected = true;
$result = $e;
}
$this->setResult($result);
}

public function onCompletion(): void {
/** @var T|Throwable $result */
$result = $this->getResult();
if ($this->rejected && $result instanceof Throwable) {
/** @var Closure(Throwable): void */
$reject = $this->fetchLocal('reject');
$reject($result);
return;
}
/** @var Closure(T): void $resolve */
$resolve = $this->fetchLocal('resolve');
$resolve($result);
}
};
Server::getInstance()->getAsyncPool()->submitTask($task);
};
return yield from Await::promise($closure);
}
public function onCompletion(): void {
/** @var T|Throwable $result */
$result = $this->getResult();
if ($this->rejected && $result instanceof Throwable) {
/** @var Closure(Throwable): void */
$reject = $this->fetchLocal('reject');
$reject($result);
return;
}
/** @var Closure(T): void $resolve */
$resolve = $this->fetchLocal('resolve');
$resolve($result);
}
};
Server::getInstance()->getAsyncPool()->submitTask($task);
return yield Await::ONCE;
}
}

0 comments on commit 29a0c9c

Please sign in to comment.