icicleio/react-adapter

将 Icicle 的事件循环和 Promise 与兼容 React 组件接口进行适配。

v0.4.0 2015-12-07 21:38 UTC

This package is auto-updated.

Last update: 2024-09-14 03:25:52 UTC


README

@icicleio on Twitter Build Status Coverage Status Semantic Version MIT License

这个库促进了为 ReactIcicle 构建的组件之间的互操作性。这个库提供了一个适配器,用于两个库之间不同的事件循环和 Promise 实现。

要求
  • PHP 5.5+
安装

推荐的安装方式是使用 Composer 包管理器。(有关安装和使用 Composer 的信息,请参阅 Composer 安装指南。)

运行以下命令以在项目中使用此库

composer require icicleio/react-adapter

您还可以手动编辑 composer.json 以将此库作为项目需求添加。

// composer.json
{
    "require": {
        "icicleio/react-adapter": "^0.4"
    }
}

ReactLoop

Icicle\ReactAdapter\Loop\ReactLoop 是 React 事件循环的直接替代品。它与活动的 Icicle 事件循环进行通信,以提供相同的功能。该类实现了 React\EventLoop\LoopInterface,因此可以与任何需要 React 事件循环的组件一起使用。

use Icicle\ReactAdapter\Loop\ReactLoop;
use Predis\Async\Client;

// Create the loop adapter.
$loop = new ReactLoop();

// $loop can be used anywhere an instance of React\EventLoop\LoopInterface is required.
$client = new Client('tcp://127.0.0.1:6379', $loop);

ReactPromise

Icicle\ReactAdapter\Promise\ReactPromise 从实现 Icicle\Awaitable\Awaitable 的 Icicle 可等待对象创建一个实现 React\Promise\ExtendedPromiseInterfaceReact\Promise\ExtendedPromiseInterface 的 Promise。这使得从 Icicle 创建的可等待对象可以在任何需要 React Promise 的组件中使用。

$iciclePromise = new \Icicle\Awaitable\Promise(function ($resolve, $reject) {
    // Resolver
});

$reactPromise = new \Icicle\ReactAdapter\Promise\ReactPromise($iciclePromise);

Awaitable\adapt()

Icicle\Awaitable 命名空间中定义了一个函数 adapt(),可以将具有 then(callable $onFulfilled, callable $onRejected) 方法的任何对象转换为实现 Icicle\Awaitable\Awaitable 的可等待对象。此函数可用于将 React Promise 转换为 Icicle 可等待对象。

$reactPromise = new \React\Promise\Promise(function ($resolve, $reject) {
    // Resolver
});

$awaitable = \Icicle\Awaitable\adapt($reactPromise);

有关 Icicle\Awaitable\adapt() 的更多信息,请参阅 可等待 API 文档