rikbruil / php-redux
Port of the Redux library for PHP and for fun
dev-master / 1.x-dev
2017-07-18 13:42 UTC
Requires
- php: >=5.6
Requires (Dev)
- fabpot/php-cs-fixer: ^1.11
- henrikbjorn/phpspec-code-coverage: ^2.0
- phpspec/phpspec: ^2.4
- react/http: ^0.4.1
- react/promise: ^2.2
- satooshi/php-coveralls: ^1.0
Suggests
- react/promise: Required if you want to use the Promise middleware
This package is not auto-updated.
Last update: 2024-12-21 19:47:45 UTC
README
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;