Skip to content

feat: add force flag to emit in Bloc and Cubit - #4862

Open
OmarAly92 wants to merge 1 commit into
felangel:masterfrom
OmarAly92:claude/cubit-bloc-emit-force-qpogyt
Open

feat: add force flag to emit in Bloc and Cubit#4862
OmarAly92 wants to merge 1 commit into
felangel:masterfrom
OmarAly92:claude/cubit-bloc-emit-force-qpogyt

Conversation

@OmarAly92

Copy link
Copy Markdown

Status

READY

Breaking Changes

NO

Description

This PR adds a force parameter to the emit() method across the bloc library, allowing developers to emit duplicate states (states equal to the current state) when needed.

Changes

  • BlocBase: Added optional force parameter to emit(). When force: true, the state is emitted even if it equals the current state, triggering onChange callbacks and notifying listeners.
  • Bloc: Updated the emit() override to accept and forward the force parameter. Modified the internal onEmit callback in event handlers to respect the force flag.
  • Emitter: Added force parameter to the call() method and the _Emitter implementation to support forced emissions in event handlers.
  • ReplayBlocMixin & ReplayCubitMixin: Updated to forward the force parameter through the replay stack operations.
  • Tests: Added comprehensive test coverage for the new functionality:
    • Cubit tests verify forced emissions trigger onChange callbacks and emit duplicate states
    • Bloc tests verify forced emissions work across events and trigger onTransition callbacks
    • Tests confirm forced emissions work with identical states and that the feature doesn't affect subsequent normal emissions

Use Case

This feature enables scenarios where developers need to re-emit the current state to trigger listeners or observers, which is useful for state refresh operations or explicit state notifications.

Adds an optional `force` parameter (defaults to `false`) to `emit` so a
state can be emitted even when it is equal to the current state.

- `Emittable.emit`, `BlocBase.emit` and `Bloc.emit` accept `{bool force = false}`
- `Emitter.call` forwards `force`, so `emit(state, force: true)` works
  inside event handlers and still triggers `onTransition`/`onChange`
- `ReplayCubitMixin.emit` and `ReplayBlocMixin.emit` forward `force`

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sy5uFShMuySLy6T8kGaXor
@OmarAly92
OmarAly92 requested a review from felangel as a code owner August 13, 2026 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants