diff --git a/__tests__/readme.js b/__tests__/readme.js index 2d8dddb7..dd5f96b5 100644 --- a/__tests__/readme.js +++ b/__tests__/readme.js @@ -33,4 +33,19 @@ describe("readme example", () => { // changed data not (dûh) expect(nextState[1]).not.toBe(baseState[1]) }) + + it("it can use this", () => { + const base = {counter: 0} + + const next = produce(base, function() { + this.counter++ + }) + expect(next.counter).toBe(1) + + // OR + const increment = produce(function() { + this.counter++ + }) + expect(increment(base).counter).toBe(1) + }) }) diff --git a/changelog.md b/changelog.md index 5215b4a2..2d24c68a 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # Changelog +### 1.0.0 (31-Jan-2018) + +* Producer functions will now always be invoked with the draft as context (`this`). See the [readme](https://github.com/mweststrate/immer#using-this). +* Freezing the data will now be automatically (by default) be disabled in production builds. By [Gregory Assasie](https://github.com/Gregjarvez) +* Fixed Flow typings. Fixes [#80](https://github.com/mweststrate/immer/issues/80). By [Marcin Szczepanski](https://github.com/mweststrate/immer/issues?q=is%3Apr+author%3Amarcins) in [#85](https://github.com/mweststrate/immer/pull/85) +* Fixed issue where constructor type was not preserved. By [iruca3](https://github.com/iruca3) through [#81](https://github.com/mweststrate/immer/pull/81) + ### 0.8.5 * Immer will automatically turn auto-freezing of in production. Use `setAutoFreeze` to manually control it. See [#46](https://github.com/mweststrate/immer/issues/78), [#76](https://github.com/mweststrate/immer/pull/76) diff --git a/package.json b/package.json index 61f5c4e9..5068dcec 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "immer", - "version": "0.8.5", + "version": "1.0.0", "description": "Create your next immutable state by mutating the current one", "main": "dist/immer.js", "umd:main": "dist/immer.umd.js", diff --git a/readme.md b/readme.md index d5b8f295..2ac8398f 100644 --- a/readme.md +++ b/readme.md @@ -175,15 +175,39 @@ const byId = produce((draft, action) => { Note that `state` is now factored out (the created reducer will accept a state, and invoke the bound producer with it). One think to keep in mind; you cannot use this construction to initialize an uninitialized state. E.g. `draft = {}` doesn't do anything useful. - ## Auto freezing Immer 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. - Use `setAutoFreeze(true / false)` to turn this feature on or off. 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. + +## Using `this` + +The recipe will be always invoked with the `draft` as `this` context. + +This means that the following constructions are also valid: + +```javascript +const base = { counter: 0 } + +const next = produce(base, function() { + this.counter++ +}) +console.log(next.counter) // 1 + +// OR +const increment = produce(function() { + this.counter++ +}) +console.log(increment(base).counter) // 1 +``` + +## TypeScript or Flow + +The Immer package ships with type definitions inside the package, which should be picked up by TypeScript and Flow out of the box and without further configuration. ## Immer on older JavaScript environments? @@ -216,6 +240,7 @@ Use `yarn test:perf` to reproduce them locally. ![performance.png](images/performance.png) Some observations: +* From `immer` perspective, this benchmark is a _worst case_ scenario, because the root collection it has to proxy is really large relatively to the rest of the data set. * The _mutate_, and _deepclone, mutate_ benchmarks establish a baseline on how expensive changing the data is, without immutability (or structural sharing in the deep clone case). * The _reducer_ and _naive reducer_ are implemented in typical Redux style reducers. The "smart" implementation slices the collection first, and then maps and freezes only the relevant todos. The "naive" implementation just maps over and processes the entire collection. * Immer with proxies is roughly speaking twice as slow as a hand written reducer. This is in practice negligible.