Skip to main content

The Actions

Type of Actions

There is 2 type of action in StatePulse IAction and ISafeAction.

IAction – Regular and Lightweight Actions

IAction represents a standard action in the system. These are the most common type of actions and do not include any built-in safety mechanisms like debouncing, deduplication, or self-cancellation.

⚠️ You should not wrap everything in ISafeAction just for peace of mind.
IAction exists for a reason — performance.

IncrementCounterResultAction.cs
using StatePulse.Net;
public record IncrementCounterResultAction : IAction
{
public int Count { get; init; }
}

✅ When to Use

Use IAction when:

  • You want maximum performance
  • The action does not require race condition protection
  • It's okay if the action is triggered multiple times
  • You’re working with local state or UI updates

You should note that IAction is record and is not recommended to add constructor arguments because of the way Dispatch works.


ISafeAction – Safe Execution for Critical Actions

ISafeAction enforces safe execution of a given action to:

  • Prevent duplicate triggers
  • Avoid race conditions
  • Maintain consistent state
  • Support self-cancellation of outdated executions

This is ideal for scenarios like API calls, where a user might interact rapidly with the UI (e.g. clicking buttons multiple times or navigating too quickly). Without safety, the first action's result might arrive after the second one, and still update the state — causing inconsistency.

IncrementCounterAction.cs
using StatePulse.Net;
public record IncrementCounterAction : ISafeAction
{
public int Delay { get; init; }
}

⚠️ Performance Consideration

Not all actions need to be safe.

Using ISafeAction introduces a small performance overhead, such as managing token lifecycles or task state tracking. This overhead is generally negligible, but unnecessary for:

  • Simple state updates
  • Fire-and-forget logic
  • Operations that are safe to duplicate

Use it only when necessary, especially for actions where result timing and state consistency matter (like network requests).