Skip to content
Myr

common.ts

This file

Path

src/common.ts

248 lines · 6.3 KiB · TypeScript

Last changed by

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

History of this file

Take it

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 1–9why, one step back
1import {
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 back
10
11/** 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 instead

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

line 15why, one step back
15 // @ts-ignore

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 16–18why, one step back
16 private _: any
17}18

Processed bunch of todosMichel Weststratebefore adoption00425e1

lines 19–22why, one step back
19const hasSymbol = typeof Symbol !== "undefined"
20export const hasMap = typeof Map !== "undefined"21export const hasSet = typeof Set !== "undefined"22

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 23–25why, one step back
23/**
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 back
26export const NOTHING: Nothing = hasSymbol
27 ? Symbol("immer-nothing")28 : ({["immer-nothing"]: true} as any)

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 29–37why, one step back
29
30/**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 back
38export const DRAFTABLE: unique symbol = hasSymbol
39 ? Symbol("immer-draftable")40 : ("__$immer_draftable" as any)

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 41why, one step back
41

Processed bunch of todosMichel Weststratebefore adoption00425e1

lines 42–44why, one step back
42export const DRAFT_STATE: unique symbol = hasSymbol
43 ? Symbol("immer-state")44 : ("__$immer_state" as any)

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 45why, one step back
45

Processed bunch of todosMichel Weststratebefore adoption00425e1

lines 46–49why, one step back
46export const iteratorSymbol: typeof Symbol.iterator = hasSymbol
47 ? Symbol.iterator48 : ("@@iterator" as any)49

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 50–59why, one step back
50/** 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 back
60 Array.isArray(value) ||

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 61–67why, one step back
61 !!value[DRAFTABLE] ||
62 !!value.constructor[DRAFTABLE] ||63 isMap(value) ||64 isSet(value)65 )66}67

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 68why, one step back
68export function isPlainObject(value: any): boolean {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 69–74why, one step back
69 if (!value || typeof value !== "object") return false
70 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 back
75export function original<T>(value: Drafted<T>): T | undefined {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 76why, one step back
76 if (value && value[DRAFT_STATE]) {

Dropped Object.assignMichel Weststratebefore adoption79a1d2c

line 77why, one step back
77 return value[DRAFT_STATE].base as any

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 78–81why, one step back
78 }
79 // otherwise return undefined80}81

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 82why, one step back
82export const ownKeys: (target: AnyObject) => PropertyKey[] =

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 83–86why, one step back
83 typeof Reflect !== "undefined" && Reflect.ownKeys
84 ? Reflect.ownKeys85 : typeof Object.getOwnPropertySymbols !== "undefined"86 ? obj =>

🎵...killing me softly, with your fingers... 🎶Michel Weststratebefore adoption19a6b66

lines 87–89why, one step back
87 Object.getOwnPropertyNames(obj).concat(
88 Object.getOwnPropertySymbols(obj) as any89 )

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 90–93why, one step back
90 : Object.getOwnPropertyNames
9192export function each<T extends Objectish>(93 obj: T,

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 94why, one step back
94 iter: (key: string | number, value: any, source: T) => void

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 95–96why, one step back
95): void
96export function each(obj: any, iter: any) {

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

line 97why, one step back
97 if (getArchtype(obj) === Archtype.Object) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 98why, one step back
98 ownKeys(obj).forEach(key => iter(key, obj[key], obj))

basic map tests succeedingMichel Weststratebefore adoption735c402

line 99why, one step back
99 } else {

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

line 100why, one step back
100 obj.forEach((entry: any, index: any) => iter(index, entry, obj))

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 101–103why, one step back
101 }
102}103

Dropped Object.assignMichel Weststratebefore adoption79a1d2c

line 104why, one step back
104export function isEnumerable(base: AnyObject, prop: PropertyKey): boolean {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 105–108why, one step back
105 const desc = Object.getOwnPropertyDescriptor(base, prop)
106 return desc && desc.enumerable ? true : false107}108

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

lines 109–132why, one step back
109export 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.Array

basic map tests succeedingMichel Weststratebefore adoption735c402

line 133why, one step back
133 : isMap(thing)

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

lines 134–141why, one step back
134 ? Archtype.Map
135 : isSet(thing)136 ? Archtype.Set137 : Archtype.Object138}139140export function has(thing: any, prop: PropertyKey): boolean {141 return getArchtype(thing) === Archtype.Map

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 142–145why, one step back
142 ? thing.has(prop)
143 : Object.prototype.hasOwnProperty.call(thing, prop)144}145

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 146why, one step back
146export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

lines 147–148why, one step back
147 // @ts-ignore
148 return getArchtype(thing) === Archtype.Map ? thing.get(prop) : thing[prop]

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 149–150why, one step back
149}
150

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

lines 151–161why, one step back
151export 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] = value

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 162–164why, one step back
162 }
163}164

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 165why, one step back
165export function is(x: any, y: any): boolean {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 166–173why, one step back
166 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
167 if (x === y) {168 return x !== 0 || 1 / x === 1 / y169 } else {170 return x !== x && y !== y171 }172}173

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 174why, one step back
174export function isMap(target: any): target is AnyMap {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 175–177why, one step back
175 return hasMap && target instanceof Map
176}177

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 178why, one step back
178export function isSet(target: any): target is AnySet {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 179–181why, one step back
179 return hasSet && target instanceof Set
180}181

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 182why, one step back
182export function latest(state: ImmerState): any {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 183–184why, one step back
183 return state.copy || state.base
184}

fixed most testsMichel Weststratebefore adoption385c0a4

line 185why, one step back
185

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 186why, one step back
186export function shallowCopy<T extends AnyObject | AnyArray>(

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 187–189why, one step back
187 base: T,
188 invokeGetters?: boolean189): T

TS type improvementsMichel Weststratebefore adoptiondd1808e

line 190why, one step back
190export function shallowCopy(base: any, invokeGetters = false) {

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 191–215why, one step back
191 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 clone

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 216–217why, one step back
216}
217

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

line 218why, one step back
218export function freeze(obj: any, deep: boolean = false): void {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 219why, one step back
219 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) return

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

lines 220–221why, one step back
220 const type = getArchtype(obj)
221 if (type === Archtype.Set) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 222why, one step back
222 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

line 223why, one step back
223 } else if (type === Archtype.Map) {

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 224–231why, one step back
224 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any
225 }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 back
232}
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 back
248}