nothin_but_the_bloc 0.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 12

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

Alternatively, your editor might support 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]
0
Health:
Code health derived from static analysis. [more]
0
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
58
Overall:
Weighted score of the above. [more]
12
Learn more about scoring.

We analyzed this package on Jul 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13

Health issues and suggestions

Fix lib/src/bloc_subject.dart. (-99.59 points)

Analysis of lib/src/bloc_subject.dart failed with 19 errors, 4 hints, including:

line 32 col 32: Undefined name 'controller'.

line 44 col 32: Undefined name 'controller'.

line 121 col 32: Undefined name 'controller'.

line 143 col 32: Undefined name 'controller'.

line 156 col 39: Undefined name 'controller'.

Fix lib/src/subscriber.dart. (-76.27 points)

Analysis of lib/src/subscriber.dart failed with 5 errors:

line 117 col 11: Undefined name 'controller'.

line 141 col 21: The getter 'controller' isn't defined for the type 'Emitter

line 146 col 21: The getter 'controller' isn't defined for the type 'Emitter

line 153 col 28: Undefined name 'controller'.

line 163 col 31: Undefined name 'controller'.

Fix lib/src/emitter.dart. (-25 points)

Analysis of lib/src/emitter.dart failed with 1 error:

line 69 col 49: Undefined name 'controller'.

Maintenance issues and suggestions

No valid SDK. (-20 points)

The analysis could not detect a valid SDK that can use this package.

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (rxdart).

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.

Package is getting outdated. (-2.47 points)

The package was last published 53 weeks ago.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
meta ^1.0.0 1.2.1
rxdart ^0.22.0 0.22.6 0.24.1
Dev dependencies
test ^1.0.0