Dispatchers

The Dispatchers package is a kernel that uses the dispatchersHelper to expose an API to fire Redux's actions.

Dispatchers helper

The dispatchersHelper is responsible for exposing an API to register action creators, get them, dispatch new actions, etc.

What is an action creator? You can find useful information about action creators in the following Redux documentation.

createAction

Creates an action due its name and rest of arguments.

Usage
Copy
createAction((actionCreatorName: String, ...args: any[]): Action;
  • actionCreatorName String

    The name of the action creator to be invoked.

  • args any

    Rest of arguments to invoke the action creator, commonly the props.

Returns
  • Action

    The desired action. The action is invisible, this means that you can not use any of its properties.

Example
Copy
export default class Plugin {
  constructor({ storeHelper, dispatchersHelper }) {
    this._dispatchersHelper = dispatchersHelper;
    this._storeHelper = storeHelper;
  }

  onInit() {
    const action = this.dispatchersHelper.createAction('setValue', 3);
    this._storeHelper.dispatch(action);
  }
}

dispatch

Invoke an action creator due its name and dispatches the resulting action.

Usage
Copy
dispatch((actionCreatorName: String, ...args: any[]): void;
  • actionCreatorName String

    The name of the action creator to be invoked.

  • args any

    Rest of arguments to invoke the action creator, commonly the props.

Example
Copy
export default class Plugin {
  constructor({ dispatchersHelper }) {
    this._dispatchersHelper = dispatchersHelper;
  }

  onInit() {
    this._dispatchersHelper.dispatch('setValue', 3);
  }
}

getDispatch

Retrieves a dispatcher for the given action creator.

Usage
Copy
getDispatch(actionCreatorName: String): Function
  • actionCreatorName String

    The name of the action creator which will be called by the retrieved dispatch.

Returns
  • Function

    The desired dispatch, returns a noop in case it doesn't exist.

Example
Copy
export default class Plugin {
  constructor({ dispatchersHelper, storeHelper }) {
    this._dispatchersHelper = dispatchersHelper;
    this._storeHelper = storeHelper;
  }

  onInit() {
    const dispatchSetValue = this._dispatchersHelper.getDispatch('setValue');
    dispatchSetValue(3);
  }
}

register

Registers an action creator to allow its usage for all the plugins.

Usage
Copy
register(actionCreatorName: String, actionCreator: Function): void
  • actionCreatorName String

    The name of the action creator.

  • actionCreator Function

    The action creator function.

Examples
Copy
const SET_VALUE = 'SET_VALUE';
const setValue = (value) => ({ type: SET_VALUE, value });

export default class Plugin {
  constructor({ dispatchersHelper }) {
    this._dispatchersHelper = dispatchersHelper;
  }

  onInit() {
    this._dispatchersHelper.register('setValue', setValue);
  }
}
Copy
// register a dispatcher dio-factory
const makeSetSeven = ({ SEVEN }) =>
  () => ({ type: 'SET', value: SEVEN }));

export default class Plugin {
  constructor({ diHelper, dispatchersHelper }) {
    diHelper.useValue('SEVEN', 7);
    this._dispatchersHelper = dispatchersHelper;
  }

  onInit() {
    this._dispatchersHelper.register('setValue', withFactory(makeSetSeven));
  }
}