Skip to content

Commit

Permalink
Merge branch 'master' into feature/curry
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Jan 9, 2018
2 parents 821cc3d + e3eacea commit 946a2b9
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 70 deletions.
12 changes: 6 additions & 6 deletions __performance_tests__/todo.js
@@ -1,6 +1,6 @@
"use strict"
import immerProxy, {setAutoFreeze as setAutoFreezeProxy} from ".."
import immerEs5, {setAutoFreeze as setAutoFreezeEs5} from "../es5"
import produceProxy, {setAutoFreeze as setAutoFreezeProxy} from ".."
import produceEs5, {setAutoFreeze as setAutoFreezeEs5} from "../es5"
import cloneDeep from "lodash.clonedeep"
import {List, Record} from "immutable"
import deepFreeze from "deep-freeze"
Expand Down Expand Up @@ -143,7 +143,7 @@ describe("performance", () => {

measure("immer (proxy) - without autofreeze", () => {
setAutoFreezeProxy(false)
immerProxy(baseState, draft => {
produceProxy(baseState, draft => {
for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {
draft[i].done = true
}
Expand All @@ -153,7 +153,7 @@ describe("performance", () => {

measure("immer (proxy) - with autofreeze", () => {
setAutoFreezeProxy(true)
immerProxy(frozenBazeState, draft => {
produceProxy(frozenBazeState, draft => {
for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {
draft[i].done = true
}
Expand All @@ -162,7 +162,7 @@ describe("performance", () => {

measure("immer (es5) - without autofreeze", () => {
setAutoFreezeEs5(false)
immerEs5(baseState, draft => {
produceEs5(baseState, draft => {
for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {
draft[i].done = true
}
Expand All @@ -172,7 +172,7 @@ describe("performance", () => {

measure("immer (es5) - with autofreeze", () => {
setAutoFreezeEs5(true)
immerEs5(frozenBazeState, draft => {
produceEs5(frozenBazeState, draft => {
for (let i = 0; i < MAX * MODIFY_FACTOR; i++) {
draft[i].done = true
}
Expand Down
56 changes: 28 additions & 28 deletions __tests__/base.js
Expand Up @@ -10,7 +10,7 @@ runBaseTest("es5 (autofreeze)", immerEs5, true)

function runBaseTest(name, lib, freeze) {
describe(`base functionality - ${name}`, () => {
const immer = lib.default
const produce = lib.default
let baseState
let origBaseState

Expand All @@ -20,12 +20,12 @@ function runBaseTest(name, lib, freeze) {
})

it("should return the original without modifications", () => {
const nextState = immer(baseState, () => {})
const nextState = produce(baseState, () => {})
expect(nextState).toBe(baseState)
})

it("should return the original without modifications when reading stuff", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
expect(s.aProp).toBe("hi")
expect(s.anObject.nested).toMatchObject({yummie: true})
})
Expand All @@ -34,17 +34,17 @@ function runBaseTest(name, lib, freeze) {

it("should not return any value: thunk", () => {
const warning = jest.spyOn(console, "warn")
immer(baseState, () => ({bad: "don't do this"}))
immer(baseState, () => [1, 2, 3])
immer(baseState, () => false)
immer(baseState, () => "")
produce(baseState, () => ({bad: "don't do this"}))
produce(baseState, () => [1, 2, 3])
produce(baseState, () => false)
produce(baseState, () => "")

expect(warning).toHaveBeenCalledTimes(4)
warning.mockClear()
})

it("should return a copy when modifying stuff", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
s.aProp = "hello world"
})
expect(nextState).not.toBe(baseState)
Expand All @@ -57,7 +57,7 @@ function runBaseTest(name, lib, freeze) {
if (
("should preserve type",
() => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
expect(Array.isArray(s)).toBe(true)
expect(s.protoType).toBe(Object)
s.anArray.push(3)
Expand All @@ -70,7 +70,7 @@ function runBaseTest(name, lib, freeze) {
})
)
it("deep change bubbles up", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
s.anObject.nested.yummie = false
})
expect(nextState).not.toBe(baseState)
Expand All @@ -81,7 +81,7 @@ function runBaseTest(name, lib, freeze) {
})

it("can add props", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
s.anObject.cookie = {tasty: true}
})
expect(nextState).not.toBe(baseState)
Expand All @@ -91,7 +91,7 @@ function runBaseTest(name, lib, freeze) {
})

it("can delete props", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
delete s.anObject.nested
})
expect(nextState).not.toBe(baseState)
Expand All @@ -100,14 +100,14 @@ function runBaseTest(name, lib, freeze) {
})

it("ignores single non-modification", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
s.aProp = "hi"
})
expect(nextState).toBe(baseState)
})

it("processes single modification", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
s.aProp = "hello"
s.aProp = "hi"
})
Expand All @@ -116,15 +116,15 @@ function runBaseTest(name, lib, freeze) {
})

it("should support reading arrays", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
s.anArray.slice()
})
expect(nextState.anArray).toBe(baseState.anArray)
expect(nextState).toBe(baseState)
})

it("should support changing arrays", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
s.anArray[3] = true
})
expect(nextState).not.toBe(baseState)
Expand All @@ -133,7 +133,7 @@ function runBaseTest(name, lib, freeze) {
})

it("should support changing arrays - 2", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
s.anArray.splice(1, 1, "a", "b")
})
expect(nextState).not.toBe(baseState)
Expand All @@ -149,7 +149,7 @@ function runBaseTest(name, lib, freeze) {
})

it("can delete array items", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
s.anArray.length = 3
})
expect(nextState).not.toBe(baseState)
Expand All @@ -159,7 +159,7 @@ function runBaseTest(name, lib, freeze) {
})

it("should support sorting arrays", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
s.anArray[2].c = 4
s.anArray.sort()
s.anArray[3].c = 5
Expand All @@ -170,7 +170,7 @@ function runBaseTest(name, lib, freeze) {
})

it("should expose property descriptors", () => {
const nextState = immer([], s => {
const nextState = produce([], s => {
expect(Object.getOwnPropertyDescriptor(s, 0)).toBe(undefined)
s.unshift("x")
expect(Object.getOwnPropertyDescriptor(s, 0)).toEqual({
Expand All @@ -192,7 +192,7 @@ function runBaseTest(name, lib, freeze) {
})

it("should support sorting arrays - 2", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
s.anArray.unshift("x")
s.anArray[3].c = 4
s.anArray.sort()
Expand All @@ -212,7 +212,7 @@ function runBaseTest(name, lib, freeze) {
})

it("should updating inside arrays", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
s.anArray[2].test = true
})
expect(nextState).not.toBe(baseState)
Expand All @@ -221,7 +221,7 @@ function runBaseTest(name, lib, freeze) {
})

it("reusing object should work", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
const obj = s.anObject
delete s.anObject
s.messy = obj
Expand All @@ -242,7 +242,7 @@ function runBaseTest(name, lib, freeze) {
})

it("refs should be transparent", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
const obj = s.anObject
s.aProp = "hello"
delete s.anObject
Expand All @@ -266,7 +266,7 @@ function runBaseTest(name, lib, freeze) {
})

it("should allow setting to undefined a defined draft property", () => {
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
s.aProp = undefined
})
expect(nextState).not.toBe(baseState)
Expand All @@ -278,7 +278,7 @@ function runBaseTest(name, lib, freeze) {
if (name === "proxy") {
it("should revoke the proxy of the baseState after immer function is executed", () => {
let proxy
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
proxy = s
s.aProp = "hello"
})
Expand All @@ -301,7 +301,7 @@ function runBaseTest(name, lib, freeze) {

it("should revoke the proxy of the baseState after immer function is executed - 2", () => {
let proxy
const nextState = immer(baseState, s => {
const nextState = produce(baseState, s => {
proxy = s.anObject
})
expect(nextState).toBe(baseState)
Expand All @@ -315,7 +315,7 @@ function runBaseTest(name, lib, freeze) {
})

it("should reflect all changes made in the draft immediately", () => {
immer(baseState, draft => {
produce(baseState, draft => {
draft.anArray[0] = 5
draft.anArray.unshift("test")
// sliced here; jest will also compare non-enumerable keys, which would include the immer Symbols
Expand Down
8 changes: 4 additions & 4 deletions __tests__/frozen.js
Expand Up @@ -8,15 +8,15 @@ runTests("es5", immerEs5)

function runTests(name, lib) {
describe("auto freeze - " + name, () => {
const immer = lib.default
const produce = lib.default
const baseState = {
object: {a: 1},
array: [1, 2]
}

it("should freeze objects after modifications", () => {
expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen
const next = immer(baseState, draft => {
const next = produce(baseState, draft => {
draft.object.c = 2
})
expect(Object.isFrozen(next.object)).toBe(true)
Expand All @@ -30,7 +30,7 @@ function runTests(name, lib) {

it("should freeze arrays after modifications", () => {
expect(Object.isFrozen(baseState.object)).toBe(false) // initially not frozen
const next = immer(baseState, draft => {
const next = produce(baseState, draft => {
draft.array.push(3)
})
expect(Object.isFrozen(next.object)).toBe(false) // not touched
Expand All @@ -47,7 +47,7 @@ function runTests(name, lib) {
const b = {a: a}
Object.freeze(a)
Object.freeze(b)
const n = immer(b, draft => {
const n = produce(b, draft => {
draft.c = true
draft.a.push(3)
})
Expand Down
4 changes: 2 additions & 2 deletions __tests__/readme.js
@@ -1,5 +1,5 @@
"use strict"
import immer from ".."
import produce from ".."

describe("readme example", () => {
it("works", () => {
Expand All @@ -14,7 +14,7 @@ describe("readme example", () => {
}
]

const nextState = immer(baseState, draft => {
const nextState = produce(baseState, draft => {
draft.push({todo: "Tweet about it"})
draft[1].done = true
})
Expand Down
5 changes: 5 additions & 0 deletions changelog.md
@@ -1,4 +1,9 @@
# Changelog

### 0.3.1

* Republished, somehow NPM still returned 0.2.2

### 0.3.0 (8-1-2018)

* Increased performance of the proxy-based implementation by a factor 2x - 3x. [#38](https://github.com/mweststrate/immer/pull/38)
Expand Down
Binary file modified images/performance.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/proxy.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/tree.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "immer",
"version": "0.3.0",
"version": "0.3.1",
"description": "Create your next immutable state by mutating the current one",
"main": "immer.js",
"types": "./index.d.ts",
Expand Down

0 comments on commit 946a2b9

Please sign in to comment.