Skip to content
Myr

common.ts

This file

Path

src/utils/common.ts

217 lines · 5.7 KiB · TypeScript

Last changed by

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

History of this file

Take it

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

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: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

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: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

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: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 32–36why, one step back
32 isMap(value) ||
33 isSet(value)34 )35}36

fix: make sure isPlainObject checks support objects send accross frames. Fixes #766 / #405xyhbefore adoption5ae3547

line 37why, one step back
37const objectCtorString = Object.prototype.constructor.toString()

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

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

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

line 41why, one step back
41 const proto = getPrototypeOf(value)

fix: make sure isPlainObject checks support objects send accross frames. Fixes #766 / #405xyhbefore adoption5ae3547

lines 42–46why, one step back
42 if (proto === null) {
43 return true44 }45 const Ctor =46 Object.hasOwnProperty.call(proto, "constructor") && proto.constructor

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

line 47why, one step back
47

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

line 48why, one step back
48 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 49why, one step back
49

fix: make sure isPlainObject checks support objects send accross frames. Fixes #766 / #405xyhbefore adoption5ae3547

lines 50–53why, one step back
50 return (
51 typeof Ctor == "function" &&52 Function.toString.call(Ctor) === objectCtorString53 )

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 54–59why, one step back
54}
5556/** Get the underlying object that is represented by the given draft */57/*#__PURE__*/58export function original<T>(value: T): T | undefined59export function original(value: Drafted<any>): any {

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 61–63why, one step back
61 return value[DRAFT_STATE].base_
62}63

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

lines 64–68why, one step back
64/**
65 * Each iterates a map, set or array.66 * Or, if any other kind of of object all it's own properties.67 * Regardless whether they are enumerable or symbols68 */

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 69–70why, one step back
69export function each<T extends Objectish>(
70 obj: T,

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

line 71why, one step back
71 iter: (key: string | number, value: any, source: T) => void

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

line 72why, one step back
72): void

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

lines 73–74why, one step back
73export function each(obj: any, iter: any) {
74 if (getArchtype(obj) === ArchType.Object) {

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 Reflect.ownKeys(obj).forEach(key => {
76 iter(key, obj[key], obj)

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 77–83why, one step back
77 })
78 } else {79 obj.forEach((entry: any, index: any) => iter(index, entry, obj))80 }81}8283/*#__PURE__*/

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

line 84why, one step back
84export function getArchtype(thing: any): ArchType {

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

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

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

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

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

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

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

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

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

lines 93–94why, one step back
93 ? ArchType.Set
94 : ArchType.Object

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

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

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 100–106why, one step back
100 ? thing.has(prop)
101 : Object.prototype.hasOwnProperty.call(thing, prop)102}103104/*#__PURE__*/105export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {106 // @ts-ignore

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

line 107why, one step back
107 return getArchtype(thing) === ArchType.Map ? thing.get(prop) : thing[prop]

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 108–112why, one step back
108}
109110/*#__PURE__*/111export function set(thing: any, propOrOldValue: PropertyKey, value: any) {112 const t = getArchtype(thing)

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

lines 113–114why, one step back
113 if (t === ArchType.Map) thing.set(propOrOldValue, value)
114 else if (t === ArchType.Set) {

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 115–130why, one step back
115 thing.add(value)
116 } else thing[propOrOldValue] = value117}118119/*#__PURE__*/120export function is(x: any, y: any): boolean {121 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js122 if (x === y) {123 return x !== 0 || 1 / x === 1 / y124 } else {125 return x !== x && y !== y126 }127}128129/*#__PURE__*/130export function isMap(target: any): target is AnyMap {

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

line 131why, one step back
131 return target instanceof Map

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 132–135why, one step back
132}
133134/*#__PURE__*/135export function isSet(target: any): target is AnySet {

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

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

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 137–143why, one step back
137}
138/*#__PURE__*/139export function latest(state: ImmerState): any {140 return state.copy_ || state.base_141}142143/*#__PURE__*/

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

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

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

lines 145–150why, one step back
145 if (isMap(base)) {
146 return new Map(base)147 }148 if (isSet(base)) {149 return new Set(base)150 }

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

line 151why, one step back
151 if (Array.isArray(base)) return Array.prototype.slice.call(base)

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

line 152why, one step back
152

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

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

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

line 154why, one step back
154

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

lines 155–165why, one step back
155 if (strict === true || (strict === "class_only" && !isPlain)) {
156 // Perform a strict copy157 const descriptors = Object.getOwnPropertyDescriptors(base)158 delete descriptors[DRAFT_STATE as any]159 let keys = Reflect.ownKeys(descriptors)160 for (let i = 0; i < keys.length; i++) {161 const key: any = keys[i]162 const desc = descriptors[key]163 if (desc.writable === false) {164 desc.writable = true165 desc.configurable = true

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

line 166why, one step back
166 }

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

lines 167–186why, one step back
167 // like object.assign, we will read any _own_, get/set accessors. This helps in dealing
168 // with libraries that trap values, like mobx or vue169 // unlike object.assign, non-enumerables will be copied as well170 if (desc.get || desc.set)171 descriptors[key] = {172 configurable: true,173 writable: true, // could live with !!desc.set as well here...174 enumerable: desc.enumerable,175 value: base[key]176 }177 }178 return Object.create(getPrototypeOf(base), descriptors)179 } else {180 // perform a sloppy copy181 const proto = getPrototypeOf(base)182 if (proto !== null && isPlain) {183 return {...base} // assumption: better inner class optimization than the assign below184 }185 const obj = Object.create(proto)186 return Object.assign(obj, base)

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 187–189why, one step back
187 }
188}189

feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f

lines 190–199why, one step back
190/**
191 * Freezes draftable objects. Returns the original object.192 * By default freezes shallowly, but if the second argument is `true` it will freeze recursively.193 *194 * @param obj195 * @param deep196 */197export function freeze<T>(obj: T, deep?: boolean): T198export function freeze<T>(obj: any, deep: boolean = false): T {199 if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj)) return obj

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 200–203why, one step back
200 if (getArchtype(obj) > 1 /* Map or Set */) {
201 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any202 }203 Object.freeze(obj)

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

lines 204–207why, one step back
204 if (deep)
205 // See #590, don't recurse into non-enumerable / Symbol properties when freezing206 // So use Object.entries (only string-like, enumerables) instead of each()207 Object.entries(obj).forEach(([key, value]) => freeze(value, true))

feature: Always freeze by default (#702)Michel Weststratebefore adoptiona406c8f

line 208why, one step back
208 return obj

fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b

lines 209–217why, one step back
209}
210211function dontMutateFrozenCollections() {212 die(2)213}214215export function isFrozen(obj: any): boolean {216 return Object.isFrozen(obj)217}