Skip to content
Myr

common.ts

This file

Path

src/common.ts

211 lines · 5.3 KiB · TypeScript

Last changed by

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

History of this file

Take it

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

line 1why, one step back
1import {

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 2–4why, one step back
2 DRAFT_STATE,
3 DRAFTABLE,4 hasSet,

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

lines 5–12why, one step back
5 Objectish,
6 Drafted,7 AnyObject,8 AnyArray,9 AnyMap,10 AnySet,11 ImmerState,12 ProxyType,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 13–15why, one step back
13 Archtype,
14 hasMap15} from "./internal"

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

line 16why, one step back
16

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 17–26why, one step back
17/** Returns true if the given value is an Immer draft */
18export function isDraft(value: any): boolean {19 return !!value && !!value[DRAFT_STATE]20}2122/** Returns true if the given value can be drafted by Immer */23export function isDraftable(value: any): boolean {24 if (!value) return false25 return (26 isPlainObject(value) ||

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 28–34why, one step back
28 !!value[DRAFTABLE] ||
29 !!value.constructor[DRAFTABLE] ||30 isMap(value) ||31 isSet(value)32 )33}34

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 36–41why, one step back
36 if (!value || typeof value !== "object") return false
37 const proto = Object.getPrototypeOf(value)38 return !proto || proto === Object.prototype39}4041/** Get the underlying object that is represented by the given draft */

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 45–48why, one step back
45 }
46 // otherwise return undefined47}48

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 50–53why, one step back
50 typeof Reflect !== "undefined" && Reflect.ownKeys
51 ? Reflect.ownKeys52 : typeof Object.getOwnPropertySymbols !== "undefined"53 ? obj =>

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

lines 54–57why, one step back
54 Object.getOwnPropertyNames(obj).concat(
55 Object.getOwnPropertySymbols(obj) as any56 )57 : /* istanbul ignore next */ Object.getOwnPropertyNames

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 58–60why, one step back
58
59export function each<T extends Objectish>(60 obj: T,

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

lines 61–64why, one step back
61 iter: (key: string | number, value: any, source: T) => void
62): void63export function each(obj: any, iter: any) {64 if (getArchtype(obj) === Archtype.Object) {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 68–70why, one step back
68 }
69}70

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 72–75why, one step back
72 const desc = Object.getOwnPropertyDescriptor(base, prop)
73 return desc && desc.enumerable ? true : false74}75

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

lines 76–103why, one step back
76export function getArchtype(thing: any): Archtype {
77 /* istanbul ignore next */78 if (!thing) die()79 if (thing[DRAFT_STATE]) {80 switch ((thing as Drafted)[DRAFT_STATE].type) {81 case ProxyType.ES5Object:82 case ProxyType.ProxyObject:83 return Archtype.Object84 case ProxyType.ES5Array:85 case ProxyType.ProxyArray:86 return Archtype.Array87 case ProxyType.Map:88 return Archtype.Map89 case ProxyType.Set:90 return Archtype.Set91 }92 }93 return Array.isArray(thing)94 ? Archtype.Array95 : isMap(thing)96 ? Archtype.Map97 : isSet(thing)98 ? Archtype.Set99 : Archtype.Object100}101102export function has(thing: any, prop: PropertyKey): boolean {103 return getArchtype(thing) === Archtype.Map

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 104–107why, one step back
104 ? thing.has(prop)
105 : Object.prototype.hasOwnProperty.call(thing, prop)106}107

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 111–112why, one step back
111}
112

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

lines 113–127why, one step back
113export function set(thing: any, propOrOldValue: PropertyKey, value: any) {
114 switch (getArchtype(thing)) {115 case Archtype.Map:116 thing.set(propOrOldValue, value)117 break118 case Archtype.Set:119 thing.delete(propOrOldValue)120 thing.add(value)121 break122 default:123 thing[propOrOldValue] = value124 }125}126127export function is(x: any, y: any): boolean {

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 128–135why, one step back
128 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
129 if (x === y) {130 return x !== 0 || 1 / x === 1 / y131 } else {132 return x !== x && y !== y133 }134}135

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 137–139why, one step back
137 return hasMap && target instanceof Map
138}139

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 141–143why, one step back
141 return hasSet && target instanceof Set
142}143

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

lines 148–157why, one step back
148export function shallowCopy<T extends AnyObject | AnyArray>(
149 base: T,150 invokeGetters?: boolean151): T152export function shallowCopy(base: any, invokeGetters = false) {153 if (Array.isArray(base)) return base.slice()154 const clone = Object.create(Object.getPrototypeOf(base))155 ownKeys(base).forEach(key => {156 if (key === DRAFT_STATE) {157 return // Never copy over draft state.

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 158why, one step back
158 }

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

lines 159–163why, one step back
159 const desc = Object.getOwnPropertyDescriptor(base, key)!
160 let {value} = desc161 if (desc.get) {162 if (!invokeGetters) {163 throw new Error("Immer drafts cannot have computed properties")

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 164why, one step back
164 }

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 166why, one step back
166 }

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

lines 167–177why, one step back
167 if (desc.enumerable) {
168 clone[key] = value169 } else {170 Object.defineProperty(clone, key, {171 value,172 writable: true,173 configurable: true174 })175 }176 })177 return clone

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

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

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

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

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

lines 186–193why, one step back
186 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections as any
187 }188 Object.freeze(obj)189 if (deep) each(obj, (_, value) => freeze(value, true))190}191192function dontMutateFrozenCollections() {193 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 194–210why, one step back
194}
195196export function createHiddenProperty(197 target: AnyObject,198 prop: PropertyKey,199 value: any200) {201 Object.defineProperty(target, prop, {202 value: value,203 enumerable: false,204 writable: true205 })206}207208/* istanbul ignore next */209export function die(): never {210 throw new Error("Illegal state, please file a bug")

feat: convert Immer to TypeScriptMichel Weststratebefore adoption52c9914

line 211why, one step back
211}