Skip to content

Latest commit

 

History

History
87 lines (61 loc) · 3.28 KB

README.md

File metadata and controls

87 lines (61 loc) · 3.28 KB

async_events

pub package Null Safety Codecov Dart CI GitHub Tag New Commits Last Commits Pull Requests Code size License

A portable asynchronous event hub supporting multiple storage types

Usage

Here's a simple usage example:

import 'package:async_events/async_events.dart';

void main() async {
  // The event storage:
  var storage = AsyncEventStorageMemory('test');

  // The event HUB:
  var hub = AsyncEventHub('test', storage);

  // Get the channels:
  var c1 = hub.channel('c1');
  var c2 = hub.channel('c2');

  // Subscribe to channels:

  var sub1 = await c1.subscribe((event) {
    print('C1 EVENT> $event');
  });

  var sub2 = await c2.subscribe((event) {
    print('C2 EVENT> $event');
  });

  // Submit somme events:

  var event1 = await c1.submit('t', {'name': 't1'});
  var event2 = await c2.submit('t', {'name': 't2'});

  // Subscribe later to the channel `c2`:
  // - `fromBegin: true`: will receive all the previous events.
  var sub2b = await c2.subscribe(fromBegin: true, (event) {
    print('C2[b] EVENT> $event');
  });

  // Submit another event to `c2`:
  var event3 = await c2.submit('t', {'name': 't3'});

  // Cancel subscription to channel `c1`:
  sub1.cancel();
}

Output:

C1 EVENT> AsyncEvent[0#1@2022-08-14T16:15:10.526863Z]<t>{name: t1}
C2 EVENT> AsyncEvent[0#1@2022-08-14T16:15:10.538934Z]<t>{name: t2}
C2[b] EVENT> AsyncEvent[0#1@2022-08-14T16:15:10.538934Z]<t>{name: t2}
C2 EVENT> AsyncEvent[0#2@2022-08-14T16:15:10.544065Z]<t>{name: t3}
C2[b] EVENT> AsyncEvent[0#2@2022-08-14T16:15:10.544065Z]<t>{name: t3}

Features and bugs

Please file feature requests and bugs at the issue tracker.

Author

Graciliano M. Passos: gmpassos@GitHub.

License

Dart free & open-source license.