Reducer Store

DataModel

A page level storage which stores, registers, unregisters reducers for all the datamodel instances. There is only one reducer store available in a page. All the datamodel instances receive same instance of reducer store. DataModel out of the box provides handful of reducers which can be used as reducer function.

defaultReducer

Changes the defaultReducer globally. For all the fields which does not have defAggFn mentioned in schema, the value of defaultReducer is used for aggregation.

Parameters:

NameTypeDescription
reducer

string

name of the default reducer. It picks up the definition from store by doing name lookup. If no name is found then it takes sum as the default reducer.

Returns:

ReducerStoreinstance of the singleton store in page.

register

Registers a reducer. A reducer has to be registered before it is used.

main
run-button
run-button
reset-button
 // DataModel instance is created from https://www.charts.com/static/cars.json data,
 // https://www.charts.com/static/cars-schema.json schema and assigned to variable dm. DataModel is retrieved
 // from muze namespace and assigned to DataModel variable.
 const reducerStore = DataModel.Reducers();

 reducerStore.register('meanSquared', (arr) => {
     const squaredVal = arr.map(item => item * item);
     let sum = 0;
     for (let i = 0, l = squaredVal.length; i < l; i++) {
         sum += squaredVal[i];
     }
     return sum;

 const outputDM= dm.groupBy(['origin'], {
     accleration: 'meanSquared'
 });
 });

Parameters:

NameTypeDescription
name

string

formal name for a reducer. If the given name already exists in store it is overridden by new definition.

reducer

function

definition of reducer function.

Returns:

functionfunction for unregistering the reducer.