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 fileTake it
feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 1why, one step back1import {fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
lines 2–4why, one step back2 DRAFT_STATE,3 DRAFTABLE,4 hasSet,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 5–11why, one step back5 Objectish,6 Drafted,7 AnyObject,8 AnyArray,9 AnyMap,10 AnySet,11 ImmerState,Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 12–22why, one step back12 hasMap,13 ArchtypeObject,14 ProxyTypeES5Object,15 ProxyTypeProxyObject,16 ProxyTypeES5Array,17 ProxyTypeProxyArray,18 ArchtypeArray,19 ProxyTypeMap,20 ArchtypeMap,21 ProxyTypeSet,22 ArchtypeSetfix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
line 23why, one step back23} from "./internal"use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 24why, one step back24import invariant from "tiny-invariant"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 25why, one step back25feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 26why, one step back26/** Returns true if the given value is an Immer draft */Some measurements fixesMichel Weststratebefore adoption737432a
line 27why, one step back27/*#__PURE__*/feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 28–32why, one step back28export 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 back33/*#__PURE__*/feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 34–37why, one step back34export 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 back38 Array.isArray(value) ||feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 39–45why, one step back39 !!value[DRAFTABLE] ||40 !!value.constructor[DRAFTABLE] ||41 isMap(value) ||42 isSet(value)43 )44}45Some measurements fixesMichel Weststratebefore adoption737432a
line 46why, one step back46/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 47why, one step back47export function isPlainObject(value: any): boolean {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 48–53why, one step back48 if (!value || typeof value !== "object") return false49 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 back54/*#__PURE__*/feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
lines 55–56why, one step back55export function original<T>(value: T): T | undefined56export function original(value: Drafted<any>): any {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 57why, one step back57 if (value && value[DRAFT_STATE]) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 58why, one step back58 return value[DRAFT_STATE].base_ as anyfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 59–62why, one step back59 }60 // otherwise return undefined61}62Some measurements fixesMichel Weststratebefore adoption737432a
line 63why, one step back63/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 64why, one step back64export const ownKeys: (target: AnyObject) => PropertyKey[] =feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 65–68why, one step back65 typeof Reflect !== "undefined" && Reflect.ownKeys66 ? Reflect.ownKeys67 : typeof Object.getOwnPropertySymbols !== "undefined"68 ? obj =>feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 69–72why, one step back69 Object.getOwnPropertyNames(obj).concat(70 Object.getOwnPropertySymbols(obj) as any71 )72 : /* istanbul ignore next */ Object.getOwnPropertyNamesfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 73–75why, one step back7374export function each<T extends Objectish>(75 obj: T,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 76–78why, one step back76 iter: (key: string | number, value: any, source: T) => void77): void78export function each(obj: any, iter: any) {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 79why, one step back79 if (getArchtype(obj) === ArchtypeObject) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 80why, one step back80 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 back81 } else {82 obj.forEach((entry: any, index: any) => iter(index, entry, obj))feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 83–85why, one step back83 }84}85Some measurements fixesMichel Weststratebefore adoption737432a
line 86why, one step back86/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 87why, one step back87export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 88–91why, one step back88 const desc = Object.getOwnPropertyDescriptor(base, prop)89 return desc && desc.enumerable ? true : false90}91Some measurements fixesMichel Weststratebefore adoption737432a
line 92why, one step back92/*#__PURE__*/Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 93why, one step back93export function getArchtype(thing: any): 0 | 1 | 2 | 3 {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 94–96why, one step back94 /* 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 back97 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 ArchtypeSetfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 107–109why, one step back107 }108 }109 return Array.isArray(thing)Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 110why, one step back110 ? ArchtypeArrayfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 111why, one step back111 : isMap(thing)Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 112why, one step back112 ? ArchtypeMapfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 113why, one step back113 : isSet(thing)Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 114–115why, one step back114 ? ArchtypeSet115 : ArchtypeObjectfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 116–117why, one step back116}117Some measurements fixesMichel Weststratebefore adoption737432a
line 118why, one step back118/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 119why, one step back119export function has(thing: any, prop: PropertyKey): boolean {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 120why, one step back120 return getArchtype(thing) === ArchtypeMapfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 121–124why, one step back121 ? thing.has(prop)122 : Object.prototype.hasOwnProperty.call(thing, prop)123}124Some measurements fixesMichel Weststratebefore adoption737432a
line 125why, one step back125/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 126–127why, one step back126export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {127 // @ts-ignoreKilled enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 128why, one step back128 return getArchtype(thing) === ArchtypeMap ? thing.get(prop) : thing[prop]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 129–130why, one step back129}130Some measurements fixesMichel Weststratebefore adoption737432a
line 131why, one step back131/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 132–133why, one step back132export function set(thing: any, propOrOldValue: PropertyKey, value: any) {133 switch (getArchtype(thing)) {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 134why, one step back134 case ArchtypeMap:feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 135–136why, one step back135 thing.set(propOrOldValue, value)136 breakKilled enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 137why, one step back137 case ArchtypeSet:feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 138–145why, one step back138 thing.delete(propOrOldValue)139 thing.add(value)140 break141 default:142 thing[propOrOldValue] = value143 }144}145Some measurements fixesMichel Weststratebefore adoption737432a
line 146why, one step back146/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 147why, one step back147export function is(x: any, y: any): boolean {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 148–155why, one step back148 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js149 if (x === y) {150 return x !== 0 || 1 / x === 1 / y151 } else {152 return x !== x && y !== y153 }154}155Some measurements fixesMichel Weststratebefore adoption737432a
line 156why, one step back156/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 157why, one step back157export function isMap(target: any): target is AnyMap {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 158–160why, one step back158 return hasMap && target instanceof Map159}160Some measurements fixesMichel Weststratebefore adoption737432a
line 161why, one step back161/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 162why, one step back162export function isSet(target: any): target is AnySet {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 163–164why, one step back163 return hasSet && target instanceof Set164}Some measurements fixesMichel Weststratebefore adoption737432a
line 165why, one step back165/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 166why, one step back166export function latest(state: ImmerState): any {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 167why, one step back167 return state.copy_ || state.base_feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 168–169why, one step back168}169Some measurements fixesMichel Weststratebefore adoption737432a
line 170why, one step back170/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 171–177why, one step back171export 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 back178 // TODO: each?feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 179–181why, one step back179 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 back182 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 183–185why, one step back183 const desc = Object.getOwnPropertyDescriptor(base, key)!184 let {value} = desc185 if (desc.get) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 186why, one step back186 invariant(invokeGetters, "Immer drafts cannot have computed properties")feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 187why, one step back187 value = desc.get.call(base)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 188why, one step back188 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 189–199why, one step back189 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 clonefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 200–201why, one step back200}201feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 202why, one step back202export function freeze(obj: any, deep: boolean): void {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 203why, one step back203 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) returnfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 204why, one step back204 const type = getArchtype(obj)Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 205why, one step back205 if (type === ArchtypeSet) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 206why, one step back206 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as anyKilled enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 207why, one step back207 } else if (type === ArchtypeMap) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 208–214why, one step back208 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any209 }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 back215 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 back216}217Some notesMichel Weststratebefore adoption746dc6f
line 218why, one step back218// TODO: used only once, inlinefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 219–225why, one step back219export 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 back226 // enumerable: false <- the defaultfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 227–228why, one step back227 writable: true228 })feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 229why, one step back229}separating plugins from coreMichel Weststratebefore adoption124518b
lines 230–231why, one step back230231export function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 232why, one step back232 invariant(Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 233why, one step back233 !state.revoked_,use invariant -321 bytesMichel Weststratebefore adoptionac57da5
lines 234–236why, one step back234 "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 back237}