# ractor

> actor like state management.inspired by actor, redux, mobx.

Latest version **1.1.7** (published 2019-01-09) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.7 |
| Published | 2019-01-09 |
| First published | 2017-06-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 18.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | corol |
| Maintainers | xaomon |

## Links

- npm: https://www.npmjs.com/package/ractor
- npm.io page: https://npm.io/package/ractor

## Dependencies (1)

- [js-actor](https://npm.io/package/js-actor.md) ^1.4.0

## Recent versions

- 1.1.7 (latest) — 2019-01-09
- 1.1.6 — 2019-01-02
- 1.1.5 — 2018-12-25
- 1.1.4 — 2018-12-18
- 1.1.3 — 2018-12-18
- 1.1.2 — 2018-12-18
- 1.1.1 — 2018-12-16
- 1.1.0 — 2018-07-12
- 1.0.15 — 2018-07-11
- 1.0.14 — 2018-06-28
- 1.0.13 — 2018-06-07
- 1.0.12 — 2018-05-29
- 1.0.11 — 2018-05-16
- 1.0.10 — 2017-12-11
- 1.0.9 — 2017-12-09
- … 10 more at https://npm.io/package/ractor/versions

## README

<p>&nbsp;</p>
<p align='center'>
  <img src="http://pkc0dr4sb.bkt.clouddn.com/ractor_shadow@1000px.png" width="300" />
</p>
<h1 align="center">Ractor</h1>
<p align='center'>Another action based state management tool</p>
<p>&nbsp;</p>

[中文文档](https://github.com/FE-Ractor/ractor/blob/master/readme.cn.md)

## Install

```sh
npm i ractor
```

## Quick Start

Ractor builds upon three main ideas.

### System

System is an event system. Normally, You need to create one for your logic app.

```ts
import { System } from "ractor";
export const system = new System("counter");
```

### Action

The abstraction of system behavior. You can create it by class.

```ts
export class Increment {}
```

### Store

Conceptually, your can think of Store like a event handler or redux’s reducer.

Store is an abstract class, abstract method createReceive needs to return an Receive object. There is a convenient helper method `receiveBuilder` to help you to create Receive object

```ts
import { Increment } from "./Increment";
import { Decrement } from "./Decrement";
import { Store } from "ractor";

export class CounterStore extends Store<{ value: number }> {
  public state = { value: 1 };
  public createReceive() {
    return this.receiveBuilder()
      .match(Increment, async () =>
        this.setState({ value: this.state.value + 1 }))
      .match(Decrement, async () =>
        this.setState({ value: this.state.value + 1 }))
      .build();
    }
}
```

There is a convenient function `createStore` to createStore quickly.

```ts
import { ReceiveBuilder } from "ractor"

const CounterStore = createStore((getState, setState) => {
  return ReceiveBuilder
    .create()
    .match(Increment, () => setState(getState() + 1))
    .match(Decrement, () => setState(getState() - 1))
    .build()
}, 0)
```

## React

There is the quick glance of [ractor-hooks](https://github.com/huangbinjie/ractor-hooks)

### Provider

Create an event system for your App.

```ts
  import { Provider } from "ractor-hooks"
  import { System } from "ractor"

  function App() {
    return <Provider system={new System("app")}><Counter /></Provider>
  }
```

### useStore

Inject Store to your component, return the state of the store which had injected.

```ts
function Counter() {
  const counter = useStore(CounterStore)
  return jsx
}
```

### useSystem

Take the system out of current context.

```ts
function Counter() {
  const system = useSystem(CounterStore)
  return <button onClick={() => system.dispatch(new Increment)}>+</button>
}
```

## Examples

+ [counter](https://stackblitz.com/edit/ractor-hooks)

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