UNPKG

react-behave

Version:

A library of behaviors for React applications.

59 lines (53 loc) 1.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.initializeStateWithValue = initializeStateWithValue; exports.asyncStateReducer = asyncStateReducer; exports.ERROR = exports.SUCCESS = exports.START = void 0; var INITIAL_STATE = { value: null, error: null, pending: false }; var START = 'START'; exports.START = START; var SUCCESS = 'SUCCESS'; exports.SUCCESS = SUCCESS; var ERROR = 'ERROR'; exports.ERROR = ERROR; function initializeStateWithValue(value) { return Object.assign({}, INITIAL_STATE, { value: value }); } function asyncStateReducer(state, action) { switch (action.type) { case START: { return Object.assign({}, state, { pending: true }); } case SUCCESS: { return { value: action.value, error: null, pending: false }; } case ERROR: { // We don't want to loose a previous value in case of later error. return Object.assign({}, state, { error: action.error, pending: false }); } default: { throw new Error("Unknown action type ".concat(action.type)); } } }