diff --git a/src/utils/index.js b/src/utils/index.js index 6e46e2a..5846078 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -3,7 +3,6 @@ import compose from './compose'; import deepMerge from './deepMerge'; import makeCancelable from './makeCancelable'; -import createState from './local-state'; import monaco from './monaco'; -export { noop, compose, deepMerge, makeCancelable, createState, monaco }; +export { noop, compose, deepMerge, makeCancelable, monaco }; diff --git a/src/utils/local-state/createState.js b/src/utils/local-state/createState.js deleted file mode 100644 index f0ec89a..0000000 --- a/src/utils/local-state/createState.js +++ /dev/null @@ -1,82 +0,0 @@ -import { compose, curry, isObject, isFunction, isEmpty } from './utils'; - -function createState(initial, handler = {}) { - validateInitial(initial); - validateHandler(handler); - - const state = { current: initial }; - - const didUpdate = curry(didStateUpdate)(state, handler); - const update = curry(updateState)(state); - const validate = curry(validateChanges)(initial); - const getChanges = curry(extractChanges)(state); - - function getState(selector = state => state) { - return selector(state.current); - } - - function setState(causedChanges) { - compose( - didUpdate, - update, - validate, - getChanges, - )(causedChanges); - } - - return [getState, setState]; -} - -function extractChanges(state, causedChanges) { - return isFunction(causedChanges) - ? causedChanges(state.current) - : causedChanges; -} - -function updateState(state, changes) { - state.current = { ...state.current, ...changes }; - - return changes; -} - -function didStateUpdate(state, handler, changes) { - isFunction(handler) - ? handler(state.current) - : Object.keys(changes) - .forEach(field => handler[field]?.(state.current[field])); - - return changes; -} - -function validateChanges(initial, changes) { - if (!isObject(changes)) errorHandler('changeType'); - if (Object.keys(changes).some(field => !initial.hasOwnProperty(field))) errorHandler('changeField'); - - return changes; -} - -function validateHandler(handler) { - if (!(isFunction(handler) || isObject(handler))) errorHandler('handlerType'); -} - -function validateInitial(initial) { - if (!initial) errorHandler('initialIsRequired'); - if (!isObject(initial)) errorHandler('initialType'); - if (isEmpty(initial)) errorHandler('initialContent'); -} - -const errorMessages = { - initialIsRequired: 'initial state is required', - initialType: 'initial state should be an object', - initialContent: 'initial state shouldn\'t be an empty object', - handlerType: 'handler should be an object or a function', - changeType: 'provided value of changes should be an object', - changeField: 'it seams you want to change a field in the store which is not specified in the "initial" state', - default: 'an unknown error has occurred in `local-storer` package', -}; - -function errorHandler(type) { - throw new Error(errorHandler[type] || errorMessages.default); -} - -export default createState; diff --git a/src/utils/local-state/index.js b/src/utils/local-state/index.js deleted file mode 100644 index 9b9f200..0000000 --- a/src/utils/local-state/index.js +++ /dev/null @@ -1,3 +0,0 @@ -import createState from './createState'; - -export default createState; diff --git a/src/utils/local-state/utils.js b/src/utils/local-state/utils.js deleted file mode 100644 index b034a7f..0000000 --- a/src/utils/local-state/utils.js +++ /dev/null @@ -1,25 +0,0 @@ -function compose(...fns) { - return x => fns.reduceRight((y, f) => f(y), x); -} - -function curry(fn) { - return function curried(...args) { - return args.length >= fn.length - ? fn.apply(this, args) - : (...nextArgs) => curried.apply(this, [...args, ...nextArgs]); - } -} - -function isObject(value) { - return ({}).toString.call(value).includes('Object'); -} - -function isEmpty(obj) { - return !Object.keys(obj).length; -} - -function isFunction(value) { - return typeof value === 'function'; -} - -export { compose, curry, isObject, isEmpty, isFunction }; diff --git a/src/utils/monaco.js b/src/utils/monaco.js index a9161e1..965d604 100644 --- a/src/utils/monaco.js +++ b/src/utils/monaco.js @@ -1,9 +1,10 @@ +import { create as createState } from 'state-local'; + import defaultConfig from '../config'; import { compose, deepMerge, makeCancelable, - createState, } from '../utils'; const [getState, setState] = createState({