common.ts
This file
Path
src/common.ts
244 lines · 6.4 KiB · TypeScript
Last changed by
Enums FTWMichel Weststratebefore adoptionfd76f1c
History of this fileTake it
Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 1–7why, one step back1import {2 Objectish,3 Drafted,4 AnyObject,5 AnyArray,6 AnyMap,7 AnySet,Enums FTWMichel Weststratebefore adoptionfd76f1c
lines 8–10why, one step back8 ImmerState,9 ProxyType,10 ArchtypeFurther cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 11why, one step back11} from "./types"Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 12–16why, 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>`16 // TODO: do this better, use unique symbol insteadSeveral refactoringsMichel Weststratebefore adoptionc7f8f5b
line 17why, one step back17 // @ts-ignoreMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 18–20why, one step back18 private _: any19}20Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 21–24why, one step back21const hasSymbol = typeof Symbol !== "undefined"22export const hasMap = typeof Map !== "undefined"23export const hasSet = typeof Set !== "undefined"24Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 25–27why, one step back25/**26 * The sentinel value returned by producers to replace the draft with undefined.27 */Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 28–30why, one step back28export const NOTHING: Nothing = hasSymbol29 ? Symbol("immer-nothing")30 : ({["immer-nothing"]: true} as any)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 31–39why, one step back3132/**33 * To let Immer treat your class instances as plain immutable objects34 * (albeit with a custom prototype), you must define either an instance property35 * or a static property on each of your custom classes.36 *37 * Otherwise, your class instance will never be drafted, which means it won't be38 * safe to mutate in a produce callback.39 */Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 40–42why, one step back40export const DRAFTABLE: unique symbol = hasSymbol41 ? Symbol("immer-draftable")42 : ("__$immer_draftable" as any)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 43why, one step back43Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 44–46why, one step back44export const DRAFT_STATE: unique symbol = hasSymbol45 ? Symbol("immer-state")46 : ("__$immer_state" as any)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 47why, one step back47Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 48–51why, one step back48export const iteratorSymbol: typeof Symbol.iterator = hasSymbol49 ? Symbol.iterator50 : ("@@iterator" as any)51Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 52–61why, one step back52/** Returns true if the given value is an Immer draft */53export function isDraft(value: any): boolean {54 return !!value && !!value[DRAFT_STATE]55}5657/** Returns true if the given value can be drafted by Immer */58export function isDraftable(value: any): boolean {59 if (!value) return false60 return (61 isPlainObject(value) ||Several refactoringsMichel Weststratebefore adoptionc7f8f5b
line 62why, one step back62 Array.isArray(value) ||Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 63–69why, one step back63 !!value[DRAFTABLE] ||64 !!value.constructor[DRAFTABLE] ||65 isMap(value) ||66 isSet(value)67 )68}69TS type improvementsMichel Weststratebefore adoptiondd1808e
line 70why, one step back70export function isPlainObject(value: any): boolean {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 71–76why, one step back71 if (!value || typeof value !== "object") return false72 const proto = Object.getPrototypeOf(value)73 return !proto || proto === Object.prototype74}7576/** Get the underlying object that is represented by the given draft */Dropped Object.assignMichel Weststratebefore adoption79a1d2c
line 77why, one step back77export function original<T>(value: Drafted<T>): T | undefined {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 78why, one step back78 if (value && value[DRAFT_STATE]) {Dropped Object.assignMichel Weststratebefore adoption79a1d2c
line 79why, one step back79 return value[DRAFT_STATE].base as anyMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 80–83why, one step back80 }81 // otherwise return undefined82}83TS type improvementsMichel Weststratebefore adoptiondd1808e
line 84why, one step back84export const ownKeys: (target: AnyObject) => PropertyKey[] =Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 85–88why, one step back85 typeof Reflect !== "undefined" && Reflect.ownKeys86 ? Reflect.ownKeys87 : typeof Object.getOwnPropertySymbols !== "undefined"88 ? obj =>🎵...killing me softly, with your fingers... 🎶Michel Weststratebefore adoption19a6b66
lines 89–91why, one step back89 Object.getOwnPropertyNames(obj).concat(90 Object.getOwnPropertySymbols(obj) as any91 )Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 92–95why, one step back92 : Object.getOwnPropertyNames9394export function each<T extends Objectish>(95 obj: T,Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 96why, one step back96 iter: (key: string | number, value: any, source: T) => voidTS type improvementsMichel Weststratebefore adoptiondd1808e
lines 97–98why, one step back97): void98export function each(obj: any, iter: any) {Several refactoringsMichel Weststratebefore adoptionc7f8f5b
line 99why, one step back99 if (getArchtype(obj) === Archtype.Object) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 100why, one step back100 ownKeys(obj).forEach(key => iter(key, obj[key], obj))basic map tests succeedingMichel Weststratebefore adoption735c402
line 101why, one step back101 } else {Several refactoringsMichel Weststratebefore adoptionc7f8f5b
line 102why, one step back102 obj.forEach((entry: any, index: any) => iter(index, entry, obj))Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 103–105why, one step back103 }104}105Dropped Object.assignMichel Weststratebefore adoption79a1d2c
line 106why, one step back106export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 107–110why, one step back107 const desc = Object.getOwnPropertyDescriptor(base, prop)108 return desc && desc.enumerable ? true : false109}110Several refactoringsMichel Weststratebefore adoptionc7f8f5b
lines 111–113why, one step back111export function getArchtype(thing: any): Archtype {112 if (!thing) die()113 if (thing[DRAFT_STATE]) {Enums FTWMichel Weststratebefore adoptionfd76f1c
lines 114–125why, one step back114 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 }Several refactoringsMichel Weststratebefore adoptionc7f8f5b
lines 126–128why, one step back126 }127 return Array.isArray(thing)128 ? Archtype.Arraybasic map tests succeedingMichel Weststratebefore adoption735c402
line 129why, one step back129 : isMap(thing)Several refactoringsMichel Weststratebefore adoptionc7f8f5b
lines 130–137why, one step back130 ? Archtype.Map131 : isSet(thing)132 ? Archtype.Set133 : Archtype.Object134}135136export function has(thing: any, prop: PropertyKey): boolean {137 return getArchtype(thing) === Archtype.MapMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 138–141why, one step back138 ? thing.has(prop)139 : Object.prototype.hasOwnProperty.call(thing, prop)140}141TS type improvementsMichel Weststratebefore adoptiondd1808e
line 142why, one step back142export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {Several refactoringsMichel Weststratebefore adoptionc7f8f5b
lines 143–144why, one step back143 // @ts-ignore144 return getArchtype(thing) === Archtype.Map ? thing.get(prop) : thing[prop]Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 145–146why, one step back145}146Several refactoringsMichel Weststratebefore adoptionc7f8f5b
lines 147–157why, 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] = valueTS type improvementsMichel Weststratebefore adoptiondd1808e
lines 158–160why, one step back158 }159}160Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 161why, one step back161export function is(x: any, y: any): boolean {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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}169Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 170why, one step back170export function isMap(target: any): target is AnyMap {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 171–173why, one step back171 return hasMap && target instanceof Map172}173Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 174why, one step back174export function isSet(target: any): target is AnySet {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 175–177why, one step back175 return hasSet && target instanceof Set176}177Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 178why, one step back178export function latest(state: ImmerState): any {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 179–180why, one step back179 return state.copy || state.base180}fixed most testsMichel Weststratebefore adoption385c0a4
line 181why, one step back181Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 182why, one step back182export function shallowCopy<T extends AnyObject | AnyArray>(Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 183–185why, one step back183 base: T,184 invokeGetters?: boolean185): TTS type improvementsMichel Weststratebefore adoptiondd1808e
line 186why, one step back186export function shallowCopy(base: any, invokeGetters = false) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 187–211why, one step back187 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.192 }193 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")198 }199 value = desc.get.call(base)200 }201 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 cloneMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 212–213why, one step back212}213Several refactoringsMichel Weststratebefore adoptionc7f8f5b
line 214why, one step back214export function freeze(obj: any, deep: boolean = false): void {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 215why, one step back215 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) returnSeveral refactoringsMichel Weststratebefore adoptionc7f8f5b
lines 216–217why, one step back216 const type = getArchtype(obj)217 if (type === Archtype.Set) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 218why, one step back218 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as anySeveral refactoringsMichel Weststratebefore adoptionc7f8f5b
line 219why, one step back219 } else if (type === Archtype.Map) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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")TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 228–243why, 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}241242export function die(): never {243 throw new Error("Illegal state, please file a bug")Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 244why, one step back244}