Skip to content

Commit

Permalink
[add] Docs for stores
Browse files Browse the repository at this point in the history
  • Loading branch information
nhunzaker committed Mar 24, 2015
1 parent 35599c8 commit 9e13924
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
File renamed without changes.
78 changes: 78 additions & 0 deletions docs/store.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Stores

The responsibility of a Store in Microcosm is to transform state from
one form to the next. They do not provide any storage of their own
(although technically this isn't enforced by Microcosm).

Stores boil down to:

```javascript
var store = {
getInitialState() {
// return a starting value
},
serialize(state) {
// transform data when app.toJSON() is called
},
deserialize(state) {
// parse data when app.seed is called
},
toString() {
// return a unique identifier
}
}
```

Of these methods, only the `toString` method is required. If this
method is not implemented Microcosm will quickly throw an error:

```
Stores must implement a toString() method
```

## Installing Stores

Microcosms must add stores:

```javascript
let app = new Microcosm()

let MyStore = {
toString() {
return 'mystore'
}
}

app.addStore(MyStore)
```

This will mix the given store on top of a set of defaults (see
`src/Store.js`) and run `getInitialState()` if it is
provided. Additionally, the Micocosm instance will now be configured
to use `MyStore` to manage state under the `mystore` key (because of
the `toString` method).

This state can be accessed like:

```javascript
app.get(MyStore)
```

## Listening to Actions

Stores listen to actions by implement them as methods:

```javascript
let MyStore = {
[Action.add](state, record) {
return state.concat(record)
},
toString() {
return 'my-store'
}
}
```

The first argument of this method will always be the application state
for the given key provided by `toString()`. It is the responsibility
of the store to return the next state as a result of actions.

0 comments on commit 9e13924

Please sign in to comment.