launch.io
Version:
Launch.IO is an Ultra Hip, Simple, and Fast, Time Traveling React State Management Library
26 lines (25 loc) • 1.08 kB
JavaScript
import bindActions from "./bindActions";
import ctx from "./context";
import createServiceApi from "./createServiceApi";
/**
* Initialize a React application with Launch.IO.
*
* @param {Array} services An `array` of application services. Each service object will consist of `name` (`string`), `initialState` (`object`), and `actions` (object of functions) properties.
* @param {Object} options A `Launch.IO` `LaunchOptions` object.
* */
export const initializeLaunch = (services, options = { enableTimeTravel: false }) => {
const serviceApi = createServiceApi(services, options);
const launcher = (action) => {
const prevCtx = ctx.snapshot();
const newState = serviceApi.createReducer(launcher)(prevCtx.state, action);
if (newState !== prevCtx.state) {
ctx.update(Object.assign(Object.assign({}, prevCtx), { state: newState }));
}
};
const contextValue = {
state: serviceApi.initialState,
actions: bindActions(serviceApi.actions, launcher),
launcher,
};
ctx.update(contextValue);
};