PHPackages                             sugarcraft/candy-async - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sugarcraft/candy-async

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sugarcraft/candy-async
======================

Shared async utilities for SugarCraft — cancellation tokens, subscriptions, and AsyncOps helpers (withTimeout, retry, debounce, throttle) built on ReactPHP.

02.1kPHP

Since Jul 11Pushed 1w agoCompare

[ Source](https://github.com/sugarcraft/candy-async)[ Packagist](https://packagist.org/packages/sugarcraft/candy-async)[ RSS](/packages/sugarcraft-candy-async/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

candy-async
===========

[](#candy-async)

Shared async utilities for SugarCraft — cancellation tokens, subscriptions, and AsyncOps helpers built on ReactPHP.

Overview
--------

[](#overview)

`candy-async` provides the foundational async vocabulary used across the SugarCraft TUI ecosystem:

- **Cancellation tokens** — `CancellationSource` / `CancellationToken` / `Cancellable` for coordinated cancellation across async operations
- **Subscriptions** — `Subscription` interface and `Subscriptions::compose()` for managing TEA-style subscription lifecycles
- **AsyncOps** — static helpers for `withTimeout`, `retry`, `debounce`, and `throttle` operations

Quickstart
----------

[](#quickstart)

```
use SugarCraft\Async\{AsyncOps, CancellationSource, Subscriptions};

$source = CancellationSource::new();

// Attach a cancellation callback
$source->token()->onCancel(fn() => echo "Cancelled!\n");

$source->cancel(); // prints "Cancelled!"

// Timeout wrapper
$loop = \React\EventLoop\Loop::get();
$promise = AsyncOps::withTimeout($loop, $somePromise, 5.0);

// Retry with backoff
$promise = AsyncOps::retry(
    fn() => $httpClient->request('GET', 'https://example.com'),
    attempts: 3,
    baseBackoffSeconds: 0.5,
);

// Debounce rapid calls
$debounced = AsyncOps::debounce(fn($input) => process($input), 0.15);
$debounced('a');
$debounced('b');
$debounced('c'); // only this fires, 150ms after last call
```

Requirements
------------

[](#requirements)

- PHP 8.3+
- `react/event-loop: ^1.6`
- `react/promise: ^3.3`

Installation
------------

[](#installation)

```
composer require sugarcraft/candy-async
```

Architecture
------------

[](#architecture)

### Cancellation

[](#cancellation)

`CancellationSource` owns the mutable cancellation flag. It exposes a read-only `CancellationToken` to consumers. When `cancel()` is called:

1. The flag is flipped (idempotent)
2. All callbacks registered via `onCancel()` fire in registration order, exactly once

This pattern allows cancellation to propagate without the consumer being able to trigger it themselves.

### Subscriptions

[](#subscriptions)

`Subscription` is the disposal handle returned by subscribe-style APIs. `Subscriptions::compose()` lets multiple subscriptions be disposed atomically:

```
$composite = Subscriptions::compose($sub1, $sub2, $sub3);
$composite->unsubscribe(); // disposes all three
```

### AsyncOps

[](#asyncops)

withTimeout and retry are stateless helpers. debounce and throttle return stateful closures that retain mutable timer/cooldown state. All helpers work via Promise plumbing and `LoopInterface` timers:

- `withTimeout` — wraps a promise; rejects with `TimeoutException` after N seconds. The inner operation is NOT cancelled and keeps running to completion.
- `retry` — retries a failed operation up to N times with exponential backoff (no per-attempt timeout; wrap with withTimeout for a deadline)
- `debounce` — only the last call within the window fires, after silence
- `throttle` — fires at most once per interval, ignoring excess calls

License
-------

[](#license)

MIT

###  Health Score

27

—

LowBetter than 46% of packages

Maintenance64

Regular maintenance activity

Popularity23

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity11

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/b1036e0717211b8030b83cbe729e8ba6ba442fdbd5285fb97a39d7dcfe339342?d=identicon)[detain](/maintainers/detain)

---

Top Contributors

[![detain](https://avatars.githubusercontent.com/u/1364504?v=4)](https://github.com/detain "detain (11 commits)")

### Embed Badge

![Health badge](/badges/sugarcraft-candy-async/health.svg)

```
[![Health](https://phpackages.com/badges/sugarcraft-candy-async/health.svg)](https://phpackages.com/packages/sugarcraft-candy-async)
```

###  Alternatives

[silverstripe/spamprotection

Spam protection module for SilverStripe.

431.0M49](/packages/silverstripe-spamprotection)[byte5/laravel-passgenerator

A Laravel package to create Apple Wallet (old Passbook) compatible tickets.

9239.0k](/packages/byte5-laravel-passgenerator)[kzykhys/steganography

Simple implementation of Steganography (Hiding a hidden message within an image)

889.8k](/packages/kzykhys-steganography)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
