common.ts
This file
Path
src/common.ts
211 lines · 5.3 KiB · TypeScript
Last changed by
fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a
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"feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 16why, one step back16feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 17–26why, one step back17/** Returns true if the given value is an Immer draft */18export function isDraft(value: any): boolean {19 return !!value && !!value[DRAFT_STATE]20}2122/** Returns true if the given value can be drafted by Immer */23export function isDraftable(value: any): boolean {24 if (!value) return false25 return (26 isPlainObject(value) ||feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 27why, one step back27 Array.isArray(value) ||feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 28–34why, one step back28 !!value[DRAFTABLE] ||29 !!value.constructor[DRAFTABLE] ||30 isMap(value) ||31 isSet(value)32 )33}34feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 35why, one step back35export function isPlainObject(value: any): boolean {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 36–41why, one step back36 if (!value || typeof value !== "object") return false37 const proto = Object.getPrototypeOf(value)38 return !proto || proto === Object.prototype39}4041/** Get the underlying object that is represented by the given draft */feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 42why, one step back42export function original<T>(value: Drafted<T>): T | undefined {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 43why, one step back43 if (value && value[DRAFT_STATE]) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 44why, one step back44 return value[DRAFT_STATE].base as anyfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 45–48why, one step back45 }46 // otherwise return undefined47}48feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 49why, one step back49export const ownKeys: (target: AnyObject) => PropertyKey[] =feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 50–53why, one step back50 typeof Reflect !== "undefined" && Reflect.ownKeys51 ? Reflect.ownKeys52 : typeof Object.getOwnPropertySymbols !== "undefined"53 ? obj =>feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 54–57why, one step back54 Object.getOwnPropertyNames(obj).concat(55 Object.getOwnPropertySymbols(obj) as any56 )57 : /* istanbul ignore next */ Object.getOwnPropertyNamesfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 58–60why, one step back5859export function each<T extends Objectish>(60 obj: T,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 61–64why, one step back61 iter: (key: string | number, value: any, source: T) => void62): void63export function each(obj: any, iter: any) {64 if (getArchtype(obj) === Archtype.Object) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 65why, one step back65 ownKeys(obj).forEach(key => iter(key, obj[key], obj))feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 66–67why, one step back66 } else {67 obj.forEach((entry: any, index: any) => iter(index, entry, obj))feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 68–70why, one step back68 }69}70feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 71why, one step back71export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 72–75why, one step back72 const desc = Object.getOwnPropertyDescriptor(base, prop)73 return desc && desc.enumerable ? true : false74}75feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 76–103why, one step back76export function getArchtype(thing: any): Archtype {77 /* istanbul ignore next */78 if (!thing) die()79 if (thing[DRAFT_STATE]) {80 switch ((thing as Drafted)[DRAFT_STATE].type) {81 case ProxyType.ES5Object:82 case ProxyType.ProxyObject:83 return Archtype.Object84 case ProxyType.ES5Array:85 case ProxyType.ProxyArray:86 return Archtype.Array87 case ProxyType.Map:88 return Archtype.Map89 case ProxyType.Set:90 return Archtype.Set91 }92 }93 return Array.isArray(thing)94 ? Archtype.Array95 : isMap(thing)96 ? Archtype.Map97 : isSet(thing)98 ? Archtype.Set99 : Archtype.Object100}101102export function has(thing: any, prop: PropertyKey): boolean {103 return getArchtype(thing) === Archtype.Mapfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 104–107why, one step back104 ? thing.has(prop)105 : Object.prototype.hasOwnProperty.call(thing, prop)106}107feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 108–110why, one step back108export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {109 // @ts-ignore110 return getArchtype(thing) === Archtype.Map ? thing.get(prop) : thing[prop]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 111–112why, one step back111}112feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 113–127why, one step back113export function set(thing: any, propOrOldValue: PropertyKey, value: any) {114 switch (getArchtype(thing)) {115 case Archtype.Map:116 thing.set(propOrOldValue, value)117 break118 case Archtype.Set:119 thing.delete(propOrOldValue)120 thing.add(value)121 break122 default:123 thing[propOrOldValue] = value124 }125}126127export function is(x: any, y: any): boolean {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 128–135why, one step back128 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js129 if (x === y) {130 return x !== 0 || 1 / x === 1 / y131 } else {132 return x !== x && y !== y133 }134}135feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 136why, one step back136export function isMap(target: any): target is AnyMap {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 137–139why, one step back137 return hasMap && target instanceof Map138}139feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 140why, one step back140export function isSet(target: any): target is AnySet {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 141–143why, one step back141 return hasSet && target instanceof Set142}143feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 144–145why, one step back144export function latest(state: ImmerState): any {145 return state.copy || state.basefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 146–147why, one step back146}147feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 148–157why, one step back148export function shallowCopy<T extends AnyObject | AnyArray>(149 base: T,150 invokeGetters?: boolean151): T152export function shallowCopy(base: any, invokeGetters = false) {153 if (Array.isArray(base)) return base.slice()154 const clone = Object.create(Object.getPrototypeOf(base))155 ownKeys(base).forEach(key => {156 if (key === DRAFT_STATE) {157 return // Never copy over draft state.feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 158why, one step back158 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 159–163why, one step back159 const desc = Object.getOwnPropertyDescriptor(base, key)!160 let {value} = desc161 if (desc.get) {162 if (!invokeGetters) {163 throw new Error("Immer drafts cannot have computed properties")feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 164why, one step back164 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 165why, one step back165 value = desc.get.call(base)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 166why, one step back166 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 167–177why, one step back167 if (desc.enumerable) {168 clone[key] = value169 } else {170 Object.defineProperty(clone, key, {171 value,172 writable: true,173 configurable: true174 })175 }176 })177 return clonefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 178–179why, one step back178}179feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 180why, one step back180export function freeze(obj: any, deep: boolean): void {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 181why, one step back181 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) returnfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 182–183why, one step back182 const type = getArchtype(obj)183 if (type === Archtype.Set) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 184why, one step back184 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as anyfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 185why, one step back185 } else if (type === Archtype.Map) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 186–193why, one step back186 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any187 }188 Object.freeze(obj)189 if (deep) each(obj, (_, value) => freeze(value, true))190}191192function dontMutateFrozenCollections() {193 throw new Error("This object has been frozen and should not be mutated")feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 194–210why, one step back194}195196export function createHiddenProperty(197 target: AnyObject,198 prop: PropertyKey,199 value: any200) {201 Object.defineProperty(target, prop, {202 value: value,203 enumerable: false,204 writable: true205 })206}207208/* istanbul ignore next */209export function die(): never {210 throw new Error("Illegal state, please file a bug")feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 211why, one step back211}