PHP code example of rdlowrey / alert

1. Go to this page and download the library: Download rdlowrey/alert library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rdlowrey / alert example snippets




use Revolt\EventLoop;

Suspension();

EventLoop::delay(5, function () use ($suspension): void {
    print '++ Executing callback created by EventLoop::delay()' . PHP_EOL;

    $suspension->resume(null);
});

print '++ Suspending to event loop...' . PHP_EOL;

$suspension->suspend();

print '++ Script end' . PHP_EOL;



sync(function () {
    print '++ Executing callback passed to async()' . PHP_EOL;

    Amp\delay(3);

    print '++ Finished callback passed to async()' . PHP_EOL;
});

print '++ Suspending to event loop...' . PHP_EOL;
Amp\delay(5);

print '++ Script end' . PHP_EOL;

doSomething(function ($error, $value) {
    if ($error) {
        /* ... */
    } else {
        /* ... */
    }
});

try {
    $value = doSomething()->await();
} catch (...) {
    /* ... */
}

public function await(): mixed

/** @param Closure(mixed $value): mixed $map */
public function map(Closure $map): Future

/** @param Closure(Throwable $exception): mixed $catch */
public function catch(Closure $catch): Future

/** @param Closure(): void $finally */
public function finally(Closure $finally): Future



use Amp\Future;
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\Request;

ogle.com",
    "news"   => "https://news.google.com",
    "bing"   => "https://www.bing.com",
    "yahoo"  => "https://www.yahoo.com",
];

try {
    $responses = Future\await(array_map(function ($uri) use ($httpClient) {
        return Amp\async(fn () => $httpClient->request(new Request($uri, 'HEAD')));
    }, $uris));

    foreach ($responses as $key => $response) {
        printf(
            "%s | HTTP/%s %d %s\n",
            $key,
            $response->getProtocolVersion(),
            $response->getStatus(),
            $response->getReason()
        );
    }
} catch (Exception $e) {
    // If any one of the requests fails the combo will fail
    echo $e->getMessage(), "\n";
}

final class DeferredFuture
{
    public function getFuture(): Future
    public function complete(mixed $value = null): void
    public function error(Throwable $throwable): void
}

 // Example async producer using DeferredFuture

use Amp\Future;
use Revolt\EventLoop;

   // Complete the async result one second from now
    EventLoop::delay(1, function () use ($deferred, $x, $y) {
        $deferred->complete($x * $y);
    });

    return $deferred->getFuture();
}

$future = asyncMultiply(6, 7);
$result = $future->await();

var_dump($result); // int(42)

request("...", new Amp\TimeoutCancellation(30));

request("...", new Amp\SignalCancellation(SIGINT));

$deferredCancellation = new Amp\DeferredCancellation();

// Register some custom callback somewhere
onSomeEvent(fn () => $deferredCancellation->cancel());

request("...", $deferredCancellation->getCancellation());

$cancellation ??= new NullCancellationToken();

function delay(
    float $timeout,
    bool $reference = true,
    ?Cancellation $cancellation = null,
): void

/** @param int|array<int> $signals */
function trapSignal(
    int|array $signals,
    bool $reference = true,
    ?Cancellation $cancellation = null,
): int

function now(): float

/**
 * @template TReturn
 * @param Closure(...):TReturn $closure
 * @return Closure(...):TReturn
 */
function weakClosure(Closure $closure): Closure

// Creates a callback which is invoked every 0.5s
// unless disabled or the object is destroyed.
$interval = new Interval(0.5, function (): void {
    // ...
});

// Disable the repeating timer, stopping future
// invocations until enabled again.
$interval->disable();

// Enable the repeating timer. The callback will
// not be invoked until the given timeout has elapsed.
$interval->enable();