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 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–110why, one step back85export 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}110Some measurements fixesMichel Weststratebefore adoption737432a
line 111why, one step back111/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 112–113why, one step back112export function has(thing: any, prop: PropertyKey): boolean {113 return getArchtype(thing) === Archtype.Mapfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 114–117why, one step back114 ? thing.has(prop)115 : Object.prototype.hasOwnProperty.call(thing, prop)116}117Some measurements fixesMichel Weststratebefore adoption737432a
line 118why, one step back118/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 119–121why, one step back119export 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 back122}123Some measurements fixesMichel Weststratebefore adoption737432a
line 124why, one step back124/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 125–138why, one step back125export 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}138Some measurements fixesMichel Weststratebefore adoption737432a
line 139why, one step back139/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 140why, one step back140export function is(x: any, y: any): boolean {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 141–148why, one step back141 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js142 if (x === y) {143 return x !== 0 || 1 / x === 1 / y144 } else {145 return x !== x && y !== y146 }147}148Some measurements fixesMichel Weststratebefore adoption737432a
line 149why, one step back149/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 150why, one step back150export function isMap(target: any): target is AnyMap {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 151–153why, one step back151 return hasMap && target instanceof Map152}153Some measurements fixesMichel Weststratebefore adoption737432a
line 154why, one step back154/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 155why, one step back155export function isSet(target: any): target is AnySet {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 156–157why, one step back156 return hasSet && target instanceof Set157}Some measurements fixesMichel Weststratebefore adoption737432a
line 158why, one step back158/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 159why, one step back159export function latest(state: ImmerState): any {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 160why, one step back160 return state.copy_ || state.base_feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 161–162why, one step back161}162Some measurements fixesMichel Weststratebefore adoption737432a
line 163why, one step back163/*#__PURE__*/feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 164–173why, one step back164export 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 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–223why, one step back208}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 back224 invariant(false, "Illegal state, please file a bug")feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 225why, one step back225}separating plugins from coreMichel Weststratebefore adoption124518b
lines 226–227why, one step back226227export function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 228why, one step back228 invariant(Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 229why, one step back229 !state.revoked_,use invariant -321 bytesMichel Weststratebefore adoptionac57da5
lines 230–232why, one step back230 "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 back233}