Stateless Controllers
The StatelessController class
A stateless controller is a controller that does not modify the state of the application. In other words, it does not have an associated slice of state in the Redux store or a reducer.
This class is used to create actions that have side effect—in other words, Redux-Saga's.
In order to use StatelessControllers, first ensure you have the optional dependency redux-saga installed and bundled with your application. See the Setup page for instructions.
Defining a StatelessController
Required Implementation
The StatelessController requires a static getName function is implemented and returns a string name for the controller. The name it returns will need to be unique for all of the Objective Redux controllers.1 This allows Objective Redux to lazy-load controllers on-the-fly and prevents reducer collisions in the store.
import { StatelessController } from 'objective-redux';
import { SwitchStateController } from './switch-state-controller';
export class SwitchStateSagas extends StatelessController {
static getName() {
return 'Switch-State-Sagas';
}
// ... our actions will go here ...
}
Defining Actions
The withEffect method can be used to add a watching generator function to the saga. It is passed a take configuration for the saga, defining how events should be handled. Then, the store method is called and passed the generator function the saga will execute.
Below is an example of a Saga that will call the toggleSwitch method of our previously created SwitchStateController.
import {
StatelessController,
configureTakeLatest,
getControllerFromSagaContext
} from 'objective-redux';
import { SwitchStateController } from './switch-state-controller';
export class SwitchStateSagas extends StatelessController {
static getName() {
return 'Switch-State-Sagas';
}
toggleSwitch = this.createSagaAction(
function* () {
const switchController = yield getControllerFromSagaContext(SwitchStateController);
yield switchController.toggleSwitch();
}
)
.withEffect(configureTakeLatest())
.register();
}
1 Optionally, namespaces can be used to help with scoping controllers and Redux store slices. See the StatelessController API documentation for more.