Skip to content
Myr

common.ts

This file

Path

src/common.ts

245 lines · 6.4 KiB · TypeScript

Last changed by

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

History of this file

Take it

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 1–11why, one step back
1import {
2 Objectish,3 Drafted,4 AnyObject,5 AnyArray,6 AnyMap,7 AnySet,8 ImmerState,9 ProxyType,10 Archtype11} from "./types"

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 12–15why, 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>`

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 16–17why, one step back
16 // @ts-ignore
17 private _!: unique symbol

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 18–19why, one step back
18}
19

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 24–26why, one step back
24/**
25 * The sentinel value returned by producers to replace the draft with undefined.26 */

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 27–29why, one step back
27export const NOTHING: Nothing = hasSymbol
28 ? Symbol("immer-nothing")29 : ({["immer-nothing"]: true} as any)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 30–38why, one step back
30
31/**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 */

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 39–41why, one step back
39export const DRAFTABLE: unique symbol = hasSymbol
40 ? Symbol("immer-draftable")41 : ("__$immer_draftable" as any)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 42why, one step back
42

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 46why, one step back
46

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 51–60why, one step back
51/** 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) ||

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 61why, one step back
61 Array.isArray(value) ||

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 70–75why, one step back
70 if (!value || typeof value !== "object") return false
71 const proto = Object.getPrototypeOf(value)72 return !proto || proto === Object.prototype73}7475/** Get the underlying object that is represented by the given draft */

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 76why, one step back
76export function original<T>(value: Drafted<T>): T | undefined {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 79–82why, one step back
79 }
80 // otherwise return undefined81}82

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 84–87why, one step back
84 typeof Reflect !== "undefined" && Reflect.ownKeys
85 ? Reflect.ownKeys86 : typeof Object.getOwnPropertySymbols !== "undefined"87 ? obj =>

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 88–91why, one step back
88 Object.getOwnPropertyNames(obj).concat(
89 Object.getOwnPropertySymbols(obj) as any90 )91 : /* istanbul ignore next */ Object.getOwnPropertyNames

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 92–94why, one step back
92
93export function each<T extends Objectish>(94 obj: T,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 95–98why, one step back
95 iter: (key: string | number, value: any, source: T) => void
96): void97export function each(obj: any, iter: any) {98 if (getArchtype(obj) === Archtype.Object) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 100–101why, one step back
100 } else {
101 obj.forEach((entry: any, index: any) => iter(index, entry, obj))

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 110–137why, one step back
110export function getArchtype(thing: any): Archtype {
111 /* istanbul ignore next */112 if (!thing) die()113 if (thing[DRAFT_STATE]) {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 }126 }127 return Array.isArray(thing)128 ? Archtype.Array129 : isMap(thing)130 ? Archtype.Map131 : isSet(thing)132 ? Archtype.Set133 : Archtype.Object134}135136export function has(thing: any, prop: PropertyKey): boolean {137 return getArchtype(thing) === Archtype.Map

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 142–144why, one step back
142export function get(thing: AnyMap | AnyObject, prop: PropertyKey): any {
143 // @ts-ignore144 return getArchtype(thing) === Archtype.Map ? thing.get(prop) : thing[prop]

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 147–161why, 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] = value158 }159}160161export function is(x: any, y: any): boolean {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 178–179why, one step back
178export function latest(state: ImmerState): any {
179 return state.copy || state.base

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 180–181why, one step back
180}
181

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 182–191why, one step back
182export function shallowCopy<T extends AnyObject | AnyArray>(
183 base: T,184 invokeGetters?: boolean185): T186export function shallowCopy(base: any, invokeGetters = false) {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.

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 192why, one step back
192 }

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 193–197why, one step back
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")

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 198why, one step back
198 }

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 199why, one step back
199 value = desc.get.call(base)

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 200why, one step back
200 }

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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")

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 228–244why, 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}241242/* istanbul ignore next */243export function die(): never {244 throw new Error("Illegal state, please file a bug")

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 245why, one step back
245}