2.6 KiB
2.6 KiB
Hook Guidelines (Bloc/Cubit State Flows)
This repository is Flutter-based. This file defines state-flow conventions using Bloc/Cubit.
Practical Conventions
- Treat
AuthBlocas the global auth source of truth; auth invalidation should flow through its event chain. - Keep Cubit/Bloc states immutable and updated via
copyWith. - Catch-and-handle branches should log errors with module-scoped logger names.
- For async flow handoff (auth/session switch, SSE stream lifecycle), ensure race ordering is explicit (queues/epoch tokens/cancel semantics).
Real File Path Examples
- Auth state source of truth and transitions:
apps/lib/features/auth/presentation/bloc/auth_bloc.dartapps/lib/features/auth/presentation/bloc/auth_event.dartapps/lib/features/auth/presentation/bloc/auth_state.dart
- Feature-local form/interaction state:
apps/lib/features/auth/presentation/cubits/login_cubit.dartapps/lib/features/settings/presentation/cubits/automation_jobs_cubit.dart
- Multi-step async orchestration with explicit stream/session control:
apps/lib/features/chat/presentation/bloc/chat_bloc.dartapps/lib/core/chat/ag_ui_service.dartapps/lib/app/app.dart
State-Flow Checklist
Before adding/changing state logic:
- Is state ownership clear (global vs feature-local)?
- Are transitions serialized when order matters?
- Are recoverable failures surfaced to UI state and logged?
- Are side effects (cache reset, prewarm, SSE cancel/restart) bound to lifecycle events, not random UI callbacks?
Anti-patterns (with evidence)
- Feature-level auth reset/navigation bypassing global auth flow.
- Correct path:
ApiClientauth-failure callback ->AuthSessionInvalidatedevent (apps/lib/app/di/injection.dart,apps/lib/features/auth/presentation/bloc/auth_bloc.dart).
- Correct path:
- Silent exception handling in async orchestration.
- Existing weak branches to avoid copying:
apps/lib/app/di/injection.dartrefresh callback catches and returnsfalsewithout structured logging.apps/lib/core/chat/ag_ui_service.dartignores malformed SSE payload parsing errors by design.
- Existing weak branches to avoid copying:
- Emitting mutable/partially-updated state objects.
- Current state classes use immutable fields +
copyWith(LoginState,ChatState,AutomationJobsState).
- Current state classes use immutable fields +
Uncertainties / Gaps
- The codebase uses both Bloc and Cubit; exact selection criteria (when to pick Bloc vs Cubit) are not formally codified beyond existing patterns.
- Some cancellation/retry branches return error strings directly (e.g., chat flow) and may need future standardization if UX/error taxonomy requirements tighten.