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 fileTake 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 back1export const NOTHING =style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 2–4why, one step back2 typeof Symbol !== "undefined"3 ? Symbol("immer-nothing")4 : {["immer-nothing"]: true}ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c
line 5why, one step back5feat: limited class supportaleclarsonbefore adoption9877d64
line 6why, one step back6export const DRAFTABLE =style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 7–9why, one step back7 typeof Symbol !== "undefined" && Symbol.for8 ? Symbol.for("immer-draftable")9 : "__$immer_draftable"feat: limited class supportaleclarsonbefore adoption9877d64
line 10why, one step back10feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 11why, one step back11export const DRAFT_STATE =style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 12–14why, one step back12 typeof Symbol !== "undefined" && Symbol.for13 ? Symbol.for("immer-state")14 : "__$immer_state"ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c
line 15why, one step back15feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 16why, one step back16export function isDraft(value) {style: use tabsuse-tabsbefore adoptiona9c0fd0
line 17why, one step back17 return !!value && !!value[DRAFT_STATE]ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c
lines 18–19why, one step back18}19feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 20why, one step back20export function isDraftable(value) {Fix: made sure non-plain & non-draftable objects are not cloned when applying patchesMichel Weststratebefore adoption99de883
lines 21–22why, one step back21 if (!value) return false22 return (feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 23–27why, one step back23 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 back28 )29}3031export function isPlainObject(value) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 32–34why, one step back32 if (!value || typeof value !== "object") return false33 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 back35 return !proto || proto === Object.prototypeci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c
lines 36–38why, one step back36}3738export function original(value) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 39–42why, one step back39 if (value && value[DRAFT_STATE]) {40 return value[DRAFT_STATE].base41 }42 // otherwise return undefinedci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c
lines 43–44why, one step back43}44feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 45–63why, one step back45// We use Maps as `drafts` for Sets, not Objects46// 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}63fix: revert Map supportAlec Larsonbefore adoption47529d3
line 64why, one step back64export const assign =style: use tabsuse-tabsbefore adoptiona9c0fd0
line 65why, one step back65 Object.assign ||feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
line 66why, one step back66 ((target, ...overrides) => {fix: Fixed #477, Immer crashes without an Object.assign polyfillMichel Weststratebefore adoption4895824
lines 67–70why, one step back67 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 back71 return targetfeat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
line 72why, one step back72 })ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c
line 73why, one step back73feat: limited class supportaleclarsonbefore adoption9877d64
line 74why, one step back74export const ownKeys =style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 75–82why, one step back75 typeof Reflect !== "undefined" && Reflect.ownKeys76 ? Reflect.ownKeys77 : typeof Object.getOwnPropertySymbols !== "undefined"78 ? obj =>79 Object.getOwnPropertyNames(obj).concat(80 Object.getOwnPropertySymbols(obj)81 )82 : Object.getOwnPropertyNamesfeat: limited class supportaleclarsonbefore adoption9877d64
lines 83–84why, one step back8384export function shallowCopy(base, invokeGetters = false) {style: use tabsuse-tabsbefore adoptiona9c0fd0
line 85why, one step back85 if (Array.isArray(base)) return base.slice()feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 86–87why, one step back86 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 back88 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 cloneci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c
lines 112–113why, one step back112}113feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 114–116why, one step back114export 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 back117 } else {feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
line 118why, one step back118 ownKeys(obj).forEach(key => iter(key, obj[key], obj))style: use tabsuse-tabsbefore adoptiona9c0fd0
line 119why, one step back119 }feat: limited class supportaleclarsonbefore adoption9877d64
lines 120–122why, one step back120}121122export function isEnumerable(base, prop) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 123–124why, one step back123 const desc = Object.getOwnPropertyDescriptor(base, prop)124 return !!desc && desc.enumerableci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c
lines 125–127why, one step back125}126127export function has(thing, prop) {feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 128–130why, one step back128 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 back131}132feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 133–136why, one step back133export function get(thing, prop) {134 return isMap(thing) ? thing.get(prop) : thing[prop]135}136ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c
line 137why, one step back137export function is(x, y) {style: use tabsuse-tabsbefore adoptiona9c0fd0
lines 138–143why, one step back138 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js139 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 back144}145feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 146–223why, one step back146export 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}223Fix: made sure non-plain & non-draftable objects are not cloned when applying patchesMichel Weststratebefore adoption99de883
lines 224–226why, one step back224export 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 back227 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 back229 const cloned = Object.create(Object.getPrototypeOf(obj))230 for (const key in obj) cloned[key] = clone(obj[key])231 return clonedci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c
line 232why, one step back232}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 back233feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
line 234why, one step back234export 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 back235 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) returnfeat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 236–240why, one step back236 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 back241 Object.freeze(obj)feat: added first class support for `Map` and `Set`Michel Weststratebefore adoptionb6f1d40
lines 242–246why, one step back242 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 back247}