Skip to content
Myr

complex-objects.md

This file

Path

website/docs/complex-objects.md

75 lines · 2.9 KiB · Markdown

Last changed by

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

History of this file

Take it

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

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 1–6why, one step back
1---
2id: complex-objects3title: Classes4---56<center>

chore: docusaurus upgradeMichel Weststratebefore adoption48a91b3

line 7why, one step back
7<div data-ea-publisher="immerjs" data-ea-type="image" className="horizontal bordered"></div>

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 8–61why, one step back
8</center>
910Plain objects (objects without a prototype), arrays, `Map`s and `Set`s are always drafted by Immer. 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.1112```js13import {immerable} from "immer"1415class Foo {16 [immerable] = true // Option 11718 constructor() {19 this[immerable] = true // Option 220 }21}2223Foo[immerable] = true // Option 324```2526### Example2728```js29import {immerable, produce} from "immer"3031class Clock {32 [immerable] = true3334 constructor(hour, minute) {35 this.hour = hour36 this.minute = minute37 }3839 get time() {40 return `${this.hour}:${this.minute}`41 }4243 tick() {44 return produce(this, draft => {45 draft.minute++46 })47 }48}4950const clock1 = new Clock(12, 10)51const clock2 = clock1.tick()52console.log(clock1.time) // 12:1053console.log(clock2.time) // 12:1154console.log(clock2 instanceof Clock) // true55```5657### Semantics in detail5859The semantics on how classes are drafted are as follows:60611. A draft of a class is a fresh object but with the same prototype as the original object.

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

line 62why, one step back
621. When creating a draft, Immer will copy all _own_ properties from the base to the draft.This includes (in strict mode) non-enumerable and symbolic properties.

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 63why, one step back
631. _Own_ getters will be invoked during the copy process, just like `Object.assign` would.

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

line 64why, one step back
641. Inherited getters and methods will remain as is and be inherited by the draft, as they are stored on the prototype which is untouched.

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 65–67why, one step back
651. Immer will not invoke constructor functions.
661. The final instance will be constructed with the same mechanism as the draft was created.671. Only getters that have a setter as well will be writable in the draft, as otherwise the value can't be copied back.

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 68–69why, one step back
68
69Because Immer will dereference own getters of objects into normal properties, it is possible to use objects that use getter/setter traps on their fields, like MobX and Vue do.

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

lines 70–71why, one step back
70
71Note that, by default, Immer does not strictly handle object's non-enumerable properties such as getters/setters for performance reason. If you want this behavior to be strict, you can opt-in with `useStrictShallowCopy(config)`. Use `true` to always copy strict, or `"class_only"` to only copy class instances strictly but use the faster loose copying for plain objects. The default is `false`. (Remember, regardless of strict mode, own getters / setters are always copied _by value_. There is currently no config to copy descriptors as-is. Feature request / PR welcome).

fix: incorrect patches for `delete` on arrays. Fixes #879Michel Weststratebefore adoptiond91a659

lines 72–75why, one step back
72
73Immer does not support exotic / engine native objects such as DOM Nodes or Buffers, nor is subclassing Map, Set or arrays supported and the `immerable` symbol can't be used on them.7475So when working for example with `Date` objects, you should always create a new `Date` instance instead of mutating an existing `Date` object.