Git Product home page Git Product logo

atom_event_bus's Introduction

Atom Event Bus

An Event Bus based on Publish/Subscribe pattern to decouple your code dependencies using dart's Stream.

Features

  • A single event bus for an app instance.
  • Subscribe to any event anywhere
  • debounce and oneOff listeners
  • uses dart stream

Diagram

Getting started

  dependencies:
    flutter:
      sdk: flutter
    
    # other deps
    atom_event_bus:

Usage

  1. Create Events
  2. Subscribe to Events by EventRule
  3. Emit Event by EventBus.emit

Example

// --------------------------- events.dart ---------------------------
final signInEvent = Event<bool>("SIGN_IN_EVENT");

// -------------------------- subscriber.dart -------------------------
class SignedInStatus extends StatefulWidget {
  const SignedInStatus({Key? key}) : super(key: key);

  @override
  _SignedInStatusState createState() => _SignedInStatusState();
}

class _SignedInStatusState extends State<SignedInStatus> {
  bool signedIn = false;
  late EventRule signInRule;

  @override
  void initState() {
    super.initState();

    signInRule = EventRule<bool>(signInEvent, targets: [
      EventListener(onSignInEvent),
    ]);
  }

  void onSignInEvent(bool status) {
    setState(() {
      signedIn = status;
    });
  }

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context).textTheme;

    return Center(
      child: Text(
        "Is Signed In? \n${signedIn ? 'Yes' : 'No'}",
        textAlign: TextAlign.center,
        style: theme.headline1,
      ),
    );
  }

  @override
  void dispose() {
    signInRule.cancel(); // cancelling the subscription
    super.dispose();
  }
}


// ----------------- emitter.dart -------------------
class ToggleSignInStatus extends StatelessWidget {
  const ToggleSignInStatus({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return FloatingActionButton(
      onPressed: () {
        EventBus.emit(signInEvent.createPayload(true));
      },
      child: const Icon(Icons.replay_outlined),
    );
  }
}


// ------------- main.dart -------------
void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'ATOM EVENT BUS',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      home: const MyHomePage(title: 'ATOM EVENT BUS'),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
        centerTitle: true,
      ),
      body: const SignedInStatus(), // <-- subscribe to Events
      floatingActionButton: const ToggleSignInStatus(), // <-- emit Events
    );
  }
}

Listeners

  • EventListener : normal event listener which get trigger for every event of corresponding event rule.
  • OneOffEventListener : event listener which get triggered only once.
  • DebouncedEventListener : event lister which prevent frequent events and only capture last event after a short delay.

Additional information

Publish–subscribe_pattern : Wikipedia

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.