attadux
Version:
Implementation of the redux-modular-ducks, forked from the extensible-duck implementation and extended to include spected validators, state machines, helpers, web workers, effect handling, action multipliers, action enhancers, action throttling/debouncing
336 lines (312 loc) • 16.8 kB
JavaScript
import _default39 from 'ramda/src/any';
import _default38 from 'ramda/src/pair';
import _default37 from 'ramda/src/mergeDeepRight';
import _default36 from 'ramda/src/always';
import _default35 from 'ramda/src/when';
import _default34 from 'ramda/src/split';
import _default33 from 'ramda/src/ifElse';
import _default32 from 'ramda/src/both';
import _default31 from 'ramda/src/last';
import _default30 from 'ramda/src/__';
import _default29 from 'ramda/src/contains';
import _default28 from 'ramda/src/head';
import _default27 from 'ramda/src/pick';
import _default26 from 'ramda/src/reduce';
import _default25 from 'ramda/src/path';
import _default24 from 'ramda/src/has';
import _default23 from 'ramda/src/useWith';
import _default22 from 'ramda/src/toPairs';
import _default21 from 'ramda/src/either';
import _default20 from 'ramda/src/allPass';
import _default19 from 'ramda/src/is';
import _default18 from 'ramda/src/all';
import _default17 from 'ramda/src/difference';
import _default16 from 'ramda/src/converge';
import _default15 from 'ramda/src/prop';
import _default14 from 'ramda/src/keys';
import _default13 from 'ramda/src/map';
import _default12 from 'ramda/src/filter';
import _default11 from 'ramda/src/values';
import _default10 from 'ramda/src/flatten';
import _default9 from 'ramda/src/uniq';
import _default8 from 'ramda/src/defaultTo';
import _default7 from 'ramda/src/assocPath';
import _default6 from 'ramda/src/unless';
import _default5 from 'ramda/src/compose';
import _default4 from 'ramda/src/curry';
import _default3 from 'ramda/src/isEmpty';
import _default2 from 'ramda/src/isNil';
import _default from 'ramda/src/anyPass';
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
import { coerceToString, listOfPairsToOneObject, coerceToArray, hasNestedProp, isStringieThingie, isValidPropName, isNotBlankString, isNotNil, isPlainObj } from '../util';
import { getTypes } from '../types';
/**
* Simple check to see whether a given duck's machines is empty.
*
* @func
* @sig {k: v} -> Boolean
* @param {Object} machines A duck's collection of state machines
* @returns {Boolean} whether or not the machines are empty
*/
export var noMachines = _default([_default2, _default3]);
/**
* Adds an object containing the current state of all the state machines onto
* a given section of the redux store
*
* @func
* @sig {k: v} -> {k: v} -> {k: v}
* @param {Object} duck A duck instance, containing the state machines and the
* prop name/path to the section of the store where their state is tracked
* @param {*} state The current state of this portion of the store
* @returns {Object} original state (or initialState, if state is nil) plus
* current state of all the state machines
*/
export var addTransitionsToState = _default4(function (state, _ref) {
var _ref$initialState = _ref.initialState;
var initialState = _ref$initialState === undefined ? {} : _ref$initialState;
var _ref$stateMachinesPro = _ref.stateMachinesPropName;
var stateMachinesPropName = _ref$stateMachinesPro === undefined ? ['states'] : _ref$stateMachinesPro;
return _default5(_default6(hasNestedProp(stateMachinesPropName), _default7(stateMachinesPropName, {})), _default8(initialState))(state);
});
/**
* Retrieves a unique list all the state transitions possible for a given machine.
* State machines are objects whose keys are states and whose values are objects
* inside of which the transition-to state is the value of each of its key/value pairs.
*
* @func
* @sig {k: v} -> [String]
* @param {Object} machine A single state machine
* @returns {String[]} A list of unique state transitions for the provided machine
*/
export var getTransitionsForMachine = _default5(_default9, _default12(isStringieThingie), _default10, _default13(_default11), _default11);
/**
* Retrieves a unique list all the inputs for a given state machine (which correspond to Action Types).
* State machines are objects whose keys are states and whose values are objects
* inside of which the input value is the key of each of its key/value pairs.
*
* @func
* @sig {k: v} -> [String]
* @param {Object} machine A single state machine
* @returns {String[]} A list of unique inputs to the provided state machine
*/
export var getStateInputsForMachine = _default5(_default9, _default12(isStringieThingie), _default10, _default13(_default14), _default11);
/**
* Retrieves a unique list all the inputs for all of a given duck's state machines.
* State machines are objects whose keys are states and whose values are objects
* inside of which the input value is the key of each of its key/value pairs.
*
* @func
* @sig {k: v} -> [String]
* @param {Object} duck A duck containing one or more state machines
* @returns {String[]} A list of unique inputs to all the state machines for the provided duck
*/
export var getStateInputsForAllMachines = _default5(_default9, _default10, _default13(getStateInputsForMachine), _default11, _default15('machines'));
/**
* Retrieves any invalid inputs from a given duck's state machines.
* State machines are objects whose keys are states and whose values are objects
* inside of which the input value is the key of each of its key/value pairs.
*
* @func
* @sig {k: v} -> [String]
* @param {Object} duck A duck containing one or more state machines and action types
* @returns {String[]} A list of any invalid inputs for the state machines of the provided duck
*/
export var invalidStateMachineInputs = _default16(_default17, [getStateInputsForAllMachines, getTypes]);
/**
* Checks whether all a given state machine's states are string values.
* State machines are objects whose keys are states and whose values are objects
* inside of which the input value is the key of each of its key/value pairs.
*
* @func
* @sig {k: v} -> Boolean
* @param {Object} machine A single state machine
* @returns {Boolean} whether or not the states for the state machine are strings
*/
export var areStateNamesStrings = _default5(_default18(_default19(String)), _default14);
/**
* Checks whether all a given state machine's inputs and transition names are string values.
* State machines are objects whose keys are states and whose values are objects
* inside of which the input value is the key and transition is the value of each of its key/value pairs.
*
* @func
* @sig {k: v} -> Boolean
* @param {Object} machine A single state machine
* @returns {Boolean} whether or not the inputs and transitions for the state machine are strings
*/
export var areInputsAndTransitionsStrings = _default5(_default18(_default20([isPlainObj, _default21(_default3, _default5(_default18(_default18(_default19(String))), _default22))])), _default11);
/**
* Checks whether all the state machine inputs for a given duck correspond to its action types.
* State machines are objects whose keys are states and whose values are objects
* inside of which the input value is the key of each of its key/value pairs.
*
* @func
* @sig {k: v} -> Boolean
* @param {Object} duck A duck containing one or more state machines and action types
* @returns {Boolean} whether or not all the inputs for the state machines are among the duck's action types
*/
export var areStateMachineInputsActionTypes = _default5(_default18(_default3), invalidStateMachineInputs);
/**
* Checks if all a state machine's transitions are states in that machine.
* State machines are objects whose keys are states and whose values are objects
* inside of which the transition-to state is the value of each of its key/value pairs.
*
* @func
* @sig {k: v} -> Boolean
* @param {Object} machine A single state machine
* @returns {Boolean} whether or not all the transition names are also states for a given state machine
*/
export var isEachTransitionAmongMachineStates = _default23(_default17, [getTransitionsForMachine, _default14]);
/**
* Checks to see whether a given machine allows for a given transition, provided its current state
*
* @func
* @sig String -> String -> {k: v} -> Boolean
* @param {String} transitionName A string value representing a potential state transition
* @param {String} currentState A string value representing the current state
* @param {Object} machine A state machine which contains the possible states and their associated transitions
* @returns {Boolean} whether or not the transition is possible from the current state
*/
export var isTransitionPossible = _default4(function (transitionName, currentState, machine) {
return _default5(_default24(coerceToString(transitionName)), _default8({}), _default25([coerceToString(currentState)]), _default8({}))(machine);
});
/**
* Creates a "machine state" object, which can be combined with others to create a single state machine.
* The keys for a machine state object (called inputs or input values) correspond to the names of redux action types.
* The values for a machine state object (called transitions) are merely
* the names of fellow machine state objects (also created by this function).
*
* @func
* @sig {k: v} -> {k: v} -> {k: v}
* @param {Object} machine A single state machine
* @param {Object} types An object whose keys/values correspond
* to redux actions and will be used to set up inputs for the state machine
* @returns {Object} a validated, immutable state machine
*/
export var createMachineStates = _default4(function () {
var machine = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var types = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
return _default5(Object.freeze, _default26(listOfPairsToOneObject, {}), _default13(function (_ref2) {
var state = _ref2[0];
var transitions = _ref2[1];
return [state, _default27(_default5(_default13(_default28), _default12(_default5(_default29(_default30, _default11(types)), _default28)), _default12(_default5(_default24(_default30, machine), _default31)), _default12(_default18(_default19(String))), _default22)(transitions), transitions)];
}), _default12(_default32(_default5(_default19(String), _default28), _default5(isPlainObj, _default31))), _default22)(machine);
});
/**
* Creates a frozen (immutable) object whose keys are all the possible "current state"
* values for the machine and whose values are objects containing each action type that
* may produce a transition to another state.
*
* @func
* @sig {k: v} -> {k: v} -> {k: v}
* @param {Object} machines An object full of one or more state machines
* @param {Object} duck A duck instance
* @returns {Object} an object of validated, immutable state machines
*/
export var createMachines = _default4(function (machines, duck) {
return _default5(Object.freeze, _default26(listOfPairsToOneObject, {}), _default13(function (_ref3) {
var name = _ref3[0];
var machine = _ref3[1];
return [name, createMachineStates(machine, duck.types)];
}), _default22)(machines);
});
/**
* Creates an string path (array) that the state machine functions will use to
* lookup and alter the current state of one or more of the state machines.
* The path supplied to this function can be a single prop name (String) or
* an Array of prop names that represent a nested prop's path.
* And if a String is passed in, a dot-separated format is also supported.
*
* @func
* @sig String -> [String]
* @param {String|String[]} path The prop name or path to the section of the
* store where the state machines current state is tracked.
* @returns {String[]} an array of strings representing the nested path to the
* prop where state machines current state is tracked in the redux store
*/
export var getStateMachinesPropPath = _default33(_default20([_default19(String), _default29('.'), isValidPropName]), _default34('.'), _default5(_default35(_default3, _default36(['states'])), _default12(isNotBlankString), _default35(_default19(String), Array)));
/**
* Builds a flattened object where the keys are the names of each of the duck's state machines,
* and the values are each set to 'initial'.
*
* @func
* @sig {k: v} -> {k: v}
* @param {Object} machines An object containing the definitions for one or more state machines
* @returns {Object} a flattened object where the current state of each state machine will be tracked
*/
export var getDefaultStateForMachines = function getDefaultStateForMachines() {
var machines = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
return _default26(function (obj, key) {
var _extends2;
return _extends({}, obj, (_extends2 = {}, _extends2[key] = 'initial', _extends2));
}, {}, _default14(machines || {}));
};
/**
* Retrieves the section of the redux store where the current state is being
* tracked for one or more state machines. The shape of this section of the store
* is usually just a flattened object with a single value for each named state
* machine, representing its current state. Each possible state for any of those
* is defined in the Ducks.
*
* @func
* @sig {k: v} -> {k: v} -> {k: v}
* @param {Object} duck A duck instance, containing the state machines and the
* prop name/path to the section of the store where their state is tracked
* @param {*} state The current state of this portion of the store
* @returns {Object} A flattened Object containing the names of each state
* machine (String) and its current state (String) as key/value pairs
*/
export var getCurrentState = function getCurrentState() {
var _ref4 = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
var _ref4$machines = _ref4.machines;
var machines = _ref4$machines === undefined ? {} : _ref4$machines;
var stateMachinesPropName = _ref4.stateMachinesPropName;
return _default5(_default37(_default13(_default36('initial'), machines)), _default27(_default14(machines)), _default8({}), _default25(coerceToArray(stateMachinesPropName)));
};
/**
* Based on the current state of the machines (managed in the store itself) and
* the rules defined for each state machine (in the ducks), an action is processed
* and may cause one or more ore the state machines to transition.
*
* @func
* @sig {k: v} -> {k: v} -> {k: v}
* @param {Object|*} state The current state of this portion of the store
* @param {Object} action A dispatched redux action
* @param {Object} duck A duck instance, containing the state machines and the
* prop name/path to the section of the store where their state is tracked
* @returns {Object} A flattened Object containing the names of each state
* machine (String) and its current state (String) as key/value pairs
*/
export var getNextState = _default4(function (state, action, _ref5) {
var _ref5$machines = _ref5.machines;
var machines = _ref5$machines === undefined ? {} : _ref5$machines;
var stateMachinesPropName = _ref5.stateMachinesPropName;
var currentState = getCurrentState({ machines: machines, stateMachinesPropName: stateMachinesPropName })(state);
return _default5(_default26(listOfPairsToOneObject, {}), _default13(function (_ref6) {
var name = _ref6[0];
var machine = _ref6[1];
return _default5(_default38(name), _default6(isNotNil, _default36(currentState[name])), _default15(action.type), _default8({}), _default15(currentState[name]))(machine);
}), _default22)(machines);
});
/**
* Checks whether a dispatched action's type is listed among the
* current state's input values for one or more of the state machines.
*
* @func
* @sig {k: v} -> {k: v} -> {k: v} -> Boolean
* @param {Object|*} state The current state of this portion of the store
* @param {Object} action A dispatch redux action
* @param {Object} duck A duck, which contains both state machines and the
* corresponding prop name/path in the redux store where their current state is tracked
* @returns {Boolean} whether or not the dispatched action is listed as an input
* value for the state machine(s) current state
*/
export var isActionTypeInCurrentState = _default4(function (state, action, _ref7) {
var machines = _ref7.machines;
var stateMachinesPropName = _ref7.stateMachinesPropName;
var currentState = getCurrentState({ machines: machines, stateMachinesPropName: stateMachinesPropName })(state);
return _default5(_default39(function (_ref8) {
var name = _ref8[0];
var machine = _ref8[1];
return _default5(isNotNil, _default15(action.type), _default8({}), _default15(_default30, machine), _default15(name))(currentState);
}), _default22)(machines);
});