Skip to content
Myr

Add original function

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by RichieAHB · committed by RichieAHB · parent 211f5cf · Browse files · 8 files+74 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+74 added−5 removed

  • tests:+52 added−2 removed
  • other:+22 added−3 removed

8 files+74 added−5 removed

modified:__tests__/flow/flow.js.flow+17 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 original(draftState).x101 // $ExpectError102 original(draftState).y103 })104}105106{107 produce([1], draftState => {108 const a: number = original(draftState)[0];109 // $ExpectError110 const b: string = original(draftState)[0];111 })112}
modified:__tests__/flow/ts.ts+13 added-1 removed
added:__tests__/original.js+22 added
modified:src/common.js+4 added
54 unmodified lines
55 }56 return value57}5859export function original(value) {60 return value && value[PROXY_STATE] ? value[PROXY_STATE].base : value61}6263const assign =64 Object.assign ||65 function assign(target, value) {66 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
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`. 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