core-native
Version:
A lightweight framework based on React Native + Redux + Redux Saga, in strict TypeScript.
46 lines • 1.54 kB
JavaScript
import { combineReducers } from "redux";
// Redux Action
const SET_STATE_ACTION = "@@framework/setState";
// state must be complete module state, not partial
export function setStateAction(module, state, type) {
return {
type,
name: SET_STATE_ACTION,
payload: { module, state },
};
}
function setStateReducer(state = {}, action) {
// Use action.name for set state action, make type specifiable to make tracking/tooling easier
if (action.name === SET_STATE_ACTION) {
const { module, state: moduleState } = action.payload;
return Object.assign(Object.assign({}, state), { [module]: moduleState });
}
return state;
}
export const LOADING_ACTION = "@@framework/loading";
export function loadingAction(show, identifier = "global") {
return {
type: LOADING_ACTION,
payload: { identifier, show },
};
}
function loadingReducer(state = {}, action) {
if (action.type === LOADING_ACTION) {
const payload = action.payload;
const count = state[payload.identifier] || 0;
return Object.assign(Object.assign({}, state), { [payload.identifier]: count + (payload.show ? 1 : -1) });
}
return state;
}
// Root Reducer
export function rootReducer() {
return combineReducers({
loading: loadingReducer,
app: setStateReducer,
});
}
// Helper function, to determine if show loading
export function showLoading(state, identifier = "global") {
return state.loading[identifier] > 0;
}
//# sourceMappingURL=reducer.js.map