Skip to content
Myr

readme.md

This file

Path

readme.md

706 lines · 28.1 KiB · Markdown

Last changed by

support typescript 2.8+aleclarsonbefore adoptionee9e089

History of this file

Take it

The walk stopped after 200 commits: lines older than f895e01 point at it.

Picked a logoMichel Weststratebefore adoptionf1ca2f6

lines 1–2why, one step back
1<img src="images/immer-logo.png" height="200px" align="right"/>
2

Added travis configMichel Weststratebefore adoptionf895e01

lines 3–4why, one step back
3# Immer
4

Merge pull request #156 from styfle/patch-1Michel Weststratebefore adoptione0a8a1a

line 5why, one step back
5[![npm](https://img.shields.io/npm/v/immer.svg)](https://www.npmjs.com/package/immer) [![size](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/immer/dist/immer.umd.js?compression=gzip)](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/immer/dist/immer.umd.js) [![install size](https://packagephobia.now.sh/badge?p=immer)](https://packagephobia.now.sh/result?p=immer) [![Build Status](https://travis-ci.org/mweststrate/immer.svg?branch=master)](https://travis-ci.org/mweststrate/immer) [![Coverage Status](https://coveralls.io/repos/github/mweststrate/immer/badge.svg?branch=master)](https://coveralls.io/github/mweststrate/immer?branch=master) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/michelweststrate)

Picked a logoMichel Weststratebefore adoptionf1ca2f6

line 6why, one step back
6

Added travis configMichel Weststratebefore adoptionf895e01

lines 7–8why, one step back
7_Create the next immutable state tree by simply modifying the current tree_
8

More readme linksMichel Weststratebefore adoptiond4a165b

line 9why, one step back
9Did Immer make a difference to your project? Consider buying me a coffee!<br/><a href="https://www.buymeacoffee.com/mweststrate" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>

added some readme clarificationsMichel Weststratebefore adoption897c284

line 10why, one step back
10

Added travis configMichel Weststratebefore adoptionf895e01

lines 11–12why, one step back
11---
12

Merge pull request #123 from styfle/patch-1benbraoubefore adoption02fe6b1

lines 13–15why, one step back
13* NPM: `npm install immer`
14* Yarn: `yarn add immer`15* CDN: Exposed global is `immer`

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 16–17why, one step back
16 * Unpkg: `<script src="https://unpkg.com/immer/dist/immer.umd.js"></script>`
17 * JSDelivr: `<script src="https://cdn.jsdelivr.net/npm/immer/dist/immer.umd.js"></script>`

Readme improvementsMichel Weststratebefore adoptiondcedda8

line 18why, one step back
18

Updated readme linksMichel Weststratebefore adoption553b6ce

lines 19–20why, one step back
19---
20

Object.is for equality, more tests. Fixes #43, #42, #48Michel Weststratebefore adoptione29a792

line 21why, one step back
21* Egghead lesson covering all of immer (7m): [Simplify creating immutable data trees with Immer](https://egghead.io/lessons/redux-simplify-creating-immutable-data-trees-with-immer)

Updated readme linksMichel Weststratebefore adoption553b6ce

lines 22–23why, one step back
22* Introduction blogpost: [Immer: Immutability the easy way](https://medium.com/@mweststrate/introducing-immer-immutability-the-easy-way-9d73d8f71cb3)
23

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 24why, one step back
24Immer (German for: always) is a tiny package that allows you to work with immutable state in a more convenient way. It is based on the [_copy-on-write_](https://en.wikipedia.org/wiki/Copy-on-write) mechanism.

Added travis configMichel Weststratebefore adoptionf895e01

line 25why, one step back
25

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 26why, one step back
26The basic idea is that you will apply all your changes to a temporarily _draftState_, which is a proxy of the _currentState_. Once all your mutations are completed, Immer will produce the _nextState_ based on the mutations to the draft state. This means that you can interact with your data by simply modifying it while keeping all the benefits of immutable data.

Added travis configMichel Weststratebefore adoptionf895e01

line 27why, one step back
27

Merge pull request #56 from jkhedani/masterMichel Weststratebefore adoption076fbd6

line 28why, one step back
28![immer-hd.png](images/hd/immer.png)

Added travis configMichel Weststratebefore adoptionf895e01

line 29why, one step back
29

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 30why, one step back
30Using Immer is like having a personal assistant; he takes a letter (the current state) and gives you a copy (draft) to jot changes onto. Once you are done, the assistant will take your draft and produce the real immutable, final letter for you (the next state).

Added travis configMichel Weststratebefore adoptionf895e01

line 31why, one step back
31

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 32why, one step back
32A mindful reader might notice that this is quite similar to `withMutations` of ImmutableJS. It is indeed, but generalized and applied to plain, native JavaScript data structures (arrays and objects) without further needing any library.

Added travis configMichel Weststratebefore adoptionf895e01

line 33why, one step back
33

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 34–41why, one step back
34## External resources
3536* Blog: [The Rise of Immer in React](https://www.netlify.com/blog/2018/09/12/the-rise-of-immer-in-react/)37* Blog: by Workday Prism on why they picked Immer to manage immutable state [The Search for a Strongly-Typed, Immutable State](https://medium.com/workday-engineering/workday-prism-analytics-the-search-for-a-strongly-typed-immutable-state-a09f6768b2b5)38* Blog: [Immutability in React and Redux: The Complete Guide](https://daveceddia.com/react-redux-immutability-guide/)39* Video tutorial: [Using Immer with React.setState](https://codedaily.io/screencasts/86/Immutable-Data-with-Immer-and-React-setState)40* [Talk](https://www.youtube.com/watch?v=-gJbS7YjcSo) + [slides](http://immer.surge.sh/) on Immer at React Finland 2018 by Michel Weststrate41

Added travis configMichel Weststratebefore adoptionf895e01

lines 42–43why, one step back
42## API
43

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 44why, one step back
44The Immer package exposes a default function that does all the work.

Added travis configMichel Weststratebefore adoptionf895e01

line 45why, one step back
45

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 46why, one step back
46`produce(currentState, producer: (draftState) => void): nextState`

Added travis configMichel Weststratebefore adoptionf895e01

line 47why, one step back
47

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 48why, one step back
48There is also a curried overload that is explained [below](#currying).

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 49why, one step back
49

Added travis configMichel Weststratebefore adoptionf895e01

lines 50–52why, one step back
50## Example
5152```javascript

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 53why, one step back
53import produce from "immer"

Merge pull request #22 from mweststrate/feature/es5Michel Weststratebefore adoption4165abb

line 54why, one step back
54

Added travis configMichel Weststratebefore adoptionf895e01

lines 55–60why, one step back
55const baseState = [
56 {57 todo: "Learn typescript",58 done: true59 },60 {

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 61why, one step back
61 todo: "Try immer",

Added travis configMichel Weststratebefore adoptionf895e01

lines 62–65why, one step back
62 done: false
63 }64]65

`immer` -> `produce`. Implements #24Michel Weststratebefore adoptione3eacea

line 66why, one step back
66const nextState = produce(baseState, draftState => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 67why, one step back
67 draftState.push({todo: "Tweet about it"})

Added travis configMichel Weststratebefore adoptionf895e01

lines 68–71why, one step back
68 draftState[1].done = true
69})70```71

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 72why, one step back
72The interesting thing about Immer is that the `baseState` will be untouched, but the `nextState` will reflect all changes made to `draftState`.

Added travis configMichel Weststratebefore adoptionf895e01

lines 73–91why, one step back
73
74```javascript75// the new item is only added to the next state,76// base state is unmodified77expect(baseState.length).toBe(2)78expect(nextState.length).toBe(3)7980// same for the changed 'done' prop81expect(baseState[1].done).toBe(false)82expect(nextState[1].done).toBe(true)8384// unchanged data is structurally shared85expect(nextState[0]).toBe(baseState[0])86// changed data not (dûh)87expect(nextState[1]).not.toBe(baseState[1])88```8990## Benefits91

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 92–97why, one step back
92* Immutability with normal JavaScript objects and arrays. No new APIs to learn!
93* Strongly typed, no string based paths selectors etc.94* Structural sharing out of the box95* Object freezing out of the box96* Deep updates are a breeze97* Boilerplate reduction. Less noise, more concise code.

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 98why, one step back
98* Small: bundled and minified: 2KB.

Added travis configMichel Weststratebefore adoptionf895e01

line 99why, one step back
99

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 100why, one step back
100Read further to see all these benefits explained.

Added travis configMichel Weststratebefore adoptionf895e01

lines 101–103why, one step back
101
102## Reducer Example103

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 104why, one step back
104Here is a simple example of the difference that Immer could make in practice.

Added travis configMichel Weststratebefore adoptionf895e01

lines 105–106why, one step back
105
106```javascript

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 107–109why, one step back
107// Redux reducer
108// Shortened, based on: https://github.com/reactjs/redux/blob/master/examples/shopping-cart/src/reducers/products.js109const byId = (state, action) => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 110–121why, one step back
110 switch (action.type) {
111 case RECEIVE_PRODUCTS:112 return {113 ...state,114 ...action.products.reduce((obj, product) => {115 obj[product.id] = product116 return obj117 }, {})118 }119 default:120 return state121 }

Added travis configMichel Weststratebefore adoptionf895e01

lines 122–124why, one step back
122}
123```124

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 125why, one step back
125After using Immer, that simply becomes:

Added travis configMichel Weststratebefore adoptionf895e01

lines 126–127why, one step back
126
127```javascript

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 128why, one step back
128import produce from "immer"

Added travis configMichel Weststratebefore adoptionf895e01

line 129why, one step back
129

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 130why, one step back
130const byId = (state, action) =>

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 131–138why, one step back
131 produce(state, draft => {
132 switch (action.type) {133 case RECEIVE_PRODUCTS:134 action.products.forEach(product => {135 draft[product.id] = product136 })137 }138 })

Added travis configMichel Weststratebefore adoptionf895e01

lines 139–140why, one step back
139```
140

transpile es5 codeMichel Weststratebefore adoptionf012154

line 141why, one step back
141Notice that it is not needed to handle the default case, a producer that doesn't do anything will simply return the original state.

Added travis configMichel Weststratebefore adoptionf895e01

line 142why, one step back
142

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 143why, one step back
143Creating Redux reducer is just a sample application of the Immer package. Immer is not just designed to simplify Redux reducers. It can be used in any context where you have an immutable data tree that you want to clone and modify (with structural sharing).

transpile es5 codeMichel Weststratebefore adoptionf012154

line 144why, one step back
144

Merge pull request #114 from xaviergonz/patch-1Michel Weststratebefore adoptionc71b173

line 145why, one step back
145_Note: it might be tempting after using producers for a while, to just place `produce` in your root reducer and then pass the draft to each reducer and work directly over such draft. Don't do that. It kills the point of Redux where each reducer is testable as pure reducer. Immer is best used when applying it to small individual pieces of logic._

Added examples from the Redux Update Patterns PageMichel Weststratebefore adoptionda3a08b

line 146why, one step back
146

Added `React.setState` exampleMichel Weststratebefore adoption2c8a71a

lines 147–148why, one step back
147## React.setState example
148

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 149why, one step back
149Deep updates in the state of React components can be greatly simplified as well by using immer. Take for example the following onClick handlers (Try in [codesandbox](https://codesandbox.io/s/m4yp57632j)):

Added `React.setState` exampleMichel Weststratebefore adoption2c8a71a

lines 150–155why, one step back
150
151```javascript152/**153 * Classic React.setState with a deep merge154 */155onBirthDayClick1 = () => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 156why, one step back
156 this.setState(prevState => ({

Added `React.setState` exampleMichel Weststratebefore adoption2c8a71a

line 157why, one step back
157 user: {

Merge pull request #104 from mikollo/patch-1Michel Weststratebefore adoptionc047a59

lines 158–159why, one step back
158 ...prevState.user,
159 age: prevState.user.age + 1

Added `React.setState` exampleMichel Weststratebefore adoption2c8a71a

line 160why, one step back
160 }

Merge pull request #104 from mikollo/patch-1Michel Weststratebefore adoptionc047a59

line 161why, one step back
161 }))

Added `React.setState` exampleMichel Weststratebefore adoption2c8a71a

lines 162–165why, one step back
162}
163164/**165 * ...But, since setState accepts functions,

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 166why, one step back
166 * we can just create a curried producer and further simplify!

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 167why, one step back
167 */

Added `React.setState` exampleMichel Weststratebefore adoption2c8a71a

line 168why, one step back
168onBirthDayClick2 = () => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 169–173why, one step back
169 this.setState(
170 produce(draft => {171 draft.user.age += 1172 })173 )

Added `React.setState` exampleMichel Weststratebefore adoption2c8a71a

lines 174–176why, one step back
174}
175```176

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 177–178why, one step back
177## Currying
178

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 179why, one step back
179Passing a function as the first argument to `produce` is intended to be used for currying. This means that you get a pre-bound producer that only needs a state to produce the value from. The producer function gets passed in the draft and any further arguments that were passed to the curried function.

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 180–196why, one step back
180
181For example:182183```javascript184// mapper will be of signature (state, index) => state185const mapper = produce((draft, index) => {186 draft.index = index187})188189// example usage190console.dir([{}, {}, {}].map(mapper))191//[{index: 0}, {index: 1}, {index: 2}])192```193194This mechanism can also nicely be leveraged to further simplify our example reducer:195196```javascript

Fixed configurability of array.length on proxies. Fixes #50Michel Weststratebefore adoption1c7df26

lines 197–198why, one step back
197import produce from 'immer'
198

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 199–201why, one step back
199const byId = produce((draft, action) => {
200 switch (action.type) {201 case RECEIVE_PRODUCTS:

Merge pull request #71 from Keysox/patch-1Michel Weststratebefore adoptiond47db4d

line 202why, one step back
202 action.products.forEach(product => {

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 203why, one step back
203 draft[product.id] = product

Merge pull request #71 from Keysox/patch-1Michel Weststratebefore adoptiond47db4d

line 204why, one step back
204 })

Readme improvementsMichel Weststratebefore adoptiondcedda8

line 205why, one step back
205 return

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 206–212why, one step back
206 })
207 }208})209```210211Note that `state` is now factored out (the created reducer will accept a state, and invoke the bound producer with it).212

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 213–215why, one step back
213If you want to initialize an uninitialized state using this construction, you can do so by passing the initial state as second argument to `produce`:
214215```javascript

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 216why, one step back
216import produce from "immer"

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

line 217why, one step back
217

Readme improvementsMichel Weststratebefore adoptiondcedda8

line 218why, one step back
218const byId = produce(

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 219–229why, one step back
219 (draft, action) => {
220 switch (action.type) {221 case RECEIVE_PRODUCTS:222 action.products.forEach(product => {223 draft[product.id] = product224 })225 return226 }227 },228 {229 1: {id: 1, name: "product-1"}

Readme improvementsMichel Weststratebefore adoptiondcedda8

lines 230–231why, one step back
230 }
231)

Added some external resources to readmeMichel Weststratebefore adoptionc018c49

lines 232–248why, one step back
232```
233234##### Fun with currying235236A random fun example just for inspiration:237a neat trick is to turn `Object.assign` into a producer to create a "spread" function that is smarter than the normal spread operator, as it doesn't produce a new state if the result doesn't actually change ([details & explanation](https://twitter.com/mweststrate/status/1045059430256119809)).238Quick example:239240```javascript241import produce from "immer"242const spread = produce(Object.assign)243244const base = {x: 1, y: 1}245246console.log({...base, y: 1} === base) // false247console.log(spread(base, {y: 1}) === base) // true! base is recycled as no actual new value was produced248console.log(spread(base, {y: 2}) === base) // false, produced a new object as it should

Merge pull request #121 from pkerschbaum/feature/initial-state-for-curry-fnMichel Weststratebefore adoption2cb5496

lines 249–250why, one step back
249```
250

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 251–253why, one step back
251## Patches
252253During the run of a producer, Immer can record all the patches that would replay the changes made by the reducer.

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 254why, one step back
254This is a very powerful tool if you want to fork your state temporarily and replay the changes to the original.

Update readmeMichel Weststratebefore adoption341cfeb

lines 255–258why, one step back
255
256Patches are useful in few scenarios:257 * To exchange incremental updates with other parties, for example over websockets258 * For debugging / traces, to see precisely how state is changed over time

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 259why, one step back
259 * As basis for undo/redo or as an approach to replay changes on a slightly different state tree

Update readmeMichel Weststratebefore adoption341cfeb

line 260why, one step back
260

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 261–272why, one step back
261To help with replaying patches, `applyPatches` comes in handy. Here is an example how patches could be used
262to record the incremental updates and (inverse) apply them:263264```javascript265import produce, {applyPatches} from "immer"266267let state = {268 name: "Micheal",269 age: 32270}271272// Let's assume the user is in a wizard, and we don't know whether

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 273why, one step back
273// his changes should end up in the base state ultimately or not...

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 274–291why, one step back
274let fork = state
275// all the changes the user made in the wizard276let changes = []277// the inverse of all the changes made in the wizard278let inverseChanges = []279280fork = produce(281 fork,282 draft => {283 draft.age = 33284 },285 // The third argument to produce is a callback to which the patches will be fed286 (patches, inversePatches) => {287 changes.push(...patches)288 inverseChanges.push(...inversePatches)289 }290)291

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 292why, one step back
292// In the meantime, our original state is replaced, as, for example,

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 293–319why, one step back
293// some changes were received from the server
294state = produce(state, draft => {295 draft.name = "Michel"296})297298// When the wizard finishes (successfully) we can replay the changes that were in the fork onto the *new* state!299state = applyPatches(state, changes)300301// state now contains the changes from both code paths!302expect(state).toEqual({303 name: "Michel", // changed by the server304 age: 33 // changed by the wizard305})306307// Finally, even after finishing the wizard, the user might change his mind and undo his changes...308state = applyPatches(state, inverseChanges)309expect(state).toEqual({310 name: "Michel", // Not reverted311 age: 32 // Reverted312})313```314315The generated patches are similar (but not the same) to the [RFC-6902 JSON patch standard](http://tools.ietf.org/html/rfc6902), except that the `path` property is an array, rather than a string.316This makes processing patches easier. If you want to normalize to the official specification, `patch.path = patch.path.join("/")` should do the trick. Anyway, this is what a bunch of patches and their inverse could look like:317318```json319[

syntax fixesMichel Weststratebefore adoptioncdaec5a

line 320why, one step back
320 { "op": "replace", "path": ["profile"], "value": { "name": "Veria", "age": 5 }},

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 321–326why, one step back
321 { "op": "remove", "path": ["tags", 3] }
322]323```324325```json326[

syntax fixesMichel Weststratebefore adoptioncdaec5a

line 327why, one step back
327 { "op": "replace", "path": ["profile"], "value": { "name": "Noa", "age": 6 }},

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 328–331why, one step back
328 { "op": "add", "path": ["tags", 3], "value": "kiddo"},
329]330```331

Added link to immer patch articleMichel Weststratebefore adoption255d135

lines 332–333why, one step back
332For a more in-depth study, see [Distributing patches and rebasing actions using Immer](https://medium.com/@mweststrate/distributing-state-changes-using-snapshots-patches-and-actions-part-2-2f50d8363988)
333

Added note about compressing patchesMichel Weststratebefore adoption7cae55b

lines 334–335why, one step back
334Tip: Check this trick to [compress patches](https://medium.com/@david.b.edelstein/using-immer-to-compress-immer-patches-f382835b6c69) produced over time.
335

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 336–337why, one step back
336## Auto freezing
337

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 338why, one step back
338Immer automatically freezes any state trees that are modified using `produce`. This protects against accidental modifications of the state tree outside of a producer. This comes with a performance impact, so it is recommended to disable this option in production. It is by default enabled. By default, it is turned on during local development and turned off in production. Use `setAutoFreeze(true / false)` to explicitly turn this feature on or off.

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 339why, one step back
339

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 340–341why, one step back
340## Returning data from producers
341

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 342why, one step back
342It is not needed to return anything from a producer, as Immer will return the (finalized) version of the `draft` anyway. However, it is allowed to just `return draft`.

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 343why, one step back
343

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 344why, one step back
344It is also allowed to return arbitrarily other data from the producer function. But _only_ if you didn't modify the draft. This can be useful to produce an entirely new state. Some examples:

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 345–347why, one step back
345
346```javascript347const userReducer = produce((draft, action) => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 348–377why, one step back
348 switch (action.type) {
349 case "renameUser":350 // OK: we modify the current state351 draft.users[action.payload.id].name = action.payload.name352 return draft // same as just 'return'353 case "loadUsers":354 // OK: we return an entirely new state355 return action.payload356 case "adduser-1":357 // NOT OK: This doesn't do change the draft nor return a new state!358 // It doesn't modify the draft (it just redeclares it)359 // In fact, this just doesn't do anything at all360 draft = {users: [...draft.users, action.payload]}361 return362 case "adduser-2":363 // NOT OK: modifying draft *and* returning a new state364 draft.userCount += 1365 return {users: [...draft.users, action.payload]}366 case "adduser-3":367 // OK: returning a new state. But, unnecessary complex and expensive368 return {369 userCount: draft.userCount + 1,370 users: [...draft.users, action.payload]371 }372 case "adduser-4":373 // OK: the immer way374 draft.userCount += 1375 draft.users.push(action.payload)376 return377 }

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

lines 378–380why, one step back
378})
379```380

Merge pull request #197 from 0xflotus/patch-1Michel Weststratebefore adoption21ad96b

line 381why, one step back
381_Note: It is not possible to return `undefined` this way, as it is indistinguishable from *not* updating the draft! Read on..._

added some readme clarificationsMichel Weststratebefore adoption897c284

line 382why, one step back
382

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 383–384why, one step back
383## Producing `undefined` using `nothing`
384

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 385why, one step back
385So, in general, one can replace the current state by just `return`ing a new value from the producer, rather than modifying the draft.

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 386–402why, one step back
386There is a subtle edge case however: if you try to write a producer that wants to replace the current state with `undefined`:
387388```javascript389produce({}, draft => {390 // don't do anything391})392```393394Versus:395396```javascript397produce({}, draft => {398 // Try to return undefined from the producer399 return undefined400})401```402

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 403why, one step back
403The problem is that in JavaScript a function that doesn't return anything also returns `undefined`!

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 404–415why, one step back
404So immer cannot differentiate between those different cases.
405So, by default, Immer will assume that any producer that returns `undefined` just tried to modify the draft.406407However, to make it clear to Immer that you intentionally want to produce the value `undefined`, you can return the built-in token `nothing`:408409```javascript410import produce, { nothing } from "immer"411412const state = {413 hello: "world"414}415

Less is moreMichel Weststratebefore adoption1802421

line 416why, one step back
416produce(state, (draft) => {})

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

line 417why, one step back
417produce(state, (draft) => undefined)

Less is moreMichel Weststratebefore adoption1802421

line 418why, one step back
418// Both return the original state: { hello: "world"}

Introduced the concept of `nothing`Michel Weststratebefore adoption798d2b2

lines 419–425why, one step back
419
420produce(state, (draft) => nothing)421// Produces a new state, 'undefined'422```423424N.B. Note that this problem is specific for the `undefined` value, any other value, including `null`, doesn't suffer from this issue.425

Merge pull request #179 from RichieAHB/rahb/add-originalMichel Weststratebefore adoptionf8479c2

lines 426–436why, one step back
426## Extracting the original object from a proxied instance
427428Immer exposes a named export `original` that will get the original object from the proxied instance inside `produce` (or return `undefined` for unproxied values). A good example of when this can be useful is when searching for nodes in a tree-like state using strict equality.429430```js431const baseState = { users: [{ name: "Richie" }] };432const nextState = produce(baseState, draftState => {433 original(draftState.users) // is === baseState.users434})435```436

Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30

lines 437–443why, one step back
437## Using `this`
438439The recipe will be always invoked with the `draft` as `this` context.440441This means that the following constructions are also valid:442443```javascript

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 444why, one step back
444const base = {counter: 0}

Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30

lines 445–446why, one step back
445
446const next = produce(base, function() {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 447why, one step back
447 this.counter++

Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30

lines 448–452why, one step back
448})
449console.log(next.counter) // 1450451// OR452const increment = produce(function() {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 453why, one step back
453 this.counter++

Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30

lines 454–457why, one step back
454})
455console.log(increment(base).counter) // 1456```457

Merge pull request #183 from drcmda/patch-1Michel Weststratebefore adoption4b9a7d3

lines 458–459why, one step back
458## Inline shortcuts using `void`
459

Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c

line 460why, one step back
460Draft mutations in Immer usually warrant a code block, since a return denotes an overwrite. Sometimes that can stretch code a little more than you might be comfortable with.

Merge pull request #183 from drcmda/patch-1Michel Weststratebefore adoption4b9a7d3

line 461why, one step back
461

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 462why, one step back
462In such cases, you can use javascripts [`void`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void) operator, which evaluates expressions and returns `undefined`.

Merge pull request #183 from drcmda/patch-1Michel Weststratebefore adoption4b9a7d3

lines 463–471why, one step back
463
464```javascript465// Single mutation466produce(draft => void (draft.user.age += 1))467468// Multiple mutations469produce(draft => void (draft.user.age += 1, draft.user.height = 186))470```471

Merge pull request #186 from arilotter/patch-1Michel Weststratebefore adoptiond6d0f69

line 472why, one step back
472Code style is highly personal, but for code bases that are to be understood by many, we recommend to stick to the classic `draft => { draft.user.age += 1}` to avoid cognitive overhead.

Be explicit about not supporting arbitrary properties on arraysMichel Weststratebefore adoption0a80c2c

line 473why, one step back
473

Documented 1.0.0 release!Michel Weststratebefore adoption5e16b30

lines 474–477why, one step back
474## TypeScript or Flow
475476The Immer package ships with type definitions inside the package, which should be picked up by TypeScript and Flow out of the box and without further configuration.477

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

lines 478–480why, one step back
478The TypeScript typings automatically remove `readonly` modifiers from your draft types and return a value that matches your original type. See this practical example:
479480```ts

Merge pull request #190 from aretecode/patch-1Michel Weststratebefore adoptiond0ac0e6

line 481why, one step back
481import produce from 'immer'

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

lines 482–483why, one step back
482
483interface State {

Merge pull request #190 from aretecode/patch-1Michel Weststratebefore adoptiond0ac0e6

line 484why, one step back
484 readonly x: number

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

lines 485–488why, one step back
485}
486487// `x` cannot be modified here488const state: State = {

Merge pull request #190 from aretecode/patch-1Michel Weststratebefore adoptiond0ac0e6

lines 489–490why, one step back
489 x: 0
490}

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

line 491why, one step back
491

Merge pull request #170 from knpwrs/patch-2Michel Weststratebefore adoptionb618bf9

line 492why, one step back
492const newState = produce<State>(state, draft => {

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

line 493why, one step back
493 // `x` can be modified here

Merge pull request #190 from aretecode/patch-1Michel Weststratebefore adoptiond0ac0e6

lines 494–495why, one step back
494 draft.x++
495})

Merge pull request #161 from knpwrs/mapped-draft-typeMichel Weststratebefore adoptionedc8b7c

lines 496–501why, one step back
496
497// `newState.x` cannot be modified here498```499500This ensures that the only place you can modify your state is in your produce callbacks. It even works recursively and with `ReadonlyArray`s!501

support typescript 2.8+aleclarsonbefore adoptionee9e089

line 502why, one step back
502Using TypeScript v2.7.x or lower is _not_ supported by Immer.

improve Draft<T> in TypeScriptAlec Larsonbefore adoption512256b

line 503why, one step back
503

Merge pull request #68 from mweststrate/feature/reuseMichel Weststratebefore adoption20394c0

line 504why, one step back
504## Immer on older JavaScript environments?

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 505why, one step back
505

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 506why, one step back
506By default `produce` tries to use proxies for optimal performance. However, on older JavaScript engines `Proxy` is not available. For example, when running Microsoft Internet Explorer or React Native on Android. In such cases, Immer will fallback to an ES5 compatible implementation which works identical, but is a bit slower.

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 507why, one step back
507

`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015

lines 508–509why, one step back
508## Importing immer
509

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 510why, one step back
510`produce` is exposed as the default export, but optionally it can be used as name import as well, as this benefits some older project setups. So the following imports are all correct, where the first is recommended:

`produce` is now exported as both default and name export. Implements #136Michel Weststratebefore adoption05e3015

lines 511–523why, one step back
511
512```javascript513import produce from "immer"514import { produce } from "immer"515516const { produce } = require("immer")517const produce = require("immer").produce518const produce = require("immer").default519520import unleashTheMagic from "immer"521import { produce as unleashTheMagic } from "immer"522```523

Improved the readme concerning limitations. Fixes #185Michel Weststratebefore adoptionf0e8c47

line 524why, one step back
524## Limitations

Added travis configMichel Weststratebefore adoptionf895e01

line 525why, one step back
525

Improved the readme concerning limitations. Fixes #185Michel Weststratebefore adoptionf0e8c47

lines 526–535why, one step back
526Immer supports the following types of data:
5271. All kinds of primitive values5281. `Date` instances, but: only if not mutated, see below5291. Arrays, but: non-standard attributes are not supported (like: `array.test = "Test"`)5301. Plain objects (objects that have as prototype either `null` or `Object`)5311. Functions, assuming they aren't mutated5321. Other value types (like class instances) can be stored in the tree, but note that Immer won't work _inside_ those objects. In other words, if you modify a class instance, this will _not_ result in clone and unmodified original, but just in a modified original.533534# Pitfalls535

Implemented and document #108: allow returning values from producersMichel Weststratebefore adoptionc589ddd

line 536why, one step back
5361. Don't redefine draft like, `draft = myCoolNewState`. Instead, either modify the `draft` or return a new state. See [Returning data from producers](#returning-data-from-producers).

Improved the readme concerning limitations. Fixes #185Michel Weststratebefore adoptionf0e8c47

line 537why, one step back
5371. Immer assumes your state to be a unidirectional tree. That is, no object should appear twice in the tree, and there should be no circular references.

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 538why, one step back
5381. Class instances are not, and will not have first-class support. Read [here](https://github.com/mweststrate/immer/issues/155#issuecomment-407725592) why classes are a conceptual mismatch (and technically extremely challenging)

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 539why, one step back
5391. For example, working with `Date` objects is no problem, just make sure you never modify them (by using methods like `setYear` on an existing instance). Instead, always create fresh `Date` instances. Which is probably what you were unconsciously doing already.

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 540why, one step back
5401. Since Immer uses proxies, reading huge amounts of data from state comes with an overhead (especially in the ES5 implementation). If this ever becomes an issue (measure before you optimize!), do the current state analysis before entering the producer function or read from the `currentState` rather than the `draftState`. Also, realize that immer is opt-in everywhere, so it is perfectly fine to manually write super performance critical reducers, and use immer for all the normal ones. Also note that `original` can be used to get the original state of an object, which is cheaper to read.

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 541why, one step back
5411. Some debuggers (at least Node 6 is known) have trouble debugging when Proxies are in play. Node 8 is known to work correctly.

Merge pull request #192 from TomFaulkner/patch-1Michel Weststratebefore adoption1401c08

line 542why, one step back
5421. Always try to pull `produce` 'up', for example `for (let x of y) produce(base, d => d.push(x))` is exponentially slower than `produce(base, d => { for (let x of y) d.push(x)})`

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

lines 543–544why, one step back
5431. It is possible to return values from producers, except, it is not possible to return `undefined` that way, as it is indistinguishable from not updating the draft at all! If you want to replace the draft with `undefined`, just return `nothing` from the producer.
5441. Immer does not support built-in data-structures like `Map` and `Set`. However, it is fine to just immutably "update" them yourself but still leverage immer wherever possible:

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 545–565why, one step back
545
546```javascript547const state = {548 title: "hello",549 tokenSet: new Set()550}551552const nextState = produce(state, draft => {553 draft.title = draft.title.toUpperCase() // let immer do it's job554 // don't use the operations onSet, as that mutates the instance!555 // draft.tokenSet.add("c1342")556557 // instead: clone the set (once!)558 const newSet = new Set(draft.tokenSet)559 // mutate the clone (just in this producer)560 newSet.add("c1342")561 // update the draft with the new set562 draft.tokenSet = newSet563})564```565

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 566why, one step back
566Or a deep update in maps (well, don't use maps for this use case, but as an example):

Added some docs on how to use Immer together with Map or SetMichel Weststratebefore adoptiond9b4d56

lines 567–583why, one step back
567
568```javascript569const state = {570 users: new Map(["michel", { name: "miche" }])571}572573const nextState = produce(state, draft => {574 const newUsers = new Map(draft.users)575 // mutate the new map and set a _new_ user object576 // but leverage produce again to base the new user object on the original one577 newUsers.set("michel", produce(draft.users.get("michel"), draft => {578 draft.name = "michel"579 }))580 draft.users = newUsers581})582```583

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

line 584why, one step back
584

Added section with cool stuffMichel Weststratebefore adoption69aedd8

lines 585–586why, one step back
585## Cool things built with immer
586

Updated who uses sectionMichel Weststratebefore adoption0152244

line 587why, one step back
587* [react-copy-write](https://github.com/aweary/react-copy-write) _Immutable state with a mutable API_

Readme improvementsMichel Weststratebefore adoptiondcedda8

line 588why, one step back
588* [redux-starter-kit](https://github.com/markerikson/redux-starter-kit) _A simple set of tools to make using Redux easier_

Added section with cool stuffMichel Weststratebefore adoption69aedd8

lines 589–590why, one step back
589* [immer based handleActions](https://gist.github.com/kitze/fb65f527803a93fb2803ce79a792fff8) _Boilerplate free actions for Redux_
590* [redux-box](https://github.com/anish000kumar/redux-box) _Modular and easy-to-grasp redux based state management, with least boilerplate_

Merge pull request #197 from 0xflotus/patch-1Michel Weststratebefore adoption21ad96b

line 591why, one step back
591* [quick-redux](https://github.com/jeffreyyoung/quick-redux) _tools to make redux development quicker and easier_

Updated who uses sectionMichel Weststratebefore adoption0152244

line 592why, one step back
592* [bey](https://github.com/jamiebuilds/bey) _Simple immutable state for React using Immer_

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

line 593why, one step back
593* [immer-wieder](https://github.com/drcmda/immer-wieder#readme) _State management lib that combines React 16 Context and immer for Redux semantics_

Merge pull request #203 from neurosnap/patch-2Michel Weststratebefore adoptionc41b4ab

line 594why, one step back
594* [robodux](https://github.com/neurosnap/robodux) _flexible way to reduce redux boilerplate_

Updated who uses sectionMichel Weststratebefore adoption0152244

line 595why, one step back
595* ... and [many more](https://www.npmjs.com/browse/depended/immer)

Added section with cool stuffMichel Weststratebefore adoption69aedd8

line 596why, one step back
596

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 597–598why, one step back
597## How does Immer work?
598

Object.is for equality, more tests. Fixes #43, #42, #48Michel Weststratebefore adoptione29a792

line 599why, one step back
599Read the (second part of the) [introduction blog](https://medium.com/@mweststrate/introducing-immer-immutability-the-easy-way-9d73d8f71cb3).

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

line 600why, one step back
600

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 601–604why, one step back
601## Example patterns.
602603_For those who have to go back to thinking in object updates :-)_604

Merge pull request #124 from capaj/patch-1benbraoubefore adoptionbbedb9f

line 605why, one step back
605```javascript

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 606why, one step back
606import produce from "immer"

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 607–609why, one step back
607
608// object mutations609const todosObj = {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 610–612why, one step back
610 id1: {done: false, body: "Take out the trash"},
611 id2: {done: false, body: "Check Email"}612}

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 613–615why, one step back
613
614// add615const addedTodosObj = produce(todosObj, draft => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 616–617why, one step back
616 draft["id3"] = {done: false, body: "Buy bananas"}
617})

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 618–620why, one step back
618
619// delete620const deletedTodosObj = produce(todosObj, draft => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 621–622why, one step back
621 delete draft["id1"]
622})

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 623–625why, one step back
623
624// update625const updatedTodosObj = produce(todosObj, draft => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 626–627why, one step back
626 draft["id1"].done = true
627})

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 628–630why, one step back
628
629// array mutations630const todosArray = [

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 631–633why, one step back
631 {id: "id1", done: false, body: "Take out the trash"},
632 {id: "id2", done: false, body: "Check Email"}633]

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 634–636why, one step back
634
635// add636const addedTodosArray = produce(todosArray, draft => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 637–638why, one step back
637 draft.push({id: "id3", done: false, body: "Buy bananas"})
638})

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 639–641why, one step back
639
640// delete641const deletedTodosArray = produce(todosArray, draft => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 642–645why, one step back
642 draft.splice(draft.findIndex(todo => todo.id === "id1"), 1)
643 // or (slower):644 // return draft.filter(todo => todo.id !== "id1")645})

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 646–648why, one step back
646
647// update648const updatedTodosArray = produce(todosArray, draft => {

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

lines 649–650why, one step back
649 draft[draft.findIndex(todo => todo.id === "id1")].done = true
650})

Added examples as suggested by #115Michel Weststratebefore adoption3f64997

lines 651–652why, one step back
651```
652

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

lines 653–654why, one step back
653## Performance
654

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 655why, one step back
655Here is a [simple benchmark](__performance_tests__/todo.js) on the performance of Immer. This test takes 50,000 todo items and updates 5,000 of them. _Freeze_ indicates that the state tree has been frozen after producing it. This is a _development_ best practice, as it prevents developers from accidentally modifying the state tree.

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

line 656why, one step back
656

Update readmeMichel Weststratebefore adoption341cfeb

line 657why, one step back
657These tests were executed on Node 9.3.0. Use `yarn test:perf` to reproduce them locally.

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

line 658why, one step back
658

Fixed performance imagesMichel Weststratebefore adoption89e0d24

line 659why, one step back
659![performance.png](images/performance.png)

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

line 660why, one step back
660

Update readmeMichel Weststratebefore adoption341cfeb

line 661why, one step back
661Most important observation:

Merge branch 'kachkaev-add-failing-tests-for-recursive-use'Michel Weststratebefore adoption3db727b

line 662why, one step back
662

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 663why, one step back
663* Immer with proxies is roughly speaking twice to three times slower as a handwritten reducer (the above test case is worst case, see `yarn test:perf` for more tests). This is in practice negligible.

Merge pull request #38 from mweststrate/feature/performanceMichel Weststratebefore adoption670fcff

line 664why, one step back
664* Immer is roughly as fast as ImmutableJS. However, the _immutableJS + toJS_ makes clear the cost that often needs to be paid later; converting the immutableJS objects back to plain objects, to be able to pass them to components, over the network etc... (And there is also the upfront cost of converting data received from e.g. the server to immutable JS)

Update readmeMichel Weststratebefore adoption341cfeb

lines 665–666why, one step back
665* Generating patches doesn't significantly slow immer down
666* The ES5 fallback implementation is roughly twice as slow as the proxy implementation, in some cases worse.

Added travis configMichel Weststratebefore adoptionf895e01

line 667why, one step back
667

Added FAQ sectionMichel Weststratebefore adoptionb9b418e

lines 668–695why, one step back
668## FAQ
669670_(for those who skimmed the above instead of actually reading)_671672**Q: Does Immer use structural sharing? So that my selectors can be memoized and such?**673674A: Yes675676**Q: Does Immer support deep updates?**677678A: Yes679680**Q: I can't rely on Proxies being present on my target environments. Can I use Immer?**681682A: Yes683684**Q: Can I typecheck my data structures when using Immer?**685686A: Yes687688**Q: Can I store `Date` objects, functions etc in my state tree when using Immer?**689690A: Yes691692**Q: Is it fast?**693694A: Yes695

Added question about freezingMichel Weststratebefore adoption6ac7707

lines 696–699why, one step back
696**Q: Idea! Can Immer freeze the state for me?**
697698A: Yes699

Added travis configMichel Weststratebefore adoptionf895e01

lines 700–701why, one step back
700## Credits
701

Spell checking (#217)Oscar Nordquistbefore adoptionf2f4f2c

line 702why, one step back
702Special thanks to @Mendix, which supports its employees to experiment completely freely two full days a month, which formed the kick-start for this project.

Merge pull request #37 from mweststrate/feature/curryMichel Weststratebefore adoption014019e

lines 703–706why, one step back
703
704## Donations705706A significant part of my OSS work is unpaid. So [donations](https://mobx.js.org/donate.html) are greatly appreciated :)