Skip to content
Myr

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 file

Take it

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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"

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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>`

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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 */

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 42why, one step back
42

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

line 46why, one step back
46

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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 */

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

lines 91–94why, one step back
91 : Object.getOwnPropertyNames
9293export function each<T extends Objectish>(94 obj: T,

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

basic map tests succeedingMichel Weststratebefore adoption735c402

line 100why, one step back
100 } else {

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

basic map tests succeedingMichel Weststratebefore adoption735c402

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 129–136why, one step back
129 ? Archtype.Map
130 : isSet(thing)131 ? Archtype.Set132 : Archtype.Object133}134135export function has(thing: any, prop: PropertyKey): boolean {136 return getArchtype(thing) === Archtype.Map

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 141–143why, one step back
141export 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 back
144}
145

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 146–160why, one step back
146export 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 back
161 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
162 if (x === y) {163 return x !== 0 || 1 / x === 1 / y164 } else {165 return x !== x && y !== y166 }167}168

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

fixed most testsMichel Weststratebefore adoption385c0a4

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

lines 181–208why, one step back
181export 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 back
209 })

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

line 210why, one step back
210 return clone

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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

Merge pull request #498 from immerjs/code-cleanupMichel Weststratebefore adoptionb9cc802

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

Merge branch 'master' into map-fixesMichel Weststratebefore adoptioncb63fb0

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