Skip to content
Myr

immer.js

This file

Path

immer.js

134 lines · 4.4 KiB · JavaScript

Last changed by

Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e

History of this file

Take it

More repo setupMichel Weststratebefore adoption1637393

lines 1–2why, one step back
1// @ts-check
2

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 3–4why, one step back
3const isProxySymbol = Symbol("immer-proxy")
4

More repo setupMichel Weststratebefore adoption1637393

line 5why, one step back
5/**

Merge pull request #9 from benbraou/typosMichel Weststratebefore adoptionae0182f

line 6why, one step back
6 * Immer takes a state, and runs a function against it.

More repo setupMichel Weststratebefore adoption1637393

lines 7–15why, one step back
7 * That function can freely mutate the state, as it will create copies-on-write.
8 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned9 *10 * @export11 * @param {any} baseState - the state to start with12 * @param {Function} thunk - function that receives a proxy of the base state as first argument and which can be freely modified13 * @returns {any} a new state, or the base state if nothing was modified14 */15function immer(baseState, thunk) {

Code cleanupMichel Weststratebefore adoption2b2d0cb

line 16why, one step back
16 // Maps baseState objects to proxies

More repo setupMichel Weststratebefore adoption1637393

line 17why, one step back
17 const proxies = new Map()

Code cleanupMichel Weststratebefore adoption2b2d0cb

line 18why, one step back
18 // Maps baseState objects to their copies

More repo setupMichel Weststratebefore adoption1637393

lines 19–22why, one step back
19 const copies = new Map()
2021 const objectTraps = {22 get(target, prop) {

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 23why, one step back
23 if (prop === isProxySymbol) return target

Code cleanupMichel Weststratebefore adoption2b2d0cb

line 24why, one step back
24 return createProxy(getCurrentSource(target)[prop])

More repo setupMichel Weststratebefore adoption1637393

lines 25–32why, one step back
25 },
26 has(target, prop) {27 return prop in getCurrentSource(target)28 },29 ownKeys(target) {30 return Reflect.ownKeys(getCurrentSource(target))31 },32 set(target, prop, value) {

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 33–34why, one step back
33 const current = createProxy(getCurrentSource(target)[prop])
34 const newValue = createProxy(value)

More repo setupMichel Weststratebefore adoption1637393

line 35why, one step back
35 if (current !== newValue) {

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 36why, one step back
36 const copy = getOrCreateCopy(target)

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 37why, one step back
37 copy[prop] = isProxy(newValue) ? newValue[isProxySymbol] : newValue

More repo setupMichel Weststratebefore adoption1637393

lines 38–41why, one step back
38 }
39 return true40 },41 deleteProperty(target, property) {

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 42why, one step back
42 const copy = getOrCreateCopy(target)

More repo setupMichel Weststratebefore adoption1637393

lines 43–47why, one step back
43 delete copy[property]
44 return true45 }46 }47

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 48–50why, one step back
48 // creates a copy for a base object if there ain't one
49 function getOrCreateCopy(base) {50 let copy = copies.get(base)

More repo setupMichel Weststratebefore adoption1637393

line 51why, one step back
51 if (!copy) {

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 52–53why, one step back
52 copy = Array.isArray(base) ? base.slice() : Object.assign({}, base)
53 copies.set(base, copy)

More repo setupMichel Weststratebefore adoption1637393

lines 54–57why, one step back
54 }
55 return copy56 }57

Merge pull request #9 from benbraou/typosMichel Weststratebefore adoptionae0182f

line 58why, one step back
58 // returns the current source of truth for a base object

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 59–61why, one step back
59 function getCurrentSource(base) {
60 const copy = copies.get(base)61 return copy || base

More repo setupMichel Weststratebefore adoption1637393

lines 62–63why, one step back
62 }
63

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 64–66why, one step back
64 // creates a proxy for plain objects / arrays
65 function createProxy(base) {66 if (isPlainObject(base) || Array.isArray(base)) {

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 67why, one step back
67 if (isProxy(base)) return base // avoid double wrapping

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 68–73why, one step back
68 if (proxies.has(base)) return proxies.get(base)
69 const proxy = new Proxy(base, objectTraps)70 proxies.set(base, proxy)71 return proxy72 }73 return base

More repo setupMichel Weststratebefore adoption1637393

lines 74–75why, one step back
74 }
75

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 76–79why, one step back
76 // checks if the given base object has modifications, either because it is modified, or
77 // because one of it's children is78 function hasChanges(base) {79 const proxy = proxies.get(base)

More repo setupMichel Weststratebefore adoption1637393

lines 80–84why, one step back
80 if (!proxy) return false // nobody did read this object
81 if (copies.has(base)) return true // a copy was created, so there are changes82 // look deeper83 const keys = Object.keys(base)84 for (let i = 0; i < keys.length; i++) {

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 85–86why, one step back
85 const value = base[keys[i]]
86 if ((Array.isArray(value) || isPlainObject(value)) && hasChanges(value)) return true

More repo setupMichel Weststratebefore adoption1637393

lines 87–90why, one step back
87 }
88 return false89 }90

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 91–97why, one step back
91 // given a base object, returns it if unmodified, or return the changed cloned if modified
92 function finalize(base) {93 if (isPlainObject(base)) return finalizeObject(base)94 if (Array.isArray(base)) return finalizeArray(base)95 return base96 }97

More repo setupMichel Weststratebefore adoption1637393

line 98why, one step back
98 function finalizeObject(thing) {

Code cleanupMichel Weststratebefore adoption2b2d0cb

line 99why, one step back
99 if (!hasChanges(thing)) return thing

Add support for arraysMichel Weststratebefore adoptiond3696cb

line 100why, one step back
100 const copy = getOrCreateCopy(thing)

More repo setupMichel Weststratebefore adoption1637393

line 101why, one step back
101 Object.keys(copy).forEach(prop => {

Code cleanupMichel Weststratebefore adoption2b2d0cb

line 102why, one step back
102 copy[prop] = finalize(copy[prop])

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 103–107why, one step back
103 })
104 return copy105 }106107 function finalizeArray(thing) {

Code cleanupMichel Weststratebefore adoption2b2d0cb

line 108why, one step back
108 if (!hasChanges(thing)) return thing

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 109–110why, one step back
109 const copy = getOrCreateCopy(thing)
110 copy.forEach((value, index) => {

Code cleanupMichel Weststratebefore adoption2b2d0cb

line 111why, one step back
111 copy[index] = finalize(copy[index])

More repo setupMichel Weststratebefore adoption1637393

lines 112–115why, one step back
112 })
113 return copy114 }115

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 116–118why, one step back
116 // create proxy for root
117 const rootClone = createProxy(baseState)118 // execute the thunk

More repo setupMichel Weststratebefore adoption1637393

line 119why, one step back
119 thunk(rootClone)

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 120–121why, one step back
120 // and finalize the modified proxy
121 return finalize(baseState)

More repo setupMichel Weststratebefore adoption1637393

lines 122–127why, one step back
122}
123124function isPlainObject(value) {125 if (value === null || typeof value !== "object") return false126 const proto = Object.getPrototypeOf(value)127 return proto === Object.prototype || proto === null

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 128–130why, one step back
128}
129130function isProxy(value) {

Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e

line 131why, one step back
131 return !!value && !!value[isProxySymbol]

More repo setupMichel Weststratebefore adoption1637393

lines 132–134why, one step back
132}
133134module.exports = immer