common.ts
This file
Path
src/utils/common.ts
240 lines · 6.7 KiB · TypeScript
Last changed by
feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
History of this fileTake it
The walk stopped after 200 commits: lines older than 6555173 point at it.
fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 1–9why, one step back1import {2 DRAFT_STATE,3 DRAFTABLE,4 Objectish,5 Drafted,6 AnyObject,7 AnyMap,8 AnySet,9 ImmerState,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 10why, one step back10 ArchType,feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 11–12why, one step back11 die,12 StrictModefix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 13–14why, one step back13} from "../internal"14Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 15–16why, one step back15export const getPrototypeOf = Object.getPrototypeOf16fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 17–30why, one step back17/** Returns true if the given value is an Immer draft */18/*#__PURE__*/19export function isDraft(value: any): boolean {20 return !!value && !!value[DRAFT_STATE]21}2223/** Returns true if the given value can be drafted by Immer */24/*#__PURE__*/25export function isDraftable(value: any): boolean {26 if (!value) return false27 return (28 isPlainObject(value) ||29 Array.isArray(value) ||30 !!value[DRAFTABLE] ||fix: protect isDraftable against undefined constructor (#969)Brian Hungbefore adoptionced4563
line 31why, one step back31 !!value.constructor?.[DRAFTABLE] ||fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 32–37why, one step back32 isMap(value) ||33 isSet(value)34 )35}3637const objectCtorString = Object.prototype.constructor.toString()feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 38why, one step back38const cachedCtorStrings = new WeakMap()fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 39–41why, one step back39/*#__PURE__*/40export function isPlainObject(value: any): boolean {41 if (!value || typeof value !== "object") return falsefeat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 42–44why, one step back42 const proto = Object.getPrototypeOf(value)43 if (proto === null || proto === Object.prototype) return true44fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 45–46why, one step back45 const Ctor =46 Object.hasOwnProperty.call(proto, "constructor") && proto.constructorchore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 47why, one step back47 if (Ctor === Object) return truefix: isPlainObject: add quick comparison between input and `Object` to short-circuit taxing `Function.toString` invocations (#805)Ben Elliottbefore adoption07575f3
line 48why, one step back48feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 49–57why, one step back49 if (typeof Ctor !== "function") return false5051 let ctorString = cachedCtorStrings.get(Ctor)52 if (ctorString === undefined) {53 ctorString = Function.toString.call(Ctor)54 cachedCtorStrings.set(Ctor, ctorString)55 }5657 return ctorString === objectCtorStringfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 58–63why, one step back58}5960/** Get the underlying object that is represented by the given draft */61/*#__PURE__*/62export function original<T>(value: T): T | undefined63export function original(value: Drafted<any>): any {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 64why, one step back64 if (!isDraft(value)) die(15, value)fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 65–67why, one step back65 return value[DRAFT_STATE].base_66}67fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
lines 68–69why, one step back68/**69 * Each iterates a map, set or array.chore: Fix typo in comment in common.ts (#1113)Fredrik Vaeng Røtnesbefore adoption073d634
line 70why, one step back70 * Or, if any other kind of object, all of its own properties.feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 71–75why, one step back71 *72 * @param obj The object to iterate over73 * @param iter The iterator function74 * @param strict When true (default), includes symbols and non-enumerable properties.75 * When false, uses looseiteration over only enumerable string properties.fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
line 76why, one step back76 */fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 77–78why, one step back77export function each<T extends Objectish>(78 obj: T,feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 79–80why, one step back79 iter: (key: string | number, value: any, source: T) => void,80 strict?: booleanfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 81why, one step back81): voidfeat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 82why, one step back82export function each(obj: any, iter: any, strict: boolean = true) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 83why, one step back83 if (getArchtype(obj) === ArchType.Object) {feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 84–87why, one step back84 // If strict, we do a full iteration including symbols and non-enumerable properties85 // Otherwise, we only iterate enumerable string properties for performance86 const keys = strict ? Reflect.ownKeys(obj) : Object.keys(obj)87 keys.forEach(key => {fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
line 88why, one step back88 iter(key, obj[key], obj)fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 89–95why, one step back89 })90 } else {91 obj.forEach((entry: any, index: any) => iter(index, entry, obj))92 }93}9495/*#__PURE__*/Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 96why, one step back96export function getArchtype(thing: any): ArchType {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 97–98why, one step back97 const state: undefined | ImmerState = thing[DRAFT_STATE]98 return stateMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 99why, one step back99 ? state.type_fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 100why, one step back100 : Array.isArray(thing)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 101why, one step back101 ? ArchType.Arrayfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 102why, one step back102 : isMap(thing)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 103why, one step back103 ? ArchType.Mapfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 104why, one step back104 : isSet(thing)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 105–106why, one step back105 ? ArchType.Set106 : ArchType.Objectfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 107–110why, one step back107}108109/*#__PURE__*/110export function has(thing: any, prop: PropertyKey): boolean {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 111why, one step back111 return getArchtype(thing) === ArchType.Mapfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 112–118why, one step back112 ? thing.has(prop)113 : Object.prototype.hasOwnProperty.call(thing, prop)114}115116/*#__PURE__*/117export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {118 // @ts-ignoreMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 119why, one step back119 return getArchtype(thing) === ArchType.Map ? thing.get(prop) : thing[prop]fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 120–124why, one step back120}121122/*#__PURE__*/123export function set(thing: any, propOrOldValue: PropertyKey, value: any) {124 const t = getArchtype(thing)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 125–126why, one step back125 if (t === ArchType.Map) thing.set(propOrOldValue, value)126 else if (t === ArchType.Set) {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 127–142why, one step back127 thing.add(value)128 } else thing[propOrOldValue] = value129}130131/*#__PURE__*/132export function is(x: any, y: any): boolean {133 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js134 if (x === y) {135 return x !== 0 || 1 / x === 1 / y136 } else {137 return x !== x && y !== y138 }139}140141/*#__PURE__*/142export function isMap(target: any): target is AnyMap {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 143why, one step back143 return target instanceof Mapfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 144–147why, one step back144}145146/*#__PURE__*/147export function isSet(target: any): target is AnySet {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 148why, one step back148 return target instanceof Setfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 149–155why, one step back149}150/*#__PURE__*/151export function latest(state: ImmerState): any {152 return state.copy_ || state.base_153}154155/*#__PURE__*/feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
line 156why, one step back156export function shallowCopy(base: any, strict: StrictMode) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 157–162why, one step back157 if (isMap(base)) {158 return new Map(base)159 }160 if (isSet(base)) {161 return new Set(base)162 }fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 163why, one step back163 if (Array.isArray(base)) return Array.prototype.slice.call(base)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 164why, one step back164feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
line 165why, one step back165 const isPlain = isPlainObject(base)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 166why, one step back166feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 167–177why, one step back167 if (strict === true || (strict === "class_only" && !isPlain)) {168 // Perform a strict copy169 const descriptors = Object.getOwnPropertyDescriptors(base)170 delete descriptors[DRAFT_STATE as any]171 let keys = Reflect.ownKeys(descriptors)172 for (let i = 0; i < keys.length; i++) {173 const key: any = keys[i]174 const desc = descriptors[key]175 if (desc.writable === false) {176 desc.writable = true177 desc.configurable = truefix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 178why, one step back178 }feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 179–198why, one step back179 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing180 // with libraries that trap values, like mobx or vue181 // unlike object.assign, non-enumerables will be copied as well182 if (desc.get || desc.set)183 descriptors[key] = {184 configurable: true,185 writable: true, // could live with !!desc.set as well here...186 enumerable: desc.enumerable,187 value: base[key]188 }189 }190 return Object.create(getPrototypeOf(base), descriptors)191 } else {192 // perform a sloppy copy193 const proto = getPrototypeOf(base)194 if (proto !== null && isPlain) {195 return {...base} // assumption: better inner class optimization than the assign below196 }197 const obj = Object.create(proto)198 return Object.assign(obj, base)fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 199–212why, one step back199 }200}201202/**203 * Freezes draftable objects. Returns the original object.204 * By default freezes shallowly, but if the second argument is `true` it will freeze recursively.205 *206 * @param obj207 * @param deep208 */209export function freeze<T>(obj: T, deep?: boolean): T210export function freeze<T>(obj: any, deep: boolean = false): T {211 if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj212 if (getArchtype(obj) > 1 /* Map or Set */) {feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 213–218why, one step back213 Object.defineProperties(obj, {214 set: dontMutateMethodOverride,215 add: dontMutateMethodOverride,216 clear: dontMutateMethodOverride,217 delete: dontMutateMethodOverride218 })fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 219–220why, one step back219 }220 Object.freeze(obj)fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
lines 221–222why, one step back221 if (deep)222 // See #590, don't recurse into non-enumerable / Symbol properties when freezingPrefix param with underscore to prevent unused param warning (#1145)Alissabefore adoption19cbe47
lines 223–224why, one step back223 // So use Object.values (only string-like, enumerables) instead of each()224 Object.values(obj).forEach(value => freeze(value, true))fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 225–231why, one step back225 return obj226}227228function dontMutateFrozenCollections() {229 die(2)230}231feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 232–235why, one step back232const dontMutateMethodOverride = {233 value: dontMutateFrozenCollections234}235fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 236why, one step back236export function isFrozen(obj: any): boolean {feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 237–238why, one step back237 // Fast path: primitives and null/undefined are always "frozen"238 if (obj === null || typeof obj !== "object") return truefix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 239–240why, one step back239 return Object.isFrozen(obj)240}