<commit_before># Stores

1. [Overview](#overview)
2. [Subscribing to different action states](#subscribing-to-different-action-states)
3. [API](#api)

## Overview

Stores define the rules in which resolved actions are converted into
new state. They are added to a Microcosm instance using `addStore`:

```javascript
// Mount a store that operates on a specific key in repo state.
// Any operations the store handles are recorded at `repo.state.key`:
repo.addStore('key', Store)

// Mount a store that operates on all repo state, handlers will
// write directly to `repo.state` itself:
repo.addStore(Store)
```

Stores do not enforce any particular structure. However specific
methods can be defined on stores to configure behavior at key points
in a Microcosm's lifecycle.

## Subscribing to different action states

Stores can provide a `register` method to dictate what actions they
listen to:

```javascript
const Store = {

  // ... Other store methods

  register() {
    return {
      [action.open]      : this.setLoading,
      [action.loading]   : this.setProgress,
      [action.done]      : this.addRecord,
      [action.failed]    : this.setError,
      [action.cancelled] : this.setCancelled
    }
  }
}
```

`action` referenced directly, like `[action]: callback`, refer to the
`done` state.

## API

### `getInitialState()`

Generate the starting value for the particular state this store is
managing. This will be called by the Microcosm using this store when
it is started.

```javascript
var Planets = {
  getInitialState() {
    return []
  }
}
```

### `serialize(state)`

Allows a store to transform data before it leaves the system. It gives
the store the opportunity to reduce non-primitive values into
JSON.

For example, if using
[ImmutableJS](https://github.com/facebook/immutable-js), this might
look like:

```javascript
const Planets = {
  getInitialState() {
    return Immutable.List()
  },
  serialize(planets) {
    return planets.toJSON()
  }
}
```

### `deserialize(state)`
<commit_msg>Add documentation for setup method<commit_after># Stores

1. [Overview](#overview)
2. [Subscribing to different action states](#subscribing-to-different-action-states)
3. [API](#api)

## Overview

Stores define the rules in which resolved actions are converted into
new state. They are added to a Microcosm instance using `addStore`:

```javascript
// Mount a store that operates on a specific key in repo state.
// Any operations the store handles are recorded at `repo.state.key`:
repo.addStore('key', Store)

// Mount a store that operates on all repo state, handlers will
// write directly to `repo.state` itself:
repo.addStore(Store)
```

Stores do not enforce any particular structure. However specific
methods can be defined on stores to configure behavior at key points
in a Microcosm's lifecycle.

## Subscribing to different action states

Stores can provide a `register` method to dictate what actions they
listen to:

```javascript
const Store = {

  // ... Other store methods

  register() {
    return {
      [action.open]      : this.setLoading,
      [action.loading]   : this.setProgress,
      [action.done]      : this.addRecord,
      [action.failed]    : this.setError,
      [action.cancelled] : this.setCancelled
    }
  }
}
```

`action` referenced directly, like `[action]: callback`, refer to the
`done` state.

## API

### `getInitialState()`

Generate the starting value for the particular state this store is
managing. This will be called by the Microcosm using this store when
it is started.

```javascript
var Planets = {
  getInitialState() {
    return []
  }
}
```

### `setup()`

Setup runs right after a store is added to a Microcosm, but before it runs
getInitialState. This is useful for one-time setup instructions.

### `serialize(state)`

Allows a store to transform data before it leaves the system. It gives
the store the opportunity to reduce non-primitive values into
JSON.

For example, if using
[ImmutableJS](https://github.com/facebook/immutable-js), this might
look like:

```javascript
const Planets = {
  getInitialState() {
    return Immutable.List()
  },
  serialize(planets) {
    return planets.toJSON()
  }
}
```

### `deserialize(state)`

{'fraction_non_alphanumeric': 0.10500118511495615, 'fraction_numerical': 0.001422137947380896, 'mean_word_length': 3.8009101251422073, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '3781713', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1203, 'n_words': 476}