Skip to content
Myr

Processed some own review comments

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

authored by Michel Weststrate · committed by Michel Weststrate · parent c45f835 · Browse files · 7 files+12 added−85 removed
This commit

State

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

Diff stats

7 files+12 added−85 removed

  • tests:+0 added−2 removed
  • other:+12 added−83 removed

7 files+12 added−85 removed

modified:__tests__/manual.js-1 removed
modified:__tests__/patch.js-1 removed
5 unmodified lines
6 enableAllPlugins,7 isDraft,8 immerable9} from "../src/immer"10import {run} from "jest"1011enableAllPlugins()1213jest.setTimeout(1000)
1108 unmodified lines
modified:src/core/current.ts+2 added-2 removed
8 unmodified lines
9 ImmerState,10 isDraftable,11 ArchtypeMap,12 ArchtypeSet,13 getArchtype13 getArchtype,14 getPlugin15} from "../internal"15import {getPlugin} from "../utils/plugins"1617export function current<T>(value: T): T18export function current(value: any): any {19 if (!isDraft(value)) die(22, value)
41 unmodified lines
modified:src/core/finalize.ts+1 added-7 removed
18 unmodified lines
19 revokeScope,20 isFrozen,21 shallowCopy22} from "../internal"23import {ProxyTypeProxyArray} from "../types/types-internal"2324export function processResult(result: any, scope: ImmerScope) {25 scope.unfinalizedDrafts_ = scope.drafts_.length26 const baseDraft = scope.drafts_![0]
59 unmodified lines
86 // For ES5, create a good copy from the draft first, with added keys and without deleted keys.87 state.type_ === ProxyTypeES5Object || state.type_ === ProxyTypeES5Array88 ? (state.copy_ = shallowCopy(state.draft_))89 : state.copy_91 const originalCopy =92 state.type_ === ProxyTypeES5Array || state.type_ === ProxyTypeProxyArray93 ? result.slice()94 : undefined90 // finalize all children of the copy91 each(result as any, (key, childValue) =>92 finalizeProperty(rootScope, state, result, key, childValue, path)93 )
4 unmodified lines
98 getPlugin("Patches").generatePatches_(99 state,100 path,101 rootScope.patches_,107 rootScope.inversePatches_!,108 originalCopy102 rootScope.inversePatches_!103 )104 }105 }106 return state.copy_
50 unmodified lines
modified:src/plugins/es5.ts+2 added-2 removed
110 unmodified lines
111 configurable: true,112 enumerable,113 get(this: any) {114 const state = this[DRAFT_STATE]115 assertUnrevoked(state)115 if (__DEV__) assertUnrevoked(state)116 // @ts-ignore117 return objectTraps.get(state, prop)118 },119 set(this: any, value) {120 const state = this[DRAFT_STATE]121 assertUnrevoked(state)121 if (__DEV__) assertUnrevoked(state)122 // @ts-ignore123 objectTraps.set(state, prop, value)124 }125 }
141 unmodified lines
modified:src/plugins/patches.ts+6 added-70 removed
22 unmodified lines
23 ProxyTypeSet,24 ArchtypeMap,25 ArchtypeSet,26 ArchtypeArray,27 die27 die,28 isDraft29} from "../internal"29import {isDraft} from "../utils/common"30import {DRAFT_STATE} from "../utils/env"3031export function enablePatches() {32 const REPLACE = "replace"33 const ADD = "add"
2 unmodified lines
36 function generatePatches_(37 state: ImmerState,38 basePath: PatchPath,39 patches: Patch[],41 inversePatches: Patch[],42 originalCopy: any40 inversePatches: Patch[]41 ): void {42 switch (state.type_) {43 case ProxyTypeProxyObject:44 case ProxyTypeES5Object:
5 unmodified lines
50 inversePatches51 )52 case ProxyTypeES5Array:53 case ProxyTypeProxyArray:56 return generateArrayPatches(57 state,58 basePath,59 patches,60 inversePatches,61 originalCopy62 )54 return generateArrayPatches(state, basePath, patches, inversePatches)55 case ProxyTypeSet:56 return generateSetPatches(57 (state as any) as SetState,58 basePath,
6 unmodified lines
65 function generateArrayPatches(66 state: ES5ArrayState | ProxyArrayState,67 basePath: PatchPath,68 patches: Patch[],77 inversePatches: Patch[],78 originalCopy: any[]69 inversePatches: Patch[]70 ) {80 // TODO: we need originalCopy to determine the indices that remained71 let {base_, assigned_} = state72 let copy_ = state.copy_!7374 // Reduce complexity by ensuring `base` is never longer.
2 unmodified lines
77 ;[base_, copy_] = [copy_, base_]78 ;[patches, inversePatches] = [inversePatches, patches]79 }8091 // for (let i = 0; i < base_.length; i++) {92 // const origValue = base_[i];93 // const value = copy_[i];94 // if (assigned_[i]) {95 // const path = basePath.concat([i]);96 // patches.push({97 // op: REPLACE,98 // path,99 // value: clonePatchValueIfNeeded(value)100 // })101 // inversePatches.push({102 // op: REPLACE,103 // path,104 // value: clonePatchValueIfNeeded(origValue)105 // })106 // }107 // }108 // if (base_.length !== copy_.length) {109 // inversePatches.push({110 // op: REPLACE,111 // path: basePath.concat(["length"]),112 // value: base_.length113 // })114 // for (let i = base_.length; i < copy_.length; i++) {115116 // }117 // }118 // if (copy_.length < base_.length) {119 // const path =120 // patches.push({})121 // }122123 // const delta = copy_.length - base_.length124125 // // Find the first replaced index.126 // let start = 0127 // while (128 // (base_[start] === copy_[start] ||129 // base_[start] === copy_[start]?.[DRAFT_STATE]?.base) &&130 // start < base_.length131 // ) {132 // ++start133 // }134135 // // Find the last replaced index. Search from the end to optimize splice patches.136 // let end = base_.length137 // while (end > start && base_[end - 1] === copy_[end + delta - 1]) {138 // --end139 // }14081 // Process replaced indices.82 for (let i = 0; i < base_.length; i++) {143 const draft = originalCopy[i] && originalCopy[i][DRAFT_STATE]144 if (145 assigned_[i] &&146 (draft ? draft.base_ !== base_[i] : copy_[i] !== base_[i])147 ) {83 if (assigned_[i] && copy_[i] !== base_[i]) {84 const path = basePath.concat([i])85 patches.push({86 op: REPLACE,87 path,
206 unmodified lines
modified:src/utils/plugins.ts+1 added-2 removed
19 unmodified lines
20 generatePatches_(21 state: ImmerState,22 basePath: PatchPath,23 patches: Patch[],24 inversePatches: Patch[],25 originalCopy: any24 inversePatches: Patch[]25 ): void26 generateReplacementPatches_(27 rootState: ImmerState,28 replacement: any,
84 unmodified lines