Updated docs. Also fixes #445
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent e819e79 · Browse files · 7 files·+16 added−37 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+16 added−37 removed
- tests:+6 added−0 removed
- other:+10 added−37 removed
Provenance
- Commit
7335671- Tree
6980df1- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
e819e79
7 files changed+16 added−37 removed
modified:__tests__/frozen.js+6 added
147 unmodified linesExpand all1481491501511521531541551561571588 unmodified lines167168169170171172173174175176147 unmodified linesExpand all ) expect(() => res.delete("b")).toThrow( "This object has been frozen and should not be mutated" )
// In draft, still editable expect(produce(res, d => void d.set("a", 2))).not.toBe(res) })
it("will freeze sets", () => { const base = new Set()8 unmodified lines ) expect(() => base.clear()).toThrow( "This object has been frozen and should not be mutated" )
// In draft, still editable expect(produce(res, d => void d.add(2))).not.toBe(res) }) })}modified:docs/api.md+1 added
18 unmodified lines19 | `nothing` | Value that can be returned from a recipe, to indicate that the value `undefined` should be produced | [Return](return.md) |20 | `original` | Given a draft object (doesn't have to be a tree root), returns the the original object at the same path in the original state tree, if present | [Original](original.md) |21 | `Patch` | Exposed TypeScript type, describes the shape of an (inverse) patch object | [Patches](patches.md) |22 | `produce` | The core API of Immer, also exposed as the `default` export | [Produce](produce.md) |23+| `produceWithPatches` | Works the same as `produce`, but instead of just returning the produced object, it returns a tuple, consisting of `[result, patches, inversePatches]`. | [Patches](patches.md) |24 | `setAutoFreeze` | Enables / disables automatic freezing of the trees produces. By default enabled in development builds | [Freezing](freezing.md) |25 | `setUseProxies` | Can be used to disable or force the use of `Proxy` objects. Useful when filing bug reports. | |26 27 ## Importing immer14 unmodified linesmodified:docs/complex-objects.md+2 added-34 removed
3 unmodified lines4 ---5 6 <div id="codefund"><!-- fallback content --></div>7 8-Plain objects and arrays are always drafted by Immer.8+Plain objects, arrays, `Map`s and `Set`s are always drafted by Immer.9 10 Every other object must use the `immerable` symbol to mark itself as compatible with Immer. When one of these objects is mutated within a producer, its prototype is preserved between copies.11 12 ```js13 unmodified lines26 For arrays, only numeric properties and the `length` property can be mutated. Custom properties are not preserved on arrays.27 28 When working with `Date` objects, you should always create a new `Date` instance instead of mutating an existing `Date` object.29 30-Built-in classes like `Map` and `Set` are not yet supported (they might be if there is popular demand). As a workaround, you should clone them before mutating them in a producer:31-32-```js33-const state = {34- set: new Set(),35- map: new Map()36-}37-const nextState = produce(state, draft => {38- // Don't use any Set methods, as that mutates the instance!39- draft.set.add("foo") // ❌40-41- // 1. Instead, clone the set (just once)42- const newSet = new Set(draft.set) // ✅43-44- // 2. Mutate the clone (just in this producer)45- newSet.add("foo")46-47- // 3. Update the draft with the new set48- draft.set = newSet49-50- // Similarly, don't use any Map methods.51- draft.map.set("foo", "bar") // ❌52-53- // 1. Instead, clone the map (just once)54- const newMap = new Map(draft.map) // ✅55-56- // 2. Mutate it57- newMap.set("foo", "bar")58-59- // 3. Update the draft60- draft.map = newMap61-})62-```30+Maps and Sets that are produced by Immer will be made artificially immutable. This means that they will throw an exception when trying mutative methods like `set`, `clear` etc. outside a producer.modified:docs/faq.md+5 added-1 removed
18 unmodified lines19 A: Yes20 21 ## Q: I can't rely on Proxies being present on my target environments. Can I use Immer?22 23-A: Yes23+A: Yes [details](http://localhost:3000/immer/docs/installation#immer-on-older-javascript-environments)24 25 ## Q: Can I typecheck my data structures when using Immer?26 27 A: Yes1 unmodified line29 ## Q: Can I store `Date` objects, functions etc in my state tree when using Immer?30 31 A: Yes32 33+## Q: Can I use Maps and Sets?34+35+A: Yes36+37 ## Q: Is it fast?38 39 A: Yes40 3 unmodified linesmodified:docs/introduction.md+1 added-1 removed
52 unmodified lines53 Head to the [next section](produce) to further dive into `produce`.54 55 ## Benefits56 57-- Immutability with normal JavaScript objects and arrays. No new APIs to learn!57+- Immutability with normal JavaScript objects, arrays, Sets and Maps. No new APIs to learn!58 - Strongly typed, no string based paths selectors etc.59 - Structural sharing out of the box60 - Object freezing out of the box61 - Deep updates are a breeze3 unmodified linesmodified:docs/performance.md+1 added-1 removed
25 unmodified lines26 Here is a [simple benchmark](https://github.com/immerjs/immer/blob/master/__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.27 28 Something that isn't reflected in the numbers above, but in reality, Immer is sometimes significantly _faster_ than a hand written reducer. The reason for that is that Immer will detect "no-op" state changes, and return the original state if nothing actually changed, which can avoid a lot of re-renderings for example. Cases are known where simply applying immer solved critical performance issues.29 30-These tests were executed on Node 10.15.1. Use `yarn test:perf` to reproduce them locally.30+These tests were executed on Node 10.16.3. Use `yarn test:perf` to reproduce them locally.31 32 33 34 Most important observation:5 unmodified linesmodified:website/static/img/performance.pngbinary
A binary file, modified.