cspray / labrador-composite-future

此软件包已被废弃且不再维护。作者建议使用labrador-kennel/composite-future软件包代替。

使用类型安全的CompositeFuture对象组合Amp Future并按需等待。

1.3.0 2023-01-26 18:18 UTC

This package is auto-updated.

Last update: 2023-01-26 18:18:51 UTC


README

表示类型安全的Amp\Future对象集合以及操作此集合的方法的对象。

安装

Composer是安装Labrador软件包的唯一支持方法。

composer require labrador-kennel/composite-future

入门

此库提供对象,旨在提供在处理Future集合时可用于类型提示的类型。当使用CompositeFuture时,还可以将返回类型提示构造为类似于处理单个Future的代码。始终,查看一些代码是开始的最佳方式!

<?php

namespace Acme\Demo;

use Amp\Future;
use Labrador\CompositeFuture\CompositeFuture;

function futuresGeneratingMethod() : CompositeFuture {
    $futures = ['a' => Future::complete(1), 'b' => Future::complete(2), 'c' => Future::error(new \Exception('something went wrong'))];
    return new CompositeFuture($futures);
}

$futures = futuresGeneratingMethod();
// Returns an array with keys equal to the index of the Future and the value to the Future resolution
// Will throw an exception when an error is encountered
$futures->await();

// Also has access to the following methods, which follow the same documentation as their corresponding
// Amp\Future functions.
$futures->awaitAll();
$futures->awaitAny();
$futures->awaitAnyN(2);
$futures->awaitFirst();

?>

动机

此库故意是一个非常简单的面向对象包装,围绕处理Future集合的实用函数。动机是为此类集合提供一个语义类型提示。在上面的示例中,如果没有CompositeFuture,我们基本上有两个选择

  1. 在函数内部等待Future;这可能不是最佳选择,因为每次调用可能需要以不同的方式处理等待。
  2. 在我们的方法中返回数组类型提示;这可能不是最佳选择,因为它的语义不如类型提示。

治理

所有Labrador软件包均遵循Labrador Governance存储库中规定的规则。