Skip to content

Commit 1a70f3a

Browse files
committed
More docs
1 parent 2cfca75 commit 1a70f3a

File tree

1 file changed

+78
-7
lines changed

1 file changed

+78
-7
lines changed

README.md

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ $suspension->suspend();
114114
print '++ Script end' . PHP_EOL;
115115
```
116116

117-
Callbacks registered on the Revolt event-loop are automatically run as coroutines and it's safe to suspend them. Apart from the event-loop API, `Amp\async()` can be used to start an independent call stack.
117+
Callbacks registered on the Revolt event-loop are automatically run as coroutines. It is safe to suspend within those
118+
callbacks. Apart from the event-loop API, `Amp\async()` can be used to start a coroutine (that is, a new fiber or an
119+
independent call stack).
118120

119121
```php
120122
<?php
@@ -138,7 +140,7 @@ print '++ Script end' . PHP_EOL;
138140
### Future
139141

140142
A `Future` is an object representing the eventual result of an asynchronous operation. Such placeholders are also
141-
called "promises" in other frameworks or languages such as JavaScript. We chose to not use the "promises" name as a
143+
called a "promise" in other frameworks or languages such as JavaScript. We chose to not use the "promise" name since a
142144
`Future` does not have a `then` method, which is typical of most promise implementations. Futures are primarily designed
143145
to be awaited in coroutines, though `Future` also has methods which act upon the result, returning another future.
144146

@@ -166,8 +168,8 @@ The callback approach has several drawbacks.
166168

167169
- Passing callbacks and doing further actions in them that depend on the result of the first action gets messy really
168170
quickly.
169-
- An explicit callback is required as input parameter to the function, and the return value is simply unused. There's no
170-
way to use this API without involving a callback.
171+
- An explicit callback is required as input parameter to the function, and the return value is simply unused. There's
172+
no way to use this API without involving a callback.
171173

172174
That's where futures come into play.
173175
They're placeholders for the result that are returned like any other return value.
@@ -304,9 +306,9 @@ Once result is ready, you complete the `Future` held by the caller using `comple
304306
```php
305307
final class DeferredFuture
306308
{
307-
public function getFuture(): Future;
308-
public function complete(mixed $value = null);
309-
public function error(Throwable $throwable);
309+
public function getFuture(): Future
310+
public function complete(mixed $value = null): void
311+
public function error(Throwable $throwable): void
310312
}
311313
```
312314

@@ -406,6 +408,75 @@ $cancellation ??= new NullCancellationToken();
406408

407409
A `CompositeCancellation` combines multiple independent cancellation objects. If any of these cancellations is cancelled, the `CompositeCancellation` itself will be cancelled.
408410

411+
### Utilities
412+
413+
Several utility functions and classes are also included in this library.
414+
415+
```php
416+
function delay(float $timeout, bool $reference = true, ?Cancellation $cancellation = null): void
417+
```
418+
419+
`delay` suspends the current coroutine (fiber) until the given timeout has elapsed or, if provided, the cancellation
420+
is cancelled. Optionally, the underlying event-loop callback may be unreferenced, allowing the event-loop to exit
421+
if no other referenced events are active.
422+
423+
```php
424+
/** @param int|array<int> $signals */
425+
function trapSignal(int|array $signals, bool $reference = true, ?Cancellation $cancellation = null): int
426+
```
427+
428+
`trapSignal` suspends the current coroutine (fiber) until one of the given signals is received by the process or, if
429+
provided, the cancellation is cancelled. Optionally, the underlying event-loop callback may be unreferenced, allowing
430+
the event-loop to exit if no other referenced events are active. The signal number of the received signal is returned.
431+
432+
```php
433+
now(): float
434+
```
435+
436+
`now` returns a high-resolution time relative to an arbitrary point in time. This function may be used to calculate
437+
time differences independent of wall-time.
438+
439+
```php
440+
/**
441+
* @template TReturn
442+
* @param Closure(...):TReturn $closure
443+
* @return Closure(...):TReturn
444+
*/
445+
function weakClosure(\Closure $closure): \Closure
446+
```
447+
448+
`weakClosure` wraps a given closure, returning a new `Closure` instance which maintains a weak-reference to any
449+
`$this` object held by the closure (a weak-closure). This allows a class instance to hold a self-referencing closure
450+
without creating a circular-reference that would prevent or delay automatic garbage collection. Invoking the returned
451+
`Closure` after the object is destroyed will throw an instance of `Error`.
452+
453+
#### Interval
454+
455+
An `Interval` registers a callback in the event-loop which is invoked within a new coroutine every given number of
456+
seconds until either the `Interval::disable()` method is called or the object is destroyed. If an `Interval` is
457+
disabled, it can be re-enabled using `Interval::enable()`.
458+
459+
Holding an instance of `Interval` within an instance of another class is a convenient way to run a repeating timer
460+
during the existence of that object. When the holding object is destroyed, the instance of `Interval` will also be
461+
destroyed, cancelling the repeating timer in the event-loop. Use `weakClosure()` to avoid having a circular reference
462+
to the holding object, which will delay garbage collection of the holding object.
463+
464+
```php
465+
// Creates a callback which is invoked every 0.5s
466+
// unless disabled or the object is destroyed.
467+
$interval = new Interval(0.5, function (): void {
468+
// ...
469+
});
470+
471+
// Disable the repeating timer, stopping future
472+
// invocations until enabled again.
473+
$interval->disable();
474+
475+
// Enable the repeating timer. The callback will
476+
// not be invoked until the given timeout has elapsed.
477+
$interval->enable();
478+
```
479+
409480
## Versioning
410481

411482
`amphp/amp` follows the [semver](http://semver.org/) semantic versioning specification like all other `amphp` packages.

0 commit comments

Comments
 (0)