API

MiniState API Documentation

MiniState is the core class of the mini-state library, providing a simple, signal-based state management solution for Angular applications. It handles async operations, loading states, errors, and success messages with a clean, declarative API.

Note: The recommended way to initialize a MiniState is through the MiniStateBuilder. It simplifies the setup and ensures best practices are followed.

Core Concept

Constructor

Creates a new MiniState instance with the specified trigger function and optional parameters.

Properties

MiniState provides both signals and observables for all state properties:

  • data: Signal for the current data value
  • loading: Signal indicating if an operation is in progress
  • errorMsg: Signal for the current error message (if any)
  • successMsg: Signal for the current success message (if any)
  • data$: Observable for the data value
  • loading$: Observable for the loading state
  • errorMsg$: Observable for error messages
  • successMsg$: Observable for success messages

Methods

Methods to initiate or repeat operations:

  • trigger(input): Initiates the async operation with the provided input
  • retrigger(): Repeats the most recent operation with the same input
  • unsubscribe(): Stops all ongoing operations and cleans up resources

Methods to customize MiniState behavior:

  • setSuccessMsgFn(fn): Sets a function that generates success messages
  • setOnTriggerFn(fn): Sets a function that runs when trigger() is called
  • setErrorMsgFn(fn): Sets a function to convert errors to user-friendly messages

Methods to directly modify the data state:

  • setData(data): Sets the data value directly
  • updateData(updateFn): Updates data using a callback function

Methods for error management:

  • setErrorMsg(msg): Sets an error message directly
  • resetErrorMsg(): Clears the current error message

Configuration Methods

Sets a function to generate custom success messages after successful operations.

  • Will emit nothing if not set
  • Particularly useful for operations like update or delete where user feedback is important

Sets a function to convert error objects to user-friendly error messages.

  • If not set, defaults to extracting message or msg property from error object

Sets a function to process and transform the raw output data before it's stored and emitted.

  • Defaults to returning the data unchanged if not set
  • Gives access to input, output, previous input, and previous output values
  • Useful for data transformation, filtering, or combining with existing data

Sets a function to be called after a successful operation completes.

  • Useful for triggering actions after a successful operation, such as navigation or starting another process
  • Executes after the data and success message have been emitted

Sets a function to be called when an operation fails.

  • Useful for handling specific error conditions, such as redirecting to an error page
  • Executes after the error message has been emitted

Sets a function to be called when a trigger is initiated, before the async operation begins.

  • Useful for pre-operation setup, validation, or logging
  • Executes immediately when trigger() is called, before the async operation starts

Complete Example

The following example demonstrates a complete user search component using MiniState:

Just using if statements rather tha input triggered popups to demonstrate the functionality clearer