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(hydrated_bloc)!: HydratedBlocOverrides API #2947

Merged
merged 2 commits into from Nov 15, 2021

Conversation

felangel
Copy link
Owner

@felangel felangel commented Nov 15, 2021

Status

READY

Breaking Changes

YES

Description

The current API used to override the default Storage is:

void main() {
  HydratedBloc.storage = CustomStorage();
  // ...
}

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

  • Have multiple Storage implementations scoped to different parts of the application
  • Have Storage overrides be scoped to a package
    • Suppose a package that depends on package:hydrated_bloc registers its own Storage, any consumer of the package will either have to overwrite the package's Storage or use the existing Storage implementation.

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

This pull request introduces a HydratedBlocOverrides class which allows developers to override Storage for a specific Zone rather than relying on a global, mutable singleton.

void main() {
  HydratedBlocOverrides.runZoned(
    () {
      // ...
    },
    storage: CustomStorage(),
  );
}

HydratedBloc instances will use the Storage for the current Zone via HydratedBlocOverrides.current.

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

HydratedBlocOverrides.runZoned(
  () {
    // StorageA
    final overrides = HydratedBlocOverrides.current;
      
    // HydratedBlocs in this zone use StorageA
    // ...

    // Later...
    HydratedBlocOverrides.runZoned(
      () {
        // StorageB
        final overrides = HydratedBlocOverrides.current;
    
        // Blocs in this zone use StorageB
        // ...
      },
      storage: StorageB(),
    );
  },
  storage: StorageA(),
);

This pull request removes the static, mutable HydratedBloc.storage APIs which is a breaking change. Storage overrides will need to be specified via HydratedBlocOverrides.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 breaking change Enhancement candidate would introduce a breaking change pkg:hydrated_bloc This issue is related to the hydrated_bloc package labels Nov 15, 2021
@felangel felangel self-assigned this Nov 15, 2021
@felangel felangel added this to In progress in bloc via automation Nov 15, 2021
@codecov
Copy link

codecov bot commented Nov 15, 2021

Codecov Report

Merging #2947 (23358ab) into feat/v8.0.0 (f155709) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@              Coverage Diff               @@
##           feat/v8.0.0     #2947    +/-   ##
==============================================
  Coverage       100.00%   100.00%            
==============================================
  Files               25         3    -22     
  Lines              691       178   -513     
==============================================
- Hits               691       178   -513     
Impacted Files Coverage Δ
packages/hydrated_bloc/lib/src/hydrated_bloc.dart 100.00% <100.00%> (ø)
packages/flutter_bloc/lib/src/bloc_builder.dart
packages/bloc_concurrency/lib/src/concurrent.dart
packages/bloc_test/lib/src/bloc_test.dart
packages/bloc_concurrency/lib/src/sequential.dart
...ages/flutter_bloc/lib/src/multi_bloc_provider.dart
...ages/flutter_bloc/lib/src/repository_provider.dart
packages/bloc/lib/src/bloc.dart
packages/replay_bloc/lib/src/replay_bloc.dart
...ages/flutter_bloc/lib/src/multi_bloc_listener.dart
... and 13 more

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 f155709...23358ab. Read the comment docs.

@felangel felangel merged commit fe549c8 into feat/v8.0.0 Nov 15, 2021
@felangel felangel deleted the feat/hydrated-bloc-overrides branch November 15, 2021 05:09
bloc automation moved this from In progress to Done Nov 15, 2021
@felangel felangel mentioned this pull request Nov 15, 2021
7 tasks
@iampato
Copy link

iampato commented Nov 27, 2021

Could you do a more verbose example
example: how do I convert this

HydratedBloc.storage = await HydratedStorage.build(
    storageDirectory: kIsWeb
        ? HydratedStorage.webStorageDirectory
        : await getTemporaryDirectory(),
  );

@Gene-Dana
Copy link
Collaborator

Could you do a more verbose example

Hi there ! May you explain what you mean a little bit more here?

@felangel
Copy link
Owner Author

felangel commented Dec 3, 2021

Could you do a more verbose example example: how do I convert this

HydratedBloc.storage = await HydratedStorage.build(
    storageDirectory: kIsWeb
        ? HydratedStorage.webStorageDirectory
        : await getTemporaryDirectory(),
  );

Hi you can check out the example.

void main() async {
  FlutterServicesBinding.ensureInitialized();
  final storage = await HydratedStorage.build(
    storageDirectory: kIsWeb
        ? HydratedStorage.webStorageDirectory
        : await getTemporaryDirectory(),
  );
  HydratedBlocOverrides.runZoned(
    () => runApp(App()),
    storage: storage,
  );
}

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:hydrated_bloc This issue is related to the hydrated_bloc package
Projects
bloc
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants