Updated docs
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 edb1cad · Browse files · 8 files·+72 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+72 added−6 removed
- other:+72 added−6 removed
Provenance
- Commit
895e0cc- Tree
a46a916- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
edb1cad
8 files changed+72 added−6 removed
modified:docs/api.md+5 added
5 unmodified lines6789101112131415161718192021222325 unmodified lines5 unmodified lines<div id="codefund"><!-- fallback content --></div>
| Exported name | Description | Section || --- | --- | --- || `(default)` | The core API of Immer, typically named `produce`: `import produce from "immer"` | [Produce](produce.md) || `applyPatches` | Given a base state or draft, and a set of patches, applies the patches | [Patches](patches.md) || `castDraft` | Converts any immutable type to its mutable counterpart. This is just a cast and doesn't actually do anything. | [TypeScript](typescript.md) || `castImmutable` | Converts any mutable type to its immutable counterpart. This is just a cast and doesn't actually do anything. | [TypeScript](typescript.md) || `createDraft` | Given a base state, creates a mutable draft for which any modifications will be recorded | [Async](async.md) || `Draft<T>` | Exposed TypeScript type to convert an immutable type to a mutable type | [TypeScript](typescript.md) || `enableAllPlugins()` | Enables all plugins mentioned below | [Installation](installation#pick-your-immer-version) || `enableES5()` | Enables support for older JavaScript engines, such as Internet Explorer and React Native | [Installation](installation#pick-your-immer-version) || `enableMapSet()` | Enables support for `Map` and `Set` collections. | [Installation](installation#pick-your-immer-version) || `enablePatches()` | Enables support for JSON patches. | [Installation](installation#pick-your-immer-version) || `finishDraft` | Given an draft created using `createDraft`, seals the draft and produces and returns the next immutable state that captures all the changes | [Async](async.md) || `Immer` | constructor that can be used to create a second "immer" instance (exposing all APIs listed in this instance), that doesn't share its settings with global instance. || `immerable` | Symbol that can be added to a constructor or prototype, to indicate that Immer should treat the class as something that can be safely drafted | [Classes](complex-objects.md) || `Immutable<T>` | Exposed TypeScript type to convert mutable types to immutable types | |25 unmodified linesmodified:docs/complex-objects.md+2 added
3 unmodified lines4567891011121365 unmodified lines3 unmodified lines---
<div id="codefund"><!-- fallback content --></div>
_⚠ Since version 6 support for `Map`s and `Set`s has to be enabled explicitly by calling [`enableMapSet()`](installation#pick-your-immer-version) once when starting your application._
Plain objects, arrays, `Map`s and `Set`s are always drafted by Immer. An example of using Maps with immer:
```javascripttest("Producers can update Maps", () => {65 unmodified linesmodified:docs/installation.md+57 added
11 unmodified lines1213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747511 unmodified lines- CDN: Exposed global is `immer` - Unpkg: `<script src="https://unpkg.com/immer/dist/immer.umd.js"></script>` - JSDelivr: `<script src="https://cdn.jsdelivr.net/npm/immer/dist/immer.umd.js"></script>`
## Pick your Immer version
_This section only applies to version 6 and later_
To make sure Immer is as small as possible, features that are not required by every project has been made opt-in, and have to be enabled explicitly. This ensures that when bundling your application for production, unused features don't take any space.
The following features can be opt-in to:
| Feature | Description | Method to call || --- | --- | --- || ES 5 support | If your application needs to be able to run on older JavaScript environments, such as Internet Explorer or React Native, enable this feature. | `enableES5()` || [ES2015 Map and Set support](complex-objects.md) | To enable Immer to operate on the native `Map` and `Set` collections, enable this feature | `enableMapSet()` || [JSON Patch support](patches.md) | Immer can keep track of all the changes you make to draft objects. This can be useful for communicating changes using JSON patches | `enablePatches()` || **All of the above** | Unsure what features you need? We recommend to enable all of the features above by default on new projects. Premature optimization of a few KB might not be worth the initial trouble. Also, enabling or disabling features doesn't impact the performance of Immer itself | `enableAllPlugins()` |
For example, if you want to use `produce` on a `Map`, you need to enable this feature once during the start of your application:
```typescript// In your application's entrypointimport {enableMapSet} from "immer"
enableMapSet()
// ...laterimport produce from "immer"
const usersById_v1 = new Map([ ["michel", {name: "Michel Weststrate", country: "NL"}]])
const usersById_v2 = produce(usersById_v1, draft => { draft.get("michel").country = "UK"})
expect(usersById_v1.get("michel").country).toBe("NL")expect(usersById_v2.get("michel").country).toBe("UK")```
Vanilla Immer kicks in at ~3KB gzipped. Every plugin that is enabled adds < 1 KB to that. The breakdown is as follows:
```Import size report for immer (minified, gzipped, in bytes):┌───────────────────────┬───────────┬────────────┬───────────┐│ (index) │ just this │ cumulative │ increment │├───────────────────────┼───────────┼────────────┼───────────┤│ import * from 'immer' │ 5610 │ 0 │ 0 ││ produce │ 3141 │ 3141 │ 0 ││ enableES5 │ 3926 │ 3934 │ 793 ││ enableMapSet │ 3940 │ 4704 │ 770 ││ enablePatches │ 3839 │ 5391 │ 687 ││ enableAllPlugins │ 5387 │ 5425 │ 34 │└───────────────────────┴───────────┴────────────┴───────────┘(this report was generated by npmjs.com/package/import-size)```
## Immer on older JavaScript environments?
By 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 (if < v0.59 or when using the Hermes engine) on Android. In such cases, Immer will fallback to an ES5 compatible implementation which works identically, but is a bit slower.
Since version 6, support for the fallback implementation has to be explicitly enabled by calling `enableES5()`modified:docs/introduction.md+1 added-1 removed
59 unmodified lines60 - Object freezing out of the box61 - Deep updates are a breeze62 - Boilerplate reduction. Less noise, more concise code.63 - First class support for patches64-- Small [](https://img.badgesize.io/https://cdn.jsdelivr.net/npm/immer/dist/immer.umd.js)64+- Small: 3KB gzippedmodified:docs/patches.md+2 added
21 unmodified lines22 </div>23 <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/react-apply-patches-using-immer-applypatches-to-synchronize-state-across-clients">Hosted on egghead.io</a>24 </details>25 26+_⚠ Since version 6 support for Patches has to be enabled explicitly by calling [`enablePatches()`](installation#pick-your-immer-version) once when starting your application._27+28 During the run of a producer, Immer can record all the patches that would replay the changes made by the reducer. This is a very powerful tool if you want to fork your state temporarily and replay the changes to the original.29 30 Patches are useful in few scenarios:31 139 unmodified linesmodified:notes.txt+3 added-3 removed
6 unmodified lines7 [x] _ postfix all members8 [x] clean up finalize9 [x] remove die10 [x] own invariant11-[ ] update docs11+[x] update docs12 [x] plugin tests13 [ ] check if we can create more local vars14 [x] verify performance15 [ ] kill ownKeys?16 [x] kill enumerations17 [ ] check todos18-[ ] kill marchChangesSweep recursive if possible and no perf impact18+[ ] ~~kill marchChangesSweep recursive if possible and no perf impact~~ does have perf impact19 [ ] optimize each closures away? pass in additional args?20 [x] document performance tip: if externally added data is frozen, it doesn't recurse (also update related issue) #46521-[ ] put some invariants behind __DEV__21+[x] put some invariants behind __DEV__22 [x] check for plugin specific utils23 [ ] bind util24 [ ] eliminate switches25 92 unmodified linesmodified:package.json+1 added-1 removed
22 unmodified lines23 "build": "rimraf dist/ && tsdx build --name immer --format esm,cjs,umd && yarn build:flow",24 "build:flow": "cpx 'src/types/immer.js.flow' dist -v",25 "publish-docs": "cd website && GIT_USER=mweststrate USE_SSH=true yarn run publish-gh-pages",26 "start": "cd website && yarn start",27- "test:size": "yarn build && yarn import-size --report . produce enableES5 enableMapSet enablePatches"27+ "test:size": "yarn build && yarn import-size --report . produce enableES5 enableMapSet enablePatches enableAllPlugins"28 },29 "husky": {30 "hooks": {31 "pre-commit": "pretty-quick --staged"54 unmodified linesmodified:readme.md+1 added-1 removed
123455678912 unmodified lines<img src="images/immer-logo.svg" height="200px" align="right"/>
# Immer
[](https://www.npmjs.com/package/immer) [](https://travis-ci.org/immerjs/immer) [](https://coveralls.io/github/mweststrate/immer?branch=master) [](https://bundlephobia.com/result?p=immer) [](https://github.com/prettier/prettier) [](#backers) [](#sponsors)[](https://www.npmjs.com/package/immer) [](https://travis-ci.org/immerjs/immer) [](https://coveralls.io/github/mweststrate/immer?branch=master)[](https://github.com/prettier/prettier) [](#backers) [](#sponsors)
_Create the next immutable state tree by simply modifying the current tree_
Winner of the "Breakthrough of the year" [React open source award](https://osawards.com/react/) and "Most impactful contribution" [JavaScript open source award](https://osawards.com/javascript/) in 201912 unmodified lines