Skip to content
Myr

common.ts

This file

Path

src/common.ts

244 lines · 6.4 KiB · TypeScript

Last changed by

Enums FTWMichel Weststratebefore adoptionfd76f1c

History of this file

Take it

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

lines 1–7why, one step back
1import {
2 Objectish,3 Drafted,4 AnyObject,5 AnyArray,6 AnyMap,7 AnySet,

Enums FTWMichel Weststratebefore adoptionfd76f1c

lines 8–10why, one step back
8 ImmerState,
9 ProxyType,10 Archtype

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

line 11why, one step back
11} from "./types"

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 12–16why, one step back
12
13/** 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 instead

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 18–20why, one step back
18 private _: any
19}20

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 43why, one step back
43

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 47why, one step back
47

Processed bunch of todosMichel Weststratebefore adoption00425e1

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 52–61why, one step back
52/** 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 back
62 Array.isArray(value) ||

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Dropped Object.assignMichel Weststratebefore adoption79a1d2c

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 80–83why, one step back
80 }
81 // otherwise return undefined82}83

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 85–88why, one step back
85 typeof Reflect !== "undefined" && Reflect.ownKeys
86 ? Reflect.ownKeys87 : typeof Object.getOwnPropertySymbols !== "undefined"88 ? obj =>

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

lines 89–91why, one step back
89 Object.getOwnPropertyNames(obj).concat(
90 Object.getOwnPropertySymbols(obj) as any91 )

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 92–95why, one step back
92 : Object.getOwnPropertyNames
9394export function each<T extends Objectish>(95 obj: T,

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

basic map tests succeedingMichel Weststratebefore adoption735c402

line 101why, one step back
101 } else {

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 103–105why, one step back
103 }
104}105

Dropped Object.assignMichel Weststratebefore adoption79a1d2c

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

lines 111–113why, one step back
111export function getArchtype(thing: any): Archtype {
112 if (!thing) die()113 if (thing[DRAFT_STATE]) {

Enums FTWMichel Weststratebefore adoptionfd76f1c

lines 114–125why, one step back
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 }

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

lines 126–128why, one step back
126 }
127 return Array.isArray(thing)128 ? Archtype.Array

basic map tests succeedingMichel Weststratebefore adoption735c402

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

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

lines 130–137why, one step back
130 ? Archtype.Map
131 : isSet(thing)132 ? Archtype.Set133 : Archtype.Object134}135136export function has(thing: any, prop: PropertyKey): boolean {137 return getArchtype(thing) === Archtype.Map

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 138–141why, one step back
138 ? thing.has(prop)
139 : Object.prototype.hasOwnProperty.call(thing, prop)140}141

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 145–146why, one step back
145}
146

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

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

TS type improvementsMichel Weststratebefore adoptiondd1808e

lines 158–160why, one step back
158 }
159}160

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 162–169why, one step back
162 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
163 if (x === y) {164 return x !== 0 || 1 / x === 1 / y165 } else {166 return x !== x && y !== y167 }168}169

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 171–173why, one step back
171 return hasMap && target instanceof Map
172}173

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 175–177why, one step back
175 return hasSet && target instanceof Set
176}177

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 179–180why, one step back
179 return state.copy || state.base
180}

fixed most testsMichel Weststratebefore adoption385c0a4

line 181why, one step back
181

Further cleanup in progressMichel Weststratebefore adoptionaeba1ff

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 183–185why, one step back
183 base: T,
184 invokeGetters?: boolean185): T

TS type improvementsMichel Weststratebefore adoptiondd1808e

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

Some initial cleanupMichel Weststratebefore adoptionc53a75f

lines 187–211why, one step back
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.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 clone

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 212–213why, one step back
212}
213

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Several refactoringsMichel Weststratebefore adoptionc7f8f5b

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 220–227why, one step back
220 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any
221 }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 back
228}
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 back
244}