Merge pull request #179 from RichieAHB/rahb/add-original
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add an original function
authored by Michel Weststrate · committed by GitHub · parents 2897e12, c16b5b0 · Browse files · 8 files·+117 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+117 added−5 removed
- tests:+92 added−2 removed
- other:+25 added−3 removed
Provenance
- Commit
f8479c2- Tree
15f5e95- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
2897e12- Parent 2
c16b5b0
8 files changed+117 added−5 removed
modified:__tests__/flow/flow.js.flow+23 added-1 removed
1 // @flow2-import produce, {setAutoFreeze, setUseProxies, produce as produce2, applyPatches, Patch} from "../../src/immer"2+import produce, {setAutoFreeze, setUseProxies, produce as produce2, applyPatches, Patch, original} from "../../src/immer"3 4 setAutoFreeze(true)5 setUseProxies(true)6 85 unmodified lines92 produce({ }, d=> {}, (patches, inverse) => {93 p = patches94 })95 applyPatches({}, p)96+}97+98+{99+ produce({x: 3}, draftState => {100+ let a = original(draftState)101+ if (a) {102+ a.x103+ // $ExpectError104+ a.y105+ }106+ })107+}108+109+{110+ produce([1], draftState => {111+ const a = original(draftState);112+ if (a) {113+ const b: number = a[0];114+ // $ExpectError115+ const c: string = a[0];116+ }117+ })118 }modified:__tests__/flow/ts.ts+20 added-1 removed
11234570 unmodified lines7677787980818283848586878889909192939495969798import produce, {setAutoFreeze, setUseProxies} from "../../src/immer"import produce, {setAutoFreeze, setUseProxies, original} from "../../src/immer"
setAutoFreeze(true)setUseProxies(true)
70 unmodified lines handlers[type] ? 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]; }})added:__tests__/original.js+49 added
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849"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() })})modified:src/common.js+7 added
54 unmodified lines55 }56 return value57 }58 59+export function original(value) {60+ if (value && value[PROXY_STATE]) {61+ return value[PROXY_STATE].base62+ }63+ // otherwise return undefined64+}65+66 const assign =67 Object.assign ||68 function assign(target, value) {69 for (let key in value) {106 unmodified linesmodified:src/immer.d.ts+3 added-1 removed
107 unmodified lines108 * By default done by using feature detection109 */110 export function setUseProxies(useProxies: boolean): void111 112-export function applyPatches<S>(state: S, patches: Patch[]): S112+export function applyPatches<S>(state: S, patches: Patch[]): S113+114+export function original<T>(value: T): T | voidmodified:src/immer.js+1 added-1 removed
11234564 unmodified linesexport {setAutoFreeze, setUseProxies} from "./common"export {setAutoFreeze, setUseProxies, original} from "./common"
import {applyPatches as applyPatchesImpl} from "./patches"import {isProxyable, getUseProxies} from "./common"import {produceProxy} from "./proxy"64 unmodified linesmodified:src/immer.js.flow+3 added-1 removed
81 unmodified lines82 * By default done by using feature detection83 */84 declare export function setUseProxies(useProxies: boolean): void85 86-declare export function applyPatches<S>(state: S, patches: Patch[]): S86+declare export function applyPatches<S>(state: S, patches: Patch[]): S87+88+declare export function original<S>(value: S): ?Smodified:readme.md+11 added
349 unmodified lines350 }351 })352 ```353 354+## Extracting the original object from a proxied instance355+356+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.357+358+```js359+const baseState = { users: [{ name: "Richie" }] };360+const nextState = produce(baseState, draftState => {361+ original(draftState.users) // is === baseState.users362+})363+```364+365 ## Using `this`366 367 The recipe will be always invoked with the `draft` as `this` context.368 239 unmodified lines