Skip to content
Myr

mapset.ts

This file

Path

src/plugins/mapset.ts

339 lines · 8.0 KiB · TypeScript

Last changed by

unclassed scopeMichel Weststratebefore adoption7a38af9

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

line 8why, one step back
8 DRAFT_STATE,

unclassed scopeMichel Weststratebefore adoption7a38af9

line 9why, one step back
9 getCurrentScope,

TSDXMichel Weststratebefore adoption4c4fecc

lines 10–13why, one step back
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

line 53why, one step back
53 parent_: parent,

unclassed scopeMichel Weststratebefore adoption7a38af9

line 54why, one step back
54 scope_: parent ? parent.scope_ : getCurrentScope()!,

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 55–63why, one step back
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–70why, one step back
68 Object.defineProperty(p, "size", {
69 get: function() {70 return latest(this[DRAFT_STATE]).size

Some notesMichel Weststratebefore adoption746dc6f

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

TSDXMichel Weststratebefore adoption4c4fecc

line 74why, one step back
74 })

More WIPMichel Weststratebefore adoptionee1c977

line 75why, one step back
75

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 79why, one step back
79

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 89why, one step back
89 }

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 92why, one step back
92

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 106why, one step back
106

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 114why, one step back
114 }

More WIPMichel Weststratebefore adoptionee1c977

line 115why, one step back
115

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 125why, one step back
125

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 131why, one step back
131 return value

More WIPMichel Weststratebefore adoptionee1c977

line 132why, one step back
132 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 135why, one step back
135 }

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 142why, one step back
142

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 146why, one step back
146

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 163why, one step back
163

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 187why, one step back
187

scramble plugin names, -56Michel Weststratebefore adoption037b474

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 192why, one step back
192

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 199why, one step back
199

TSDXMichel Weststratebefore adoption4c4fecc

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

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

line 206why, one step back
206 parent_: parent,

unclassed scopeMichel Weststratebefore adoption7a38af9

line 207why, one step back
207 scope_: parent ? parent.scope_ : getCurrentScope()!,

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 208–216why, one step back
208 modified_: false,
209 finalized_: false,210 copy_: undefined,211 base_: target,212 draft_: this,213 drafts_: new Map(),214 revoked_: false,215 isManual_: false216 } as SetState

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 220why, one step back
220

TSDXMichel Weststratebefore adoption4c4fecc

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

Some notesMichel Weststratebefore adoption746dc6f

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

TSDXMichel Weststratebefore adoption4c4fecc

line 226why, one step back
226 })

More WIPMichel Weststratebefore adoptionee1c977

line 227why, one step back
227

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 230–231why, one step back
230 assertUnrevoked(state)
231 // 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 232–233why, one step back
232 if (!state.copy_) {
233 return state.base_.has(value)

More WIPMichel Weststratebefore adoptionee1c977

line 234why, one step back
234 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 237–239why, one step back
237 return true
238 return false239 }

More WIPMichel Weststratebefore adoptionee1c977

line 240why, one step back
240

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 250why, one step back
250 }

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 253why, one step back
253

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 263why, one step back
263 return (

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 270why, one step back
270

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 277why, one step back
277 }

More WIPMichel Weststratebefore adoptionee1c977

line 278why, one step back
278

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 284why, one step back
284 }

More WIPMichel Weststratebefore adoptionee1c977

line 285why, one step back
285

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 291why, one step back
291 }

More WIPMichel Weststratebefore adoptionee1c977

line 292why, one step back
292

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 296why, one step back
296

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

scramble plugin names, -56Michel Weststratebefore adoption037b474

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 316why, one step back
316 }

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 327why, one step back
327 } else {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Preserve error codes in productionMichel Weststratebefore adoptiond005fab

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Tests for pluginsMichel Weststratebefore adoptiondb16520

line 338why, one step back
338 loadPlugin("MapSet", {proxyMap_, proxySet_})

TSDXMichel Weststratebefore adoption4c4fecc

line 339why, one step back
339}