Skip to content
Myr

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 file

Take 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 back
1import {
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 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

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

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

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

lines 15–16why, one step back
15export const getPrototypeOf = Object.getPrototypeOf
16

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 17–30why, one step back
17/** 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 back
31 !!value.constructor?.[DRAFTABLE] ||

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 32–37why, one step back
32 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 back
38const cachedCtorStrings = new WeakMap()

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 39–41why, one step back
39/*#__PURE__*/
40export function isPlainObject(value: any): boolean {41 if (!value || typeof value !== "object") return false

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

lines 42–44why, one step back
42 const proto = Object.getPrototypeOf(value)
43 if (proto === null || proto === Object.prototype) return true44

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 45–46why, one step back
45 const Ctor =
46 Object.hasOwnProperty.call(proto, "constructor") && proto.constructor

chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d

line 47why, one step back
47 if (Ctor === Object) return true

fix: isPlainObject: add quick comparison between input and `Object` to short-circuit taxing `Function.toString` invocations (#805)Ben Elliottbefore adoption07575f3

line 48why, one step back
48

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

lines 49–57why, one step back
49 if (typeof Ctor !== "function") return false
5051 let ctorString = cachedCtorStrings.get(Ctor)52 if (ctorString === undefined) {53 ctorString = Function.toString.call(Ctor)54 cachedCtorStrings.set(Ctor, ctorString)55 }5657 return ctorString === objectCtorString

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 58–63why, one step back
58}
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 back
64 if (!isDraft(value)) die(15, value)

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 65–67why, one step back
65 return value[DRAFT_STATE].base_
66}67

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

lines 68–69why, one step back
68/**
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 back
70 * 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 back
71 *
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 back
76 */

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

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

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 81why, one step back
81): void

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

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

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

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

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

lines 84–87why, one step back
84 // If strict, we do a full iteration including symbols and non-enumerable properties
85 // 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 back
88 iter(key, obj[key], obj)

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

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

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

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

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

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

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 100why, one step back
100 : Array.isArray(thing)

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

line 101why, one step back
101 ? ArchType.Array

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 102why, one step back
102 : isMap(thing)

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

line 103why, one step back
103 ? ArchType.Map

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 104why, one step back
104 : isSet(thing)

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

lines 105–106why, one step back
105 ? ArchType.Set
106 : ArchType.Object

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 107–110why, one step back
107}
108109/*#__PURE__*/110export function has(thing: any, prop: PropertyKey): boolean {

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

line 111why, one step back
111 return getArchtype(thing) === ArchType.Map

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 112–118why, one step back
112 ? thing.has(prop)
113 : Object.prototype.hasOwnProperty.call(thing, prop)114}115116/*#__PURE__*/117export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {118 // @ts-ignore

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

line 119why, one step back
119 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 back
120}
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 back
125 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 back
127 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 back
143 return target instanceof Map

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 144–147why, one step back
144}
145146/*#__PURE__*/147export function isSet(target: any): target is AnySet {

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

line 148why, one step back
148 return target instanceof Set

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 149–155why, one step back
149}
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 back
156export function shallowCopy(base: any, strict: StrictMode) {

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

lines 157–162why, one step back
157 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 back
163 if (Array.isArray(base)) return Array.prototype.slice.call(base)

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

line 164why, one step back
164

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

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

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

line 166why, one step back
166

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

lines 167–177why, one step back
167 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 = true

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

line 178why, one step back
178 }

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

lines 179–198why, one step back
179 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing
180 // 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 back
199 }
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 back
213 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 back
219 }
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 back
221 if (deep)
222 // See #590, don't recurse into non-enumerable / Symbol properties when freezing

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

lines 223–224why, one step back
223 // 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 back
225 return obj
226}227228function dontMutateFrozenCollections() {229 die(2)230}231

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

lines 232–235why, one step back
232const dontMutateMethodOverride = {
233 value: dontMutateFrozenCollections234}235

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

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

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

lines 237–238why, one step back
237 // Fast path: primitives and null/undefined are always "frozen"
238 if (obj === null || typeof obj !== "object") return true

fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173

lines 239–240why, one step back
239 return Object.isFrozen(obj)
240}