Document and test things that are _not_ drafted
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 b9cc802 · Browse files · 3 files·+53 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+53 added−0 removed
- tests:+34 added−0 removed
- other:+19 added−0 removed
Provenance
- Commit
6e76122- Tree
377afaf- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
b9cc802
3 files changed+53 added−0 removed
modified:__tests__/base.js+34 added
560 unmodified lines561 expect(() => m.set("x", 3)).toThrow(562 "Cannot use a proxy that has been revoked"563 )564 })565+566+ it("does not draft map keys", () => {567+ // anything else would be terribly confusing568+ const key = {a: 1}569+ const map = new Map([[key, 2]])570+ const next = produce(map, d => {571+ const dKey = Array.from(d.keys())[0]572+ expect(isDraft(dKey)).toBe(false)573+ expect(dKey).toBe(key)574+ dKey.a += 1575+ d.set(dKey, d.get(dKey) + 1)576+ d.set(key, d.get(key) + 1)577+ expect(d.get(key)).toBe(4)578+ expect(key.a).toBe(2)579+ })580+ const entries = Array.from(next.entries())581+ expect(entries).toEqual([[key, 4]])582+ expect(entries[0][0]).toBe(key)583+ expect(entries[0][0].a).toBe(2)584+ })585 })586 587 describe("set drafts", () => {588 it("supports iteration", () => {785 unmodified lines1374 expect(base[0].a).toEqual(1)1375 expect(result[0].a).toEqual(2)1376 })1377 1378+ it("does not draft external data", () => {1379+ const externalData = {x: 3}1380+ const base = {}1381+ const next = produce(base, draft => {1382+ // potentially, we *could* draft external data automatically, but only if those statements are not switched...1383+ draft.y = externalData1384+ draft.y.x += 11385+ externalData.x += 11386+ })1387+ expect(next).toEqual({y: {x: 5}})1388+ expect(externalData.x).toBe(5)1389+ expect(next.y).toBe(externalData)1390+ })1391+1392 autoFreeze &&1393 test("issue #469, state not frozen", () => {1394 const project = produce(1395 {532 unmodified linesmodified:docs/complex-objects.md+2 added
70 unmodified lines71727374757670 unmodified lines
When working with `Date` objects, you should always create a new `Date` instance instead of mutating an existing `Date` object.
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.
_Note: The **keys** of a map are never drafted! This is done to avoid confusing semantics and keep keys always referentially equal_modified:docs/pitfalls.md+17 added
8 unmodified lines910111213141516171819202122232425262728298 unmodified lines1. 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.1. 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.1. 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)})`1. 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.1. Note that data that comes from the closure, and not from the base state, will never be drafted, even when the data has become part of the new draft:
```javascriptfunction onReceiveTodo(todo) { const nextTodos = produce(todos, draft => { draft.todos[todo.id] = todo // Note, because 'todo' is coming from external, and not from the 'draft', // it isn't draft so the following modification affects the original todo! draft.todos[todo.id].done = true
// The reason for this, is that it means that the behavior of the 2 lines above // is equivalent to code, making this whole process more consistent todo.done = true draft.todos[todo.id] = todo })}```