dreamstate
Version:
Store management library based on react context and observers
29 lines (26 loc) • 1.29 kB
JavaScript
import { DreamstateError } from '../core/error/DreamstateError.js';
import { NestedStore } from '../core/storing/NestedStore.js';
import { EDreamstateErrorCode } from '../types/error.js';
import { isObject } from './typechecking.js';
/**
* Creates a nested sub-state for deeper shallow checking, useful when the context contains nested objects
* that need to be checked separately during updates.
*
* As an example:
* - `{ first: 'first', second: { one: 1, two: 2 } }` - `first` and `second` will be checked,
* while `one` and `two` will be ignored.
* - `{ first: 'first', second: createNested({ one: 1, two: 2 }) }` - `first`, `one`, and `two`
* will be checked during updates.
*
* @template T The type of the nested object.
* @param {T} initialValue The initial value of the nested store object.
* @returns {TNested<T>} An instance of a nested store containing the initial state, marked for deeper shallow checking.
*/
function createNested(initialValue) {
if (isObject(initialValue)) {
return Object.assign(new NestedStore(), initialValue);
} else {
throw new DreamstateError(EDreamstateErrorCode.INCORRECT_PARAMETER, "Nested stores should be initialized with an object, got '".concat(typeof initialValue, "' instead."));
}
}
export { createNested };