-
Notifications
You must be signed in to change notification settings - Fork 22
Make stores more powerful #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
12c4e3f
Add additional hooks for stores. Give stores more power
nhunzaker 7a850b0
Store doesnt need update module
nhunzaker 1374e1b
Downcase store in custom store tests
nhunzaker ae115aa
Update docs, change set to stage
nhunzaker b2344fa
Bold, not italic in immutable recipe
nhunzaker 28f9981
Add documentation for setup method
nhunzaker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# ImmutableJS Integration | ||
|
||
1. [Immutable Everywhere](#immutable-everywhere) | ||
2. [Immutable in, Vanilla out](#immutable-in-vanilla-out) | ||
|
||
## Immutable Everywhere | ||
|
||
The most basic integration method is to simply use ImmutableJS: | ||
|
||
```javascript | ||
import Immutable from 'immutable' | ||
import actions from 'actions' | ||
|
||
const Store = { | ||
getInitialState() { | ||
return Immutable.Map() | ||
}, | ||
|
||
add(state, record) { | ||
return state.set(record.id, record) | ||
}, | ||
|
||
remove(state, id) { | ||
return state.remove(id) | ||
}, | ||
|
||
register() { | ||
return { | ||
[actions.create]: this.add, | ||
[actions.destroy]: this.remove | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Immutable in, Vanilla out | ||
|
||
We've found it can be much simpler to expose vanilla JavaScript data to our | ||
presentation layer. Unfortunately, this tends to mitigates much of the benefit | ||
of immutable data. It also can impose penalties serializing between the two formats. | ||
|
||
It is worth walking through the phases of state a Microcosm works through in order | ||
to better understand how Microcosm accommodates this use case: | ||
|
||
1. **archive** - For performance, Microcosm purges old actions and writes their | ||
final result to a cache. | ||
2. **staging** - State before a making change. This is a preparatory state allowing | ||
stores the ability to transform data one last time before assigning it publicly. | ||
3. **state** - Publicaly available state. This is what is exposed via `repo.state`, | ||
|
||
Essentially, Microcosm can maintain ImmutableJS data structures internally, exposing | ||
plain JavaScript for public consumption. There are two key methods responsible for this: | ||
|
||
1. **commit** - A middleware function that dictates how a Store assigns to `repo.state`. | ||
2. **shouldCommit** - A predicate function that controls invocation of `commit`. | ||
|
||
In practice, this results in a small adjustment to the store described earlier: | ||
|
||
```javascript | ||
import Immutable from 'immutable' | ||
import actions from 'actions' | ||
|
||
const Store = { | ||
getInitialState() { | ||
return Immutable.Map() | ||
}, | ||
|
||
shouldCommit(next, previous) { | ||
return Immutable.is(next, previous) === false | ||
}, | ||
|
||
commit(state) { | ||
return Array.from(state.values()) | ||
}, | ||
|
||
add(state, record) { | ||
return state.set(record.id, record) | ||
}, | ||
|
||
remove(state, id) { | ||
return state.remove(id) | ||
}, | ||
|
||
register() { | ||
return { | ||
[actions.create]: this.add, | ||
[actions.destroy]: this.remove | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Here we've added a `shouldCommit` that utilizes the `Immutable.is` equality check. | ||
Additionally, `commit` describes how `Immutable` should convert into a regular form. | ||
In this case, it will convert into an Array. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this. This is inefficiency (though not much). I don't like shallow copying the staged changes, and I don't like the anonymous function call. I also don't like the
[key, value]
tuple.Something to muse on...