<commit_before># Microcosm

![CircleCI](https://circleci.com/gh/vigetlabs/microcosm.svg?style=svg)

Flux with central, isolated state.

**Important**. Master is prepping for the next major release
(10.x). See the
[9-stable branch](https://github.com/vigetlabs/microcosm/tree/9-stable)
for relevant documentation and examples.

1. [Documentation](docs)
2. [Overview](#overview)
3. [Opinions](#opinions)
4. [What is it trying to solve?](#what-is-it-trying-to-solve)

## Overview

Microcosm is a variant of [Flux](https://facebook.github.io/flux/)
that makes it easier to control and modify state in a pure,
centralized way. It thinks of stores and actions as stateless
collections of pure functions, keeping all data encapsulated in a
specific instance of Microcosm.

### Stateless stores

Stores hold no state; instead they are responsible for writing state
to the repository owned by a Microcosm instance.

This allows stores to be simple collections of pure functions that
transform old data into new data. The `register` hook tells Microcosm
what actions a store should respond to:

```javascript
const Planets = {
  // Tells a Microcosm how a store should respond to actions
  register() {
    return {
      [addPlanet] : this.add
    }
  },
  // Store handlers are pure functions that take an old state and
  // transform it into a new state
  add(planets, props) {
    return planets.concat(props)
  }
}
```

Visit [the API documentation for stores](./docs/api/stores.md) to
read more.

### No action constants

Microcosm automatically generates action type constants based upon the
referential identity of the action and the current state of its
lifecycle.

An action can be in several states: `open`, `loading`, `done`,
`failed`, and `cancelled`. Stores can subscribe to each of these
states through the use of a `register` function:

```javascript
function createPlanet (params) {
  return ajax.post('/planets', params)
}

app.addStore('planets', {
  getInitialState() {
    return { records: [], loading: false }
  },

  addPlanet(planets, props) {
    return { ...planets, records: planets.records.concat(props) }
  },

  setLoading(planets) {
    return { ...planets, loading: true }
  },

  register() {
    [createPlanet]      : this.addPlanet,
    [createPlanet.open] : this.setLoading,
  }
})


// Creates a action of type "createPlanet" that dispatches to stores
app.push(createPlanet, params)
```

When an action is pushed, it is placed into a journal of all actions
that have occurred. During the `open` state, the action is in a state
where the request has been opened however it is not complete. When the
request finishes, the action spawned by `createPlanet` will move into
a `done` state. Microcosm will then re-run through the list of actions
documented in the journey to produce a new application state that
accounts for the completion of the request.

This is heavily inspired by event sourced message queues such as the
[LMAX Architecture](http://martinfowler.com/articles/lmax.html), or
[Apache Kafka](http://kafka.apache.org/), though there is significant
difference in implementation.

Visit [the API documentation for actions](./docs/api/actions.md) to
read more.

### Actions are first-class entities

Every action spawned by `app.push()` returns an `Action` to represent
its resolution. This object provides a consistent API, no matter what
value is returned from an action creator:

```<commit_msg>Remove note about LMAX, keep it in inspiration<commit_after># Microcosm

![CircleCI](https://circleci.com/gh/vigetlabs/microcosm.svg?style=svg)

Flux with central, isolated state.

**Important**. Master is prepping for the next major release
(10.x). See the
[9-stable branch](https://github.com/vigetlabs/microcosm/tree/9-stable)
for relevant documentation and examples.

1. [Documentation](docs)
2. [Overview](#overview)
3. [Opinions](#opinions)
4. [What is it trying to solve?](#what-is-it-trying-to-solve)

## Overview

Microcosm is a variant of [Flux](https://facebook.github.io/flux/)
that makes it easier to control and modify state in a pure,
centralized way. It thinks of stores and actions as stateless
collections of pure functions, keeping all data encapsulated in a
specific instance of Microcosm.

### Stateless stores

Stores hold no state; instead they are responsible for writing state
to the repository owned by a Microcosm instance.

This allows stores to be simple collections of pure functions that
transform old data into new data. The `register` hook tells Microcosm
what actions a store should respond to:

```javascript
const Planets = {
  // Tells a Microcosm how a store should respond to actions
  register() {
    return {
      [addPlanet] : this.add
    }
  },
  // Store handlers are pure functions that take an old state and
  // transform it into a new state
  add(planets, props) {
    return planets.concat(props)
  }
}
```

Visit [the API documentation for stores](./docs/api/stores.md) to
read more.

### No action constants

Microcosm automatically generates action type constants based upon the
referential identity of the action and the current state of its
lifecycle.

An action can be in several states: `open`, `loading`, `done`,
`failed`, and `cancelled`. Stores can subscribe to each of these
states through the use of a `register` function:

```javascript
function createPlanet (params) {
  return ajax.post('/planets', params)
}

app.addStore('planets', {
  getInitialState() {
    return { records: [], loading: false }
  },

  addPlanet(planets, props) {
    return { ...planets, records: planets.records.concat(props) }
  },

  setLoading(planets) {
    return { ...planets, loading: true }
  },

  register() {
    [createPlanet]      : this.addPlanet,
    [createPlanet.open] : this.setLoading,
  }
})


// Creates a action of type "createPlanet" that dispatches to stores
app.push(createPlanet, params)
```

When an action is pushed, it is placed into a journal of all actions
that have occurred. During the `open` state, the action is in a state
where the request has been opened however it is not complete. When the
request finishes, the action spawned by `createPlanet` will move into
a `done` state. Microcosm will then re-run through the list of actions
documented in the journey to produce a new application state that
accounts for the completion of the request.

Visit [the API documentation for actions](./docs/api/actions.md) to
read more.

### Actions are first-class entities

Every action spawned by `app.push()` returns an `Action` to represent
its resolution. This object provides a consistent API, no matter what
value is returned from an action creator:

```
{'fraction_non_alphanumeric': 0.09295690043550082, 'fraction_numerical': 0.0024027631776543023, 'mean_word_length': 4.158791634391944, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1933626', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1874, 'n_words': 819}