UNPKG

@raulpesilva/re-state

Version:

easy way to create a shared state to the entire application

42 lines (32 loc) 1.81 kB
"use strict"; exports.__esModule = true; exports.createReStateMethods = void 0; var _createGetReState = require("./createGetReState"); var _createReState = require("./createReState"); var _createReStateDispatch = require("./createReStateDispatch"); var _createReStateSelect = require("./createReStateSelect"); var _store = require("./store"); /** * Creates a set of methods to use with React. * @param name Unique name of the state * @param initialValue initial value of the state * @param valueOfReset value to reset when calling resetReState * @returns a set of methods to use with React */ var createReStateMethods = function createReStateMethods(name, initialValue, valueOfReset) { var _methods; if (!name) throw new Error('Name is required'); if (typeof name !== 'string') throw new Error('Name need to be a string'); var capitalizedName = name.charAt(0).toUpperCase() + name.slice(1); var use = (0, _createReState.createReState)(name, initialValue); var useSelect = (0, _createReStateSelect.createReStateSelect)(name); var dispatch = (0, _createReStateDispatch.createReStateDispatch)(name); var get = (0, _createGetReState.createGetReState)(name); if (valueOfReset) (0, _store.setReStateInitialValue)(name, valueOfReset.value); var methods = (_methods = {}, _methods["use" + capitalizedName] = use, _methods["use" + capitalizedName + "Select"] = useSelect, _methods["dispatch" + capitalizedName] = dispatch, _methods["get" + capitalizedName] = get, _methods["reset" + capitalizedName] = function reset() { var _valueOfReset$value; return dispatch((_valueOfReset$value = valueOfReset == null ? void 0 : valueOfReset.value) != null ? _valueOfReset$value : initialValue); }, _methods); return methods; }; exports.createReStateMethods = createReStateMethods;