UNPKG

dreamstate

Version:

Store management library based on react context and observers

50 lines (47 loc) 2.45 kB
import { shallowEqualObjects } from 'shallow-equal'; import { DreamstateError } from '../error/DreamstateError.js'; import { ActionsStore } from '../storing/ActionsStore.js'; import { ComputedValue } from '../storing/ComputedValue.js'; import { NestedStore } from '../storing/NestedStore.js'; import { EDreamstateErrorCode } from '../../types/error.js'; import { isObject } from '../../utils/typechecking.js'; /** * Compares two context manager states to determine if there are any changes that would require * observers to update. Performs a shallow comparison while respecting specific meta-fields like * `NestedStore`, `ComputedValue`, `ActionsStore`, etc. to ensure that nested objects and specialized * stores are taken into account during the comparison. * * The comparison helps decide whether the observers need to react to the changes and re-render accordingly. * * @template T - The type of the context state. * @param {T} previousContext - The previous context to be compared against. * @param {T} nextContext - The new context to check for differences. * @returns {boolean} - `true` if observers should update (i.e., if there is a difference between * the contexts); `false` otherwise. */ function shouldObserversUpdate(previousContext, nextContext) { if (!isObject(nextContext)) { throw new DreamstateError(EDreamstateErrorCode.INCORRECT_PARAMETER, "Context should be non-nullable object, supplied '".concat(typeof nextContext, "' type instead.")); } if (!previousContext) { return true; } return Object.keys(nextContext).some(function (key) { /* * Ignore computed values. * Ignore action values. * Since nested computed stores are not representing data itself, we should not verify anything there. */ if (nextContext[key] instanceof ComputedValue || nextContext[key] instanceof ActionsStore) { return false; } /* * Shallow check for mutable objects created by library. * Optimization for sub-states to prevent pollution of context and improve performance. * We cannot guess about each object because it is (1) not obvious, (2) can be unwanted and (3) will not work for * some objects like native MediaStream/MediaStreamTrack. */ return nextContext[key] instanceof NestedStore ? !shallowEqualObjects(nextContext[key], previousContext[key]) : nextContext[key] !== previousContext[key]; }); } export { shouldObserversUpdate };