2017 © Pedro Peláez
 

library php-redux

Port of the Redux library for PHP and for fun

image

rikbruil/php-redux

Port of the Redux library for PHP and for fun

  • Tuesday, July 18, 2017
  • by rikbruil
  • Repository
  • 4 Watchers
  • 33 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Redux for PHP

Build Status Coverage Status Latest Stable Version License Scrutinizer Code Quality, (*1)

Installation

composer require rikbruil/php-redux

Code example

// Provide the initial state for the application
// This can be any type of value (preferably scalars)
$initialState = 0;

// Provide a reducer
// Reducers modify state based on a given action
// They follow the pattern: (state, action) => state
$reducer = function ($state, $action) {
    $type = $action['type'];

    if ($type === 'INCREMENT') {
        return $state + 1;
    }

    if ($type === 'DECREMENT') {
        return $state - 1;
    }

    return $state;
};

// Create the application store with the reducer in place
$store = new \Rb\Redux\Store::create($reducer, $initialState);

// Listeners are low-level functionality.
// They can be used to feed state into other (third-party) components.
// Listeners will fire when application state changes.
$listener = function (\Rb\Redux\StoreInterface $store) {
    $state = $store->getState();
    echo 'Counter: ' . $state . PHP_EOL;
};

// Optional: Middleware is allowed to replace the dispatch() method of the store.
// In this example it allows sending Promises that resolve to actions
$middleware = new \Rb\Redux\Middleware\Chain([
    new PromiseMiddleWare()
]);

// Middlewares need to be applied to the store
$middleware($store); // short-hand for: $middleware->apply($store);

// This will update global state to 1
$store->dispatch(['type' => 'INCREMENT']);

// We can safely dispatch promises due to the middleware
$deferred = new Deferred();
$promise = $deferred->promise();

// The listeners won't dispatch until the promise is resolved.
$store->dispatch($promise);

// Will still return 1
echo $store->getState() . PHP_EOL;

sleep(2);

// We will now resolve the promise, triggering the listeners
// This will decrement global state back to 0
$deferred->resolve(['type' => 'DECREMENT']);

// Will now return 0
echo $store->getState() . PHP_EOL;

The Versions

18/07 2017

dev-master

9999999-dev https://github.com/rikbruil/php-redux

Port of the Redux library for PHP and for fun

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Rik Bruil

flux fsa redux