common.ts
This file
Path
src/common.ts
248 lines · 6.3 KiB · TypeScript
Last changed by
Several refactoringsMichel Weststratebefore adoptionc7f8f5b
History of this fileTake it
Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
lines 1–9why, one step back1import {2 Objectish,3 Drafted,4 AnyObject,5 AnyArray,6 AnyMap,7 AnySet,8 ImmerState9} from "./types"Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 10–14why, one step back1011/** Use a class type for `nothing` so its type is unique */12export class Nothing {13 // This lets us do `Exclude<T, Nothing>`14 // TODO: do this better, use unique symbol insteadSeveral refactoringsMichel Weststratebefore adoptionc7f8f5b
line 15why, one step back15 // @ts-ignoreMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 16–18why, one step back16 private _: any17}18Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 19–22why, one step back19const hasSymbol = typeof Symbol !== "undefined"20export const hasMap = typeof Map !== "undefined"21export const hasSet = typeof Set !== "undefined"22Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 23–25why, one step back23/**24 * The sentinel value returned by producers to replace the draft with undefined.25 */Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 26–28why, one step back26export const NOTHING: Nothing = hasSymbol27 ? Symbol("immer-nothing")28 : ({["immer-nothing"]: true} as any)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 29–37why, one step back2930/**31 * To let Immer treat your class instances as plain immutable objects32 * (albeit with a custom prototype), you must define either an instance property33 * or a static property on each of your custom classes.34 *35 * Otherwise, your class instance will never be drafted, which means it won't be36 * safe to mutate in a produce callback.37 */Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 38–40why, one step back38export const DRAFTABLE: unique symbol = hasSymbol39 ? Symbol("immer-draftable")40 : ("__$immer_draftable" as any)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 41why, one step back41Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 42–44why, one step back42export const DRAFT_STATE: unique symbol = hasSymbol43 ? Symbol("immer-state")44 : ("__$immer_state" as any)Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 45why, one step back45Processed bunch of todosMichel Weststratebefore adoption00425e1
lines 46–49why, one step back46export const iteratorSymbol: typeof Symbol.iterator = hasSymbol47 ? Symbol.iterator48 : ("@@iterator" as any)49Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 50–59why, one step back50/** Returns true if the given value is an Immer draft */51export function isDraft(value: any): boolean {52 return !!value && !!value[DRAFT_STATE]53}5455/** Returns true if the given value can be drafted by Immer */56export function isDraftable(value: any): boolean {57 if (!value) return false58 return (59 isPlainObject(value) ||Several refactoringsMichel Weststratebefore adoptionc7f8f5b
line 60why, one step back60 Array.isArray(value) ||Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 61–67why, one step back61 !!value[DRAFTABLE] ||62 !!value.constructor[DRAFTABLE] ||63 isMap(value) ||64 isSet(value)65 )66}67TS type improvementsMichel Weststratebefore adoptiondd1808e
line 68why, one step back68export function isPlainObject(value: any): boolean {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 69–74why, one step back69 if (!value || typeof value !== "object") return false70 const proto = Object.getPrototypeOf(value)71 return !proto || proto === Object.prototype72}7374/** Get the underlying object that is represented by the given draft */Dropped Object.assignMichel Weststratebefore adoption79a1d2c
line 75why, one step back75export function original<T>(value: Drafted<T>): T | undefined {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 76why, one step back76 if (value && value[DRAFT_STATE]) {Dropped Object.assignMichel Weststratebefore adoption79a1d2c
line 77why, one step back77 return value[DRAFT_STATE].base as anyMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 78–81why, one step back78 }79 // otherwise return undefined80}81TS type improvementsMichel Weststratebefore adoptiondd1808e
line 82why, one step back82export const ownKeys: (target: AnyObject) => PropertyKey[] =Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 83–86why, one step back83 typeof Reflect !== "undefined" && Reflect.ownKeys84 ? Reflect.ownKeys85 : typeof Object.getOwnPropertySymbols !== "undefined"86 ? obj =>🎵...killing me softly, with your fingers... 🎶Michel Weststratebefore adoption19a6b66
lines 87–89why, one step back87 Object.getOwnPropertyNames(obj).concat(88 Object.getOwnPropertySymbols(obj) as any89 )Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 90–93why, one step back90 : Object.getOwnPropertyNames9192export function each<T extends Objectish>(93 obj: T,Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 94why, one step back94 iter: (key: string | number, value: any, source: T) => voidTS type improvementsMichel Weststratebefore adoptiondd1808e
lines 95–96why, one step back95): void96export function each(obj: any, iter: any) {Several refactoringsMichel Weststratebefore adoptionc7f8f5b
line 97why, one step back97 if (getArchtype(obj) === Archtype.Object) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 98why, one step back98 ownKeys(obj).forEach(key => iter(key, obj[key], obj))basic map tests succeedingMichel Weststratebefore adoption735c402
line 99why, one step back99 } else {Several refactoringsMichel Weststratebefore adoptionc7f8f5b
line 100why, one step back100 obj.forEach((entry: any, index: any) => iter(index, entry, obj))Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 101–103why, one step back101 }102}103Dropped Object.assignMichel Weststratebefore adoption79a1d2c
line 104why, one step back104export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 105–108why, one step back105 const desc = Object.getOwnPropertyDescriptor(base, prop)106 return desc && desc.enumerable ? true : false107}108Several refactoringsMichel Weststratebefore adoptionc7f8f5b
lines 109–132why, one step back109export enum Archtype {110 Object,111 Array,112 Map,113 Set114}115116const toArchType = {117 proxy_object: Archtype.Object,118 es5_object: Archtype.Object,119 proxy_array: Archtype.Array,120 es5_array: Archtype.Array,121 map: Archtype.Map,122 set: Archtype.Set123}124125export function getArchtype(thing: any): Archtype {126 if (!thing) die()127 if (thing[DRAFT_STATE]) {128 // @ts-ignore129 return toArchType[(thing as Drafted)[DRAFT_STATE].type]130 }131 return Array.isArray(thing)132 ? Archtype.Arraybasic map tests succeedingMichel Weststratebefore adoption735c402
line 133why, one step back133 : isMap(thing)Several refactoringsMichel Weststratebefore adoptionc7f8f5b
lines 134–141why, one step back134 ? Archtype.Map135 : isSet(thing)136 ? Archtype.Set137 : Archtype.Object138}139140export function has(thing: any, prop: PropertyKey): boolean {141 return getArchtype(thing) === Archtype.MapMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 142–145why, one step back142 ? thing.has(prop)143 : Object.prototype.hasOwnProperty.call(thing, prop)144}145TS type improvementsMichel Weststratebefore adoptiondd1808e
line 146why, one step back146export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {Several refactoringsMichel Weststratebefore adoptionc7f8f5b
lines 147–148why, one step back147 // @ts-ignore148 return getArchtype(thing) === Archtype.Map ? thing.get(prop) : thing[prop]Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 149–150why, one step back149}150Several refactoringsMichel Weststratebefore adoptionc7f8f5b
lines 151–161why, one step back151export function set(thing: any, propOrOldValue: PropertyKey, value: any) {152 switch (getArchtype(thing)) {153 case Archtype.Map:154 thing.set(propOrOldValue, value)155 break156 case Archtype.Set:157 thing.delete(propOrOldValue)158 thing.add(value)159 break160 default:161 thing[propOrOldValue] = valueTS type improvementsMichel Weststratebefore adoptiondd1808e
lines 162–164why, one step back162 }163}164Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 165why, one step back165export function is(x: any, y: any): boolean {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 166–173why, one step back166 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js167 if (x === y) {168 return x !== 0 || 1 / x === 1 / y169 } else {170 return x !== x && y !== y171 }172}173Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 174why, one step back174export function isMap(target: any): target is AnyMap {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 175–177why, one step back175 return hasMap && target instanceof Map176}177Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 178why, one step back178export function isSet(target: any): target is AnySet {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 179–181why, one step back179 return hasSet && target instanceof Set180}181Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 182why, one step back182export function latest(state: ImmerState): any {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 183–184why, one step back183 return state.copy || state.base184}fixed most testsMichel Weststratebefore adoption385c0a4
line 185why, one step back185Further cleanup in progressMichel Weststratebefore adoptionaeba1ff
line 186why, one step back186export function shallowCopy<T extends AnyObject | AnyArray>(Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 187–189why, one step back187 base: T,188 invokeGetters?: boolean189): TTS type improvementsMichel Weststratebefore adoptiondd1808e
line 190why, one step back190export function shallowCopy(base: any, invokeGetters = false) {Some initial cleanupMichel Weststratebefore adoptionc53a75f
lines 191–215why, one step back191 if (Array.isArray(base)) return base.slice()192 const clone = Object.create(Object.getPrototypeOf(base))193 ownKeys(base).forEach(key => {194 if (key === DRAFT_STATE) {195 return // Never copy over draft state.196 }197 const desc = Object.getOwnPropertyDescriptor(base, key)!198 let {value} = desc199 if (desc.get) {200 if (!invokeGetters) {201 throw new Error("Immer drafts cannot have computed properties")202 }203 value = desc.get.call(base)204 }205 if (desc.enumerable) {206 clone[key] = value207 } else {208 Object.defineProperty(clone, key, {209 value,210 writable: true,211 configurable: true212 })213 }214 })215 return cloneMerge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 216–217why, one step back216}217Several refactoringsMichel Weststratebefore adoptionc7f8f5b
line 218why, one step back218export function freeze(obj: any, deep: boolean = false): void {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 219why, one step back219 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) returnSeveral refactoringsMichel Weststratebefore adoptionc7f8f5b
lines 220–221why, one step back220 const type = getArchtype(obj)221 if (type === Archtype.Set) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 222why, one step back222 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as anySeveral refactoringsMichel Weststratebefore adoptionc7f8f5b
line 223why, one step back223 } else if (type === Archtype.Map) {Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
lines 224–231why, one step back224 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any225 }226 Object.freeze(obj)227 if (deep) each(obj, (_, value) => freeze(value, true))228}229230function dontMutateFrozenCollections() {231 throw new Error("This object has been frozen and should not be mutated")TS type improvementsMichel Weststratebefore adoptiondd1808e
lines 232–247why, one step back232}233234export function createHiddenProperty(235 target: AnyObject,236 prop: PropertyKey,237 value: any238) {239 Object.defineProperty(target, prop, {240 value: value,241 enumerable: false,242 writable: true243 })244}245246export function die(): never {247 throw new Error("Illegal state, please file a bug")Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0
line 248why, one step back248}