Skip to content
Myr

common.js

This file

Path

src/common.js

247 lines · 6.1 KiB · JavaScript

Last changed by

fix: Fixed #477, Immer crashes without an Object.assign polyfillMichel Weststratebefore adoption4895824

History of this file

Take it

The walk stopped after 200 commits: lines older than 8a1c84c point at it.

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

line 1why, one step back
1export const NOTHING =

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 2–4why, one step back
2 typeof Symbol !== "undefined"
3 ? Symbol("immer-nothing")4 : {["immer-nothing"]: true}

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

line 5why, one step back
5

feat: limited class supportaleclarsonbefore adoption9877d64

line 6why, one step back
6export const DRAFTABLE =

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 7–9why, one step back
7 typeof Symbol !== "undefined" && Symbol.for
8 ? Symbol.for("immer-draftable")9 : "__$immer_draftable"

feat: limited class supportaleclarsonbefore adoption9877d64

line 10why, one step back
10

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 11why, one step back
11export const DRAFT_STATE =

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 12–14why, one step back
12 typeof Symbol !== "undefined" && Symbol.for
13 ? Symbol.for("immer-state")14 : "__$immer_state"

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

line 15why, one step back
15

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 16why, one step back
16export function isDraft(value) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 17why, one step back
17 return !!value && !!value[DRAFT_STATE]

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

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

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 20why, one step back
20export function isDraftable(value) {

Fix: made sure non-plain & non-draftable objects are not cloned when applying patchesMichel Weststratebefore adoption99de883

lines 21–22why, one step back
21 if (!value) return false
22 return (

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 23–27why, one step back
23 isPlainObject(value) ||
24 !!value[DRAFTABLE] ||25 !!value.constructor[DRAFTABLE] ||26 isMap(value) ||27 isSet(value)

Fix: made sure non-plain & non-draftable objects are not cloned when applying patchesMichel Weststratebefore adoption99de883

lines 28–31why, one step back
28 )
29}3031export function isPlainObject(value) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 32–34why, one step back
32 if (!value || typeof value !== "object") return false
33 if (Array.isArray(value)) return true34 const proto = Object.getPrototypeOf(value)

Fix: made sure non-plain & non-draftable objects are not cloned when applying patchesMichel Weststratebefore adoption99de883

line 35why, one step back
35 return !proto || proto === Object.prototype

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

lines 36–38why, one step back
36}
3738export function original(value) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 39–42why, one step back
39 if (value && value[DRAFT_STATE]) {
40 return value[DRAFT_STATE].base41 }42 // otherwise return undefined

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

lines 43–44why, one step back
43}
44

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 45–63why, one step back
45// We use Maps as `drafts` for Sets, not Objects
46// See proxy.js47export function assignSet(target, override) {48 override.forEach(value => {49 // When we add new drafts we have to remove their originals if present50 const prev = original(value)51 if (prev) target.delete(prev)52 target.add(value)53 })54 return target55}5657// We use Maps as `drafts` for Maps, not Objects58// See proxy.js59export function assignMap(target, override) {60 override.forEach((value, key) => target.set(key, value))61 return target62}63

fix: revert Map supportAlec Larsonbefore adoption47529d3

line 64why, one step back
64export const assign =

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 65why, one step back
65 Object.assign ||

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

line 66why, one step back
66 ((target, ...overrides) => {

fix: Fixed #477, Immer crashes without an Object.assign polyfillMichel Weststratebefore adoption4895824

lines 67–70why, one step back
67 overrides.forEach(override => {
68 if (typeof override === "object" && override !== null)69 Object.keys(override).forEach(key => (target[key] = override[key]))70 })

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 71why, one step back
71 return target

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

line 72why, one step back
72 })

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

line 73why, one step back
73

feat: limited class supportaleclarsonbefore adoption9877d64

line 74why, one step back
74export const ownKeys =

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 75–82why, one step back
75 typeof Reflect !== "undefined" && Reflect.ownKeys
76 ? Reflect.ownKeys77 : typeof Object.getOwnPropertySymbols !== "undefined"78 ? obj =>79 Object.getOwnPropertyNames(obj).concat(80 Object.getOwnPropertySymbols(obj)81 )82 : Object.getOwnPropertyNames

feat: limited class supportaleclarsonbefore adoption9877d64

lines 83–84why, one step back
83
84export function shallowCopy(base, invokeGetters = false) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 85why, one step back
85 if (Array.isArray(base)) return base.slice()

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 86–87why, one step back
86 if (isMap(base)) return new Map(base)
87 if (isSet(base)) return new Set(base)

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 88–111why, one step back
88 const clone = Object.create(Object.getPrototypeOf(base))
89 ownKeys(base).forEach(key => {90 if (key === DRAFT_STATE) {91 return // Never copy over draft state.92 }93 const desc = Object.getOwnPropertyDescriptor(base, key)94 let {value} = desc95 if (desc.get) {96 if (!invokeGetters) {97 throw new Error("Immer drafts cannot have computed properties")98 }99 value = desc.get.call(base)100 }101 if (desc.enumerable) {102 clone[key] = value103 } else {104 Object.defineProperty(clone, key, {105 value,106 writable: true,107 configurable: true108 })109 }110 })111 return clone

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

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

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 114–116why, one step back
114export function each(obj, iter) {
115 if (Array.isArray(obj) || isMap(obj) || isSet(obj)) {116 obj.forEach((entry, index) => iter(index, entry, obj))

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 117why, one step back
117 } else {

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 119why, one step back
119 }

feat: limited class supportaleclarsonbefore adoption9877d64

lines 120–122why, one step back
120}
121122export function isEnumerable(base, prop) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 123–124why, one step back
123 const desc = Object.getOwnPropertyDescriptor(base, prop)
124 return !!desc && desc.enumerable

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

lines 125–127why, one step back
125}
126127export function has(thing, prop) {

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 128–130why, one step back
128 return isMap(thing)
129 ? thing.has(prop)130 : Object.prototype.hasOwnProperty.call(thing, prop)

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

lines 131–132why, one step back
131}
132

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 133–136why, one step back
133export function get(thing, prop) {
134 return isMap(thing) ? thing.get(prop) : thing[prop]135}136

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

line 137why, one step back
137export function is(x, y) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 138–143why, one step back
138 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
139 if (x === y) {140 return x !== 0 || 1 / x === 1 / y141 } else {142 return x !== x && y !== y143 }

Fix: made sure non-plain & non-draftable objects are not cloned when applying patchesMichel Weststratebefore adoption99de883

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

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 146–223why, one step back
146export const hasSymbol = typeof Symbol !== "undefined"
147148export const hasMap = typeof Map !== "undefined"149150export function isMap(target) {151 return hasMap && target instanceof Map152}153154export const hasSet = typeof Set !== "undefined"155156export function isSet(target) {157 return hasSet && target instanceof Set158}159160export function makeIterable(next) {161 let self162 return (self = {163 [Symbol.iterator]: () => self,164 next165 })166}167168/** Map.prototype.values _-or-_ Map.prototype.entries */169export function iterateMapValues(state, prop, receiver) {170 const isEntries = prop !== "values"171 return () => {172 const iterator = latest(state)[Symbol.iterator]()173 return makeIterable(() => {174 const result = iterator.next()175 if (!result.done) {176 const [key] = result.value177 const value = receiver.get(key)178 result.value = isEntries ? [key, value] : value179 }180 return result181 })182 }183}184185export function makeIterateSetValues(createProxy) {186 function iterateSetValues(state, prop) {187 const isEntries = prop === "entries"188 return () => {189 const iterator = latest(state)[Symbol.iterator]()190 return makeIterable(() => {191 const result = iterator.next()192 if (!result.done) {193 const value = wrapSetValue(state, result.value)194 result.value = isEntries ? [value, value] : value195 }196 return result197 })198 }199 }200201 function wrapSetValue(state, value) {202 const key = original(value) || value203 let draft = state.drafts.get(key)204 if (!draft) {205 if (state.finalized || !isDraftable(value) || state.finalizing) {206 return value207 }208 draft = createProxy(value, state)209 state.drafts.set(key, draft)210 if (state.modified) {211 state.copy.add(draft)212 }213 }214 return draft215 }216217 return iterateSetValues218}219220function latest(state) {221 return state.copy || state.base222}223

Fix: made sure non-plain & non-draftable objects are not cloned when applying patchesMichel Weststratebefore adoption99de883

lines 224–226why, one step back
224export function clone(obj) {
225 if (!isDraftable(obj)) return obj226 if (Array.isArray(obj)) return obj.map(clone)

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 227–228why, one step back
227 if (isMap(obj)) return new Map(obj)
228 if (isSet(obj)) return new Set(obj)

Fix: made sure non-plain & non-draftable objects are not cloned when applying patchesMichel Weststratebefore adoption99de883

lines 229–231why, one step back
229 const cloned = Object.create(Object.getPrototypeOf(obj))
230 for (const key in obj) cloned[key] = clone(obj[key])231 return cloned

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

line 232why, one step back
232}

BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581

line 233why, one step back
233

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

line 234why, one step back
234export function freeze(obj, deep = false) {

BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581

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

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 236–240why, one step back
236 if (isSet(obj)) {
237 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections238 } else if (isMap(obj)) {239 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections240 }

BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581

line 241why, one step back
241 Object.freeze(obj)

feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40

lines 242–246why, one step back
242 if (deep) each(obj, (_, value) => freeze(value, true))
243}244245function dontMutateFrozenCollections() {246 throw new Error("This object has been frozen and should not be mutated")

BREAKING CHANGE: In development mode, any new value stored in a tree will be deeply frozen. Fixes #412 #363 #260 #230 #313 #229Michel Weststratebefore adoption9fa2581

line 247why, one step back
247}