Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 53e9ce8 point at it.

perf: proxy assignaleclarsonbefore adoption53e9ce8

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

feat: limited class supportaleclarsonbefore adoption9877d64

lines 6–7why, one step back
6export const DRAFTABLE =
7 typeof Symbol !== "undefined"

fix: share internal symbols between immer installationsAlec Larsonbefore adoption02fed2c

line 8why, one step back
8 ? Symbol.for("immer-draftable")

feat: limited class supportaleclarsonbefore adoption9877d64

lines 9–10why, one step back
9 : "__$immer_draftable"
10

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

fix: share internal symbols between immer installationsAlec Larsonbefore adoption02fed2c

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 13why, one step back
13

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 14–15why, one step back
14export function isDraft(value) {
15 return !!value && !!value[DRAFT_STATE]

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 16–17why, one step back
16}
17

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

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

feat: limited class supportaleclarsonbefore adoption9877d64

line 19why, one step back
19 if (!value || typeof value !== "object") return false

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 20–21why, one step back
20 if (Array.isArray(value)) return true
21 const proto = Object.getPrototypeOf(value)

feat: limited class supportaleclarsonbefore adoption9877d64

line 22why, one step back
22 if (!proto || proto === Object.prototype) return true

nit: merge isMap and isSet checksAlec Larsonbefore adoption4164f0d

line 23why, one step back
23 if (isMap(value) || isSet(value)) return true

feat: limited class supportaleclarsonbefore adoption9877d64

line 24why, one step back
24 return !!value[DRAFTABLE] || !!value.constructor[DRAFTABLE]

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 25–27why, one step back
25}
2627export function original(value) {

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

lines 28–29why, one step back
28 if (value && value[DRAFT_STATE]) {
29 return value[DRAFT_STATE].base

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 30–33why, one step back
30 }
31 // otherwise return undefined32}33

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 34–35why, one step back
34// We use Maps as `drafts` for Sets, not Objects
35// See proxy.js

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

line 36why, one step back
36export function assignSet(target, override) {

refactor: helper functionsAlec Larsonbefore adoptionb6b542d

line 37why, one step back
37 override.forEach(value => {

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

line 38why, one step back
38 // When we add new drafts we have to remove their originals if present

refactor: helper functionsAlec Larsonbefore adoptionb6b542d

lines 39–40why, one step back
39 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 back
41 target.add(value)

refactor: helper functionsAlec Larsonbefore adoptionb6b542d

line 42why, one step back
42 })

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 43–45why, one step back
43 return target
44}45

feat: add support of non-primitive keys for `Maps`keenondrumsbefore adoption6501ec5

lines 46–48why, one step back
46// We use Maps as `drafts` for Maps, not Objects
47// See proxy.js48export function assignMap(target, override) {

refactor: helper functionsAlec Larsonbefore adoptionb6b542d

line 49why, one step back
49 override.forEach((value, key) => target.set(key, value))

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

lines 50–51why, one step back
50 return target
51}

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 52why, one step back
52

refactor: helper functionsAlec Larsonbefore adoptionb6b542d

lines 53–59why, one step back
53export 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 target

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 60why, one step back
60 })

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 61why, one step back
61

feat: limited class supportaleclarsonbefore adoption9877d64

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

test: polyfillsaleclarsonbefore adoption551d877

line 63why, one step back
63 typeof Reflect !== "undefined" && Reflect.ownKeys

feat: limited class supportaleclarsonbefore adoption9877d64

line 64why, one step back
64 ? Reflect.ownKeys

fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b

lines 65–66why, one step back
65 : typeof Object.getOwnPropertySymbols !== "undefined"
66 ? obj =>

feat: limited class supportaleclarsonbefore adoption9877d64

lines 67–69why, one step back
67 Object.getOwnPropertyNames(obj).concat(
68 Object.getOwnPropertySymbols(obj)69 )

fix: use getOwnPropertySymbols only when it existsaleclarsonbefore adoptiond57433b

line 70why, one step back
70 : Object.getOwnPropertyNames

feat: limited class supportaleclarsonbefore adoption9877d64

lines 71–73why, one step back
71
72export function shallowCopy(base, invokeGetters = false) {73 if (Array.isArray(base)) return base.slice()

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 74why, one step back
74 if (isMap(base)) return new Map(base)

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 75why, one step back
75 if (isSet(base)) return new Set(base)

feat: limited class supportaleclarsonbefore adoption9877d64

lines 76–81why, one step back
76 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 back
82 let {value} = desc

feat: limited class supportaleclarsonbefore adoption9877d64

lines 83–86why, one step back
83 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 back
87 value = desc.get.call(base)

feat: limited class supportaleclarsonbefore adoption9877d64

lines 88–89why, one step back
88 }
89 if (desc.enumerable) {

chore: comments and formattingAlec Larsonbefore adoptionff96173

line 90why, one step back
90 clone[key] = value

feat: limited class supportaleclarsonbefore adoption9877d64

lines 91–92why, one step back
91 } else {
92 Object.defineProperty(clone, key, {

chore: comments and formattingAlec Larsonbefore adoptionff96173

line 93why, one step back
93 value,

feat: limited class supportaleclarsonbefore adoption9877d64

lines 94–99why, one step back
94 writable: true,
95 configurable: true96 })97 }98 })99 return clone

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 100–101why, one step back
100}
101

refactor: helper functionsAlec Larsonbefore adoptionb6b542d

lines 102–106why, one step back
102export 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 back
107 }
108}109110export function isEnumerable(base, prop) {

nit: ensure isEnumerable returns a booleanAlec Larsonbefore adoption53d0cdf

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

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 113–115why, one step back
113}
114115export function has(thing, prop) {

refactor: helper functionsAlec Larsonbefore adoptionb6b542d

lines 116–118why, one step back
116 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 back
119}
120121export function get(thing, prop) {

refactor: helper functionsAlec Larsonbefore adoptionb6b542d

line 122why, one step back
122 return isMap(thing) ? thing.get(prop) : thing[prop]

perf: proxy assignaleclarsonbefore adoption53e9ce8

lines 123–131why, one step back
123}
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 back
132}
133

feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04

lines 134–135why, one step back
134const hasMap = typeof Map !== "undefined"
135

feat: `Map` drafts and patcheskeenondrumsbefore adoption30db0f3

line 136why, one step back
136export function isMap(target) {

feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04

line 137why, one step back
137 return hasMap && target instanceof Map

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

lines 138–139why, one step back
138}
139

feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04

lines 140–141why, one step back
140const hasSet = typeof Set !== "undefined"
141

feat: `Set` draftsAndrey Goncharovbefore adoption4355b63

line 142why, one step back
142export function isSet(target) {

feat: add es5 support for Map/SetAndrey Boldyrevbefore adoptionea9ba04

lines 143–207why, one step back
143 return hasSet && target instanceof Set
144}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.base

perf: proxy assignaleclarsonbefore adoption53e9ce8

line 208why, one step back
208}