scramble plugin names, -56
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 fdc78db · Browse files · 9 files·+57 added−95 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+57 added−95 removed
- tests:+4 added−1 removed
- other:+53 added−94 removed
Provenance
- Commit
037b474- Tree
abafb8b- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
fdc78db
9 files changed+57 added−95 removed
modified:__tests__/redux.ts+4 added-1 removed
3 unmodified lines456788910111213141516171883 unmodified lines3 unmodified lines applyPatches, Patch, nothing, Draft, Immutable Immutable, enableES5} from "../src/immer"import * as redux from "redux"
enableES5()
interface State { counter: number}
83 unmodified linesmodified:src/plugins/es5.ts+13 added-13 removed
23 unmodified lines24 25 type ES5State = ES5ArrayState | ES5ObjectState26 27 export function enableES5() {28- function willFinalizeES5(28+ function willFinalizeES5_(29 scope: ImmerScope,30 result: any,31 isReplaced: boolean32 ) {16 unmodified lines49 markChangesSweep(scope.drafts_)50 }51 }52 53- function createES5Proxy<T>(53+ function createES5Proxy_<T>(54 base: T,55 parent?: ImmerState56 ): Drafted<T, ES5ObjectState | ES5ArrayState> {57 const isArray = Array.isArray(base)55 unmodified lines113 assertUnrevoked(state)114 state.assigned_[prop] = true115 if (!state.modified_) {116 if (is(value, peek(latest(state), prop))) return117- markChangedES5(state)117+ markChangedES5_(state)118 prepareCopy(state)119 }120 // @ts-ignore121 state.copy_![prop] = value122 }123 124- function markChangedES5(state: ImmerState) {124+ function markChangedES5_(state: ImmerState) {125 if (!state.modified_) {126 state.modified_ = true127- if (state.parent_) markChangedES5(state.parent_)127+ if (state.parent_) markChangedES5_(state.parent_)128 }129 }130 131 function prepareCopy(state: ES5State) {48 unmodified lines180 const state: ES5State = drafts[i][DRAFT_STATE]181 if (!state.modified_) {182 switch (state.type_) {183 case ProxyTypeES5Array:184- if (hasArrayChanges(state)) markChangedES5(state)184+ if (hasArrayChanges(state)) markChangedES5_(state)185 break186 case ProxyTypeES5Object:187- if (hasObjectChanges(state)) markChangedES5(state)187+ if (hasObjectChanges(state)) markChangedES5_(state)188 break189 }190 }191 }14 unmodified lines206 if ((key as any) === DRAFT_STATE) return207 // The `undefined` check is a fast path for pre-existing keys.208 if ((base_ as any)[key] === undefined && !has(base_, key)) {209 assigned_[key] = true210- markChangedES5(state)210+ markChangedES5_(state)211 } else if (!assigned_[key]) {212 // Only untouched properties trigger recursion.213 markChangesRecursively(draft_[key])214 }2 unmodified lines217 each(base_, key => {218 // The `undefined` check is a fast path for pre-existing keys.219 if (draft_[key] === undefined && !has(draft_, key)) {220 assigned_[key] = false221- markChangedES5(state)221+ markChangedES5_(state)222 }223 })224 } else if (type_ === ProxyTypeES5Array) {225 if (hasArrayChanges(state as ES5ArrayState)) {226- markChangedES5(state)226+ markChangedES5_(state)227 assigned_.length = true228 }229 230 if (draft_.length < base_.length) {61 unmodified lines292 return false293 }294 295 loadPlugin("es5", {296- createES5Proxy,297- markChangedES5,298- willFinalizeES5296+ createES5Proxy_,297+ markChangedES5_,298+ willFinalizeES5_299 })300 }modified:src/plugins/mapset.ts+3 added-3 removed
186 unmodified lines187 188 return DraftMap189 })(Map)190 191- function proxyMap<T extends AnyMap>(target: T, parent?: ImmerState): T {191+ function proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T {192 // @ts-ignore193 return new DraftMap(target, parent)194 }195 118 unmodified lines314 315 return DraftSet316 })(Set)317 318- function proxySet<T extends AnySet>(target: T, parent?: ImmerState): T {318+ function proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T {319 // @ts-ignore320 return new DraftSet(target, parent)321 }322 12 unmodified lines335 })336 }337 }338 339- loadPlugin("mapset", {proxyMap, proxySet})339+ loadPlugin("mapset", {proxyMap_, proxySet_})340 }modified:src/plugins/patches.ts+3 added-3 removed
27 unmodified lines28 } from "../internal"29 import invariant from "tiny-invariant"30 31 export function enablePatches() {32- function generatePatches(32+ function generatePatches_(33 state: ImmerState,34 basePath: PatchPath,35 patches: Patch[],36 inversePatches: Patch[]153 unmodified lines190 i++191 })192 }193 194- function applyPatches<T>(draft: T, patches: Patch[]): T {194+ function applyPatches_<T>(draft: T, patches: Patch[]): T {195 patches.forEach(patch => {196 const {path, op} = patch197 198 let base: any = draft66 unmodified lines265 for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])266 return cloned267 }268 269- loadPlugin("patches", {applyPatches, generatePatches})269+ loadPlugin("patches", {applyPatches_, generatePatches_})270 }modified:src/finalize.ts+6 added-5 removed
5 unmodified lines6 PatchPath,7 each,8 has,9 freeze,10- generatePatches,10 shallowCopy,11 ImmerState,12 isDraft,13 SetState,14 set,15 is,16 get,18- willFinalize,17 ProxyTypeES5Object,18 ProxyTypeES5Array,21- ProxyTypeSet19+ ProxyTypeSet,20+ getPlugin21 } from "./internal"22 import invariant from "tiny-invariant"23 24 export function processResult(result: any, scope: ImmerScope) {25 const baseDraft = scope.drafts_![0]26 const isReplaced = result !== undefined && result !== baseDraft28- willFinalize(scope, result, isReplaced)27+ if (!scope.immer_.useProxies_)28+ getPlugin("es5").willFinalizeES5_(scope, result, isReplaced)29 if (isReplaced) {30 if (baseDraft[DRAFT_STATE].modified_) {31 scope.revoke_()32 invariant(false, "An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.") // prettier-ignore2 unmodified lines35 // Finalize the result in case it contains (or is) a subset of the draft.36 result = finalize(scope, result)37 if (!scope.parent_) maybeFreeze(scope, result)38 }39+ // TODO: move to plugin?40 if (scope.patches_) {41 scope.patches_.push({42 op: "replace",43 path: [],50 unmodified lines94 // everything inside is frozen, we can freeze here95 maybeFreeze(rootScope, result, false)96 // first time finalizing, let's create those patches97 if (path && rootScope.patches_) {97- generatePatches(98+ getPlugin("patches").generatePatches_(99 state,100 path,101 rootScope.patches_,102 rootScope.inversePatches_!50 unmodified linesmodified:src/immerClass.ts+8 added-20 removed
1 import {2- createES5Proxy,3- willFinalizeES5,4- markChangedES5,2 IProduceWithPatches,3 IProduce,4 ImmerState,5 Drafted,6 unmodified lines12 DRAFT_STATE,13 Draft,14 PatchListener,15 isDraft,19- applyPatches,16 isMap,21- proxyMap,17 isSet,23- proxySet,18 markChangedProxy,19 createProxyProxy20 } from "./internal"21 import invariant from "tiny-invariant"22 import {freeze} from "./common"23+import {getPlugin} from "./plugins"24 25 declare const __DEV__: boolean26 /* istanbul ignore next */27 function verifyMinified() {}170 unmodified lines198 break199 }200 }201 202+ const applyPatchesImpl = getPlugin("patches").applyPatches_203 if (isDraft(base)) {204 // N.B: never hits if some patch a replacement, patches are never drafts209- return applyPatches(base, patches)205+ return applyPatchesImpl(base, patches)206 }207 // Otherwise, produce a copy of the base state.208 return this.produce(base, (draft: Drafted) =>213- applyPatches(draft, patches.slice(i + 1))209+ applyPatchesImpl(draft, patches.slice(i + 1))210 )211 }212 }213 3 unmodified lines217 parent?: ImmerState218 ): Drafted<T, ImmerState> {219 // precondition: createProxy should be guarded by isDraftable, so we know we can safely draft220 const draft: Drafted = isMap(value)225- ? proxyMap(value, parent)221+ ? getPlugin("mapset").proxyMap_(value, parent)222 : isSet(value)227- ? proxySet(value, parent)223+ ? getPlugin("mapset").proxySet_(value, parent)224 : immer.useProxies_225 ? createProxyProxy(value, parent)230- : createES5Proxy(value, parent)226+ : getPlugin("es5").createES5Proxy_(value, parent)227 228 const scope = parent ? parent.scope_ : ImmerScope.current_!229 scope.drafts_.push(draft)230 return draft231 }232 237-export function willFinalize(238- scope: ImmerScope,239- thing: any,240- isReplaced: boolean241-) {242- if (!scope.immer_.useProxies_) willFinalizeES5(scope, thing, isReplaced)243-}244-233 export function markChanged(immer: Immer, state: ImmerState) {234 if (immer.useProxies_) {235 markChangedProxy(state)236 } else {249- markChangedES5(state)237+ getPlugin("es5").markChangedES5_(state)238 }239 }modified:src/internal.ts+1 added-1 removed
1234567899export * from "./env"export * from "./types-external"export * from "./types-internal"export * from "./common"export * from "./plugins"export * from "./scope"export * from "./finalize"export * from "./proxy"export * from "./immerClass"export * from "./plugins"modified:src/plugins.ts+15 added-47 removed
15 unmodified lines16 17 /** Plugin utilities */18 const plugins: {19 patches?: {20- generatePatches: typeof generatePatches21- applyPatches: typeof applyPatches20+ generatePatches_(21+ state: ImmerState,22+ basePath: PatchPath,23+ patches: Patch[],24+ inversePatches: Patch[]25+ ): void26+ applyPatches_<T>(draft: T, patches: Patch[]): T27 }28 es5?: {24- willFinalizeES5: typeof willFinalizeES525- createES5Proxy: typeof createES5Proxy26- markChangedES5: typeof markChangedES529+ willFinalizeES5_(scope: ImmerScope, result: any, isReplaced: boolean): void30+ createES5Proxy_<T>(31+ base: T,32+ parent?: ImmerState33+ ): Drafted<T, ES5ObjectState | ES5ArrayState>34+ markChangedES5_(state: ImmerState): void35 }36 mapset?: {29- proxyMap: typeof proxyMap30- proxySet: typeof proxySet37+ proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T38+ proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T39 }40 } = {}41 42 type Plugins = typeof plugins42 unmodified lines85 base_: AnyArray86 copy_: AnyArray | null87 }88 81-export function willFinalizeES5(82- scope: ImmerScope,83- result: any,84- isReplaced: boolean85-) {86- getPlugin("es5").willFinalizeES5(scope, result, isReplaced)87-}88-89-export function createES5Proxy<T>(90- base: T,91- parent?: ImmerState92-): Drafted<T, ES5ObjectState | ES5ArrayState> {93- return getPlugin("es5").createES5Proxy(base, parent)94-}95-96-export function markChangedES5(state: ImmerState) {97- getPlugin("es5").markChangedES5(state)98-}99-89 /** Map / Set plugin */90 91 export interface MapState extends ImmerBaseState {92 type_: typeof ProxyTypeMap12 unmodified lines105 revoked_: boolean106 draft_: Drafted<AnySet, SetState>107 }108 120-export function proxyMap<T extends AnyMap>(target: T, parent?: ImmerState): T {121- return getPlugin("mapset").proxyMap(target, parent)122-}123-124-export function proxySet<T extends AnySet>(target: T, parent?: ImmerState): T {125- return getPlugin("mapset").proxySet(target, parent)126-}127-109 /** Patches plugin */110 111 export type PatchPath = (string | number)[]131-132-export function generatePatches(133- state: ImmerState,134- basePath: PatchPath,135- patches: Patch[],136- inversePatches: Patch[]137-): void {138- getPlugin("patches").generatePatches(state, basePath, patches, inversePatches)139-}140-141-export function applyPatches<T>(draft: T, patches: Patch[]): T {142- return getPlugin("patches").applyPatches(draft, patches)143-}modified:notes.txt+4 added-2 removed
6 unmodified lines7 [x] _ postfix all members8 [x] clean up finalize9 [x] remove die10 [ ] own invariant11-[ ] optimize enumerations11 [ ] update docs12 [ ] plugin tests13 [ ] check if we can create more local vars15-[ ] verify performance14+[x] verify performance15 [ ] kill ownKeys?16 [x] kill enumerations17 [ ] check todos18 [ ] kill marchChangesSweep recursive if possible and no perf impact83 unmodified lines102 │ enableMapSet │ 4071 │ 4847 │ 792 │103 │ enablePatches │ 3936 │ 5528 │ 681 │104 └───────────────────────┴───────────┴────────────┴───────────┘105 (this report was generated by npmjs.com/package/import-size)106+107+Minimize plugin api's:108+ 3196