Skip to content

Event Source

Jens Ayton edited this page Apr 25, 2020 · 2 revisions

An Event Source is used to send external signals into a Mobius Loop as Events. The typical use case is to listen for things like:

  • Network connectivity changes (online/offline)
  • Timers (periodic ticks, eg. once per second)
  • Headphones connected/disconnected
  • GPS location changes
  • etc.

In principle an Event Source could be used to send events from the UI (like clicks) but this is discouraged. Instead prefer using MobiusLoop.dispatchEvent(...) or a Connectable if you use a MobiusController.

Structurally an Event Source is very similar to an Effect Handler but an Event Source does not need any Effects to be triggered before starting to send Events.

You can configure it by setting an EventSource on a Mobius.Builder:

let loop = Mobius.loop(update: update, effectHandler: EffectHandler())
    .withEventSource(myEventSource)
    .start(from: .empty)
Clone this wiki locally