Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than ba24aab point at it.

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 1–4why, one step back
1import {
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 back
5 Draft,

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 6–9why, one step back
6 AnyObject,
7 AnyMap,8 AnySet,9 ImmerState,

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 10why, one step back
10 ArchType,

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

lines 11–12why, one step back
11 die,
12 StrictMode

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 13–14why, one step back
13} from "../internal"
14

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 15why, one step back
15const O = Object

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 16why, one step back
16

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 17–26why, one step back
17export const getPrototypeOf = O.getPrototypeOf
1819export const CONSTRUCTOR = "constructor"20export const PROTOTYPE = "prototype"2122export const CONFIGURABLE = "configurable"23export const ENUMERABLE = "enumerable"24export const WRITABLE = "writable"25export const VALUE = "value"26

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 27–28why, one step back
27/** 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 back
29export let isDraft = (value: any): boolean => !!value && !!value[DRAFT_STATE]

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 30–36why, one step back
30
31/** 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 back
37 isArray(value) ||

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

line 38why, one step back
38 !!value[DRAFTABLE] ||

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 39why, one step back
39 !!value[CONSTRUCTOR]?.[DRAFTABLE] ||

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 40–44why, one step back
40 isMap(value) ||
41 isSet(value)42 )43}44

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 45why, one step back
45const objectCtorString = O[PROTOTYPE][CONSTRUCTOR].toString()

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

line 46why, one step back
46const cachedCtorStrings = new WeakMap()

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 47–48why, one step back
47/*#__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 back
49 if (!value || !isObjectish(value)) return false
50 const proto = getPrototypeOf(value)51 if (proto === null || proto === O[PROTOTYPE]) return true

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

line 52why, one step back
52

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 53why, one step back
53 const Ctor = O.hasOwnProperty.call(proto, CONSTRUCTOR) && proto[CONSTRUCTOR]

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 54–55why, one step back
54 if (Ctor === Object) return true
55

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 56why, one step back
56 if (!isFunction(Ctor)) return false

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

lines 57–64why, one step back
57
58 let ctorString = cachedCtorStrings.get(Ctor)59 if (ctorString === undefined) {60 ctorString = Function.toString.call(Ctor)61 cachedCtorStrings.set(Ctor, ctorString)62 }6364 return ctorString === objectCtorString

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 65–68why, one step back
65}
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 back
69export function original<T>(value: Draft<T>): T
70export function original(value: Draft<any>): any {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 71why, one step back
71 if (!isDraft(value)) die(15, value)

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 72–74why, one step back
72 return value[DRAFT_STATE].base_
73}74

fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))Michel Weststratebefore adoption8949a3e

lines 75–76why, one step back
75/**
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 back
77 * 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 back
78 *
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 back
83 */

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 84–85why, one step back
84export 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 back
86 iter: (key: string | number, value: any, source: T) => void,
87 strict?: boolean

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

line 88why, one step back
88): void

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

line 89why, one step back
89export function each(obj: any, iter: any, strict: boolean = true) {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 90why, one step back
90 if (getArchtype(obj) === ArchType.Object) {

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

lines 91–92why, one step back
91 // If strict, we do a full iteration including symbols and non-enumerable properties
92 // Otherwise, we only iterate enumerable string properties for performance

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 93why, one step back
93 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 back
94 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 back
95 iter(key, obj[key], obj)

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 96–102why, one step back
96 })
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 back
103export function getArchtype(thing: any): ArchType {

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 104–105why, one step back
104 const state: undefined | ImmerState = thing[DRAFT_STATE]
105 return state

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 106why, one step back
106 ? state.type_

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 107why, one step back
107 : isArray(thing)

Merge pull request #1289 from maximilliangrand/fix/array-methods-noop-structural-sharingMark Eriksonbefore adoption907395a

lines 108–113why, one step back
108 ? ArchType.Array
109 : isMap(thing)110 ? ArchType.Map111 : isSet(thing)112 ? ArchType.Set113 : ArchType.Object

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 114–116why, one step back
114}
115116/*#__PURE__*/

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 117–122why, one step back
117export let has = (
118 thing: any,119 prop: PropertyKey,120 type = getArchtype(thing)121): boolean =>122 type === ArchType.Map

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

line 123why, one step back
123 ? thing.has(prop)

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 124why, one step back
124 : O[PROTOTYPE].hasOwnProperty.call(thing, prop)

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 125–126why, one step back
125
126/*#__PURE__*/

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 127–131why, one step back
127export let get = (
128 thing: AnyMap | AnyObject,129 prop: PropertyKey,130 type = getArchtype(thing)131): any =>

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

line 132why, one step back
132 // @ts-ignore

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 133why, one step back
133 type === ArchType.Map ? thing.get(prop) : thing[prop]

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 134–135why, one step back
134
135/*#__PURE__*/

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 136–143why, one step back
136export 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: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 144–157why, one step back
144 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}157

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 158–159why, one step back
158export let isArray = Array.isArray
159

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

line 160why, one step back
160/*#__PURE__*/

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 161why, one step back
161export let isMap = (target: any): target is AnyMap => target instanceof Map

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 162–163why, one step back
162
163/*#__PURE__*/

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 164–173why, one step back
164export let isSet = (target: any): target is AnySet => target instanceof Set
165166export 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"173

feat: Override array methods to avoid proxy creation while iterating and updating (#1184)Mark Eriksonbefore adoptiond38a9d0

lines 174–178why, one step back
174export function isArrayIndex(value: string | number): value is number | string {
175 const n = +value176 return Number.isInteger(n) && String(n) === value177}178

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 179–181why, one step back
179export let getProxyDraft = <T extends any>(value: T): ImmerState | null => {
180 if (!isObjectish(value)) return null181 return (value as {[DRAFT_STATE]: any})?.[DRAFT_STATE]

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

line 182why, one step back
182}

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 183why, one step back
183

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

line 184why, one step back
184/*#__PURE__*/

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 185–188why, one step back
185export 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 back
189 // Use the copy only if the draft was actually modified. An unmodified
190 // 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) : value

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 194–195why, one step back
194}
195

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 196–198why, one step back
196export let getFinalValue = (state: ImmerState): any =>
197 state.modified_ ? state.copy_ : state.base_198

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

line 199why, one step back
199/*#__PURE__*/

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

line 200why, one step back
200export function shallowCopy(base: any, strict: StrictMode) {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 201–206why, one step back
201 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 back
207 if (isArray(base)) return Array[PROTOTYPE].slice.call(base)

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 208why, one step back
208

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

line 209why, one step back
209 const isPlain = isPlainObject(base)

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 210why, one step back
210

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

lines 211–212why, one step back
211 if (strict === true || (strict === "class_only" && !isPlain)) {
212 // Perform a strict copy

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 213why, one step back
213 const descriptors = O.getOwnPropertyDescriptors(base)

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

lines 214–218why, one step back
214 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 back
219 if (desc[WRITABLE] === false) {
220 desc[WRITABLE] = true221 desc[CONFIGURABLE] = true

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

line 222why, one step back
222 }

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

lines 223–227why, one step back
223 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing
224 // 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 back
228 [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 back
232 }
233 }

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 234why, one step back
234 return O.create(getPrototypeOf(base), descriptors)

feat(performance): Make non-strict mode faster for classes. Addresses #1071Michel Weststratebefore adoption53e3203

lines 235–240why, one step back
235 } 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 back
241 const obj = O.create(proto)
242 return O.assign(obj, base)

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 243–254why, one step back
243 }
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 back
255 if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

line 256why, one step back
256 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 back
257 O.defineProperties(obj, {

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

lines 258–262why, one step back
258 set: dontMutateMethodOverride,
259 add: dontMutateMethodOverride,260 clear: dontMutateMethodOverride,261 delete: dontMutateMethodOverride262 })

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

line 263why, one step back
263 }

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 264why, one step back
264 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 back
265 if (deep)
266 // See #590, don't recurse into non-enumerable / Symbol properties when freezing

Prefix param with underscore to prevent unused param warning (#1145)Alissabefore adoption19cbe47

line 267why, one step back
267 // 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 back
268 each(
269 obj,270 (_key, value) => {271 freeze(value, true)272 },273 false274 )

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

lines 275–281why, one step back
275 return obj
276}277278function dontMutateFrozenCollections() {279 die(2)280}281

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

line 282why, one step back
282const dontMutateMethodOverride = {

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 283why, one step back
283 [VALUE]: dontMutateFrozenCollections

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

lines 284–285why, one step back
284}
285

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

line 286why, one step back
286export function isFrozen(obj: any): boolean {

feat: Optimize Immer performance where possible, introduce `setUseStrictIteration` (#1164)Mark Eriksonbefore adoptione1996ce

line 287why, one step back
287 // 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 back
288 if (obj === null || !isObjectish(obj)) return true
289 return O.isFrozen(obj)

chore: Fix typo (#954)Marc Barbeaubefore adoptionba24aab

line 290why, one step back
290}