store-util.ts


Overview

The store-util.ts file provides utility functions designed to assist with managing and interacting with application state effects, particularly within a namespaced effect-loading context. It includes helper functions for checking the loading status of asynchronous effects and a general-purpose delay function to introduce asynchronous wait periods.

These utilities are typically used in Redux or Redux-Saga-like state management patterns where effects (side effects such as API calls or asynchronous operations) are namespaced and tracked via boolean flags indicating their loading status.


Functions

getOneNamespaceEffectsLoading

export const getOneNamespaceEffectsLoading = (
  namespace: string,
  effects: Record<string, boolean>,
  effectNames: Array<string>,
) => boolean

Description

Checks if any one of the specified effects within a given namespace is currently loading.

This function inspects a dictionary (effects) where keys are namespaced effect identifiers (in the form "namespace/effectName") and values are booleans representing the loading state of each effect. It returns true if at least one of the provided effect names under the given namespace is loading, otherwise false.

Parameters

Returns

Usage Example

const effects = {
  'user/fetchUser': false,
  'user/updateUser': true,
  'auth/login': false,
};

const isUserLoading = getOneNamespaceEffectsLoading('user', effects, ['fetchUser', 'updateUser']);
console.log(isUserLoading); // Output: true (because 'user/updateUser' is true)

delay

export const delay = (ms: number) => Promise<void>

Description

Creates a promise that resolves after a specified number of milliseconds. This function is a simple utility to pause asynchronous code execution for a given duration.

Parameters

Returns

Usage Example

async function fetchData() {
  console.log('Start fetch');
  await delay(1000);  // wait for 1 second
  console.log('Data fetched after 1 second delay');
}

Implementation Details


Interaction with Other Parts of the Application


Visual Diagram

flowchart TD
    A[getOneNamespaceEffectsLoading] -->|Checks| B[effects: Record<string, boolean>]
    A -->|Uses| C[effectNames: string[]]
    A -->|Filters by| D[namespace: string]
    A -->|Returns| E[boolean]

    F[delay] -->|Takes| G[ms: number]
    F -->|Returns| H[Promise<void>]

Summary

The store-util.ts file provides two concise, reusable utility functions to support state management in applications:

These functions facilitate cleaner code and better abstraction when dealing with asynchronous state and side effects.