felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.69k stars 3.38k forks source link

feat: Allow providing 'dispose' to RepositoryProvider #3400

Open AlexanderFarkas opened 2 years ago

AlexanderFarkas commented 2 years ago

Description

Sometimes you want to recreate Repository. Usually repositories have StreamControllers for entity streams.

class EntityRepository {

  final entityUpdated = StreamController<Entity>.broadcast();
  final entityCreated = StreamController<Entity>.broadcast();
  final entityDeleted = StreamController<int>.broadcast(); // by id

  Future<void> dispose() => Future.wait([
    entityUpdated.dispose(),
    entityCreated.dispose(),
    entityDeleted.dispose(),
  ]);

  Future<void> createEntity() async {
    final entity = await makeHttpCreateCall();
    entityCreated.add(entity);
  }

  Future<void> updateEntity() async {
    final entity = await makeHttpUpdateCall();
    entityUpdated.add(entity);
  }

  Future<void> deleteEntity() async {
    final entityId = await makeHttpDeleteCall();
    entityDeleted.add(entityId);
  }
}

Desired Solution

It would be useful to have dispose in RepositoryProvider, considering it's already implemented in Provider.

AlexanderFarkas commented 2 years ago

I've seen that a couple of such feature proposals were rejected. But I believe that these StreamControllers are part of internal implementation of repository. So they need to be managed by repository, including disposal.