common.js
This file
Path
src/common.js
208 lines · 5.9 KiB · JavaScript
Last changed by
feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04
History of this fileTake it
The walk stopped after 200 commits: lines older than 53e9ce8 point at it.
perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 1–5why, one step back1export const NOTHING =2 typeof Symbol !== "undefined"3 ? Symbol("immer-nothing")4 : {["immer-nothing"]: true}5feat: limited class supportaleclarsonbefore adoption9877d64
lines 6–7why, one step back6export const DRAFTABLE =7 typeof Symbol !== "undefined"fix: share internal symbols between immer installationsAlec Larsonbefore adoption02fed2c
line 8why, one step back8 ? Symbol.for("immer-draftable")feat: limited class supportaleclarsonbefore adoption9877d64
lines 9–10why, one step back9 : "__$immer_draftable"10feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 11why, one step back11export const DRAFT_STATE =fix: share internal symbols between immer installationsAlec Larsonbefore adoption02fed2c
line 12why, one step back12 typeof Symbol !== "undefined" ? Symbol.for("immer-state") : "__$immer_state"perf: proxy assignaleclarsonbefore adoption53e9ce8
line 13why, one step back13feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 14–15why, one step back14export function isDraft(value) {15 return !!value && !!value[DRAFT_STATE]perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 16–17why, one step back16}17feat: add `Immer` classaleclarsonbefore adoptionecd04dc
line 18why, one step back18export function isDraftable(value) {feat: limited class supportaleclarsonbefore adoption9877d64
line 19why, one step back19 if (!value || typeof value !== "object") return falseperf: proxy assignaleclarsonbefore adoption53e9ce8
lines 20–21why, one step back20 if (Array.isArray(value)) return true21 const proto = Object.getPrototypeOf(value)feat: limited class supportaleclarsonbefore adoption9877d64
line 22why, one step back22 if (!proto || proto === Object.prototype) return truenit: merge isMap and isSet checksAlec Larsonbefore adoption4164f0d
line 23why, one step back23 if (isMap(value) || isSet(value)) return truefeat: limited class supportaleclarsonbefore adoption9877d64
line 24why, one step back24 return !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE]perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 25–27why, one step back25}2627export function original(value) {feat: add `Immer` classaleclarsonbefore adoptionecd04dc
lines 28–29why, one step back28 if (value && value[DRAFT_STATE]) {29 return value[DRAFT_STATE].baseperf: proxy assignaleclarsonbefore adoption53e9ce8
lines 30–33why, one step back30 }31 // otherwise return undefined32}33feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 34–35why, one step back34// We use Maps as `drafts` for Sets, not Objects35// See proxy.jsfeat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
line 36why, one step back36export function assignSet(target, override) {refactor: helper functionsAlec Larsonbefore adoptionb6b542d
line 37why, one step back37 override.forEach(value => {feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
line 38why, one step back38 // When we add new drafts we have to remove their originals if presentrefactor: helper functionsAlec Larsonbefore adoptionb6b542d
lines 39–40why, one step back39 const prev = original(value)40 if (prev) target.delete(prev)feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
line 41why, one step back41 target.add(value)refactor: helper functionsAlec Larsonbefore adoptionb6b542d
line 42why, one step back42 })feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 43–45why, one step back43 return target44}45feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
lines 46–48why, one step back46// We use Maps as `drafts` for Maps, not Objects47// See proxy.js48export function assignMap(target, override) {refactor: helper functionsAlec Larsonbefore adoptionb6b542d
line 49why, one step back49 override.forEach((value, key) => target.set(key, value))feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 50–51why, one step back50 return target51}feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 52why, one step back52refactor: helper functionsAlec Larsonbefore adoptionb6b542d
lines 53–59why, one step back53export const assign =54 Object.assign ||55 ((target, ...overrides) => {56 overrides.forEach(override =>57 Object.keys(override).forEach(key => (target[key] = override[key]))58 )59 return targetfeat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 60why, one step back60 })perf: proxy assignaleclarsonbefore adoption53e9ce8
line 61why, one step back61feat: limited class supportaleclarsonbefore adoption9877d64
line 62why, one step back62export const ownKeys =test: polyfillsaleclarsonbefore adoption551d877
line 63why, one step back63 typeof Reflect !== "undefined" && Reflect.ownKeysfeat: limited class supportaleclarsonbefore adoption9877d64
line 64why, one step back64 ? Reflect.ownKeysfix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b
lines 65–66why, one step back65 : typeof Object.getOwnPropertySymbols !== "undefined"66 ? obj =>feat: limited class supportaleclarsonbefore adoption9877d64
lines 67–69why, one step back67 Object.getOwnPropertyNames(obj).concat(68 Object.getOwnPropertySymbols(obj)69 )fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b
line 70why, one step back70 : Object.getOwnPropertyNamesfeat: limited class supportaleclarsonbefore adoption9877d64
lines 71–73why, one step back7172export function shallowCopy(base, invokeGetters = false) {73 if (Array.isArray(base)) return base.slice()feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 74why, one step back74 if (isMap(base)) return new Map(base)feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 75why, one step back75 if (isSet(base)) return new Set(base)feat: limited class supportaleclarsonbefore adoption9877d64
lines 76–81why, one step back76 const clone = Object.create(Object.getPrototypeOf(base))77 ownKeys(base).forEach(key => {78 if (key === DRAFT_STATE) {79 return // Never copy over draft state.80 }81 const desc = Object.getOwnPropertyDescriptor(base, key)chore: comments and formattingAlec Larsonbefore adoptionff96173
line 82why, one step back82 let {value} = descfeat: limited class supportaleclarsonbefore adoption9877d64
lines 83–86why, one step back83 if (desc.get) {84 if (!invokeGetters) {85 throw new Error("Immer drafts cannot have computed properties")86 }chore: comments and formattingAlec Larsonbefore adoptionff96173
line 87why, one step back87 value = desc.get.call(base)feat: limited class supportaleclarsonbefore adoption9877d64
lines 88–89why, one step back88 }89 if (desc.enumerable) {chore: comments and formattingAlec Larsonbefore adoptionff96173
line 90why, one step back90 clone[key] = valuefeat: limited class supportaleclarsonbefore adoption9877d64
lines 91–92why, one step back91 } else {92 Object.defineProperty(clone, key, {chore: comments and formattingAlec Larsonbefore adoptionff96173
line 93why, one step back93 value,feat: limited class supportaleclarsonbefore adoption9877d64
lines 94–99why, one step back94 writable: true,95 configurable: true96 })97 }98 })99 return cloneperf: proxy assignaleclarsonbefore adoption53e9ce8
lines 100–101why, one step back100}101refactor: helper functionsAlec Larsonbefore adoptionb6b542d
lines 102–106why, one step back102export function each(obj, iter) {103 if (Array.isArray(obj) || isMap(obj) || isSet(obj)) {104 obj.forEach((entry, index) => iter(index, entry, obj))105 } else {106 ownKeys(obj).forEach(key => iter(key, obj[key], obj))feat: limited class supportaleclarsonbefore adoption9877d64
lines 107–110why, one step back107 }108}109110export function isEnumerable(base, prop) {nit: ensure isEnumerable returns a booleanAlec Larsonbefore adoption53d0cdf
lines 111–112why, one step back111 const desc = Object.getOwnPropertyDescriptor(base, prop)112 return !!desc && desc.enumerableperf: proxy assignaleclarsonbefore adoption53e9ce8
lines 113–115why, one step back113}114115export function has(thing, prop) {refactor: helper functionsAlec Larsonbefore adoptionb6b542d
lines 116–118why, one step back116 return isMap(thing)117 ? thing.has(prop)118 : Object.prototype.hasOwnProperty.call(thing, prop)feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5
lines 119–121why, one step back119}120121export function get(thing, prop) {refactor: helper functionsAlec Larsonbefore adoptionb6b542d
line 122why, one step back122 return isMap(thing) ? thing.get(prop) : thing[prop]perf: proxy assignaleclarsonbefore adoption53e9ce8
lines 123–131why, one step back123}124125export function is(x, y) {126 // From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js127 if (x === y) {128 return x !== 0 || 1 / x === 1 / y129 } else {130 return x !== x && y !== y131 }feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
lines 132–133why, one step back132}133feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04
lines 134–135why, one step back134const hasMap = typeof Map !== "undefined"135feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3
line 136why, one step back136export function isMap(target) {feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04
line 137why, one step back137 return hasMap && target instanceof Mapfeat: `Set` draftsAndrey Goncharovbefore adoption4355b63
lines 138–139why, one step back138}139feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04
lines 140–141why, one step back140const hasSet = typeof Set !== "undefined"141feat: `Set` draftsAndrey Goncharovbefore adoption4355b63
line 142why, one step back142export function isSet(target) {feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04
lines 143–207why, one step back143 return hasSet && target instanceof Set144}145146export function makeIterable(next) {147 let self148 return (self = {149 [Symbol.iterator]: () => self,150 next151 })152}153154/** Map.prototype.values _-or-_ Map.prototype.entries */155export function iterateMapValues(state, prop, receiver) {156 const isEntries = prop !== "values"157 return () => {158 const iterator = latest(state)[Symbol.iterator]()159 return makeIterable(() => {160 const result = iterator.next()161 if (!result.done) {162 const [key] = result.value163 const value = receiver.get(key)164 result.value = isEntries ? [key, value] : value165 }166 return result167 })168 }169}170171export function makeIterateSetValues(createProxy) {172 function iterateSetValues(state, prop) {173 const isEntries = prop === "entries"174 return () => {175 const iterator = latest(state)[Symbol.iterator]()176 return makeIterable(() => {177 const result = iterator.next()178 if (!result.done) {179 const value = wrapSetValue(state, result.value)180 result.value = isEntries ? [value, value] : value181 }182 return result183 })184 }185 }186187 function wrapSetValue(state, value) {188 const key = original(value) || value189 let draft = state.drafts.get(key)190 if (!draft) {191 if (state.finalized || !isDraftable(value) || state.finalizing) {192 return value193 }194 draft = createProxy(value, state)195 state.drafts.set(key, draft)196 if (state.modified) {197 state.copy.add(draft)198 }199 }200 return draft201 }202203 return iterateSetValues204}205206function latest(state) {207 return state.copy || state.baseperf: proxy assignaleclarsonbefore adoption53e9ce8
line 208why, one step back208}