Addings comments!

This commit is contained in:
Kai Moseley 2016-12-11 11:33:00 +00:00
parent 43cf6935c5
commit 5271513b70
3 changed files with 23 additions and 8 deletions

View File

@ -1,4 +1,4 @@
import { buildReducer } from './redux-arg/buildReducers';
import { buildStoreChunk } from './redux-arg/buildReducers';
import { createStore, compose, combineReducers } from 'redux';
import { Types } from './redux-arg/structure';
@ -35,7 +35,7 @@ const exampleReducer = {
})
};
const test = buildReducer('example', exampleReducer);
const test = buildStoreChunk('example', exampleReducer);
const store = createStore(
combineReducers({

View File

@ -10,8 +10,12 @@ import { reduce, find } from 'lodash';
import { createReducer } from './reducers';
import { PROP_TYPES } from './structure';
// Build a chunk of the eventual store. The selectors and actions
// generated will specifically operate on the store chunk generated. Selectors will be
// relative to the baseSelector provided or, if not specified, the root of the store, using
// the name of the chunk as the base property.
export function buildReducer(name: string, structure: any, {
export function buildStoreChunk(name: string, structure: any, {
baseSelector = state => state[name],
locationString = '',
}: {
@ -51,7 +55,7 @@ export function buildReducer(name: string, structure: any, {
//createReducer function, which will create the correct reducer for the given structure
//(which can be either object, array, or primitive).
let childReducer = containsReducers
? buildReducer(propName, propStructure, {
? buildStoreChunk(propName, propStructure, {
locationString: locationString ? `${locationString}.${propName}` : propName,
baseSelector: (state: any) => baseSelector(state)[propName],
})

View File

@ -18,6 +18,9 @@ export type PartialReducer = {
export type Selector = (state: Object) => any;
//==============================
// JS imports
//==============================
import {
PROP_TYPES,
} from './structure';
@ -27,7 +30,6 @@ import { createObjectReducer } from './reducers/objectReducer';
import { createArrayReducer } from './reducers/arrayReducer';
import { createPrimitiveReducer } from './reducers/primitiveReducer';
function determineReducerType(reducerDescriptor, {
name,
locationString,
@ -57,10 +59,12 @@ function callReducer({ name, reducerFn, reducerStructureDescriptor, locationStri
});
}
export const createReducer = compose(callReducer, determineReducerType);
export function createReducerBehaviors(behaviorsConfig: any, locationString: string): any {
//Take a reducer behavior config object, and create the reducer behaviors using the location string
//Take a reducer behavior config object, and create the reducer behaviors using the location string.
//This is necessary since all action types are effectively global when Redux processes an action
//(i.e. every reducer will be ran using the action object). Therefore we need to ensure that all
//actions only result in the specific reducer performing a change. Actions are also generated using
//the location string/name combination, so will match up 1:1.
return reduce(behaviorsConfig, (memo, behavior, name) => ({
...memo,
[`${locationString}.${name}`]: behavior.reducer,
@ -68,6 +72,11 @@ export function createReducerBehaviors(behaviorsConfig: any, locationString: str
}
export function calculateDefaults(typeDescription: StructureType | PrimitiveType) {
//Using the structure of a type, calculate the default for that type.
//Types can take two forms; a 'StructureType' and a 'PrimitiveType'. The former
//can (and usually does) contain nested type descriptions, so we need to recurse
//through the definition until defaults are found, and build up the corresponding
//structure.
const { type, structure = {}, defaultValue = '' } = typeDescription();
const complex = [PROP_TYPES._array, PROP_TYPES._shape].indexOf(type) > -1;
@ -78,3 +87,5 @@ export function calculateDefaults(typeDescription: StructureType | PrimitiveType
[propName]: calculateDefaults(propValue),
}), {});
}
export const createReducer = compose(callReducer, determineReducerType);