common.ts
This file
Path
src/common.ts
243 lines · 6.3 KiB · TypeScript
Last changed by
Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
History of this fileTake it
Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
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"Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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>`Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 16–17why, one step back16 // @ts-ignore17 private _!: unique symbolMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 18–19why, one step back18}19Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 20–23why, one step back20const hasSymbol = typeof Symbol !== "undefined"21export const hasMap = typeof Map !== "undefined"22export const hasSet = typeof Set !== "undefined"23Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 24–26why, one step back24/**25 * The sentinel value returned by producers to replace the draft with undefined.26 */Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 27–29why, one step back27export const NOTHING: Nothing = hasSymbol28 ? Symbol("immer-nothing")29 : ({["immer-nothing"]: true} as any)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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 */Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 39–41why, one step back39export const DRAFTABLE: unique symbol = hasSymbol40 ? Symbol("immer-draftable")41 : ("__$immer_draftable" as any)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 42why, one step back42Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 43–45why, one step back43export const DRAFT_STATE: unique symbol = hasSymbol44 ? Symbol("immer-state")45 : ("__$immer_state" as any)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 46why, one step back46Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 47–50why, one step back47export const iteratorSymbol: typeof Symbol.iterator = hasSymbol48 ? Symbol.iterator49 : ("@@iterator" as any)50Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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) ||Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 61why, one step back61 Array.isArray(value) ||Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 62–68why, one step back62 !!value[DRAFTABLE] ||63 !!value.constructor[DRAFTABLE] ||64 isMap(value) ||65 isSet(value)66 )67}68Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 69why, one step back69export function isPlainObject(value: any): boolean {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
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 */Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 76why, one step back76export function original<T>(value: Drafted<T>): T | undefined {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 77why, one step back77 if (value && value[DRAFT_STATE]) {Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 78why, one step back78 return value[DRAFT_STATE].base as anyMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 79–82why, one step back79 }80 // otherwise return undefined81}82Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 83why, one step back83export const ownKeys: (target: AnyObject) => PropertyKey[] =Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 84–87why, one step back84 typeof Reflect !== "undefined" && Reflect.ownKeys85 ? Reflect.ownKeys86 : typeof Object.getOwnPropertySymbols !== "undefined"87 ? obj =>Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 88–90why, one step back88 Object.getOwnPropertyNames(obj).concat(89 Object.getOwnPropertySymbols(obj) as any90 )Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 91–94why, one step back91 : Object.getOwnPropertyNames9293export function each<T extends Objectish>(94 obj: T,Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
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) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 99why, one step back99 ownKeys(obj).forEach(key => iter(key, obj[key], obj))basic map tests succeedingMichel Weststratebefore adoption735c402
line 100why, one step back100 } else {Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 101why, one step back101 obj.forEach((entry: any, index: any) => iter(index, entry, obj))Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 102–104why, one step back102 }103}104Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 105why, one step back105export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 106–109why, one step back106 const desc = Object.getOwnPropertyDescriptor(base, prop)107 return desc && desc.enumerable ? true : false108}109Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 110–127why, one step back110export function getArchtype(thing: any): Archtype {111 if (!thing) die()112 if (thing[DRAFT_STATE]) {113 switch ((thing as Drafted)[DRAFT_STATE].type) {114 case ProxyType.ES5Object:115 case ProxyType.ProxyObject:116 return Archtype.Object117 case ProxyType.ES5Array:118 case ProxyType.ProxyArray:119 return Archtype.Array120 case ProxyType.Map:121 return Archtype.Map122 case ProxyType.Set:123 return Archtype.Set124 }125 }126 return Array.isArray(thing)127 ? Archtype.Arraybasic map tests succeedingMichel Weststratebefore adoption735c402
line 128why, one step back128 : isMap(thing)Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 129–136why, one step back129 ? Archtype.Map130 : isSet(thing)131 ? Archtype.Set132 : Archtype.Object133}134135export function has(thing: any, prop: PropertyKey): boolean {136 return getArchtype(thing) === Archtype.MapMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 137–140why, one step back137 ? thing.has(prop)138 : Object.prototype.hasOwnProperty.call(thing, prop)139}140Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 141–143why, one step back141export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {142 // @ts-ignore143 return getArchtype(thing) === Archtype.Map ? thing.get(prop) : thing[prop]Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 144–145why, one step back144}145Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 146–160why, one step back146export function set(thing: any, propOrOldValue: PropertyKey, value: any) {147 switch (getArchtype(thing)) {148 case Archtype.Map:149 thing.set(propOrOldValue, value)150 break151 case Archtype.Set:152 thing.delete(propOrOldValue)153 thing.add(value)154 break155 default:156 thing[propOrOldValue] = value157 }158}159160export function is(x: any, y: any): boolean {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 161–168why, one step back161 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js162 if (x === y) {163 return x !== 0 || 1 / x === 1 / y164 } else {165 return x !== x && y !== y166 }167}168Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 169why, one step back169export function isMap(target: any): target is AnyMap {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 170–172why, one step back170 return hasMap && target instanceof Map171}172Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 173why, one step back173export function isSet(target: any): target is AnySet {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 174–176why, one step back174 return hasSet && target instanceof Set175}176Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 177–178why, one step back177export function latest(state: ImmerState): any {178 return state.copy || state.basefixed most testsMichel Weststratebefore adoption385c0a4
lines 179–180why, one step back179}180Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 181–208why, one step back181export function shallowCopy<T extends AnyObject | AnyArray>(182 base: T,183 invokeGetters?: boolean184): T185export function shallowCopy(base: any, invokeGetters = false) {186 if (Array.isArray(base)) return base.slice()187 const clone = Object.create(Object.getPrototypeOf(base))188 ownKeys(base).forEach(key => {189 if (key === DRAFT_STATE) {190 return // Never copy over draft state.191 }192 const desc = Object.getOwnPropertyDescriptor(base, key)!193 let {value} = desc194 if (desc.get) {195 if (!invokeGetters) {196 throw new Error("Immer drafts cannot have computed properties")197 }198 value = desc.get.call(base)199 }200 if (desc.enumerable) {201 clone[key] = value202 } else {203 Object.defineProperty(clone, key, {204 value,205 writable: true,206 configurable: true207 })208 }Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 209why, one step back209 })Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 210why, one step back210 return cloneMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 211–212why, one step back211}212Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 213why, one step back213export function freeze(obj: any, deep: boolean = false): void {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 214why, one step back214 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) returnMerge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 215–216why, one step back215 const type = getArchtype(obj)216 if (type === Archtype.Set) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 217why, one step back217 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as anyMerge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
line 218why, one step back218 } else if (type === Archtype.Map) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 219–226why, one step back219 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any220 }221 Object.freeze(obj)222 if (deep) each(obj, (_, value) => freeze(value, true))223}224225function dontMutateFrozenCollections() {226 throw new Error("This object has been frozen and should not be mutated")Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802
lines 227–242why, one step back227}228229export function createHiddenProperty(230 target: AnyObject,231 prop: PropertyKey,232 value: any233) {234 Object.defineProperty(target, prop, {235 value: value,236 enumerable: false,237 writable: true238 })239}240241export function die(): never {242 throw new Error("Illegal state, please file a bug")Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 243why, one step back243}