Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@

1. [React Router](recipes/react-router.md)
2. [AJAX](recipes/ajax.md)
3. [ImmutableJS](recipes/immutable-js.md)
4. [Preact](recipes/preact.md)
5. [Hydrating State](recipes/hydrating-state.md)
3. [Preact](recipes/preact.md)
4. [Hydrating State](recipes/hydrating-state.md)
61 changes: 9 additions & 52 deletions docs/api/domains.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const Domain = {

// ... Other domain methods

register() {
register () {
return {
[action.open] : this.setLoading,
[action.loading] : this.setProgress,
Expand All @@ -58,7 +58,7 @@ it is started.

```javascript
var Planets = {
getInitialState() {
getInitialState () {
return []
}
}
Expand Down Expand Up @@ -86,10 +86,10 @@ look like:

```javascript
const Planets = {
getInitialState() {
getInitialState () {
return Immutable.List()
},
serialize(planets) {
serialize (planets) {
return planets.toJSON()
}
}
Expand All @@ -103,13 +103,13 @@ the example in `serialize`:

```javascript
const Planets = {
getInitialState() {
getInitialState () {
return Immutable.List()
},
serialize(planets) {
serialize (planets) {
return planets.toJSON()
},
deserialize(raw) {
deserialize (raw) {
return Immutable.List(raw)
}
}
Expand All @@ -125,58 +125,15 @@ import { addPlanet } from '../actions/planets'

const Planets = {
//...
register() {
register () {
return {
[addPlanet]: this.append
}
},
append(planets, params) {
append (planets, params) {
return planets.concat(params)
}
}

repo.push(Actions.add, { name: 'earth' }) // this will add Earth
```

### `commit(next, staged)`

Think of this as: How should a domin write to the "public" `repo.state` (to be consumed by your React components) when storing a different "private" representation of your data in your domain?

For example: if you want to serialize a complex data structure, such as a `Map`, into a form easier for public consumption:

```javascript
import Immutable from 'immutable'

const Planets = {
getInitialState() {
return Immutable.Map()
},

commit(next) {
return Array.from(next.values())
}
}
```

### `shouldCommit(last, next)`

Based on the next and last state, should `commit` be called? Useful for
custom change management behavior.

```javascript
import Immutable from 'immutable'

const Planets = {
getInitialState() {
return Immutable.Map()
},

shouldCommit(last, next) {
return Immutable.is(last, next)
}

commit(next) {
return Array.from(next.values())
}
}
```
96 changes: 0 additions & 96 deletions docs/recipes/immutable-js.md

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"eslint": "^3.15.0",
"express": "^4.14.0",
"html-webpack-plugin": "^2.28.0",
"immutable": "^3.8.1",
"jest": "^18.0.0",
"microcosm-debugger": "^2.0.0",
"microtime": "^2.1.2",
Expand Down
1 change: 0 additions & 1 deletion site/src/layouts/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ <h4 class="sidebar__heading">Recipes</h4>
<ul>
<li><a href="$base_url$/recipes/ajax.html">AJAX</a></li>
<li><a href="$base_url$/recipes/react-router.html">React Router</a></li>
<li><a href="$base_url$/recipes/immutable-js.html">ImmutableJS</a></li>
<li><a href="$base_url$/recipes/hydrating-state.html">Hydrating State</a></li>
<li><a href="$base_url$/recipes/preact.html">Preact</a></li>
</ul>
Expand Down
69 changes: 8 additions & 61 deletions src/microcosm.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ function Microcosm (options, state, deserialize) {

this.indexes = {}

// Track changes with a mutable flag
this.dirty = false

// Microcosm is now ready. Call the setup lifecycle method
this.setup(options)

Expand Down Expand Up @@ -156,74 +153,22 @@ inherit(Microcosm, Emitter, {
}
},

/**
* Identify the last and next staged state, then ask the associated domain
* if it should commit it. If so, roll state forward.
*/
commit (staged) {
let domains = this.realm.domains
let next = staged

for (var i = 0, len = domains.length; i < len; i++) {
var [key, domain] = domains[i]

next = this.write(next, key, domain)
}

return next
},

/**
* Write state
*/
write (state, key, domain) {
if (domain.commit) {
let next = get(state, key)
let last = get(this.cached, key)
let old = get(this.state, key)

// This gives libraries such as ImmutableJS a chance to serialize
// into a primitive JavaScript form before being publically exposed.
if (old != null && domain.shouldCommit) {
if (domain.shouldCommit && domain.shouldCommit(last, next)) {
return set(state, key, domain.commit(next))
} else {
return set(state, key, old)
}
} else if (old == null || last !== next) {
return set(state, key, domain.commit(next))
}
}

return state
},

/**
/*
* Run through the action history, dispatching their associated
* types and payloads to domains for processing. Emits "change".
*/
reconcile (action) {
if (this.follower) {
this.state = this.parent.state
this.dirty = this.parent.dirty
this.staged = this.parent.staged

return this
}

let original = this.state

// Update children with their parent's state
if (this.parent) {
this.staged = merge(this.staged, this.parent.state)
this.state = merge(this.state, this.parent.state)
this.staged = merge(this.staged, this.parent.staged)
}

this.staged = this.dispatch(this.staged, action)
this.state = this.commit(this.staged)

if (this.state != original) {
this.dirty = true
}

return this
},
Expand All @@ -232,13 +177,15 @@ inherit(Microcosm, Emitter, {
* Publish a release if anything changed
*/
release (action) {
let dirty = this.staged !== this.state

this.state = this.staged

if (action) {
this._emit('effect', action)
}

if (this.dirty) {
this.dirty = false

if (dirty) {
this._emit('change', this.state)
}
},
Expand Down
Loading