Skip to content
Myr

Documented patches

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 4eb6bce · Browse files · 4 files+129 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+129 added−2 removed

  • tests:+49 added−1 removed
  • other:+80 added−1 removed

4 files+129 added−2 removed

modified:__tests__/readme.js+49 added-1 removed
modified:changelog.md+4 added
1# Changelog23### 1.5.045* Added support for patches, through [#168](https://github.com/mweststrate/immer/pull/168). See the [patches](https://github.com/mweststrate/immer#patches) section for details67### 1.4.089* Improved TypeScript typings; if the base state is declared as `readonly`, the `draft` object will be upcasted to a writeable version. Fixes [#97](https://github.com/mweststrate/immer/issues/97) through [#161](https://github.com/mweststrate/immer/pull/161) by [@knpwrs](https://github.com/knpwrs)10* It is now possible to use both `import produce from "immer"` and `import {produce} from "immer"`. Implements [#136](https://github.com/mweststrate/immer/issues/136)
161 unmodified lines
modified:package.json+1 added-1 removed
modified:readme.md+75 added
218 unmodified lines
219 }220)221```222223## Patches224225During the run of a producer, Immer can record all the patches that would replay the changes made by the reducer.226This is a very powerful tool if you want to fork your state temporarily, and replay the changes to the original.227To help with replaying patches, `applyPatches` comes in handy. Here is an example how patches could be used228to record the incremental updates and (inverse) apply them:229230```javascript231import produce, {applyPatches} from "immer"232233let state = {234 name: "Micheal",235 age: 32236}237238// Let's assume the user is in a wizard, and we don't know whether239// his changes should be end up in the base state ultimately or not...240let fork = state241// all the changes the user made in the wizard242let changes = []243// the inverse of all the changes made in the wizard244let inverseChanges = []245246fork = produce(247 fork,248 draft => {249 draft.age = 33250 },251 // The third argument to produce is a callback to which the patches will be fed252 (patches, inversePatches) => {253 changes.push(...patches)254 inverseChanges.push(...inversePatches)255 }256)257258// In the mean time, our original state is replaced, as, for example,259// some changes were received from the server260state = produce(state, draft => {261 draft.name = "Michel"262})263264// When the wizard finishes (successfully) we can replay the changes that were in the fork onto the *new* state!265state = applyPatches(state, changes)266267// state now contains the changes from both code paths!268expect(state).toEqual({269 name: "Michel", // changed by the server270 age: 33 // changed by the wizard271})272273// Finally, even after finishing the wizard, the user might change his mind and undo his changes...274state = applyPatches(state, inverseChanges)275expect(state).toEqual({276 name: "Michel", // Not reverted277 age: 32 // Reverted278})279```280281The 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.282This 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:283284```json285[286 { "op": "replace", "path": ["profile"], "value": { "name": "Veria", age: 5 }},287 { "op": "remove", "path": ["tags", 3] }288]289```290291```json292[293 { "op": "replace", "path": ["profile"], "value": { "name": "Noa", age: 6 }},294 { "op": "add", "path": ["tags", 3], "value": "kiddo"},295]296```297298## Auto freezing299300Immer 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.301
244 unmodified lines