common.ts
This file
Path
src/utils/common.ts
290 lines · 7.9 KiB · TypeScript
Last changed by
Merge pull request #1289 from maximilliangrand/fix/array-methods-noop-structural-sharingMark Eriksonbefore adoption907395a
History of this fileTake it
The walk stopped after 200 commits: lines older than 07f5cc9 point at it.
chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 1–4why, one step back1import {2 DRAFT_STATE,3 DRAFTABLE,4 Objectish,fix: update current and original typings to assert draft value is unwrapped (#1236)Dean Kerrbefore adoptionbf2d154
line 5why, one step back5 Draft,chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 6–9why, one step back6 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 StrictModechore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 13–14why, one step back13} from "../internal"14perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 15why, one step back15const O = ObjectMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 16why, one step back16perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 17–26why, one step back17export const getPrototypeOf = O.getPrototypeOf1819export const CONSTRUCTOR = "constructor"20export const PROTOTYPE = "prototype"2122export const CONFIGURABLE = "configurable"23export const ENUMERABLE = "enumerable"24export const WRITABLE = "writable"25export const VALUE = "value"26chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 27–28why, one step back27/** Returns true if the given value is an Immer draft */28/*#__PURE__*/perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 29why, one step back29export let isDraft = (value: any): boolean => !!value && !!value[DRAFT_STATE]chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 30–36why, one step back3031/** Returns true if the given value can be drafted by Immer */32/*#__PURE__*/33export function isDraftable(value: any): boolean {34 if (!value) return false35 return (36 isPlainObject(value) ||perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 37why, one step back37 isArray(value) ||chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 38why, one step back38 !!value[DRAFTABLE] ||perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 39why, one step back39 !!value[CONSTRUCTOR]?.[DRAFTABLE] ||chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 40–44why, one step back40 isMap(value) ||41 isSet(value)42 )43}44perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 45why, one step back45const objectCtorString = O[PROTOTYPE][CONSTRUCTOR].toString()feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 46why, one step back46const cachedCtorStrings = new WeakMap()chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 47–48why, one step back47/*#__PURE__*/48export function isPlainObject(value: any): boolean {perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 49–51why, one step back49 if (!value || !isObjectish(value)) return false50 const proto = getPrototypeOf(value)51 if (proto === null || proto === O[PROTOTYPE]) return truefeat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 52why, one step back52perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 53why, one step back53 const Ctor = O.hasOwnProperty.call(proto, CONSTRUCTOR) && proto[CONSTRUCTOR]chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 54–55why, one step back54 if (Ctor === Object) return true55perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 56why, one step back56 if (!isFunction(Ctor)) return falsefeat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 57–64why, one step back5758 let ctorString = cachedCtorStrings.get(Ctor)59 if (ctorString === undefined) {60 ctorString = Function.toString.call(Ctor)61 cachedCtorStrings.set(Ctor, ctorString)62 }6364 return ctorString === objectCtorStringchore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 65–68why, one step back65}6667/** Get the underlying object that is represented by the given draft */68/*#__PURE__*/fix: update current and original typings to assert draft value is unwrapped (#1236)Dean Kerrbefore adoptionbf2d154
lines 69–70why, one step back69export function original<T>(value: Draft<T>): T70export function original(value: Draft<any>): any {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 71why, one step back71 if (!isDraft(value)) die(15, value)chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 72–74why, one step back72 return value[DRAFT_STATE].base_73}74fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
lines 75–76why, one step back75/**76 * Each iterates a map, set or array.chore: Fix typo in comment in common.ts (#1113)Fredrik Vaeng Røtnesbefore adoption073d634
line 77why, one step back77 * 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 78–82why, one step back78 *79 * @param obj The object to iterate over80 * @param iter The iterator function81 * @param strict When true (default), includes symbols and non-enumerable properties.82 * 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 83why, one step back83 */chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 84–85why, one step back84export function each<T extends Objectish>(85 obj: T,feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 86–87why, one step back86 iter: (key: string | number, value: any, source: T) => void,87 strict?: booleanchore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 88why, one step back88): voidfeat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 89why, one step back89export function each(obj: any, iter: any, strict: boolean = true) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 90why, one step back90 if (getArchtype(obj) === ArchType.Object) {feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 91–92why, one step back91 // If strict, we do a full iteration including symbols and non-enumerable properties92 // Otherwise, we only iterate enumerable string properties for performanceperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 93why, one step back93 const keys = strict ? Reflect.ownKeys(obj) : O.keys(obj)feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 94why, one step back94 keys.forEach(key => {fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
line 95why, one step back95 iter(key, obj[key], obj)chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 96–102why, one step back96 })97 } else {98 obj.forEach((entry: any, index: any) => iter(index, entry, obj))99 }100}101102/*#__PURE__*/Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 103why, one step back103export function getArchtype(thing: any): ArchType {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 104–105why, one step back104 const state: undefined | ImmerState = thing[DRAFT_STATE]105 return stateMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 106why, one step back106 ? state.type_perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 107why, one step back107 : isArray(thing)Merge pull request #1289 from maximilliangrand/fix/array-methods-noop-structural-sharingMark Eriksonbefore adoption907395a
lines 108–113why, one step back108 ? ArchType.Array109 : isMap(thing)110 ? ArchType.Map111 : isSet(thing)112 ? ArchType.Set113 : ArchType.Objectchore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 114–116why, one step back114}115116/*#__PURE__*/perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 117–122why, one step back117export let has = (118 thing: any,119 prop: PropertyKey,120 type = getArchtype(thing)121): boolean =>122 type === ArchType.Mapchore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 123why, one step back123 ? thing.has(prop)perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 124why, one step back124 : O[PROTOTYPE].hasOwnProperty.call(thing, prop)chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 125–126why, one step back125126/*#__PURE__*/perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 127–131why, one step back127export let get = (128 thing: AnyMap | AnyObject,129 prop: PropertyKey,130 type = getArchtype(thing)131): any =>chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 132why, one step back132 // @ts-ignoreperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 133why, one step back133 type === ArchType.Map ? thing.get(prop) : thing[prop]chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 134–135why, one step back134135/*#__PURE__*/perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 136–143why, one step back136export let set = (137 thing: any,138 propOrOldValue: PropertyKey,139 value: any,140 type = getArchtype(thing)141) => {142 if (type === ArchType.Map) thing.set(propOrOldValue, value)143 else if (type === ArchType.Set) {chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 144–157why, one step back144 thing.add(value)145 } else thing[propOrOldValue] = value146}147148/*#__PURE__*/149export function is(x: any, y: any): boolean {150 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js151 if (x === y) {152 return x !== 0 || 1 / x === 1 / y153 } else {154 return x !== x && y !== y155 }156}157perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 158–159why, one step back158export let isArray = Array.isArray159chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 160why, one step back160/*#__PURE__*/perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 161why, one step back161export let isMap = (target: any): target is AnyMap => target instanceof Mapchore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 162–163why, one step back162163/*#__PURE__*/perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 164–173why, one step back164export let isSet = (target: any): target is AnySet => target instanceof Set165166export let isObjectish = (target: any) => typeof target === "object"167168export let isFunction = (target: any): target is Function =>169 typeof target === "function"170171export let isBoolean = (target: any): target is boolean =>172 typeof target === "boolean"173feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0
lines 174–178why, one step back174export function isArrayIndex(value: string | number): value is number | string {175 const n = +value176 return Number.isInteger(n) && String(n) === value177}178perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 179–181why, one step back179export let getProxyDraft = <T extends any>(value: T): ImmerState | null => {180 if (!isObjectish(value)) return null181 return (value as {[DRAFT_STATE]: any})?.[DRAFT_STATE]chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 182why, one step back182}perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 183why, one step back183chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 184why, one step back184/*#__PURE__*/perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 185–188why, one step back185export let latest = (state: ImmerState): any => state.copy_ || state.base_186187export let getValue = <T extends object>(value: T): T => {188 const proxyDraft = getProxyDraft(value)Merge pull request #1289 from maximilliangrand/fix/array-methods-noop-structural-sharingMark Eriksonbefore adoption907395a
lines 189–193why, one step back189 // Use the copy only if the draft was actually modified. An unmodified190 // draft can still have a copy_ (e.g. the get trap caches child drafts191 // there), and using it would both break structural sharing and leak192 // soon-to-be-revoked child drafts into the result.193 return proxyDraft ? getFinalValue(proxyDraft) : valuechore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 194–195why, one step back194}195perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 196–198why, one step back196export let getFinalValue = (state: ImmerState): any =>197 state.modified_ ? state.copy_ : state.base_198chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 199why, one step back199/*#__PURE__*/feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
line 200why, one step back200export function shallowCopy(base: any, strict: StrictMode) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 201–206why, one step back201 if (isMap(base)) {202 return new Map(base)203 }204 if (isSet(base)) {205 return new Set(base)206 }perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 207why, one step back207 if (isArray(base)) return Array[PROTOTYPE].slice.call(base)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 208why, one step back208feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
line 209why, one step back209 const isPlain = isPlainObject(base)Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 210why, one step back210feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 211–212why, one step back211 if (strict === true || (strict === "class_only" && !isPlain)) {212 // Perform a strict copyperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 213why, one step back213 const descriptors = O.getOwnPropertyDescriptors(base)feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 214–218why, one step back214 delete descriptors[DRAFT_STATE as any]215 let keys = Reflect.ownKeys(descriptors)216 for (let i = 0; i < keys.length; i++) {217 const key: any = keys[i]218 const desc = descriptors[key]perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 219–221why, one step back219 if (desc[WRITABLE] === false) {220 desc[WRITABLE] = true221 desc[CONFIGURABLE] = truechore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 222why, one step back222 }feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 223–227why, one step back223 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing224 // with libraries that trap values, like mobx or vue225 // unlike object.assign, non-enumerables will be copied as well226 if (desc.get || desc.set)227 descriptors[key] = {perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 228–231why, one step back228 [CONFIGURABLE]: true,229 [WRITABLE]: true, // could live with !!desc.set as well here...230 [ENUMERABLE]: desc[ENUMERABLE],231 [VALUE]: base[key]feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 232–233why, one step back232 }233 }perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 234why, one step back234 return O.create(getPrototypeOf(base), descriptors)feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203
lines 235–240why, one step back235 } else {236 // perform a sloppy copy237 const proto = getPrototypeOf(base)238 if (proto !== null && isPlain) {239 return {...base} // assumption: better inner class optimization than the assign below240 }perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 241–242why, one step back241 const obj = O.create(proto)242 return O.assign(obj, base)chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 243–254why, one step back243 }244}245246/**247 * Freezes draftable objects. Returns the original object.248 * By default freezes shallowly, but if the second argument is `true` it will freeze recursively.249 *250 * @param obj251 * @param deep252 */253export function freeze<T>(obj: T, deep?: boolean): T254export function freeze<T>(obj: any, deep: boolean = false): T {Merge pull request #1193 from immerjs/bugfix/1192-non-draftable-valuesMark Eriksonbefore adoption4d2179b
line 255why, one step back255 if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return objchore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 256why, one step back256 if (getArchtype(obj) > 1 /* Map or Set */) {perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 257why, one step back257 O.defineProperties(obj, {feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 258–262why, one step back258 set: dontMutateMethodOverride,259 add: dontMutateMethodOverride,260 clear: dontMutateMethodOverride,261 delete: dontMutateMethodOverride262 })chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 263why, one step back263 }perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 264why, one step back264 O.freeze(obj)fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e
lines 265–266why, one step back265 if (deep)266 // See #590, don't recurse into non-enumerable / Symbol properties when freezingPrefix param with underscore to prevent unused param warning (#1145)Alissabefore adoption19cbe47
line 267why, one step back267 // So use Object.values (only string-like, enumerables) instead of each()perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 268–274why, one step back268 each(269 obj,270 (_key, value) => {271 freeze(value, true)272 },273 false274 )chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
lines 275–281why, one step back275 return obj276}277278function dontMutateFrozenCollections() {279 die(2)280}281feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 282why, one step back282const dontMutateMethodOverride = {perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 283why, one step back283 [VALUE]: dontMutateFrozenCollectionsfeat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
lines 284–285why, one step back284}285chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 286why, one step back286export function isFrozen(obj: any): boolean {feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce
line 287why, one step back287 // Fast path: primitives and null/undefined are always "frozen"perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 288–289why, one step back288 if (obj === null || !isObjectish(obj)) return true289 return O.isFrozen(obj)chore(test): fix production build unit testsMichel Weststratebefore adoption07f5cc9
line 290why, one step back290}