Skip to content
Myr

common.ts

This file

Path

src/common.ts

237 lines · 5.9 KiB · TypeScript

Last changed by

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

History of this file

Take it

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 1why, one step back
1import {

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 2–4why, one step back
2 DRAFT_STATE,
3 DRAFTABLE,4 hasSet,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 5–11why, one step back
5 Objectish,
6 Drafted,7 AnyObject,8 AnyArray,9 AnyMap,10 AnySet,11 ImmerState,

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 12–22why, one step back
12 hasMap,
13 ArchtypeObject,14 ProxyTypeES5Object,15 ProxyTypeProxyObject,16 ProxyTypeES5Array,17 ProxyTypeProxyArray,18 ArchtypeArray,19 ProxyTypeMap,20 ArchtypeMap,21 ProxyTypeSet,22 ArchtypeSet

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 23why, one step back
23} from "./internal"

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 24why, one step back
24import invariant from "tiny-invariant"

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 25why, one step back
25

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 26why, one step back
26/** Returns true if the given value is an Immer draft */

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 28–32why, one step back
28export function isDraft(value: any): boolean {
29 return !!value && !!value[DRAFT_STATE]30}3132/** Returns true if the given value can be drafted by Immer */

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 34–37why, one step back
34export function isDraftable(value: any): boolean {
35 if (!value) return false36 return (37 isPlainObject(value) ||

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 38why, one step back
38 Array.isArray(value) ||

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 39–45why, one step back
39 !!value[DRAFTABLE] ||
40 !!value.constructor[DRAFTABLE] ||41 isMap(value) ||42 isSet(value)43 )44}45

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 47why, one step back
47export function isPlainObject(value: any): boolean {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 48–53why, one step back
48 if (!value || typeof value !== "object") return false
49 const proto = Object.getPrototypeOf(value)50 return !proto || proto === Object.prototype51}5253/** Get the underlying object that is represented by the given draft */

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

lines 55–56why, one step back
55export function original<T>(value: T): T | undefined
56export function original(value: Drafted<any>): any {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 57why, one step back
57 if (value && value[DRAFT_STATE]) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 58why, one step back
58 return value[DRAFT_STATE].base_ as any

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 59–62why, one step back
59 }
60 // otherwise return undefined61}62

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 64why, one step back
64export const ownKeys: (target: AnyObject) => PropertyKey[] =

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 65–68why, one step back
65 typeof Reflect !== "undefined" && Reflect.ownKeys
66 ? Reflect.ownKeys67 : typeof Object.getOwnPropertySymbols !== "undefined"68 ? obj =>

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 69–72why, one step back
69 Object.getOwnPropertyNames(obj).concat(
70 Object.getOwnPropertySymbols(obj) as any71 )72 : /* istanbul ignore next */ Object.getOwnPropertyNames

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 73–75why, one step back
73
74export function each<T extends Objectish>(75 obj: T,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 76–78why, one step back
76 iter: (key: string | number, value: any, source: T) => void
77): void78export function each(obj: any, iter: any) {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 79why, one step back
79 if (getArchtype(obj) === ArchtypeObject) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 80why, one step back
80 ownKeys(obj).forEach(key => iter(key, obj[key], obj))

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 81–82why, one step back
81 } else {
82 obj.forEach((entry: any, index: any) => iter(index, entry, obj))

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 83–85why, one step back
83 }
84}85

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 87why, one step back
87export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 88–91why, one step back
88 const desc = Object.getOwnPropertyDescriptor(base, prop)
89 return desc && desc.enumerable ? true : false90}91

Some measurements fixesMichel Weststratebefore adoption737432a

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

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 93why, one step back
93export function getArchtype(thing: any): 0 | 1 | 2 | 3 {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 94–96why, one step back
94 /* istanbul ignore next */
95 if (thing[DRAFT_STATE]) {96 switch ((thing as Drafted)[DRAFT_STATE].type) {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 97–106why, one step back
97 case ProxyTypeES5Object:
98 case ProxyTypeProxyObject:99 return ArchtypeObject100 case ProxyTypeES5Array:101 case ProxyTypeProxyArray:102 return ArchtypeArray103 case ProxyTypeMap:104 return ArchtypeMap105 case ProxyTypeSet:106 return ArchtypeSet

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 107–109why, one step back
107 }
108 }109 return Array.isArray(thing)

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 110why, one step back
110 ? ArchtypeArray

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 112why, one step back
112 ? ArchtypeMap

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 114–115why, one step back
114 ? ArchtypeSet
115 : ArchtypeObject

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 116–117why, one step back
116}
117

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 119why, one step back
119export function has(thing: any, prop: PropertyKey): boolean {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 120why, one step back
120 return getArchtype(thing) === ArchtypeMap

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 121–124why, one step back
121 ? thing.has(prop)
122 : Object.prototype.hasOwnProperty.call(thing, prop)123}124

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 126–127why, one step back
126export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {
127 // @ts-ignore

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 128why, one step back
128 return getArchtype(thing) === ArchtypeMap ? thing.get(prop) : thing[prop]

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 129–130why, one step back
129}
130

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 132–133why, one step back
132export function set(thing: any, propOrOldValue: PropertyKey, value: any) {
133 switch (getArchtype(thing)) {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 134why, one step back
134 case ArchtypeMap:

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 135–136why, one step back
135 thing.set(propOrOldValue, value)
136 break

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 137why, one step back
137 case ArchtypeSet:

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 138–145why, one step back
138 thing.delete(propOrOldValue)
139 thing.add(value)140 break141 default:142 thing[propOrOldValue] = value143 }144}145

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 147why, one step back
147export function is(x: any, y: any): boolean {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 148–155why, one step back
148 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
149 if (x === y) {150 return x !== 0 || 1 / x === 1 / y151 } else {152 return x !== x && y !== y153 }154}155

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 157why, one step back
157export function isMap(target: any): target is AnyMap {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 158–160why, one step back
158 return hasMap && target instanceof Map
159}160

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 162why, one step back
162export function isSet(target: any): target is AnySet {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 163–164why, one step back
163 return hasSet && target instanceof Set
164}

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 166why, one step back
166export function latest(state: ImmerState): any {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 167why, one step back
167 return state.copy_ || state.base_

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 168–169why, one step back
168}
169

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 171–177why, one step back
171export function shallowCopy<T extends AnyObject | AnyArray>(
172 base: T,173 invokeGetters?: boolean174): T175export function shallowCopy(base: any, invokeGetters = false) {176 if (Array.isArray(base)) return base.slice()177 const clone = Object.create(Object.getPrototypeOf(base))

Some notesMichel Weststratebefore adoption746dc6f

line 178why, one step back
178 // TODO: each?

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 179–181why, one step back
179 ownKeys(base).forEach(key => {
180 if (key === DRAFT_STATE) {181 return // Never copy over draft state.

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 182why, one step back
182 }

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 183–185why, one step back
183 const desc = Object.getOwnPropertyDescriptor(base, key)!
184 let {value} = desc185 if (desc.get) {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 186why, one step back
186 invariant(invokeGetters, "Immer drafts cannot have computed properties")

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 187why, one step back
187 value = desc.get.call(base)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 188why, one step back
188 }

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 189–199why, one step back
189 if (desc.enumerable) {
190 clone[key] = value191 } else {192 Object.defineProperty(clone, key, {193 value,194 writable: true,195 configurable: true196 })197 }198 })199 return clone

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 200–201why, one step back
200}
201

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 202why, one step back
202export function freeze(obj: any, deep: boolean): void {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 203why, one step back
203 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) return

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 204why, one step back
204 const type = getArchtype(obj)

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 205why, one step back
205 if (type === ArchtypeSet) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 206why, one step back
206 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 207why, one step back
207 } else if (type === ArchtypeMap) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 208–214why, one step back
208 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any
209 }210 Object.freeze(obj)211 if (deep) each(obj, (_, value) => freeze(value, true))212}213214function dontMutateFrozenCollections() {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 215why, one step back
215 invariant(false, "This object has been frozen and should not be mutated")

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 216–217why, one step back
216}
217

Some notesMichel Weststratebefore adoption746dc6f

line 218why, one step back
218// TODO: used only once, inline

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 219–225why, one step back
219export function createHiddenProperty(
220 target: AnyObject,221 prop: PropertyKey,222 value: any223) {224 Object.defineProperty(target, prop, {225 value: value,

Some notesMichel Weststratebefore adoption746dc6f

line 226why, one step back
226 // enumerable: false <- the default

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 227–228why, one step back
227 writable: true
228 })

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 229why, one step back
229}

separating plugins from coreMichel Weststratebefore adoption124518b

lines 230–231why, one step back
230
231export function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 232why, one step back
232 invariant(

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 233why, one step back
233 !state.revoked_,

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

lines 234–236why, one step back
234 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +
235 JSON.stringify(latest(state))236 )

separating plugins from coreMichel Weststratebefore adoption124518b

line 237why, one step back
237}