Some minor cleanup
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 ebda6f8 · Browse files · 4 files·+32 added−53 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+32 added−53 removed
- other:+32 added−53 removed
Provenance
- Commit
718b7f6- Tree
b2b9327- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
ebda6f8
4 files changed+32 added−53 removed
modified:src/plugins/es5.ts+13 added-4 removed
6 unmodified lines7 each,8 has,9 isDraft,10 isDraftable,11- isEnumerable,11 shallowCopy,12 latest,14- createHiddenProperty,13 DRAFT_STATE,14 assertUnrevoked,15 is,16 loadPlugin,17 ImmerScope,18 createProxy,19 ProxyTypeES5Array,22- ProxyTypeES5Object20+ ProxyTypeES5Object,21+ AnyObject22 } from "../internal"23 24 type ES5State = ES5ArrayState | ES5ObjectState25 48 unmodified lines74 revoked_: false,75 isManual_: false76 }77 79- createHiddenProperty(draft, DRAFT_STATE, state)78+ Object.defineProperty(draft, DRAFT_STATE, {79+ value: state,80+ // enumerable: false <- the default81+ writable: true82+ })83 return draft84 }85 86 // Access a property without creating an Immer draft.207 unmodified lines294 // For all other cases, we don't have to compare, as they would have been picked up by the index setters295 return false296 }297 298+ /*#__PURE__*/299+ function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {300+ const desc = Object.getOwnPropertyDescriptor(base, prop)301+ return desc && desc.enumerable ? true : false302+ }303+304 loadPlugin("es5", {305 createES5Proxy_,306 markChangedES5_,307 willFinalizeES5_2 unmodified linesmodified:src/common.ts+10 added-46 removed
9 unmodified lines10 AnySet,11 ImmerState,12 hasMap,13 ArchtypeObject,14- ProxyTypeES5Object,15- ProxyTypeProxyObject,16- ProxyTypeES5Array,17- ProxyTypeProxyArray,14 ArchtypeArray,19- ProxyTypeMap,15 ArchtypeMap,21- ProxyTypeSet,16 ArchtypeSet17 } from "./internal"18 import invariant from "tiny-invariant"19 57 unmodified lines77 }78 }79 80 /*#__PURE__*/87-export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {88- const desc = Object.getOwnPropertyDescriptor(base, prop)89- return desc && desc.enumerable ? true : false90-}91-92-/*#__PURE__*/81 export function getArchtype(thing: any): 0 | 1 | 2 | 3 {82 /* istanbul ignore next */95- if (thing[DRAFT_STATE]) {96- switch ((thing as Drafted)[DRAFT_STATE].type) {97- case ProxyTypeES5Object:98- case ProxyTypeProxyObject:99- return ArchtypeObject100- case ProxyTypeES5Array:101- case ProxyTypeProxyArray:102- return ArchtypeArray103- case ProxyTypeMap:104- return ArchtypeMap105- case ProxyTypeSet:106- return ArchtypeSet107- }108- }109- return Array.isArray(thing)83+ const state: undefined | ImmerState = thing[DRAFT_STATE]84+ return state85+ ? state.type_ > 386+ ? state.type_ - 4 // cause Object and Array map back from 4 and 587+ : (state.type_ as any) // others are the same88+ : Array.isArray(thing)89 ? ArchtypeArray90 : isMap(thing)91 ? ArchtypeMap92 : isSet(thing)85 unmodified lines178 return clone179 }180 181 export function freeze(obj: any, deep: boolean): void {203- if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) return204- const type = getArchtype(obj)205- if (type === ArchtypeSet) {206- obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any207- } else if (type === ArchtypeMap) {208- obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any182+ if (isDraft(obj) || Object.isFrozen(obj) || !isDraftable(obj)) return183+ if (getArchtype(obj) > 1 /* Map or Set */) {184+ obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any185 }186 Object.freeze(obj)187 if (deep) each(obj, (_, value) => freeze(value, true))188 }1 unmodified line190 function dontMutateFrozenCollections() {191 invariant(false, "This object has been frozen and should not be mutated")192 }193 218-// TODO: used only once, inline219-export function createHiddenProperty(220- target: AnyObject,221- prop: PropertyKey,222- value: any223-) {224- Object.defineProperty(target, prop, {225- value: value,226- // enumerable: false <- the default227- writable: true228- })229-}230-194+// TODO: move to ES5 / Map195 export function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {196 invariant(197 !state.revoked_,198 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +3 unmodified linesmodified:notes.txt+8 added-2 removed
1 Things to optimize2 3 [x] extract errors4 [x] don't enable all features by default5-[ ] "undefined" "object" "function" prototype undefined + buildin FNs ?5+[ ] "undefined" "object" "function" prototype undefined bind, getOwnPropertyies, isArray etc + buildin FNs ?6 [x] kill hooks7 [x] _ postfix all members8 [x] clean up finalize9 [x] remove die6 unmodified lines16 [x] kill enumerations17 [ ] check todos18 [ ] kill marchChangesSweep recursive if possible and no perf impact19 [ ] optimize each closures away? pass in additional args?20-[ ] document performance tip: if externally added data is frozen, it doesn't recurse (also update related issue)20+[x] document performance tip: if externally added data is frozen, it doesn't recurse (also update related issue) #46521 [ ] put some invariants behind __DEV__22+[x] check for plugin specific utils23+[ ] bind util24+[ ] eliminate switches25 26 Import size report for immer:27 ┌───────────────────────┬───────────┬────────────┬───────────┐28 │ (index) │ just this │ cumulative │ increment │79 unmodified lines108 (this report was generated by npmjs.com/package/import-size)109 110 Minimize plugin api's:111 3196112+113+add draft optimization114+3199modified:tsdx.config.js+1 added-1 removed
12345667891015 unmodified linesconst {terser} = require("rollup-plugin-terser")
module.exports = { // This function will run for each entry/format/env combination rollup(config, options) { if (options.format === "esm") { if (options.format === "esm" || options.env === "production") { config.plugins.push( terser({ sourcemap: true, module: true,15 unmodified lines