marcojakob / dart-event-bus

An Event Bus using Dart Streams for decoupling applications
MIT License
755 stars 82 forks source link

Holding events #46

Closed jrojas25 closed 2 years ago

jrojas25 commented 2 years ago

I'm having issues when firing an event before the listener is set, for this case the listener never gets the event. Are there any mechanism to hold the event until the listener is ready?

visign3d commented 2 years ago

maybe try the synchronous event bus EventBus eventBus = EventBus(sync: true);

sunnykinger commented 2 years ago

maybe try the synchronous event bus EventBus eventBus = EventBus(sync: true);

doesn't work

marcojakob commented 2 years ago

The EventBus uses Dart's Broadcast Streams. Broadcast streams do not cache events if nobody is listening:

Broadcast streams work the same as single subscription streams, but they can have multiple listeners, and if nobody’s listening when a piece of data is ready, that data is tossed out.

If you want to cache events it's probably best to create your own version of an event bus or have a look at something like StreamQueue.

BTW: Sync is not the same as caching the event:

  /// If [sync] is true, events are passed directly to the stream's listeners
  /// during a [fire] call. If false (the default), the event will be passed to
  /// the listeners at a later time, after the code creating the event has
  /// completed.