chrono-forge
Version:
A comprehensive framework for building resilient Temporal workflows, advanced state management, and real-time streaming activities in TypeScript. Designed for a seamless developer experience with powerful abstractions, dynamic orchestration, and full cont
82 lines (81 loc) • 3.58 kB
TypeScript
/**
* Entity store management module
*
* This module provides a Redux reducer and utility functions for managing
* normalized entity data in a Redux store. It uses immutability-helper
* to perform immutable updates on the entity state.
*/
import { Schema } from 'normalizr';
import { Spec } from 'immutability-helper';
import type { EntityAction, EntityStrategy } from './actions';
/**
* Represents a single entity's state in the store
* @typedef {Object} EntityState
* @property {string|number} [key] - Entity identifier
* @property {any} [value] - Entity data
*/
export type EntityState = Record<string, any>;
/**
* Represents the structure of the entities state in the Redux store
* @typedef {Object} EntitiesState
* @property {EntityState} [entityType] - Map of entity types to their states
*/
export type EntitiesState = Record<string, Record<string, EntityState>>;
/**
* Default empty state for the entities reducer
* @type {EntitiesState}
*/
export declare const defaultState: EntitiesState;
/**
* Initial state for the entities reducer
* @type {EntitiesState}
*/
export declare const initialState: EntitiesState;
/**
* Normalizes entity data using normalizr
* @template T - The type of data to normalize
* @param {T|T[]} data - The entity or array of entities to normalize
* @param {Schema|string} entitySchema - The schema to use for normalization or entity name
* @returns {EntitiesState} The normalized entities state
*/
export declare const normalizeEntities: <T>(data: T | T[], entitySchema: Schema | string) => EntitiesState;
/**
* Creates an update statement for immutability-helper to merge normalized entities into state
* @param {EntitiesState} state - Current entities state
* @param {EntitiesState} normalizedEntities - Normalized entities to merge into state
* @returns {Spec<EntitiesState>} An immutability-helper spec object for updating the state
*/
export declare const createUpdateStatement: (state: EntitiesState, normalizedEntities: EntitiesState) => Spec<EntitiesState>;
/**
* Handles updating entities with different strategies
* @param {EntitiesState} state - Current entities state
* @param {EntityState} entities - Entities to update
* @param {EntityStrategy} [strategy='$merge'] - Strategy to use for the update
* @returns {Spec<EntitiesState>} An immutability-helper spec object for updating the state
*/
export declare const handleUpdateEntities: (state: EntitiesState, entities: EntityState, strategy?: EntityStrategy) => Spec<EntitiesState>;
/**
* Creates a spec for deleting entities from the state
* @param {EntitiesState} state - Current entities state
* @param {EntitiesState} entities - Entities to delete
* @returns {Spec<EntitiesState>} An immutability-helper spec object for deleting entities
*/
export declare const handleDeleteEntities: (state: EntitiesState, entities: EntitiesState) => Spec<EntitiesState>;
/**
* Reducer function for the entities state
* Handles various entity-related actions
* @param {EntitiesState} [state=initialState] - Current entities state
* @param {EntityAction} action - Action to process
* @returns {EntitiesState} Updated entities state
*/
export declare function reducer(state: EntitiesState | undefined, action: EntityAction): EntitiesState;
/**
* Extension of the immutability-helper Spec type that allows for indexable properties
* Used for building dynamic update specs
* @typedef {Object} IndexableSpec
* @template T - The type of the spec
*/
export type IndexableSpec<T> = Spec<T> & {
[key: string]: any;
};
export default reducer;