Skip to content
Myr

mapset.ts

This file

Path

src/plugins/mapset.ts

340 lines · 8.0 KiB · TypeScript

Last changed by

Preserve error codes in productionMichel Weststratebefore adoptiond005fab

History of this file

Take it

More WIPMichel Weststratebefore adoptionee1c977

lines 1–7why, one step back
1// types only!
2import {3 ImmerState,4 AnyMap,5 AnySet,6 MapState,7 SetState,

TSDXMichel Weststratebefore adoption4c4fecc

lines 8–13why, one step back
8 DRAFT_STATE,
9 ImmerScope,10 latest,11 iteratorSymbol,12 isDraftable,13 createProxy,

New build set. Current statsMichel Weststratebefore adoption9aec0fe

line 14why, one step back
14 loadPlugin,

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 15–16why, one step back
15 markChanged,
16 ProxyTypeMap,

Preserve error codes in productionMichel Weststratebefore adoptiond005fab

lines 17–18why, one step back
17 ProxyTypeSet,
18 die

More WIPMichel Weststratebefore adoptionee1c977

lines 19–20why, one step back
19} from "../internal"
20

TSDXMichel Weststratebefore adoption4c4fecc

lines 21–26why, one step back
21export function enableMapSet() {
22 /* istanbul ignore next */23 var extendStatics = function(d: any, b: any): any {24 extendStatics =25 Object.setPrototypeOf ||26 ({__proto__: []} instanceof Array &&

More WIPMichel Weststratebefore adoptionee1c977

line 27why, one step back
27 function(d, b) {

TSDXMichel Weststratebefore adoption4c4fecc

lines 28–40why, one step back
28 d.__proto__ = b
29 }) ||30 function(d, b) {31 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]32 }33 return extendStatics(d, b)34 }3536 // Ugly hack to resolve #502 and inherit built in Map / Set37 function __extends(d: any, b: any): any {38 extendStatics(d, b)39 function __(this: any): any {40 this.constructor = d

More WIPMichel Weststratebefore adoptionee1c977

line 41why, one step back
41 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 42–45why, one step back
42 d.prototype =
43 // @ts-ignore44 ((__.prototype = b.prototype), new __())45 }

More WIPMichel Weststratebefore adoptionee1c977

line 46why, one step back
46

TSDXMichel Weststratebefore adoption4c4fecc

lines 47–51why, one step back
47 const DraftMap = (function(_super) {
48 __extends(DraftMap, _super)49 // Create class manually, cause #50250 function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {51 this[DRAFT_STATE] = {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 52why, one step back
52 type_: ProxyTypeMap,

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 53–63why, one step back
53 parent_: parent,
54 scope_: parent ? parent.scope_ : ImmerScope.current_!,55 modified_: false,56 finalized_: false,57 copy_: undefined,58 assigned_: undefined,59 base_: target,60 draft_: this as any,61 isManual_: false,62 revoked_: false63 } as MapState

TSDXMichel Weststratebefore adoption4c4fecc

line 64why, one step back
64 return this

More WIPMichel Weststratebefore adoptionee1c977

line 65why, one step back
65 }

TSDXMichel Weststratebefore adoption4c4fecc

line 66why, one step back
66 const p = DraftMap.prototype

More WIPMichel Weststratebefore adoptionee1c977

line 67why, one step back
67

TSDXMichel Weststratebefore adoption4c4fecc

lines 68–71why, one step back
68 // TODO: smaller build size if we create a util for Object.defineProperty
69 Object.defineProperty(p, "size", {70 get: function() {71 return latest(this[DRAFT_STATE]).size

Some notesMichel Weststratebefore adoption746dc6f

lines 72–74why, one step back
72 }
73 // enumerable: false,74 // configurable: true

TSDXMichel Weststratebefore adoption4c4fecc

line 75why, one step back
75 })

More WIPMichel Weststratebefore adoptionee1c977

line 76why, one step back
76

TSDXMichel Weststratebefore adoption4c4fecc

lines 77–79why, one step back
77 p.has = function(key: any): boolean {
78 return latest(this[DRAFT_STATE]).has(key)79 }

More WIPMichel Weststratebefore adoptionee1c977

line 80why, one step back
80

TSDXMichel Weststratebefore adoption4c4fecc

line 81why, one step back
81 p.set = function(key: any, value: any) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 82why, one step back
82 const state: MapState = this[DRAFT_STATE]

TSDXMichel Weststratebefore adoption4c4fecc

lines 83–84why, one step back
83 assertUnrevoked(state)
84 if (latest(state).get(key) !== value) {

More WIPMichel Weststratebefore adoptionee1c977

line 85why, one step back
85 prepareMapCopy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 86–89why, one step back
86 markChanged(state.scope_.immer_, state)
87 state.assigned_!.set(key, true)88 state.copy_!.set(key, value)89 state.assigned_!.set(key, true)

More WIPMichel Weststratebefore adoptionee1c977

line 90why, one step back
90 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 91–92why, one step back
91 return this
92 }

More WIPMichel Weststratebefore adoptionee1c977

line 93why, one step back
93

TSDXMichel Weststratebefore adoption4c4fecc

lines 94–96why, one step back
94 p.delete = function(key: any): boolean {
95 if (!this.has(key)) {96 return false

More WIPMichel Weststratebefore adoptionee1c977

lines 97–98why, one step back
97 }
98

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 99why, one step back
99 const state: MapState = this[DRAFT_STATE]

TSDXMichel Weststratebefore adoption4c4fecc

lines 100–101why, one step back
100 assertUnrevoked(state)
101 prepareMapCopy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 102–104why, one step back
102 markChanged(state.scope_.immer_, state)
103 state.assigned_!.set(key, false)104 state.copy_!.delete(key)

TSDXMichel Weststratebefore adoption4c4fecc

lines 105–106why, one step back
105 return true
106 }

More WIPMichel Weststratebefore adoptionee1c977

line 107why, one step back
107

TSDXMichel Weststratebefore adoption4c4fecc

line 108why, one step back
108 p.clear = function() {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 109why, one step back
109 const state: MapState = this[DRAFT_STATE]

TSDXMichel Weststratebefore adoption4c4fecc

lines 110–111why, one step back
110 assertUnrevoked(state)
111 prepareMapCopy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 112–114why, one step back
112 markChanged(state.scope_.immer_, state)
113 state.assigned_ = new Map()114 return state.copy_!.clear()

TSDXMichel Weststratebefore adoption4c4fecc

line 115why, one step back
115 }

More WIPMichel Weststratebefore adoptionee1c977

line 116why, one step back
116

TSDXMichel Weststratebefore adoption4c4fecc

lines 117–120why, one step back
117 p.forEach = function(
118 cb: (value: any, key: any, self: any) => void,119 thisArg?: any120 ) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 121why, one step back
121 const state: MapState = this[DRAFT_STATE]

TSDXMichel Weststratebefore adoption4c4fecc

lines 122–125why, one step back
122 latest(state).forEach((_value: any, key: any, _map: any) => {
123 cb.call(thisArg, this.get(key), key, this)124 })125 }

More WIPMichel Weststratebefore adoptionee1c977

line 126why, one step back
126

TSDXMichel Weststratebefore adoption4c4fecc

line 127why, one step back
127 p.get = function(key: any): any {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 128why, one step back
128 const state: MapState = this[DRAFT_STATE]

TSDXMichel Weststratebefore adoption4c4fecc

lines 129–130why, one step back
129 assertUnrevoked(state)
130 const value = latest(state).get(key)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 131why, one step back
131 if (state.finalized_ || !isDraftable(value)) {

TSDXMichel Weststratebefore adoption4c4fecc

line 132why, one step back
132 return value

More WIPMichel Weststratebefore adoptionee1c977

line 133why, one step back
133 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 134why, one step back
134 if (value !== state.base_.get(key)) {

TSDXMichel Weststratebefore adoption4c4fecc

line 135why, one step back
135 return value // either already drafted or reassigned

More WIPMichel Weststratebefore adoptionee1c977

line 136why, one step back
136 }

TSDXMichel Weststratebefore adoption4c4fecc

line 137why, one step back
137 // despite what it looks, this creates a draft only once, see above condition

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 138why, one step back
138 const draft = createProxy(state.scope_.immer_, value, state)

TSDXMichel Weststratebefore adoption4c4fecc

line 139why, one step back
139 prepareMapCopy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 140why, one step back
140 state.copy_!.set(key, draft)

TSDXMichel Weststratebefore adoption4c4fecc

lines 141–142why, one step back
141 return draft
142 }

More WIPMichel Weststratebefore adoptionee1c977

line 143why, one step back
143

TSDXMichel Weststratebefore adoption4c4fecc

lines 144–146why, one step back
144 p.keys = function(): IterableIterator<any> {
145 return latest(this[DRAFT_STATE]).keys()146 }

More WIPMichel Weststratebefore adoptionee1c977

line 147why, one step back
147

TSDXMichel Weststratebefore adoption4c4fecc

lines 148–163why, one step back
148 p.values = function(): IterableIterator<any> {
149 const iterator = this.keys()150 return {151 [iteratorSymbol]: () => this.values(),152 next: () => {153 const r = iterator.next()154 /* istanbul ignore next */155 if (r.done) return r156 const value = this.get(r.value)157 return {158 done: false,159 value160 }161 }162 } as any163 }

More WIPMichel Weststratebefore adoptionee1c977

line 164why, one step back
164

TSDXMichel Weststratebefore adoption4c4fecc

lines 165–179why, one step back
165 p.entries = function(): IterableIterator<[any, any]> {
166 const iterator = this.keys()167 return {168 [iteratorSymbol]: () => this.entries(),169 next: () => {170 const r = iterator.next()171 /* istanbul ignore next */172 if (r.done) return r173 const value = this.get(r.value)174 return {175 done: false,176 value: [r.value, value]177 }178 }179 } as any

More WIPMichel Weststratebefore adoptionee1c977

lines 180–181why, one step back
180 }
181

TSDXMichel Weststratebefore adoption4c4fecc

lines 182–183why, one step back
182 p[iteratorSymbol] = function() {
183 return this.entries()

More WIPMichel Weststratebefore adoptionee1c977

lines 184–185why, one step back
184 }
185

TSDXMichel Weststratebefore adoption4c4fecc

lines 186–187why, one step back
186 return DraftMap
187 })(Map)

More WIPMichel Weststratebefore adoptionee1c977

line 188why, one step back
188

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 189why, one step back
189 function proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T {

TSDXMichel Weststratebefore adoption4c4fecc

lines 190–192why, one step back
190 // @ts-ignore
191 return new DraftMap(target, parent)192 }

More WIPMichel Weststratebefore adoptionee1c977

line 193why, one step back
193

TSDXMichel Weststratebefore adoption4c4fecc

line 194why, one step back
194 function prepareMapCopy(state: MapState) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 195–197why, one step back
195 if (!state.copy_) {
196 state.assigned_ = new Map()197 state.copy_ = new Map(state.base_)

TSDXMichel Weststratebefore adoption4c4fecc

lines 198–199why, one step back
198 }
199 }

More WIPMichel Weststratebefore adoptionee1c977

line 200why, one step back
200

TSDXMichel Weststratebefore adoption4c4fecc

lines 201–205why, one step back
201 const DraftSet = (function(_super) {
202 __extends(DraftSet, _super)203 // Create class manually, cause #502204 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {205 this[DRAFT_STATE] = {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

line 206why, one step back
206 type_: ProxyTypeSet,

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 207–217why, one step back
207 parent_: parent,
208 scope_: parent ? parent.scope_ : ImmerScope.current_!,209 modified_: false,210 finalized_: false,211 copy_: undefined,212 base_: target,213 draft_: this,214 drafts_: new Map(),215 revoked_: false,216 isManual_: false217 } as SetState

TSDXMichel Weststratebefore adoption4c4fecc

lines 218–220why, one step back
218 return this
219 }220 const p = DraftSet.prototype

More WIPMichel Weststratebefore adoptionee1c977

line 221why, one step back
221

TSDXMichel Weststratebefore adoption4c4fecc

lines 222–224why, one step back
222 Object.defineProperty(p, "size", {
223 get: function() {224 return latest(this[DRAFT_STATE]).size

Some notesMichel Weststratebefore adoption746dc6f

lines 225–226why, one step back
225 }
226 // enumerable: true,

TSDXMichel Weststratebefore adoption4c4fecc

line 227why, one step back
227 })

More WIPMichel Weststratebefore adoptionee1c977

line 228why, one step back
228

TSDXMichel Weststratebefore adoption4c4fecc

line 229why, one step back
229 p.has = function(value: any): boolean {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 230why, one step back
230 const state: SetState = this[DRAFT_STATE]

TSDXMichel Weststratebefore adoption4c4fecc

lines 231–232why, one step back
231 assertUnrevoked(state)
232 // bit of trickery here, to be able to recognize both the value, and the draft of its value

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 233–234why, one step back
233 if (!state.copy_) {
234 return state.base_.has(value)

More WIPMichel Weststratebefore adoptionee1c977

line 235why, one step back
235 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 236–237why, one step back
236 if (state.copy_.has(value)) return true
237 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))

TSDXMichel Weststratebefore adoption4c4fecc

lines 238–240why, one step back
238 return true
239 return false240 }

More WIPMichel Weststratebefore adoptionee1c977

line 241why, one step back
241

TSDXMichel Weststratebefore adoption4c4fecc

line 242why, one step back
242 p.add = function(value: any): any {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 243why, one step back
243 const state: SetState = this[DRAFT_STATE]

TSDXMichel Weststratebefore adoption4c4fecc

line 244why, one step back
244 assertUnrevoked(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 245–247why, one step back
245 if (state.copy_) {
246 state.copy_.add(value)247 } else if (!state.base_.has(value)) {

More WIPMichel Weststratebefore adoptionee1c977

line 248why, one step back
248 prepareSetCopy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 249–250why, one step back
249 markChanged(state.scope_.immer_, state)
250 state.copy_!.add(value)

More WIPMichel Weststratebefore adoptionee1c977

line 251why, one step back
251 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 252–253why, one step back
252 return this
253 }

More WIPMichel Weststratebefore adoptionee1c977

line 254why, one step back
254

TSDXMichel Weststratebefore adoption4c4fecc

lines 255–257why, one step back
255 p.delete = function(value: any): any {
256 if (!this.has(value)) {257 return false

More WIPMichel Weststratebefore adoptionee1c977

lines 258–259why, one step back
258 }
259

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 260why, one step back
260 const state: SetState = this[DRAFT_STATE]

TSDXMichel Weststratebefore adoption4c4fecc

lines 261–262why, one step back
261 assertUnrevoked(state)
262 prepareSetCopy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 263why, one step back
263 markChanged(state.scope_.immer_, state)

TSDXMichel Weststratebefore adoption4c4fecc

line 264why, one step back
264 return (

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 265–267why, one step back
265 state.copy_!.delete(value) ||
266 (state.drafts_.has(value)267 ? state.copy_!.delete(state.drafts_.get(value))

TSDXMichel Weststratebefore adoption4c4fecc

lines 268–270why, one step back
268 : /* istanbul ignore next */ false)
269 )270 }

More WIPMichel Weststratebefore adoptionee1c977

line 271why, one step back
271

TSDXMichel Weststratebefore adoption4c4fecc

line 272why, one step back
272 p.clear = function() {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 273why, one step back
273 const state: SetState = this[DRAFT_STATE]

TSDXMichel Weststratebefore adoption4c4fecc

lines 274–275why, one step back
274 assertUnrevoked(state)
275 prepareSetCopy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 276–277why, one step back
276 markChanged(state.scope_.immer_, state)
277 return state.copy_!.clear()

TSDXMichel Weststratebefore adoption4c4fecc

line 278why, one step back
278 }

More WIPMichel Weststratebefore adoptionee1c977

line 279why, one step back
279

TSDXMichel Weststratebefore adoption4c4fecc

line 280why, one step back
280 p.values = function(): IterableIterator<any> {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 281why, one step back
281 const state: SetState = this[DRAFT_STATE]

TSDXMichel Weststratebefore adoption4c4fecc

lines 282–283why, one step back
282 assertUnrevoked(state)
283 prepareSetCopy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 284why, one step back
284 return state.copy_!.values()

TSDXMichel Weststratebefore adoption4c4fecc

line 285why, one step back
285 }

More WIPMichel Weststratebefore adoptionee1c977

line 286why, one step back
286

TSDXMichel Weststratebefore adoption4c4fecc

line 287why, one step back
287 p.entries = function entries(): IterableIterator<[any, any]> {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 288why, one step back
288 const state: SetState = this[DRAFT_STATE]

TSDXMichel Weststratebefore adoption4c4fecc

lines 289–290why, one step back
289 assertUnrevoked(state)
290 prepareSetCopy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 291why, one step back
291 return state.copy_!.entries()

TSDXMichel Weststratebefore adoption4c4fecc

line 292why, one step back
292 }

More WIPMichel Weststratebefore adoptionee1c977

line 293why, one step back
293

TSDXMichel Weststratebefore adoption4c4fecc

lines 294–296why, one step back
294 p.keys = function(): IterableIterator<any> {
295 return this.values()296 }

More WIPMichel Weststratebefore adoptionee1c977

line 297why, one step back
297

TSDXMichel Weststratebefore adoption4c4fecc

lines 298–299why, one step back
298 p[iteratorSymbol] = function() {
299 return this.values()

More WIPMichel Weststratebefore adoptionee1c977

lines 300–301why, one step back
300 }
301

TSDXMichel Weststratebefore adoption4c4fecc

lines 302–307why, one step back
302 p.forEach = function forEach(cb: any, thisArg?: any) {
303 const iterator = this.values()304 let result = iterator.next()305 while (!result.done) {306 cb.call(thisArg, result.value, result.value, this)307 result = iterator.next()

More WIPMichel Weststratebefore adoptionee1c977

lines 308–310why, one step back
308 }
309 }310

TSDXMichel Weststratebefore adoption4c4fecc

lines 311–313why, one step back
311 return DraftSet
312 })(Set)313

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 314why, one step back
314 function proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T {

TSDXMichel Weststratebefore adoption4c4fecc

lines 315–316why, one step back
315 // @ts-ignore
316 return new DraftSet(target, parent)

More WIPMichel Weststratebefore adoptionee1c977

line 317why, one step back
317 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 318–319why, one step back
318
319 function prepareSetCopy(state: SetState) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 320why, one step back
320 if (!state.copy_) {

TSDXMichel Weststratebefore adoption4c4fecc

line 321why, one step back
321 // create drafts for all entries to preserve insertion order

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 322–323why, one step back
322 state.copy_ = new Set()
323 state.base_.forEach(value => {

TSDXMichel Weststratebefore adoption4c4fecc

line 324why, one step back
324 if (isDraftable(value)) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 325–327why, one step back
325 const draft = createProxy(state.scope_.immer_, value, state)
326 state.drafts_.set(value, draft)327 state.copy_!.add(draft)

TSDXMichel Weststratebefore adoption4c4fecc

line 328why, one step back
328 } else {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 329why, one step back
329 state.copy_!.add(value)

TSDXMichel Weststratebefore adoption4c4fecc

lines 330–332why, one step back
330 }
331 })332 }

Preserve error codes in productionMichel Weststratebefore adoptiond005fab

lines 333–336why, one step back
333 }
334335 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {336 if (state.revoked_) die(3, JSON.stringify(latest(state)))

TSDXMichel Weststratebefore adoption4c4fecc

lines 337–338why, one step back
337 }
338

scramble plugin names, -56Michel Weststratebefore adoption037b474

line 339why, one step back
339 loadPlugin("mapset", {proxyMap_, proxySet_})

TSDXMichel Weststratebefore adoption4c4fecc

line 340why, one step back
340}