Skip to content
Myr

common.js

This file

Path

src/common.js

246 lines · 6.0 KiB · JavaScript

Last changed by

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

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

lines 66–69why, one step back
66 ((target, ...overrides) => {
67 overrides.forEach(override =>68 Object.keys(override).forEach(key => (target[key] = override[key]))69 )

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 70why, one step back
70 return target

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

line 71why, one step back
71 })

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

line 72why, one step back
72

feat: limited class supportaleclarsonbefore adoption9877d64

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat: limited class supportaleclarsonbefore adoption9877d64

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

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

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 116why, one step back
116 } else {

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

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 118why, one step back
118 }

feat: limited class supportaleclarsonbefore adoption9877d64

lines 119–121why, one step back
119}
120121export function isEnumerable(base, prop) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

lines 124–126why, one step back
124}
125126export function has(thing, prop) {

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

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

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

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

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

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

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

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

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

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

lines 145–222why, one step back
145export const hasSymbol = typeof Symbol !== "undefined"
146147export const hasMap = typeof Map !== "undefined"148149export function isMap(target) {150 return hasMap && target instanceof Map151}152153export const hasSet = typeof Set !== "undefined"154155export function isSet(target) {156 return hasSet && target instanceof Set157}158159export function makeIterable(next) {160 let self161 return (self = {162 [Symbol.iterator]: () => self,163 next164 })165}166167/** Map.prototype.values _-or-_ Map.prototype.entries */168export function iterateMapValues(state, prop, receiver) {169 const isEntries = prop !== "values"170 return () => {171 const iterator = latest(state)[Symbol.iterator]()172 return makeIterable(() => {173 const result = iterator.next()174 if (!result.done) {175 const [key] = result.value176 const value = receiver.get(key)177 result.value = isEntries ? [key, value] : value178 }179 return result180 })181 }182}183184export function makeIterateSetValues(createProxy) {185 function iterateSetValues(state, prop) {186 const isEntries = prop === "entries"187 return () => {188 const iterator = latest(state)[Symbol.iterator]()189 return makeIterable(() => {190 const result = iterator.next()191 if (!result.done) {192 const value = wrapSetValue(state, result.value)193 result.value = isEntries ? [value, value] : value194 }195 return result196 })197 }198 }199200 function wrapSetValue(state, value) {201 const key = original(value) || value202 let draft = state.drafts.get(key)203 if (!draft) {204 if (state.finalized || !isDraftable(value) || state.finalizing) {205 return value206 }207 draft = createProxy(value, state)208 state.drafts.set(key, draft)209 if (state.modified) {210 state.copy.add(draft)211 }212 }213 return draft214 }215216 return iterateSetValues217}218219function latest(state) {220 return state.copy || state.base221}222

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

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

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

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

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

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

ci: collect coverage on node 10 onlyaleclarsonbefore adoption8a1c84c

line 231why, one step back
231}

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 232why, one step back
232

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

line 233why, one step back
233export 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 234why, one step back
234 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) return

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

lines 235–239why, one step back
235 if (isSet(obj)) {
236 obj.add = obj.clear = obj.delete = dontMutateFrozenCollections237 } else if (isMap(obj)) {238 obj.set = obj.clear = obj.delete = dontMutateFrozenCollections239 }

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 240why, one step back
240 Object.freeze(obj)

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

lines 241–245why, one step back
241 if (deep) each(obj, (_, value) => freeze(value, true))
242}243244function dontMutateFrozenCollections() {245 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 246why, one step back
246}