common.ts
This file
Path
src/common.ts
245 lines · 6.4 KiB · TypeScript
Last changed by
feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
History of this fileTake it
feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 1–11why, one step back1import {2 Objectish,3 Drafted,4 AnyObject,5 AnyArray,6 AnyMap,7 AnySet,8 ImmerState,9 ProxyType,10 Archtype11} from "./types"feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 12–15why, one step back1213/** Use a class type for `nothing` so its type is unique */14export class Nothing {15 // This lets us do `Exclude<T, Nothing>`feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 16–17why, one step back16 // @ts-ignore17 private _!: unique symbolfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 18–19why, one step back18}19feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 20–23why, one step back20const hasSymbol = typeof Symbol !== "undefined"21export const hasMap = typeof Map !== "undefined"22export const hasSet = typeof Set !== "undefined"23feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 24–26why, one step back24/**25 * The sentinel value returned by producers to replace the draft with undefined.26 */feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 27–29why, one step back27export const NOTHING: Nothing = hasSymbol28 ? Symbol("immer-nothing")29 : ({["immer-nothing"]: true} as any)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 30–38why, one step back3031/**32 * To let Immer treat your class instances as plain immutable objects33 * (albeit with a custom prototype), you must define either an instance property34 * or a static property on each of your custom classes.35 *36 * Otherwise, your class instance will never be drafted, which means it won't be37 * safe to mutate in a produce callback.38 */feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 39–41why, one step back39export const DRAFTABLE: unique symbol = hasSymbol40 ? Symbol("immer-draftable")41 : ("__$immer_draftable" as any)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 42why, one step back42feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 43–45why, one step back43export const DRAFT_STATE: unique symbol = hasSymbol44 ? Symbol("immer-state")45 : ("__$immer_state" as any)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 46why, one step back46feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 47–50why, one step back47export const iteratorSymbol: typeof Symbol.iterator = hasSymbol48 ? Symbol.iterator49 : ("@@iterator" as any)50feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 51–60why, one step back51/** Returns true if the given value is an Immer draft */52export function isDraft(value: any): boolean {53 return !!value && !!value[DRAFT_STATE]54}5556/** Returns true if the given value can be drafted by Immer */57export function isDraftable(value: any): boolean {58 if (!value) return false59 return (60 isPlainObject(value) ||feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 61why, one step back61 Array.isArray(value) ||feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 62–68why, one step back62 !!value[DRAFTABLE] ||63 !!value.constructor[DRAFTABLE] ||64 isMap(value) ||65 isSet(value)66 )67}68feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 69why, one step back69export function isPlainObject(value: any): boolean {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 70–75why, one step back70 if (!value || typeof value !== "object") return false71 const proto = Object.getPrototypeOf(value)72 return !proto || proto === Object.prototype73}7475/** Get the underlying object that is represented by the given draft */feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 76why, one step back76export function original<T>(value: Drafted<T>): T | undefined {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 77why, one step back77 if (value && value[DRAFT_STATE]) {feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 78why, one step back78 return value[DRAFT_STATE].base as anyfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 79–82why, one step back79 }80 // otherwise return undefined81}82feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 83why, one step back83export const ownKeys: (target: AnyObject) => PropertyKey[] =feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 84–87why, one step back84 typeof Reflect !== "undefined" && Reflect.ownKeys85 ? Reflect.ownKeys86 : typeof Object.getOwnPropertySymbols !== "undefined"87 ? obj =>feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 88–91why, one step back88 Object.getOwnPropertyNames(obj).concat(89 Object.getOwnPropertySymbols(obj) as any90 )91 : /* istanbul ignore next */ Object.getOwnPropertyNamesfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 92–94why, one step back9293export function each<T extends Objectish>(94 obj: T,feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 95–98why, one step back95 iter: (key: string | number, value: any, source: T) => void96): void97export function each(obj: any, iter: any) {98 if (getArchtype(obj) === Archtype.Object) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 99why, one step back99 ownKeys(obj).forEach(key => iter(key, obj[key], obj))feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 100–101why, one step back100 } else {101 obj.forEach((entry: any, index: any) => iter(index, entry, obj))feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 102–104why, one step back102 }103}104feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 105why, one step back105export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 106–109why, one step back106 const desc = Object.getOwnPropertyDescriptor(base, prop)107 return desc && desc.enumerable ? true : false108}109feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 110–137why, one step back110export function getArchtype(thing: any): Archtype {111 /* istanbul ignore next */112 if (!thing) die()113 if (thing[DRAFT_STATE]) {114 switch ((thing as Drafted)[DRAFT_STATE].type) {115 case ProxyType.ES5Object:116 case ProxyType.ProxyObject:117 return Archtype.Object118 case ProxyType.ES5Array:119 case ProxyType.ProxyArray:120 return Archtype.Array121 case ProxyType.Map:122 return Archtype.Map123 case ProxyType.Set:124 return Archtype.Set125 }126 }127 return Array.isArray(thing)128 ? Archtype.Array129 : isMap(thing)130 ? Archtype.Map131 : isSet(thing)132 ? Archtype.Set133 : Archtype.Object134}135136export function has(thing: any, prop: PropertyKey): boolean {137 return getArchtype(thing) === Archtype.Mapfeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 138–141why, one step back138 ? thing.has(prop)139 : Object.prototype.hasOwnProperty.call(thing, prop)140}141feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 142–144why, one step back142export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {143 // @ts-ignore144 return getArchtype(thing) === Archtype.Map ? thing.get(prop) : thing[prop]feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 145–146why, one step back145}146feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 147–161why, one step back147export function set(thing: any, propOrOldValue: PropertyKey, value: any) {148 switch (getArchtype(thing)) {149 case Archtype.Map:150 thing.set(propOrOldValue, value)151 break152 case Archtype.Set:153 thing.delete(propOrOldValue)154 thing.add(value)155 break156 default:157 thing[propOrOldValue] = value158 }159}160161export function is(x: any, y: any): boolean {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 162–169why, one step back162 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js163 if (x === y) {164 return x !== 0 || 1 / x === 1 / y165 } else {166 return x !== x && y !== y167 }168}169feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 170why, one step back170export function isMap(target: any): target is AnyMap {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 171–173why, one step back171 return hasMap && target instanceof Map172}173feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 174why, one step back174export function isSet(target: any): target is AnySet {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 175–177why, one step back175 return hasSet && target instanceof Set176}177feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 178–179why, one step back178export function latest(state: ImmerState): any {179 return state.copy || state.basefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 180–181why, one step back180}181feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 182–191why, one step back182export function shallowCopy<T extends AnyObject | AnyArray>(183 base: T,184 invokeGetters?: boolean185): T186export function shallowCopy(base: any, invokeGetters = false) {187 if (Array.isArray(base)) return base.slice()188 const clone = Object.create(Object.getPrototypeOf(base))189 ownKeys(base).forEach(key => {190 if (key === DRAFT_STATE) {191 return // Never copy over draft state.feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 192why, one step back192 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 193–197why, one step back193 const desc = Object.getOwnPropertyDescriptor(base, key)!194 let {value} = desc195 if (desc.get) {196 if (!invokeGetters) {197 throw new Error("Immer drafts cannot have computed properties")feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 198why, one step back198 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 199why, one step back199 value = desc.get.call(base)feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 200why, one step back200 }feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 201–211why, one step back201 if (desc.enumerable) {202 clone[key] = value203 } else {204 Object.defineProperty(clone, key, {205 value,206 writable: true,207 configurable: true208 })209 }210 })211 return clonefeat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 212–213why, one step back212}213feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 214why, one step back214export function freeze(obj: any, deep: boolean): void {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 215why, one step back215 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) returnfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
lines 216–217why, one step back216 const type = getArchtype(obj)217 if (type === Archtype.Set) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 218why, one step back218 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as anyfeat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8
line 219why, one step back219 } else if (type === Archtype.Map) {feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
lines 220–227why, one step back220 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any221 }222 Object.freeze(obj)223 if (deep) each(obj, (_, value) => freeze(value, true))224}225226function dontMutateFrozenCollections() {227 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 228–244why, one step back228}229230export function createHiddenProperty(231 target: AnyObject,232 prop: PropertyKey,233 value: any234) {235 Object.defineProperty(target, prop, {236 value: value,237 enumerable: false,238 writable: true239 })240}241242/* istanbul ignore next */243export function die(): never {244 throw new Error("Illegal state, please file a bug")feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914
line 245why, one step back245}