Skip to content
Myr

immer.js

This file

Path

immer.js

170 lines · 5.4 KiB · JavaScript

Last changed by

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

History of this file

Take it

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

lines 1–6why, one step back
1/**
2 * @typedef {Object} RevocableProxy3 * @property {any} proxy4 * @property {Function} revoke5 */6

More repo setupMichel Weststratebefore adoption1637393

lines 7–8why, one step back
7// @ts-check
8

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

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

More repo setupMichel Weststratebefore adoption1637393

line 11why, one step back
11/**

Merge pull request #9 from benbraou/typosMichel Weststratebefore adoptionae0182f

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

More repo setupMichel Weststratebefore adoption1637393

lines 13–21why, one step back
13 * That function can freely mutate the state, as it will create copies-on-write.
14 * This means that the original state will stay unchanged, and once the function finishes, the modified state is returned15 *16 * @export17 * @param {any} baseState - the state to start with18 * @param {Function} thunk - function that receives a proxy of the base state as first argument and which can be freely modified19 * @returns {any} a new state, or the base state if nothing was modified20 */21function immer(baseState, thunk) {

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

lines 22–27why, one step back
22
23 /**24 * Maps baseState objects to revocable proxies25 * @type {Map<Object,RevocableProxy>}26 */27 const revocableProxies = new Map()

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

line 29why, one step back
29

More repo setupMichel Weststratebefore adoption1637393

lines 30–33why, one step back
30 const copies = new Map()
3132 const objectTraps = {33 get(target, prop) {

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

More repo setupMichel Weststratebefore adoption1637393

lines 36–43why, one step back
36 },
37 has(target, prop) {38 return prop in getCurrentSource(target)39 },40 ownKeys(target) {41 return Reflect.ownKeys(getCurrentSource(target))42 },43 set(target, prop, value) {

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

More repo setupMichel Weststratebefore adoption1637393

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

lines 48–50why, one step back
48 copy[prop] = isProxy(newValue)
49 ? newValue[isProxySymbol]50 : newValue

More repo setupMichel Weststratebefore adoption1637393

lines 51–54why, one step back
51 }
52 return true53 },54 deleteProperty(target, property) {

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

More repo setupMichel Weststratebefore adoption1637393

lines 56–57why, one step back
56 delete copy[property]
57 return true

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

line 58why, one step back
58 },

More repo setupMichel Weststratebefore adoption1637393

lines 59–60why, one step back
59 }
60

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

More repo setupMichel Weststratebefore adoption1637393

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

More repo setupMichel Weststratebefore adoption1637393

lines 67–70why, one step back
67 }
68 return copy69 }70

Merge pull request #9 from benbraou/typosMichel Weststratebefore adoptionae0182f

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 72–74why, one step back
72 function getCurrentSource(base) {
73 const copy = copies.get(base)74 return copy || base

More repo setupMichel Weststratebefore adoption1637393

line 75why, one step back
75 }

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

line 76why, one step back
76

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

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

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

lines 81–85why, one step back
81 if (revocableProxies.has(base)) return revocableProxies.get(base).proxy
82 //const proxy = new Proxy(base, objectTraps)83 const revocableProxy = Proxy.revocable(base, objectTraps)84 revocableProxies.set(base, revocableProxy)85 return revocableProxy.proxy

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 86–87why, one step back
86 }
87 return base

More repo setupMichel Weststratebefore adoption1637393

lines 88–89why, one step back
88 }
89

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 90–92why, one step back
90 // checks if the given base object has modifications, either because it is modified, or
91 // because one of it's children is92 function hasChanges(base) {

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

line 93why, one step back
93 const proxy = revocableProxies.get(base)

More repo setupMichel Weststratebefore adoption1637393

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

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

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

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

lines 100–104why, one step back
100 if (
101 (Array.isArray(value) || isPlainObject(value)) &&102 hasChanges(value)103 )104 return true

More repo setupMichel Weststratebefore adoption1637393

lines 105–108why, one step back
105 }
106 return false107 }108

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 109–115why, one step back
109 // given a base object, returns it if unmodified, or return the changed cloned if modified
110 function finalize(base) {111 if (isPlainObject(base)) return finalizeObject(base)112 if (Array.isArray(base)) return finalizeArray(base)113 return base114 }115

More repo setupMichel Weststratebefore adoption1637393

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

More repo setupMichel Weststratebefore adoption1637393

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

lines 121–125why, one step back
121 })
122 return copy123 }124125 function finalizeArray(thing) {

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Add support for arraysMichel Weststratebefore adoptiond3696cb

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

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

More repo setupMichel Weststratebefore adoption1637393

lines 130–133why, one step back
130 })
131 return copy132 }133

Code cleanupMichel Weststratebefore adoption2b2d0cb

lines 134–136why, one step back
134 // create proxy for root
135 const rootClone = createProxy(baseState)136 // execute the thunk

Merge branch 'Gregjarvez-master'Michel Weststratebefore adoptiona151fed

lines 137–142why, one step back
137 const maybeVoidReturn = thunk(rootClone)
138 //values either than undefined will trigger warning;139 !Object.is(maybeVoidReturn, undefined) &&140 console.warn(141 `Immer callback expects no return value. However ${typeof maybeVoidReturn} was returned`,142 )

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

lines 143–144why, one step back
143 // revoke all proxies
144 revoke(revocableProxies)

Code cleanupMichel Weststratebefore adoption2b2d0cb

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

Merge pull request #14 from benbraou/issue10Michel Weststratebefore adoptione775cca

lines 147–157why, one step back
147}
148149/**150 * Revoke all the proxies stored in the revocableProxies map151 * 152 * @param {Map<Object,RevocableProxy>} revocableProxies153 */154function revoke (revocableProxies){155 for (var revocableProxy of revocableProxies.values()) {156 revocableProxy.revoke()157 }

More repo setupMichel Weststratebefore adoption1637393

lines 158–163why, one step back
158}
159160function isPlainObject(value) {161 if (value === null || typeof value !== "object") return false162 const proto = Object.getPrototypeOf(value)163 return proto === Object.prototype || proto === null

Fixed bug, wrote readmeMichel Weststratebefore adoption1b8a694

lines 164–166why, one step back
164}
165166function isProxy(value) {

Merge pull request #13 from benbraou/issue12Michel Weststratebefore adoption8eab91e

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

More repo setupMichel Weststratebefore adoption1637393

lines 168–170why, one step back
168}
169170module.exports = immer