|
| 1 | +--- |
| 2 | +description: frictionless state management |
| 3 | +--- |
| 4 | + |
| 5 | +# Overmind |
| 6 | + |
| 7 | +> Web application development is about **defining**, **changing** and **consuming state** to produce a user experience. Overmind aims for a developer experience where that is all you focus on, reducing the orchestration of state management to a minimum. Making you a **happier** and more **productive** developer! |
| 8 | +
|
| 9 | +## APPLICATION INSIGHT |
| 10 | + |
| 11 | +Develop the application state, effects and actions without leaving [VS Code](https://code.visualstudio.com/), or use the standalone development tool. Everything that happens in your app is tracked and you can seamlessly code and run logic to verify that everything works as expected without having to implement UI. |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +## A SINGLE STATE TREE |
| 16 | + |
| 17 | +Building your application as a single state tree is the most straight forward mental model. You get a complete overview, but can still organize the state by namespacing it into domains. The devtools allows you to edit and mock out state. |
| 18 | + |
| 19 | +```typescript |
| 20 | +{ |
| 21 | + isAuthenticating: false, |
| 22 | + dashboard: { |
| 23 | + issues: [], |
| 24 | + selectedIssueId: null, |
| 25 | + }, |
| 26 | + user: null, |
| 27 | + form: new Form() |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +## SEPARATION OF LOGIC |
| 32 | + |
| 33 | +Separate 3rd party APIs and logic not specific to your application by using **effects**. This will keep your application logic pure and without low level APIs cluttering your code. |
| 34 | + |
| 35 | +```typescript |
| 36 | +export const api = { |
| 37 | + async fetchItems(): Promise<Item[]> { |
| 38 | + const response = await fetch('/api/items') |
| 39 | + |
| 40 | + return response.json() |
| 41 | + } |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +## SAFE AND PREDICTABLE CHANGES |
| 46 | + |
| 47 | +When you build applications that perform many state changes things can get out of hand. In Overmind you can only perform state changes from **actions** and all changes are tracked by the development tool. |
| 48 | + |
| 49 | +```typescript |
| 50 | +export const getItems: AsyncAction = async ({ state, effects }) => { |
| 51 | + state.isLoadingItems = true |
| 52 | + state.items = await effects.api.fetchItems() |
| 53 | + state.isLoadingItems = false |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## FUNCTIONAL ACTIONS |
| 58 | + |
| 59 | +When pieces of logic become complex it is beneficial to write functional code. Overmind provides an API named **operators** which gives you functional power. Ignore it, use it where it makes sense or make your whole codebase functional. It is up to you! |
| 60 | + |
| 61 | +```typescript |
| 62 | +export const search: Operator<string> = pipe( |
| 63 | + mutate(({ state }, query) => { |
| 64 | + state.query = query |
| 65 | + }), |
| 66 | + filter((_, query) => query.length > 2), |
| 67 | + debounce(200), |
| 68 | + mutate(async ({ state, effects }, query) => { |
| 69 | + state.isSearching = true |
| 70 | + state.searchResult = await effects.getSearchResult(query) |
| 71 | + state.isSearching = false |
| 72 | + }) |
| 73 | +) |
| 74 | +``` |
| 75 | + |
| 76 | +## SNAPSHOT TESTING OF LOGIC |
| 77 | + |
| 78 | +Bring in your application configuration of state, effects and actions. Create mocks for any effects. Take a snapshot of mutations performed in an action to ensure all intermediate states are met. |
| 79 | + |
| 80 | +```typescript |
| 81 | +import { createOvermindMock } from 'overmind' |
| 82 | +import { config } from './' |
| 83 | + |
| 84 | +test('should get items', async () => { |
| 85 | + const overmind = createOvermindMock(config, { |
| 86 | + api: { |
| 87 | + fetchItems: () => Promise.resolve([{ id: 0, title: "foo" }]) |
| 88 | + } |
| 89 | + }) |
| 90 | + |
| 91 | + await overmind.actions.getItems() |
| 92 | + |
| 93 | + expect(overmind.mutations).toMatchSnapshot() |
| 94 | +}) |
| 95 | +``` |
| 96 | + |
| 97 | +## WE WROTE THE TYPING |
| 98 | + |
| 99 | +Overmind has you covered on typing. If you choose to use Typescript the whole API is built for excellent typing support. You will not spend time telling Typescript how your app works, Typescript will tell you! |
| 100 | + |
0 commit comments