Dispatcher allows us to update the state of the Store from away the store and to manage dependencies to create Mutation.
Here is an example store, in this section we create a dispatcher to commit the mutation into this Store:
struct State: StateType {
var count: Int = 0
}
enum Activity {
case happen
}
final class MyStore: Store<State, Activity> {
...
}
MyStore has a typealias to define a dispatcher:
MyStore.Dispatcher
<aside>
💡 Actual type of MyStore.Dispatcher
is DispatcherBase<State, Never>
It is a typealias to write shortly.
</aside>
Let’s take a look how we create a dispatcher with using the typealias:
final class MyDispatcher: MyStore.Dispatcher {
}
Now we can create an instance of MyDispatcher
:
let store = MyStore()
let dispatcher = MyDispatcher(targetStore: store)
Next, we add an action that commits a mutation:
final class MyDispatcher: MyStore.Dispatcher {
func doSomething() {
commit {
$0.count = 100
}
}
}