Skip to content

Pattern: Batch for Complex Mutations

Problem

Implement pattern: batch for complex mutations in a production-friendly way with @vielzeug/stateit while keeping setup and cleanup explicit.

Runnable Example

The snippet below is copy-paste runnable in a TypeScript project with @vielzeug/stateit installed.

When a domain operation touches multiple fields at once, wrap in batch() so watchers see only the final state:

ts
import { batch } from '@vielzeug/stateit';

export function applySettings(settings: UserSettings) {
  batch(() => {
    userStore.patch({ theme: settings.theme });
    userStore.patch({ language: settings.language });
    userStore.patch({ notifications: settings.notifications });
  });
  // → one notification for all three changes together
}

Expected Output

  • The example runs without type errors in a standard TypeScript setup.
  • The main flow produces the behavior described in the recipe title.

Common Pitfalls

  • Forgetting cleanup/dispose calls can leak listeners or stale state.
  • Skipping explicit typing can hide integration issues until runtime.
  • Not handling error branches makes examples harder to adapt safely.