Overmind also provides a functional API to help you manage complex logic. This API is inspired by asynchronous flow libraries like RxJS, though it is designed to manage application state and effects. If you want to create and use a traditional action “operator style” you define it like this:
The mutate function is one of many operators. Operators are small composable pieces of logic that can be combined in many ways. This allows you to express complexity in a declarative way. You typically use the pipe operator in combination with the other operators to do this:
Any of these operators can be used with other operators. You can even insert a pipe inside an other pipe. This kind of composition is what makes functional programming so powerful.
catchError
async
This operator runs if any of the previous operators throws an error. It allows you to manage that error by changing your state, run effects or even return a new value to the next operators.
import { Operator, pipe, mutate, catchError } from'overmind'exportconstdoSomething:Operator<string> =pipe(mutate(() => {thrownewError('foo') }),mutate(() => {// This one is skipped })catchError(({ state }, error) => {state.error =error.messagereturn'value_to_be_passed_on' }),mutate(() => {// This one continues executing with replaced value }))
debounce
When action is called multiple times within the set time limit, only the last action will move beyond the point of the debounce.
import { Operator, pipe, debounce } from'overmind'import*as o from'./operators'exportconstsearch:Operator<string> =pipe(debounce(200),o.performSearch())
This operator allows you to ensure that if an action is called, the next action will only continue past this point if a certain duration has passed. Typically used when an action is called many times in a short amount of time.
import { Operator, pipe, throttle } from'overmind'import*as o from'./operators'exportconstonMouseDrag:Operator<string> =pipe(throttle(200),o.handleMouseDrag())
tryCatch
This operator allows you to scope execution and manage errors. This operator does not return a new value to the execution.