Skip to content
Myr

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

8 files+117 added−5 removed

modified:__tests__/flow/flow.js.flow+23 added-1 removed
1// @flow2import produce, {setAutoFreeze, setUseProxies, produce as produce2, applyPatches, Patch} from "../../src/immer"2import produce, {setAutoFreeze, setUseProxies, produce as produce2, applyPatches, Patch, original} from "../../src/immer"34setAutoFreeze(true)5setUseProxies(true)6
85 unmodified lines
92 produce({ }, d=> {}, (patches, inverse) => {93 p = patches94 })95 applyPatches({}, p)96}9798{99 produce({x: 3}, draftState => {100 let a = original(draftState)101 if (a) {102 a.x103 // $ExpectError104 a.y105 }106 })107}108109{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
added:__tests__/original.js+49 added
modified:src/common.js+7 added
54 unmodified lines
55 }56 return value57}5859export function original(value) {60 if (value && value[PROXY_STATE]) {61 return value[PROXY_STATE].base62 }63 // otherwise return undefined64}6566const assign =67 Object.assign ||68 function assign(target, value) {69 for (let key in value) {
106 unmodified lines
modified:src/immer.d.ts+3 added-1 removed
107 unmodified lines
108 * By default done by using feature detection109 */110export function setUseProxies(useProxies: boolean): void111112export function applyPatches<S>(state: S, patches: Patch[]): S112export function applyPatches<S>(state: S, patches: Patch[]): S113114export function original<T>(value: T): T | void
modified:src/immer.js+1 added-1 removed
modified:src/immer.js.flow+3 added-1 removed
81 unmodified lines
82 * By default done by using feature detection83 */84declare export function setUseProxies(useProxies: boolean): void8586declare export function applyPatches<S>(state: S, patches: Patch[]): S86declare export function applyPatches<S>(state: S, patches: Patch[]): S8788declare export function original<S>(value: S): ?S
modified:readme.md+11 added
349 unmodified lines
350 }351})352```353354## Extracting the original object from a proxied instance355356Immer 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.357358```js359const baseState = { users: [{ name: "Richie" }] };360const nextState = produce(baseState, draftState => {361 original(draftState.users) // is === baseState.users362})363```364365## Using `this`366367The recipe will be always invoked with the `draft` as `this` context.368
239 unmodified lines