Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bloc)!: BlocOverrides API #2932

Merged
merged 12 commits into from
Nov 14, 2021
Merged

feat(bloc)!: BlocOverrides API #2932

merged 12 commits into from
Nov 14, 2021

Conversation

felangel
Copy link
Owner

@felangel felangel commented Nov 9, 2021

Status

READY

Breaking Changes

YES

Description

The current API used to override the default BlocObserver and EventTransformer is:

void main() {
  Bloc.observer = CustomBlocObserver();
  Bloc.transformer = customEventTransformer();
  
  // ...
}

This approach relies on a global singleton for both the BlocObserver and EventTransformer. As a result, it's not possible to:

  • Have multiple BlocObserver or EventTransformer implementations scoped to different parts of the application
  • Have BlocObserver or EventTransformer overrides be scoped to a package
    • Suppose a package that depends on package:bloc registers its own BlocObserver, any consumer of the package will either have to overwrite the package's BlocObserver or report to the package's BlocObserver.

It's also more difficult to test because of the shared, global state across tests.

This pull request introduces a BlocOverrides class which allows developers to override BlocObserver and/or EventTransformer for a specific Zone rather than relying on a global, mutable singleton.

void main() {
  BlocOverrides.runZoned(
    () {
      // ...
    },
    blocObserver: CustomBlocObserver(),
    eventTransformer: customEventTransformer(),
  );
}

Bloc instances will use the BlocObserver and/or EventTransformer for the current Zone via BlocOverrides.current. If there are no BlocOverrides for the zone, they will use the existing internal defaults (base BlocObserver and concurrent EventTransformer -- no change in terms of behavior/functionality).

As previously mentioned, this would allow each zone to function independently with its own BlocOverrides and BlocOverrides are immutable meaning the BlocOverrides specified when the zone is created cannot be modified.

BlocOverrides.runZoned(
  () {
    // BlocObserverA and eventTransformerA
    final overrides = BlocOverrides.current;
      
    // Blocs in this zone report to BlocObserverA
    // and use eventTransformerA as the default transformer.
    // ...

    // Later...
    BlocOverrides.runZoned(
      () {
        // BlocObserverB and eventTransformerB
        final overrides = BlocOverrides.current;
    
        // Blocs in this zone report to BlocObserverB
        // and use eventTransformerB as the default transformer.
        // ...
      },
      blocObserver: BlocObserverB(),
      eventTransformer: eventTransformerB(),
    );
  },
  blocObserver: BlocObserverA(),
  eventTransformer: eventTransformerA(),
);

This pull request removes the static, mutable Bloc.observer and Bloc.transformer APIs which is a breaking change. BlocObserver and EventTransformer overrides will need to be specified via BlocOverrides.runZoned instead as described above.

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

@felangel felangel added enhancement New feature or request pkg:bloc This issue is related to the bloc package breaking change Enhancement candidate would introduce a breaking change labels Nov 9, 2021
@felangel felangel self-assigned this Nov 9, 2021
@felangel felangel added this to In progress in bloc via automation Nov 9, 2021
@codecov
Copy link

codecov bot commented Nov 9, 2021

Codecov Report

Merging #2932 (eef7b60) into feat/v8.0.0 (a08e238) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@              Coverage Diff              @@
##           feat/v8.0.0     #2932   +/-   ##
=============================================
  Coverage       100.00%   100.00%           
=============================================
  Files               25        25           
  Lines              671       691   +20     
=============================================
+ Hits               671       691   +20     
Impacted Files Coverage Δ
packages/bloc/lib/src/bloc_observer.dart 100.00% <ø> (ø)
packages/bloc/lib/src/bloc.dart 100.00% <100.00%> (ø)
packages/bloc_test/lib/src/bloc_test.dart 100.00% <100.00%> (ø)
packages/replay_bloc/lib/src/replay_bloc.dart 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a08e238...eef7b60. Read the comment docs.

@felangel felangel added this to the v8.0.0 milestone Nov 9, 2021
@felangel felangel marked this pull request as ready for review November 10, 2021 05:50
lhmzhou
lhmzhou previously approved these changes Nov 14, 2021
Copy link

@lhmzhou lhmzhou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :shipit:

@felangel felangel force-pushed the feat/bloc-overrides branch 4 times, most recently from e904b3c to 54140b8 Compare November 14, 2021 07:35
@felangel felangel merged commit dee9261 into feat/v8.0.0 Nov 14, 2021
@felangel felangel deleted the feat/bloc-overrides branch November 14, 2021 22:09
bloc automation moved this from In progress to Done Nov 14, 2021
felangel added a commit that referenced this pull request Nov 17, 2021
felangel added a commit that referenced this pull request Nov 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Enhancement candidate would introduce a breaking change enhancement New feature or request pkg:bloc This issue is related to the bloc package
Projects
bloc
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants