Skip to content
Myr

common.ts

This file

Path

src/common.ts

233 lines · 5.8 KiB · TypeScript

Last changed by

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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–12why, one step back
5 Objectish,
6 Drafted,7 AnyObject,8 AnyArray,9 AnyMap,10 AnySet,11 ImmerState,12 ProxyType,

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

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

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

line 17why, one step back
17

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 20–24why, one step back
20export function isDraft(value: any): boolean {
21 return !!value && !!value[DRAFT_STATE]22}2324/** Returns true if the given value can be drafted by Immer */

Some measurements fixesMichel Weststratebefore adoption737432a

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 26–29why, one step back
26export function isDraftable(value: any): boolean {
27 if (!value) return false28 return (29 isPlainObject(value) ||

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 31–37why, one step back
31 !!value[DRAFTABLE] ||
32 !!value.constructor[DRAFTABLE] ||33 isMap(value) ||34 isSet(value)35 )36}37

Some measurements fixesMichel Weststratebefore adoption737432a

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 40–45why, one step back
40 if (!value || typeof value !== "object") return false
41 const proto = Object.getPrototypeOf(value)42 return !proto || proto === Object.prototype43}4445/** Get the underlying object that is represented by the given draft */

Some measurements fixesMichel Weststratebefore adoption737432a

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 51–54why, one step back
51 }
52 // otherwise return undefined53}54

Some measurements fixesMichel Weststratebefore adoption737432a

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 57–60why, one step back
57 typeof Reflect !== "undefined" && Reflect.ownKeys
58 ? Reflect.ownKeys59 : typeof Object.getOwnPropertySymbols !== "undefined"60 ? obj =>

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

lines 61–64why, one step back
61 Object.getOwnPropertyNames(obj).concat(
62 Object.getOwnPropertySymbols(obj) as any63 )64 : /* istanbul ignore next */ Object.getOwnPropertyNames

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 65–67why, one step back
65
66export function each<T extends Objectish>(67 obj: T,

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

lines 68–71why, one step back
68 iter: (key: string | number, value: any, source: T) => void
69): void70export function each(obj: any, iter: any) {71 if (getArchtype(obj) === Archtype.Object) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 75–77why, one step back
75 }
76}77

Some measurements fixesMichel Weststratebefore adoption737432a

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 80–83why, one step back
80 const desc = Object.getOwnPropertyDescriptor(base, prop)
81 return desc && desc.enumerable ? true : false82}83

Some measurements fixesMichel Weststratebefore adoption737432a

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

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

lines 85–110why, one step back
85export function getArchtype(thing: any): Archtype {
86 /* istanbul ignore next */87 if (!thing) die()88 if (thing[DRAFT_STATE]) {89 switch ((thing as Drafted)[DRAFT_STATE].type) {90 case ProxyType.ES5Object:91 case ProxyType.ProxyObject:92 return Archtype.Object93 case ProxyType.ES5Array:94 case ProxyType.ProxyArray:95 return Archtype.Array96 case ProxyType.Map:97 return Archtype.Map98 case ProxyType.Set:99 return Archtype.Set100 }101 }102 return Array.isArray(thing)103 ? Archtype.Array104 : isMap(thing)105 ? Archtype.Map106 : isSet(thing)107 ? Archtype.Set108 : Archtype.Object109}110

Some measurements fixesMichel Weststratebefore adoption737432a

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

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

lines 112–113why, one step back
112export function has(thing: any, prop: PropertyKey): boolean {
113 return getArchtype(thing) === Archtype.Map

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 114–117why, one step back
114 ? thing.has(prop)
115 : Object.prototype.hasOwnProperty.call(thing, prop)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

lines 119–121why, one step back
119export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {
120 // @ts-ignore121 return getArchtype(thing) === Archtype.Map ? thing.get(prop) : thing[prop]

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 122–123why, one step back
122}
123

Some measurements fixesMichel Weststratebefore adoption737432a

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

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

lines 125–138why, one step back
125export function set(thing: any, propOrOldValue: PropertyKey, value: any) {
126 switch (getArchtype(thing)) {127 case Archtype.Map:128 thing.set(propOrOldValue, value)129 break130 case Archtype.Set:131 thing.delete(propOrOldValue)132 thing.add(value)133 break134 default:135 thing[propOrOldValue] = value136 }137}138

Some measurements fixesMichel Weststratebefore adoption737432a

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 141–148why, one step back
141 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
142 if (x === y) {143 return x !== 0 || 1 / x === 1 / y144 } else {145 return x !== x && y !== y146 }147}148

Some measurements fixesMichel Weststratebefore adoption737432a

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 151–153why, one step back
151 return hasMap && target instanceof Map
152}153

Some measurements fixesMichel Weststratebefore adoption737432a

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 156–157why, one step back
156 return hasSet && target instanceof Set
157}

Some measurements fixesMichel Weststratebefore adoption737432a

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

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

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 161–162why, one step back
161}
162

Some measurements fixesMichel Weststratebefore adoption737432a

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

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

lines 164–173why, one step back
164export function shallowCopy<T extends AnyObject | AnyArray>(
165 base: T,166 invokeGetters?: boolean167): T168export function shallowCopy(base: any, invokeGetters = false) {169 if (Array.isArray(base)) return base.slice()170 const clone = Object.create(Object.getPrototypeOf(base))171 ownKeys(base).forEach(key => {172 if (key === DRAFT_STATE) {173 return // Never copy over draft state.

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 174why, one step back
174 }

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

lines 175–177why, one step back
175 const desc = Object.getOwnPropertyDescriptor(base, key)!
176 let {value} = desc177 if (desc.get) {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 180why, one step back
180 }

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

lines 181–191why, one step back
181 if (desc.enumerable) {
182 clone[key] = value183 } else {184 Object.defineProperty(clone, key, {185 value,186 writable: true,187 configurable: true188 })189 }190 })191 return clone

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 192–193why, one step back
192}
193

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

lines 196–197why, one step back
196 const type = getArchtype(obj)
197 if (type === Archtype.Set) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

line 199why, one step back
199 } else if (type === Archtype.Map) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 200–206why, one step back
200 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any
201 }202 Object.freeze(obj)203 if (deep) each(obj, (_, value) => freeze(value, true))204}205206function dontMutateFrozenCollections() {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

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

lines 208–223why, one step back
208}
209210export function createHiddenProperty(211 target: AnyObject,212 prop: PropertyKey,213 value: any214) {215 Object.defineProperty(target, prop, {216 value: value,217 enumerable: false,218 writable: true219 })220}221222/* istanbul ignore next */223export function die(): never {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 224why, one step back
224 invariant(false, "Illegal state, please file a bug")

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 225why, one step back
225}

separating plugins from coreMichel Weststratebefore adoption124518b

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 228why, one step back
228 invariant(

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

separating plugins from coreMichel Weststratebefore adoption124518b

line 233why, one step back
233}