DispatchStrategy
Decides what an EventRouter does with an event while other events are running or waiting.
A ready-made strategy names, in its brackets, the events it looks at. An event handler usually names its own event:
// A second tap on Save is ignored while a save is running.
override val strategy = DispatchStrategy.DropIfAlreadyRunning<OnSaveClick>()Naming a type that several events share makes the strategy look at all of them. Adding a key makes it look only at the events with the same key:
// A change of the query or of the filter cancels the load of the other.
override val strategy = DispatchStrategy.Latest<OnCriteriaChange>()
// The changes to one task run in order, and the tasks do not wait for each other.
override val strategy = DispatchStrategy.Sequential<OnTaskDoneChange> { it.taskId }A strategy made for a type can only be used by the handler of an event of that type. Event handlers that must agree share one value.
For anything else, write the function:
// A refresh cancels "load more", and a second refresh is ignored.
override val strategy = DispatchStrategy<OnRefresh> { _, active ->
if (active.any { it is OnRefresh }) {
Dispatch.Ignore
} else {
Dispatch.Replace(active.filterIsInstance<OnLoadMore>())
}
}A strategy says nothing about how long a run lasts. A handler that does not return by itself, such as one that collects a flow, is active until the router's scope is cancelled, unless a strategy replaces it first. The router knows nothing about a user interface: nothing starts or stops a run because a screen is shown or hidden.
Types
The ready-made strategies. Each one answers Dispatch.Run when no active event matches the events it looks at.