jcombee/reduced

A php library for managing state.

dev-main 2022-05-14 13:22 UTC

This package is auto-updated.

Last update: 2024-11-14 18:55:01 UTC


README

A state management library for PHP.

Content

Instalation

composer require jcombee/reduced

How to use

This example bellow uses the classes in: tests/Example.

    $state = new LocationState(0, 0);
    $store = new Store($state);

    $store->subscribe(function (LocationState $state) {
        echo "Location: {$state->x}, {$state->y}";
    });

    $store->registerReducer(new MovementReducer());

    $store->dispatch(new MovementAction(DirectionEnum::UP));
    $store->dispatch(new MovementAction(DirectionEnum::LEFT))
    $store->dispatch(new MovementAction(DirectionEnum::DOWN))
    $store->dispatch(new MovementAction(DirectionEnum::RIGHT))

    $state = $store->getState();

    /**
     * Location: 0, -1
     * Location: -1, -1
     * Location: -1, 0
     * Location: 0, 0
     */