From 8b46e9305e4144bb7099cf4148243d44ec3ccf65 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Sun, 3 Feb 2019 18:31:19 +0100 Subject: [PATCH] docs: removed deprecation note on this, see #308 docs: add example of an `immerable` class --- __tests__/readme.js | 39 ++++++++++++++++++++++++++++++++++++++- readme.md | 2 -- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/__tests__/readme.js b/__tests__/readme.js index ef1c9c89c..cb90d880a 100644 --- a/__tests__/readme.js +++ b/__tests__/readme.js @@ -1,5 +1,5 @@ "use strict" -import produce, {applyPatches} from "../src/index" +import produce, {applyPatches, immerable} from "../src/index" describe("readme example", () => { it("works", () => { @@ -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") + }) }) diff --git a/readme.md b/readme.md index 916da5a2a..5d798a1f2 100644 --- a/readme.md +++ b/readme.md @@ -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: