Skip to content

Instantly share code, notes, and snippets.

@trowski
Last active September 1, 2020 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trowski/75febd5008052189182a71f193034fdd to your computer and use it in GitHub Desktop.
Save trowski/75febd5008052189182a71f193034fdd to your computer and use it in GitHub Desktop.
<?php
interface Awaitable
{
/** Same as Amp\Promise */
public function onResolve(callable $onResolve): void;
}
/**
* This function returns an instance of Fiber as an implementation detail.
*
* @param callable $context
* @param mixed ...$args
*
* @return Awaitable
*/
function async(callable $context, ...$args): Awaitable { }
/**
* Suspend the current context until the given awaitable resolves. This method will add a callback to the given
* awaitable to call Fiber::resume() or Fiber::throw().
*
* @param Awaitable $awaitable
*
* @return mixed Awaitable resolution value.
*/
function await(Awaitable $awaitable): mixed { }
final class Fiber implements Awaitable
{
/**
* @param callable $onResolve
*/
public function onResolve(callable $onResolve): void { }
/**
* @internal Called by async() function.
*
* Initializes a fiber context.
*
* @param callable $context
* @param mixed ...$args
*/
private static function start(callable $context, ...$args): Fiber { }
/**
* @internal Called by await() function.
*
* Suspend the current task until the given awaitable resolves. This method will add a callback to the given
* awaitable to call Fiber::resume() or Fiber::throw().
*
* @param Awaitable $awaitable
*
* @return mixed Awaitable resolution value.
*/
private static function await(Awaitable $awaitable): mixed { }
/**
* @internal
*
* Resume the task, the given value is passed into the task as return value of await().
*/
private function resume($v = null): void { }
/**
* @internal
*
* Resume the task with an error thrown from await().
*/
private function throw(\Throwable $e): void { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment