Skip to content

Lightweight library that eases using components built for React PHP in a traditional, blocking environment

License

Notifications You must be signed in to change notification settings

clue-labs/reactphp-block

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

clue/block-react Build Status

Lightweight library that eases integrating async components built for React PHP in a traditional, blocking environment.

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

Introduction

React PHP provides you a great set of base components and a huge ecosystem of third party libraries in order to perform async operations. The event-driven paradigm and asynchronous processing of any number of streams in real time enables you to build a whole new set of application on top of it. This is great for building modern, scalable applications from scratch and will likely result in you relying on a whole new software architecture.

But let's face it: Your day-to-day business is unlikely to allow you to build everything from scratch and ditch your existing production environment.

This is where this library comes into play:

Let's block React PHP

More specifically, this library eases the pain of integrating async components into your traditional, synchronous (blocking) application stack.

Quickstart example

The following example code demonstrates how this library can be used along with an async HTTP client to process two non-blocking HTTP requests and block until the first (faster) one resolves.

function blockingExample()
{
    // use a unique event loop instance for all parallel operations
    $loop = React\EventLoop\Factory::create();
    $blocker = new Blocker($loop);
    
    // this example uses an HTTP client
    // this could be pretty much everything that binds to an event loop
    $browser = new Clue\React\Buzz\Browser($loop);
    
    // set up two parallel requests
    $request1 = $browser->get('http://www.google.com/');
    $request2 = $browser->get('http://www.google.co.uk/');
    
    // keep the loop running (i.e. block) until the first response arrives
    $fasterResponse = $blocker->awaitRace(array($request1, $request2));
    
    return $fasterResponse->getBody();
}

Usage

Blocker

The Blocker is responsible for orchestrating the EventLoop in order to make it run (block) until your conditions are fulfilled.

$loop = React\EventLoop\Factory::create();
$blocker = new Blocker($loop);

wait()

The wait($seconds) method can be used to wait/sleep for $time seconds.

awaitOne()

The awaitOne(PromiseInterface $promise) method can be used to block waiting for the given $promise to resolve.

awaitRace()

The awaitRace(array $promises) method can be used to wait for ANY of the given promises to resolve.

awaitAll()

The awaitAll(array $promises) method can be used to wait for ALL of the given promises to resolve.

Install

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

{
    "require": {
        "clue/block-react": "dev-master"
    }
}

License

MIT

About

Lightweight library that eases using components built for React PHP in a traditional, blocking environment

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%