icicleio / react-adapter
将 Icicle 的事件循环和 Promise 与兼容 React 组件接口进行适配。
Requires
- icicleio/icicle: ^0.9.1
- react/event-loop: ^0.4
- react/promise: ^2.2
Requires (Dev)
- phpunit/phpunit: ^4.6
README
这个库促进了为 React 和 Icicle 构建的组件之间的互操作性。这个库提供了一个适配器,用于两个库之间不同的事件循环和 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\ExtendedPromiseInterface
和 React\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 文档。