Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an original function #179

Merged
merged 3 commits into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion __tests__/flow/flow.js.flow
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import produce, {setAutoFreeze, setUseProxies, produce as produce2, applyPatches, Patch} from "../../src/immer"
import produce, {setAutoFreeze, setUseProxies, produce as produce2, applyPatches, Patch, original} from "../../src/immer"

setAutoFreeze(true)
setUseProxies(true)
Expand Down Expand Up @@ -93,4 +93,26 @@ produce({x: 3}, [])
p = patches
})
applyPatches({}, p)
}

{
produce({x: 3}, draftState => {
let a = original(draftState)
if (a) {
a.x
// $ExpectError
a.y
}
})
}

{
produce([1], draftState => {
const a = original(draftState);
if (a) {
const b: number = a[0];
// $ExpectError
const c: string = a[0];
}
})
}
21 changes: 20 additions & 1 deletion __tests__/flow/ts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import produce, {setAutoFreeze, setUseProxies} from "../../src/immer"
import produce, {setAutoFreeze, setUseProxies, original} from "../../src/immer"

setAutoFreeze(true)
setUseProxies(true)
Expand Down Expand Up @@ -77,3 +77,22 @@ produce({x: 3}, [])
? produce<any, any>(handlers[type])(state, payload)
: state
}

produce({x: 3, z: {}}, draftState => {
const a = draftState;

if (a) {
a.x
// $ExpectError
a.y
}
})

produce([1], draftState => {
const a = original(draftState);
if (a) {
// $ExpectError
const b: string = a[0];
const c: number = a[0];
}
})
49 changes: 49 additions & 0 deletions __tests__/original.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict"
import produce, {original, setUseProxies} from "../src/immer"

describe("original", () => {
const baseState = {
a: [],
b: {}
}

it("should return the original from the draft", () => {
setUseProxies(true)

produce(baseState, draftState => {
expect(original(draftState)).toBe(baseState)
expect(original(draftState.a)).toBe(baseState.a)
expect(original(draftState.b)).toBe(baseState.b)
})

setUseProxies(false)

produce(baseState, draftState => {
expect(original(draftState)).toBe(baseState)
expect(original(draftState.a)).toBe(baseState.a)
expect(original(draftState.b)).toBe(baseState.b)
})
})

it("should return the original from the proxy", () => {
produce(baseState, draftState => {
expect(original(draftState)).toBe(baseState)
expect(original(draftState.a)).toBe(baseState.a)
expect(original(draftState.b)).toBe(baseState.b)
})
})

it("should return undefined for new values on the draft", () => {
produce(baseState, draftState => {
draftState.c = {}
draftState.d = 3
expect(original(draftState.c)).toBeUndefined()
expect(original(draftState.d)).toBeUndefined()
})
})

it("should return undefined for an object that is not proxied", () => {
expect(original({})).toBeUndefined()
expect(original(3)).toBeUndefined()
})
})
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,17 @@ const userReducer = produce((draft, action) => {
})
```

## Extracting the original object from a proxied instance

Immer exposes a named export `original` that will get the original object from the proxied instance inside `produce` (or return `undefined` for unproxied values). A good example of when this can be useful is when searching for nodes in a tree-like state using strict equality.

```js
const baseState = { users: [{ name: "Richie" }] };
const nextState = produce(baseState, draftState => {
original(draftState.users) // is === baseState.users
})
```

## Using `this`

The recipe will be always invoked with the `draft` as `this` context.
Expand Down
7 changes: 7 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ export function freeze(value) {
return value
}

export function original(value) {
if (value && value[PROXY_STATE]) {
return value[PROXY_STATE].base
}
// otherwise return undefined
}

const assign =
Object.assign ||
function assign(target, value) {
Expand Down
4 changes: 3 additions & 1 deletion src/immer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,6 @@ export function setAutoFreeze(autoFreeze: boolean): void
*/
export function setUseProxies(useProxies: boolean): void

export function applyPatches<S>(state: S, patches: Patch[]): S
export function applyPatches<S>(state: S, patches: Patch[]): S

export function original<T>(value: T): T | void
2 changes: 1 addition & 1 deletion src/immer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {setAutoFreeze, setUseProxies} from "./common"
export {setAutoFreeze, setUseProxies, original} from "./common"

import {applyPatches as applyPatchesImpl} from "./patches"
import {isProxyable, getUseProxies} from "./common"
Expand Down
4 changes: 3 additions & 1 deletion src/immer.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,6 @@ declare export function setAutoFreeze(autoFreeze: boolean): void
*/
declare export function setUseProxies(useProxies: boolean): void

declare export function applyPatches<S>(state: S, patches: Patch[]): S
declare export function applyPatches<S>(state: S, patches: Patch[]): S

declare export function original<S>(value: S): ?S