Skip to content
Myr

immer.js

This file

Path

immer.js

147 lines · 4.8 KiB · JavaScript

Last changed by

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

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)

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

lines 37–39why, one step back
37 copy[prop] = isProxy(newValue)
38 ? newValue[isProxySymbol]39 : newValue

More repo setupMichel Weststratebefore adoption1637393

lines 40–43why, one step back
40 }
41 return true42 },43 deleteProperty(target, property) {

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

More repo setupMichel Weststratebefore adoption1637393

lines 45–46why, one step back
45 delete copy[property]
46 return true

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 47why, one step back
47 },

More repo setupMichel Weststratebefore adoption1637393

lines 48–49why, one step back
48 }
49

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

More repo setupMichel Weststratebefore adoption1637393

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

More repo setupMichel Weststratebefore adoption1637393

lines 56–59why, one step back
56 }
57 return copy58 }59

Merge pull request #9 from benbraou/typosMichel Weststratebefore adoptionae0182f

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

More repo setupMichel Weststratebefore adoption1637393

lines 64–65why, one step back
64 }
65

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

More repo setupMichel Weststratebefore adoption1637393

lines 76–77why, one step back
76 }
77

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

More repo setupMichel Weststratebefore adoption1637393

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

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

line 87why, one step back
87 const value = base[keys[i]]

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

lines 88–92why, one step back
88 if (
89 (Array.isArray(value) || isPlainObject(value)) &&90 hasChanges(value)91 )92 return true

More repo setupMichel Weststratebefore adoption1637393

lines 93–96why, one step back
93 }
94 return false95 }96

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 97–103why, one step back
97 // given a base object, returns it if unmodified, or return the changed cloned if modified
98 function finalize(base) {99 if (isPlainObject(base)) return finalizeObject(base)100 if (Array.isArray(base)) return finalizeArray(base)101 return base102 }103

More repo setupMichel Weststratebefore adoption1637393

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

More repo setupMichel Weststratebefore adoption1637393

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 109–113why, one step back
109 })
110 return copy111 }112113 function finalizeArray(thing) {

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

More repo setupMichel Weststratebefore adoption1637393

lines 118–121why, one step back
118 })
119 return copy120 }121

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 122–124why, one step back
122 // create proxy for root
123 const rootClone = createProxy(baseState)124 // execute the thunk

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

lines 125–132why, one step back
125 const maybeVoidReturn = thunk(rootClone)
126127 //values either than undefined will trigger warning;128 !Object.is(maybeVoidReturn, undefined) &&129 console.warn(130 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`,131 )132

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

More repo setupMichel Weststratebefore adoption1637393

lines 135–140why, one step back
135}
136137function isPlainObject(value) {138 if (value === null || typeof value !== "object") return false139 const proto = Object.getPrototypeOf(value)140 return proto === Object.prototype || proto === null

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 141–143why, one step back
141}
142143function isProxy(value) {

Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e

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

More repo setupMichel Weststratebefore adoption1637393

lines 145–147why, one step back
145}
146147module.exports = immer