# machinate

> practical state management

Latest version **1.1.3** (published 2018-05-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install machinate
pnpm add machinate
yarn add machinate
bun add machinate
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.3 |
| Published | 2018-05-27 |
| First published | 2018-02-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 2.2 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Pratik Ringshia |
| Maintainers | pringshia |
| Keywords | state, management, react, practical, easy, simple |

## Links

- npm: https://www.npmjs.com/package/machinate
- Repository: https://github.com/pringshia/machinate
- Homepage: https://github.com/pringshia/machinate#readme
- Issues: https://github.com/pringshia/machinate/issues
- npm.io page: https://npm.io/package/machinate

## Dependencies (4)

- [dagjs](https://npm.io/package/dagjs.md) ^0.1.3
- [react](https://npm.io/package/react.md) ^16.2.0
- [prop-types](https://npm.io/package/prop-types.md) ^15.6.0
- [hoist-non-react-statics](https://npm.io/package/hoist-non-react-statics.md) ^2.5.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.3 (latest) — 2018-05-27
- 1.1.2 — 2018-05-27
- 1.1.1 — 2018-03-16
- 1.1.0 — 2018-03-15
- 1.0.15 — 2018-03-14
- 1.0.14 — 2018-03-12
- 1.0.13 — 2018-03-04
- 1.0.12 — 2018-02-25
- 1.0.11 — 2018-02-20
- 1.0.10 — 2018-02-20
- 1.0.9 — 2018-02-20
- 1.0.8 — 2018-02-19
- 1.0.7 — 2018-02-19
- 1.0.6 — 2018-02-19
- 1.0.5 — 2018-02-19
- … 5 more at https://npm.io/package/machinate/versions

## README

# 🕵️‍♂️ machinate

[![npm](https://img.shields.io/npm/v/machinate.svg)]() [![build](https://api.travis-ci.org/pringshia/machinate.svg?branch=master)]()

#### **⚠️ Not yet ready for production use.**

---

**machinate (mack·in·eight)** _verb_:

* _Practical, simple, and hassle-free state management._

* _A cutesy reverse [portmanteau](https://en.wikipedia.org/wiki/Portmanteau) of "state machine"_.

* _To engage in plots and intrigues; scheme. (This is what your app does if you're not managing state properly, we'll help put an end to that.)_

---

### 🏁 **Goals**:

* 🚀 Instantly launch your app in any given state.
* 🤓 Heavy emphasis on developer experience. Your time and sanity are respected.
* 🚼 Baked in best practices. Guardrails for guidance.
* 🍞 Dead simple. Plain javascript objects. Minimal boilerplate.
* 🛠 High quality tooling.

# Install

`yarn add machinate`

# Use

Every app has states. 1) Define them. 2) Implement them. 3) Connect them.

### **1. define your app's states**

```jsx
// object where keys are a domain, values are the states of that domain.

const scheme = {
  Auth: ["LoggedIn", "LoggedOut", "Unknown"]
};
```

### **2. implement the states**

```jsx
import { Machinate, States } from "machinate";

const initialState = { Auth: "LoggedOut" };

const App = (
    <Machinate
        scheme={scheme}
        initial={initialState}>

        <States of="Auth"
            LoggedIn={...}
            LoggedOut={...}
            Unknown={...}
        />
    </Machinate>
);

ReactDOM.render(<App />, document.body)
```

---

**💡 Tip!**
If you forget to implement a state, `machinate` will warn you:

```jsx
<States of="Auth"
    LoggedIn={...}
    LoggedOut={...}
    // 'Unknown' prop left out intentionally
>
```

`web console:`

```
Warning: Failed prop type: The prop `Unknown` is marked as required in `Auth[Domain]`, but its value is `undefined`.
```

---

### **3. connecting states**

Each state prop receives an object with the following parameters:

1.  **`data`**: the data if any, associated with the state
2.  **`transition(stateName, optionalData)`**: move the machine to `stateName` and set the data associated with the state to `data`
3.  **`go(stateName, optionalData)`**: same as `transition()`, but wrapped in a function to delay execution; essentially a convenient helper for transitioning on callbacks
4.  **`update(stateName, updateFn)`**: same as `transition()`, except the second parameter is a function that transforms the current data to new data

Here we use the `go()` function:

```jsx
<States of="Auth"
    LoggedIn={({data}) => <h1>Hi {data.user}</h1>}
    LoggedOut={({ go }) => (
        <button
            onClick={ go("Auth.LoggedIn", {user: "bob"}) }
            value="Login"
        />
    )}
    Unknown={() => null)}
/>
```

**Note:** `"Auth.LoggedIn"` is shorthand notation, referring to the `LoggedIn` state of the `Auth` domain.

---

### 🔀 Syntactic Shortcuts

The following are all equivalent:

```jsx
const scheme = {
  Auth: ["LoggedIn", "LoggedOut"]
};

const scheme = {
  Auth: {
    states: ["LoggedIn", "LoggedOut"],
    deps: []
  }
};
```

```jsx
const initialState = {
  Auth: "LoggedOut"
};

const initialState = {
  Auth: {
    state: "LoggedOut",
    data: null
  }
};
```

---
_Source: https://npm.io/package/machinate · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
