GithubHelp home page GithubHelp logo

aloisdeniel / built_bloc Goto Github PK

View Code? Open in Web Editor NEW
51.0 3.0 5.0 122 KB

Generate the BLoC pattern boilerplate.

License: MIT License

Dart 83.94% Java 5.75% Objective-C 5.61% Shell 4.69%
dart bloc pattern generator flutter

built_bloc's Introduction

built_bloc

Generate the BLoC pattern boilerplate.

Why ?

After using the BLoC pattern a little, it seems pretty cool for sharing code and separation of concerns, but I quickly found myself to write a lot of repetitive boilerplate code : I felt the need for a generator to assist me.

Here is a simple vanilla bloc example which obviously highlights repeated patterns while declaring subjects, streams, sinks and subscriptions.

class VanillaExampleBloc {

  Sink<void> get reset => this._reset.sink;

  Sink<int> get add => this._add.sink;

  Stream<int> get count => this._count.stream;

  final PublishSubject<int> _add = PublishSubject<int>();

  final PublishSubject<void> _reset = PublishSubject<void>();

  final BehaviorSubject<int> _count = BehaviorSubject<int>.seeded(0);

  List<StreamSubscription> subscriptions;

  List<Subject> subjects;

  VanillaExampleBloc() {
    subscriptions = [
      this._add.listen(_onAdd),
      this._reset.listen(_onReset),
    ];
    subjects = [
      this._add,
      this._count,
      this._reset,
    ];
  }

  void _onAdd(int value) {
    this._count.add(this._count.value + value);
  }

  void _onReset(int value) {
    this._count.add(0);
  }

  @mustCallSuper
  void dispose() {
    this.subjects.forEach((s) => s.close());
    this.subscriptions.forEach((s) => s.cancel());
  }
}

With built_bloc, you can replace all of that with just a few lines of code :

@bloc
class ExampleBloc extends Bloc with _ExampleBloc {
  @stream
  final BehaviorSubject<int> _count = BehaviorSubject<int>(seedValue: 0);

  @sink
  @Bind("_onAdd")
  final PublishSubject<int> _add = PublishSubject<int>();

  @sink
  @Bind("_onReset")
  final PublishSubject<void> _reset = PublishSubject<void>();

  void _onAdd(int value) {
    this._count.add(this._count.value + value);
  }

  void _onReset() {
    this._count.add(0);
  }
}

How to use ?

See the built_bloc_generator package.

built_bloc's People

Contributors

aloisdeniel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

built_bloc's Issues

Changelog

Would be nice to have a changelog to know what have change between versions.

Generated code for Bind should have an option to ignore errors

Basically here is what's generated when using @Bind

@override
  void subscribeParent(LoginPhoneBloc value) {
    this._parent = value;
    this._parent._phoneNumber.listen(value._managePhoneNumber);
  }

Problem is that if later you're doing something like

_phoneNumber.addError('Wrong phone!!!');

You get a crash, but it's a wanted error that will be show on UI side, I don't want it to spread.

The right generated code should be:

this._parent._phoneNumber.listen(value._managePhoneNumber, onError: (_) {});

By default I think @Bind should swallow errors, as in bloc you mainly addError because you want it. but an additional ignoreError boolean field to override that behavior can be useful (maybe ^^)

Generate dynamic instead of type

final BehaviorSubject _test = BehaviorSubject<bool>(); is generate to Stream<dynamic> get msiEnabled => _parent._msiEnabled.stream;

final BehaviorSubject<bool> _test = BehaviorSubject(); works fine :)

Null safety support

In order to migrate to flutter v2 we will need a null safety support on this package, is there any plan about it ?

Thanks :D

Another case of dynamic generated

@sink
@Bind('_handleColorModification')
final PublishSubject<Color> _changeColor = PublishSubject();  

Is generated to :

Sink<dynamic> get changeColor => this._parent._changeColor.sink;

Don't expose streams but observables

I generally don't expose raw Stream type, as we use RX it's more interesting to expose Observable for publishSubject, ValueObservable for behaviorSubject.

Write public class and generate private instead of opposite

@bloc
class _GeneratedExampleBloc extends Bloc {
  @sink
  PublishSubject<int> get add => fromPublish(onData: this._onAdd);

  @stream
  BehaviorSubject<int> get count => fromBehavior(0);

  void _onAdd(int value) {
    this._count.add(this._count.value + 1);
  }
}

become something like:

@bloc
class GeneratedExampleBloc extends Bloc with _GeneratedExampleBloc {
  @sink
  PublishSubject<int> _add => fromPublish(onData: this._onAdd);

  @stream
  BehaviorSubject<int> _count = fromBehavior(0);

  void _onAdd(int value) {
    this._count.add(this._count.value + 1);
  }
}

The point is to write something you'll use directly on your classes instead of some private class, I think it will also have the possibility to have some code coverage to our class as private class and generated are generally ignored from code coverage. Having them public will be really useful.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.