Code Monkey home page Code Monkey logo

token_bloc's Introduction

TokenBloc

Pub.dev Badge GitHub Build Badge MIT License Badge Very Good Analysis Style Badge

About

A dart package that helps implement the BLoC design pattern with the power of Dart Stream extended by the capabilities provided by RxDart library.

Usage

Creating a TokenBloc class:

import 'package:token_bloc/token_bloc.dart';

/// A bloc that manages a counter.
class CounterBloc extends TokenBloc {
  /// Creates a [CounterBloc] and sets the action handlers.
  CounterBloc() {
    /// When an [incrementCounterAction] is dispatched, the [_onIncrementAction] method is called.
    onVoid(incrementCounterAction, _onIncrementAction);
    /// When an [decrementCounterAction] is dispatched, the [_onDecrementAction] method is called.
    onVoid(decrementCounterAction, _onDecrementAction);
  }

  /// The state of the counter with an initial value of 0.
  final counterState = TokenState<int>.seeded(0);

  /// The action to increment the counter.
  final incrementCounterAction = TokenActionVoid();

  /// The action to decrement the counter.
  final decrementCounterAction = TokenActionVoid();

  void _onIncrementAction() {
    /// The current value of the counter is obtained with the [valueOf] method.
    final newValue = valueOf(counterState) + 1;

    /// The new value is emitted with the [emitStateOf] method.
    emitStateOf(counterState, newValue);
  }

  void _onDecrementAction() {
    final newValue = valueOf(counterState) - 1;
    emitStateOf(counterState, newValue);
  }

  /// The [subjects] getter is used to obtain the token subjects that the bloc manages.
  @override
  List<TokenSubject<dynamic>> get subjects => [
        counterState,
        incrementCounterAction,
        decrementCounterAction,
      ];
}

Using a bloc class:

Future<void> main() async {
  /// Create the `bloc`.
  final bloc = CounterBloc();

  /// Listen to the counter state changes.
  bloc.counterState.listen((state) {
    print('Counter: ${state.value}');
  });

  /// Dispatch the increment action.
  bloc.incrementCounterAction();

  /// Dispatch the decrement action.
  bloc.decrementCounterAction();

  /// Dispose the `bloc` when it is no longer needed.
  await subscription.dispose();
}

Contributing

We welcome contributions to this package. If you would like to contribute, please feel free to open an issue or a pull request.

Example

To see a complete example, see the TokenBloc Example folder.

License

TokenBloc is licensed under the MIT License. See the LICENSE for details.

token_bloc's People

Contributors

alvarobcprado avatar

Stargazers

 avatar  avatar

Watchers

 avatar

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.