clue/promise-stream-react 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

clue/promise-stream-react

最新稳定版本:v0.1.2

Composer 安装命令:

composer require clue/promise-stream-react

包简介

The missing link between Promise-land and Stream-land for React PHP

README 文档

README

This package has now been migrated over to react/promise-stream and only exists for BC reasons.

$ composer require react/promise-stream

If you've previously used this package, upgrading should take no longer than a few minutes. All functions have been merged as-is from the latest v0.1.2 release with no other changes, so you can simply update your code to use the updated namespace like this:

// old from clue/promise-stream-react
\Clue\React\Promise\Stream\buffer(…);

// new
\React\Promise\Stream\buffer(…);

See react/promise-stream for more details.

The below documentation applies to the last release of this package. Further development will take place in the updated react/promise-stream, so you're highly recommended to upgrade as soon as possible.

Legacy clue/promise-stream-react Build Status

The missing link between Promise-land and Stream-land, built on top of React PHP.

Table of Contents

Note: This project is in beta stage! Feel free to report any issues you encounter.

Usage

This lightweight library consists only of a few simple functions. All functions reside under the Clue\React\Promise\Stream namespace.

The below examples assume you use an import statement similar to this:

use Clue\React\Promise\Stream;

Stream\buffer(…);

Alternatively, you can also refer to them with their fully-qualified name:

\Clue\React\Promise\Stream\buffer(…);

buffer()

The buffer(ReadableStreamInterface $stream) function can be used to create a Promise which resolves with the stream data buffer.

$stream = accessSomeJsonStream();

Stream\buffer($stream)->then(function ($contents) {
    var_dump(json_decode($contents));
});

The promise will resolve with all data chunks concatenated once the stream closes.

The promise will resolve with an empty string if the stream is already closed.

The promise will reject if the stream emits an error.

The promise will reject if it is canceled.

first()

The first(ReadableStreamInterface|WritableStreamInterface $stream, $event = 'data') function can be used to create a Promise which resolves once the given event triggers for the first time.

$stream = accessSomeJsonStream();

Stream\first($stream)->then(function ($chunk) {
    echo 'The first chunk arrived: ' . $chunk;
});

The promise will resolve with whatever the first event emitted or null if the event does not pass any data. If you do not pass a custom event name, then it will wait for the first "data" event and resolve with a string containing the first data chunk.

The promise will reject once the stream closes – unless you're waiting for the "close" event, in which case it will resolve.

The promise will reject if the stream is already closed.

The promise will reject if it is canceled.

all()

The all(ReadableStreamInterface|WritableStreamInterface $stream, $event = 'data') function can be used to create a Promise which resolves with an array of all the event data.

$stream = accessSomeJsonStream();

Stream\all($stream)->then(function ($chunks) {
    echo 'The stream consists of ' . count($chunks) . ' chunk(s)';
});

The promise will resolve with an array of whatever all events emitted or null if the events do not pass any data. If you do not pass a custom event name, then it will wait for all the "data" events and resolve with an array containing all the data chunks.

The promise will resolve with an array once the stream closes.

The promise will resolve with an empty array if the stream is already closed.

The promise will reject if the stream emits an error.

The promise will reject if it is canceled.

unwrapReadable()

The unwrapReadable(PromiseInterface $promise) function can be used to unwrap a Promise which resolves with a ReadableStreamInterface.

This function returns a readable stream instance (implementing ReadableStreamInterface) right away which acts as a proxy for the future promise resolution. Once the given Promise resolves with a ReadableStreamInterface, its data will be piped to the output stream.

//$promise = someFunctionWhichResolvesWithAStream();
$promise = startDownloadStream($uri);

$stream = Stream\unwrapReadable($promise);

$stream->on('data', function ($data) {
   echo $data;
});

$stream->on('end', function () {
   echo 'DONE';
});

If the given promise is either rejected or fulfilled with anything but an instance of ReadableStreamInterface, then the output stream will emit an error event and close:

$promise = startDownloadStream($invalidUri);

$stream = Stream\unwrapReadable($promise);

$stream->on('error', function (Exception $error) {
    echo 'Error: ' . $error->getMessage();
});

The given $promise SHOULD be pending, i.e. it SHOULD NOT be fulfilled or rejected at the time of invoking this function. If the given promise is already settled and does not resolve with an instance of ReadableStreamInterface, then you will not be able to receive the error event.

unwrapWritable()

The unwrapWritable(PromiseInterface $promise) function can be used to unwrap a Promise which resolves with a WritableStreamInterface.

This function returns a writable stream instance (implementing WritableStreamInterface) right away which acts as a proxy for the future promise resolution. Once the given Promise resolves with a WritableStreamInterface, any data you wrote to the proxy will be piped to the inner stream.

//$promise = someFunctionWhichResolvesWithAStream();
$promise = startUploadStream($uri);

$stream = Stream\unwrapWritable($promise);

$stream->write('hello');
$stream->end('world');

$stream->on('close', function () {
   echo 'DONE';
});

If the given promise is either rejected or fulfilled with anything but an instance of WritableStreamInterface, then the output stream will emit an error event and close:

$promise = startUploadStream($invalidUri);

$stream = Stream\unwrapWritable($promise);

$stream->on('error', function (Exception $error) {
    echo 'Error: ' . $error->getMessage();
});

The given $promise SHOULD be pending, i.e. it SHOULD NOT be fulfilled or rejected at the time of invoking this function. If the given promise is already settled and does not resolve with an instance of WritableStreamInterface, then you will not be able to receive the error event.

Install

The recommended way to install this library is through Composer. New to Composer?

This will install the latest supported version:

$ composer require clue/promise-stream-react:^0.1.2

See also the CHANGELOG for details about version upgrades.

License

MIT

clue/promise-stream-react 适用场景与选型建议

clue/promise-stream-react 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.36k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2016 年 04 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「stream」 「Buffer」 「async」 「promise」 「reactphp」 「unwrap」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 clue/promise-stream-react 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 clue/promise-stream-react 我们能提供哪些服务?
定制开发 / 二次开发

基于 clue/promise-stream-react 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.36k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 29
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-04-12