common.ts
This file
Path
src/common.ts
229 lines · 5.8 KiB · TypeScript
Last changed by
Some notesMichel Weststratebefore adoption746dc6f
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–12why, one step back5 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 back13 Archtype,14 hasMap15} from "./internal"use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 16why, one step back16import invariant from "tiny-invariant"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 17why, one step back17feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 18why, one step back18/** Returns true if the given value is an Immer draft */Some measurements fixesMichel Weststratebefore adoption737432a
line 19why, one step back19/*#__PURE__*/feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 20–24why, one step back20export 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 back25/*#__PURE__*/feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 26–29why, one step back26export 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 back30 Array.isArray(value) ||feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 31–37why, one step back31 !!value[DRAFTABLE] ||32 !!value.constructor[DRAFTABLE] ||33 isMap(value) ||34 isSet(value)35 )36}37Some measurements fixesMichel Weststratebefore adoption737432a
line 38why, one step back38/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 39why, one step back39export function isPlainObject(value: any): boolean {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 40–45why, one step back40 if (!value || typeof value !== "object") return false41 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 back46/*#__PURE__*/feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1
lines 47–48why, one step back47export function original<T>(value: T): T | undefined48export function original(value: Drafted<any>): any {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 49why, one step back49 if (value && value[DRAFT_STATE]) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 50why, one step back50 return value[DRAFT_STATE].base_ as anyfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 51–54why, one step back51 }52 // otherwise return undefined53}54Some measurements fixesMichel Weststratebefore adoption737432a
line 55why, one step back55/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 56why, one step back56export const ownKeys: (target: AnyObject) => PropertyKey[] =feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 57–60why, one step back57 typeof Reflect !== "undefined" && Reflect.ownKeys58 ? Reflect.ownKeys59 : typeof Object.getOwnPropertySymbols !== "undefined"60 ? obj =>feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 61–64why, one step back61 Object.getOwnPropertyNames(obj).concat(62 Object.getOwnPropertySymbols(obj) as any63 )64 : /* istanbul ignore next */ Object.getOwnPropertyNamesfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 65–67why, one step back6566export function each<T extends Objectish>(67 obj: T,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 68–71why, one step back68 iter: (key: string | number, value: any, source: T) => void69): void70export function each(obj: any, iter: any) {71 if (getArchtype(obj) === Archtype.Object) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 72why, one step back72 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 back73 } else {74 obj.forEach((entry: any, index: any) => iter(index, entry, obj))feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 75–77why, one step back75 }76}77Some measurements fixesMichel Weststratebefore adoption737432a
line 78why, one step back78/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 79why, one step back79export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 80–83why, one step back80 const desc = Object.getOwnPropertyDescriptor(base, prop)81 return desc && desc.enumerable ? true : false82}83Some measurements fixesMichel Weststratebefore adoption737432a
line 84why, one step back84/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 85–109why, one step back85export function getArchtype(thing: any): Archtype {86 /* istanbul ignore next */87 if (thing[DRAFT_STATE]) {88 switch ((thing as Drafted)[DRAFT_STATE].type) {89 case ProxyType.ES5Object:90 case ProxyType.ProxyObject:91 return Archtype.Object92 case ProxyType.ES5Array:93 case ProxyType.ProxyArray:94 return Archtype.Array95 case ProxyType.Map:96 return Archtype.Map97 case ProxyType.Set:98 return Archtype.Set99 }100 }101 return Array.isArray(thing)102 ? Archtype.Array103 : isMap(thing)104 ? Archtype.Map105 : isSet(thing)106 ? Archtype.Set107 : Archtype.Object108}109Some measurements fixesMichel Weststratebefore adoption737432a
line 110why, one step back110/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 111–112why, one step back111export function has(thing: any, prop: PropertyKey): boolean {112 return getArchtype(thing) === Archtype.Mapfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 113–116why, one step back113 ? thing.has(prop)114 : Object.prototype.hasOwnProperty.call(thing, prop)115}116Some measurements fixesMichel Weststratebefore adoption737432a
line 117why, one step back117/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 118–120why, one step back118export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {119 // @ts-ignore120 return getArchtype(thing) === Archtype.Map ? thing.get(prop) : thing[prop]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 121–122why, one step back121}122Some measurements fixesMichel Weststratebefore adoption737432a
line 123why, one step back123/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 124–137why, one step back124export function set(thing: any, propOrOldValue: PropertyKey, value: any) {125 switch (getArchtype(thing)) {126 case Archtype.Map:127 thing.set(propOrOldValue, value)128 break129 case Archtype.Set:130 thing.delete(propOrOldValue)131 thing.add(value)132 break133 default:134 thing[propOrOldValue] = value135 }136}137Some measurements fixesMichel Weststratebefore adoption737432a
line 138why, one step back138/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 139why, one step back139export function is(x: any, y: any): boolean {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 140–147why, one step back140 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js141 if (x === y) {142 return x !== 0 || 1 / x === 1 / y143 } else {144 return x !== x && y !== y145 }146}147Some measurements fixesMichel Weststratebefore adoption737432a
line 148why, one step back148/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 149why, one step back149export function isMap(target: any): target is AnyMap {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 150–152why, one step back150 return hasMap && target instanceof Map151}152Some measurements fixesMichel Weststratebefore adoption737432a
line 153why, one step back153/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 154why, one step back154export function isSet(target: any): target is AnySet {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 155–156why, one step back155 return hasSet && target instanceof Set156}Some measurements fixesMichel Weststratebefore adoption737432a
line 157why, one step back157/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 158why, one step back158export function latest(state: ImmerState): any {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 159why, one step back159 return state.copy_ || state.base_feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 160–161why, one step back160}161Some measurements fixesMichel Weststratebefore adoption737432a
line 162why, one step back162/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 163–169why, one step back163export function shallowCopy<T extends AnyObject | AnyArray>(164 base: T,165 invokeGetters?: boolean166): T167export function shallowCopy(base: any, invokeGetters = false) {168 if (Array.isArray(base)) return base.slice()169 const clone = Object.create(Object.getPrototypeOf(base))Some notesMichel Weststratebefore adoption746dc6f
line 170why, one step back170 // TODO: each?feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 171–173why, one step back171 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 back174 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 175–177why, one step back175 const desc = Object.getOwnPropertyDescriptor(base, key)!176 let {value} = desc177 if (desc.get) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 178why, one step back178 invariant(invokeGetters, "Immer drafts cannot have computed properties")feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 179why, one step back179 value = desc.get.call(base)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 180why, one step back180 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 181–191why, one step back181 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 clonefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 192–193why, one step back192}193feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 194why, one step back194export function freeze(obj: any, deep: boolean): void {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 195why, one step back195 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) returnfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 196–197why, one step back196 const type = getArchtype(obj)197 if (type === Archtype.Set) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 198why, one step back198 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as anyfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 199why, one step back199 } else if (type === Archtype.Map) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 200–206why, one step back200 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any201 }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 back207 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–209why, one step back208}209Some notesMichel Weststratebefore adoption746dc6f
line 210why, one step back210// TODO: used only once, inlinefeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 211–217why, one step back211export function createHiddenProperty(212 target: AnyObject,213 prop: PropertyKey,214 value: any215) {216 Object.defineProperty(target, prop, {217 value: value,Some notesMichel Weststratebefore adoption746dc6f
line 218why, one step back218 // enumerable: false <- the defaultfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 219–220why, one step back219 writable: true220 })feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 221why, one step back221}separating plugins from coreMichel Weststratebefore adoption124518b
lines 222–223why, one step back222223export function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 224why, one step back224 invariant(Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 225why, one step back225 !state.revoked_,use invariant -321 bytesMichel Weststratebefore adoptionac57da5
lines 226–228why, one step back226 "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " +227 JSON.stringify(latest(state))228 )separating plugins from coreMichel Weststratebefore adoption124518b
line 229why, one step back229}