nothin_but_the_bloc 0.0.3

A library that aims to make BLoC principles more intuitive to implement.

Usage #

A simple usage example:

import 'package:nothin_but_the_bloc/nothin_but_the_bloc.dart';
   
class BlocEmitter extends Bloc {
  Emitter<int> emitter = Emitter();

  void emitValue(int data) {
    print('BlocEmitter is broadcasting an event: $data');
    emitter.emit(data);
  }

  void dispose() {
    emitter.close();
  }
}

class BlocSubscriber extends Bloc {
  Subscriber<int> subscriber;

  BlocSubscriberA(BlocEmitter bloc) {
    subscriber = bloc.emitter.createSubscriber(onEvent: onEvent);
  }

  void onEvent(int data) {
    print('BlocSubscriber received event from BlocEmitter: $data');
  }
}

void main() {
  final blocEmitter = BlocEmitter();
  final blocSubscriber = BlocSubscriber(blocEmitter);
  
  emitter.emitValue(5);
  
  // Printed:
  // BlocEmitter is broadcasting an event: 5
  // BlocSubscriber received event from BlocEmitter: 5
  
  blocSubscriber.dispose();
  blocEmitter.dispose();
}

For a more extended example, see the example project.

TODO #

  • Make more comprehensive documentation
  • Override StreamController and Subject inherited methods in Emitter and Subscriber to return an Emitter/Subscriber (so they can be properly chained)

[0.0.3]

  • Renamed addEmitter and addEmitters method in Subscriber to listenToEmitter and listenToEmitters.
  • Added fromStream and just factory constructors to Subscriber.
  • Created base abstract class BlocSubject that Emitter and Subscriber now extend.
  • All transformation methods exposed by Subject and StreamController that return an Observer have been reimplemented to return a Subscriber to facilitate a unified usage experience.

[0.0.2]

  • Removed dependency on pedantic to eliminate a case of failed dependency resolution. Copied the linter rules from version 1.7.0 to maintain analyzer behavior.

[0.0.1]

  • Initial version
  • Documentation is slap-dashed together. It will be expanded and cleaned up in the future.

example/example.dart

import 'package:nothin_but_the_bloc/nothin_but_the_bloc.dart';

void main() async {
  final emitterA = BlocEmitterA();

  final emitterB = BlocEmitterB();
  BlocRegistry.register(emitterB);

  final subscriberA = BlocSubscriberA(emitterA);
  final subscriberB = BlocSubscriberB(emitterA);
  final subscriberC = BlocSubscriberC();
  final subscriberD = BlocSubscriberD(emitterA);

  final ioBloc = IOBloc(emitterA.emitter);
  final ioBlocListener = IOBlocListener(ioBloc.emitter);

  await emitterA.emitValue(5);
  await emitterB.emitValue('a');
  await emitterB.emitValue('b');
  await emitterB.emitValue('c');
  await emitterB.emitValue('d');

  emitterA.dispose();
  emitterB.dispose();
  ioBloc.dispose();

  subscriberA.dispose();
  subscriberB.dispose();
  subscriberC.dispose();
  subscriberD.dispose();
  ioBlocListener.dispose();
}

class BlocEmitterA extends Bloc {
  Emitter<int> emitter = Emitter();

  void emitValue(int data) {
    print('EmitterA is broadcasting an event: $data');
    emitter.emit(data);
  }

  void dispose() {
    emitter.close();
  }
}

class BlocEmitterB extends Bloc {
  Emitter<String> emitter;

  BlocEmitterB() {
    emitter = Emitter<String>();
  }

  String transformer(int data) => 's${data.toString()}';

  void emitValue(String data) {
    print('EmitterB is broadcasting an event: $data');
    emitter.emit(data);
  }

  void dispose() {
    emitter.close();
  }
}

class BlocSubscriberA extends Bloc {
  Subscriber<int> subscriber;

  BlocSubscriberA(BlocEmitterA emitterA) {
    subscriber = emitterA.emitter.createSubscriber(onEvent: onEvent);
  }

  void onEvent(int data) {
    print('SubscriberA recieved event from EmitterA: $data');
  }
}

class BlocSubscriberB extends Bloc {
  Subscriber<String> subscriber;

  BlocSubscriberB(BlocEmitterA emitterA) {
    subscriber = emitterA.emitter.createSubscriber().map(transformer)
      ..listen(onEvent);
  }

  String transformer(int data) => 's${data.toString()}';

  void onEvent(String data) {
    print('SubscriberB recieved event from EmitterA: $data');
  }

  void dispose() {
    subscriber.close();
  }
}

class BlocSubscriberC extends Bloc {
  Subscriber<int> subscriber;

  BlocSubscriberC() {
    subscriber = BlocRegistry.get<BlocEmitterB>()
        .emitter
        .createSubscriber()
        .map(transformer)
          ..listen(onEvent);
  }

  int transformer(String data) {
    int total = 0;
    for (var code in data.codeUnits) {
      total += code;
    }
    return total;
  }

  void onEvent(int data) {
    print('SubscriberC recieved event from EmitterB: $data');
  }

  void dispose() {
    subscriber.close();
  }
}

class BlocSubscriberD extends Bloc {
  Subscriber<int> subscriberA;
  Subscriber<String> subscriberB;

  BlocSubscriberD(BlocEmitterA emitterA) {
    subscriberA = emitterA.emitter.createSubscriber(onEvent: onEventA);
    subscriberB = BlocRegistry.get<BlocEmitterB>()
        .emitter
        .createSubscriber()
        .skip(2)
          ..listen(onEventB);
  }

  void onEventA(int data) {
    print('SubscriberD recieved event from EmitterA: $data');
  }

  void onEventB(String data) {
    print('SubscriberD recieved event from EmitterB: $data');
  }

  void dispose() {
    subscriberA.close();
    subscriberB.close();
  }
}

class IOBloc extends SingleIOBloc<int, String> {
  IOBloc(Emitter<int> emitterA) {
    subscriber.listenToEmitter(emitterA);
  }

  @override
  void onInput(int data) {
    print('IOBloc recieved an event: $data');
    emit('IO' + data.toString());
  }
}

class IOBlocListener extends SingleIOBloc<String, Null> {
  IOBlocListener(Emitter<String> ioBloc) {
    subscriber.listenToEmitter(ioBloc);
  }

  @override
  void onInput(String data) {
    print('IOBlocListener recieved an event: $data');
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  nothin_but_the_bloc: ^0.0.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:nothin_but_the_bloc/nothin_but_the_bloc.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
5
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
51
Learn more about scoring.

We analyzed this package on Aug 18, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:nothin_but_the_bloc/nothin_but_the_bloc.dart.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
meta ^1.0.0 1.1.7
rxdart ^0.22.0 0.22.1+1
Dev dependencies
test ^1.0.0