ObjectiveStore
class ObjectiveStore
The ObjectiveStore handles the connection of controllers, reducers, and sagas to Redux. Each ObjectiveStore has its own Redux store that it manages. The store will also setup the Redux-Saga middleware, if it finds the dependency.
Middleware can be applied at construction. Sagas and reducers can be added at any time, as needed.
constructor
public constructor(config: ObjectiveStoreOptions = {}): ObjectiveStore
Creates an instance of the ObjectiveStore.
In setting up the instance, the class will create a ReduxStore. If Redux-Saga is available, tbe middleware will be setup automatically as well.
Parameters
config.composeMiddlewareFn?: any
A function used to compose the Redux middleware.
config.initialState?: any
The initial state to which the store should be initialized.
config.injector?: ReducerInjector
An injector object for adding reducers and sagas to the store.
config.middleware?: Middleware<any>[]
Middleware that should be applied to the store. This should not include saga middleware.
config.preDispatchHook?: PreDispatchHookFn
A function that will be called before actions are dispatched.The function should take an action and return either null or a promise. If a promise is returned, the action will
be dispatched when the promise resolves.
config.reducer?: Reducer<any, AnyAction>
The initial reducer for the store.
config.sagaContext?: any
The context that should be given to sagas.
Returns
Examples
// No need to setup the Redux-Saga middleware-- Objective-Redux will handle it.
const objectiveStore = new ObjectiveStore();
import { ReducerInjector, ObjectiveStore } from 'objective-redux';
import { createInjectorsEnhancer } from 'redux-injectors';
import { initialState, initialReducers } from './elsewhere';
const injector = new ReducerInjector(initialReducers);
const createReducer = injector.getReducerCreationFn();
const runSaga = injector.getRunSagaFn();
const middleware = [
createInjectorsEnhancer({ createReducer, runSaga }),
];
const objectiveStore = new ObjectiveStore({
reducer,
initialState,
middleware,
injector,
});
dispatch
public dispatch(action: AnyAction): AnyAction
Dispatches a Redux action to the store without using a Controller.
Parameters
Returns
Examples
const objectiveStore = new ObjectiveStore();
objectiveStore.dispatch(myAction());
getState
public getState(): any
Gets the state object from the Redux store.
Returns
Examples
const objectiveStore = new ObjectiveStore();
const state = objectiveStore.getState();
registerSaga
public registerSaga(sagaFn: SagaFn<void>, statelessController: StatelessController|null = null): void
Adds and and begins running a saga as part in the context of the store that the store manages.
Note: This method should not be called manually for StatelessControllers! The controller will handle this call on its own when the controller is first initialized.
Parameters
from a StatelessController.
Returns
Examples
function* sagaFn() {
yield console.log('Hello, world!');
}
const objectiveStore = new ObjectiveStore();
objectiveStore.registerSaga(sagaFn);
replaceReducer
public replaceReducer(nextReducer: Reducer<any, AnyAction>): void
Replaced the existing reducer with a new one.
THIS METHOD SHOULD NOT BE USED DIRECTLY. Use the ReducerInjector class, instead.
Parameters
Returns
subscribe
public subscribe(listener: () => void): Unsubscribe
Subscribes to the Redux store events.
Parameters
Returns
Examples
const objectiveStore = new ObjectiveStore();
const unsubscribeFn = objectiveStore.subscribe(myCallback);