PHP code example of sue / event-loop

1. Go to this page and download the library: Download sue/event-loop 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/ */

    

sue / event-loop example snippets


use Sue\EventLoop\loop;

$loop = loop();

//loop启动后程序就会阻塞在这
//如果loop上没有任何待处理的callback, loop则自动退出
$loop->run();

//loop停止运行
$loop->stop(); 

setInterval(1, function () {
    if (isLoopRunning()) {
        echo "Loop is running";
    }
});
loop()->run();

try {
    $promise = someIoHeavyOperation();
    $result = await($promise); //程序会阻塞在这里一直等待$promise被resolved或者rejected
    handle($result);
} catch (Throwable $e) {
    //error handle
} 

//await方法第个参数接受一个float作为promise的timeout,以免promise长时间阻塞程序流程
$result = await($promise, 10);

use Sue\EventLoop\setTimeout;
use Sue\EventLoop\loop;
use Sue\EventLoop\cancelTimer;

 //延迟5秒执行
setTimeout(5, function () {
    echo "hello world from 5 seconds ago";
});
loop()->run();

//提前终止执行
$timer = setTimeout(5, function () {
    echo "hello world from 5 seconds ago";
});
cancelTimer($timer);
loop()->run();


use React\EventLoop\TimerInterface;

use function Sue\EventLoop\setInterval;
use function Sue\EventLoop\cancelTimer;

//每60秒执行一次
setInterval(60, function () {
    echo "one minute has been passed\n";
});

// 中止运行
$timer = setInterval(1, function (string $name, int $age, TimerInterface $timer) {
    if ($some_condition) {
        cancelTimer($timer);
    }
}, 'foo', 18);
loop()->run();

use React\EventLoop\TimerInterface;

use function Sue\EventLoop\setInterval;
use function Sue\EventLoop\cancelTimer;

$timer = setInterval(1, function (TimerInterface $timer) {
        echo "working...\n";
});
if ($some_condition) {
    cancelTimer($timer);
}

use RuntimeException;
use Sue\EventLoop\Exceptions\PromiseCancelledException;

use function Sue\EventLoop\nextTick;

$promise = nextTick(function () {
    return "hello world";
});
$promise->then(function (string $content) {
    //handler
});

//异常处理
$promise = nextTick(function () {
    throw new RuntimeException('boom');
});
$promise->then(null, function (RuntimeException $e) {
    echo "error: " . $e;
});

//中止执行
$promise = nextTick(function () {
    return "hello world";
});
if ($some_condition) {
    $promise->cancel();
}
$promise->otherwise(function (PromiseCancelledException $exception) {
    //exception
});

use Sue\EventLoop\loop;
use Sue\EventLoop\throttle;
use Sue\EventLoop\setInterval;

$callback = function () {
    echo "hello world\n";
};
setInterval(1, function () use ($callback) {
    static $count = 10;
    while ($count--) {
        echo "tick\n";
        throttle(3, $callback);
    }
});
loop()->run();

/** expect output
tick
tick
tick
hello world
tick
tick
tick
hello world
tick
tick
tick
hello world
tick
**/

//如果想提前手动中止
$promise = throttle(3, $callback);
if ($some_condition) {
    $promise->cancel();
}
$promise->otherwise(function (PromiseCancelledException $exception) {
    //exception
});

use Sue\EventLoop\loop;
use Sue\EventLoop\debounce;
use Sue\EventLoop\setInterval;

$callback = function () {
    echo "hello world\n";
};
debounce(1, function () use ($callback) {
    static $count = 10;
    while ($count--) {
        echo "tick\n";
        throttle(3, $callback);
    }
});
loop()->run();
/** expect output
tick
tick
tick
tick
tick
tick
tick
tick
tick
tick
hello world
**/

//如果想提前中止
$promise = debounce(1, $callback);
if ($some_condition) {
    $promise->cancel();
}
$promise->otherwise(function (PromiseCancelledException $exception) {
    //exception
});