Skip to content

Commit

Permalink
docs: removed deprecation note on this, see #308
Browse files Browse the repository at this point in the history
docs: add example of an `immerable` class
  • Loading branch information
mweststrate committed Apr 15, 2019
1 parent 9363313 commit 8b46e93
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
39 changes: 38 additions & 1 deletion __tests__/readme.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use strict"
import produce, {applyPatches} from "../src/index"
import produce, {applyPatches, immerable} from "../src/index"

describe("readme example", () => {
it("works", () => {
Expand Down Expand Up @@ -146,4 +146,41 @@ describe("readme example", () => {
users: new Map([["michel", {name: "michel"}]])
})
})

it("supports immerable", () => {
class Clock {
constructor(hours = 0, minutes = 0) {
this.hours = hours
this.minutes = minutes
}

increment(hours, minutes = 0) {
return produce(this, d => {
d.hours += hours
d.minutes += minutes
})
}

toString() {
return `${("" + this.hours).padStart(2, 0)}:${(
"" + this.minutes
).padStart(2, 0)}`
}
}
Clock[immerable] = true

const midnight = new Clock()
const lunch = midnight.increment(12, 30)

expect(midnight).not.toBe(lunch)
expect(lunch).toBeInstanceOf(Clock)
expect(midnight.toString()).toBe("00:00")
expect(lunch.toString()).toBe("12:30")

const diner = lunch.increment(6)

expect(diner).not.toBe(lunch)
expect(lunch).toBeInstanceOf(Clock)
expect(diner.toString()).toBe("18:30")
})
})
2 changes: 0 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,6 @@ This ensures that the only place you can modify your state is in your produce ca

## Using `this`

_Deprecated, this will probably be removed in a next major version, see [#308](https://github.com/mweststrate/immer/issues/308)_

The recipe will be always invoked with the `draft` as `this` context.

This means that the following constructions are also valid:
Expand Down

0 comments on commit 8b46e93

Please sign in to comment.