Skip to content
Myr

mapset.ts

This file

Path

src/plugins/mapset.ts

340 lines · 8.1 KiB · TypeScript

Last changed by

scramble plugin names, -56Michel Weststratebefore adoption037b474

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–14why, one step back
8 DRAFT_STATE,
9 ImmerScope,10 latest,11 assertUnrevoked,12 iteratorSymbol,13 isDraftable,14 createProxy,

New build set. Current statsMichel Weststratebefore adoption9aec0fe

line 15why, one step back
15 loadPlugin,

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

lines 16–18why, one step back
16 markChanged,
17 ProxyTypeMap,18 ProxyTypeSet

More WIPMichel Weststratebefore adoptionee1c977

line 19why, one step back
19} from "../internal"

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 20why, one step back
20import invariant from "tiny-invariant"

More WIPMichel Weststratebefore adoptionee1c977

line 21why, one step back
21

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 42why, one step back
42 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 43–46why, one step back
43 d.prototype =
44 // @ts-ignore45 ((__.prototype = b.prototype), new __())46 }

More WIPMichel Weststratebefore adoptionee1c977

line 47why, one step back
47

TSDXMichel Weststratebefore adoption4c4fecc

line 48why, one step back
48 const DraftMap = (function(_super) {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

line 49why, one step back
49 invariant(_super, "Map is not polyfilled")

TSDXMichel Weststratebefore adoption4c4fecc

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

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 66why, one step back
66 return this

More WIPMichel Weststratebefore adoptionee1c977

line 67why, one step back
67 }

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 69why, one step back
69

TSDXMichel Weststratebefore adoption4c4fecc

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

Some notesMichel Weststratebefore adoption746dc6f

lines 74–76why, one step back
74 }
75 // enumerable: false,76 // configurable: true

TSDXMichel Weststratebefore adoption4c4fecc

line 77why, one step back
77 })

More WIPMichel Weststratebefore adoptionee1c977

line 78why, one step back
78

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 82why, one step back
82

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 92why, one step back
92 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 93–94why, one step back
93 return this
94 }

More WIPMichel Weststratebefore adoptionee1c977

line 95why, one step back
95

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

lines 99–100why, one step back
99 }
100

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 102–103why, one step back
102 assertUnrevoked(state)
103 prepareMapCopy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 107–108why, one step back
107 return true
108 }

More WIPMichel Weststratebefore adoptionee1c977

line 109why, one step back
109

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 112–113why, one step back
112 assertUnrevoked(state)
113 prepareMapCopy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 117why, one step back
117 }

More WIPMichel Weststratebefore adoptionee1c977

line 118why, one step back
118

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 128why, one step back
128

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 134why, one step back
134 return value

More WIPMichel Weststratebefore adoptionee1c977

line 135why, one step back
135 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 138why, one step back
138 }

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 143–144why, one step back
143 return draft
144 }

More WIPMichel Weststratebefore adoptionee1c977

line 145why, one step back
145

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 149why, one step back
149

TSDXMichel Weststratebefore adoption4c4fecc

lines 150–165why, one step back
150 p.values = function(): IterableIterator<any> {
151 const iterator = this.keys()152 return {153 [iteratorSymbol]: () => this.values(),154 next: () => {155 const r = iterator.next()156 /* istanbul ignore next */157 if (r.done) return r158 const value = this.get(r.value)159 return {160 done: false,161 value162 }163 }164 } as any165 }

More WIPMichel Weststratebefore adoptionee1c977

line 166why, one step back
166

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

lines 186–187why, one step back
186 }
187

TSDXMichel Weststratebefore adoption4c4fecc

lines 188–189why, one step back
188 return DraftMap
189 })(Map)

More WIPMichel Weststratebefore adoptionee1c977

line 190why, one step back
190

scramble plugin names, -56Michel Weststratebefore adoption037b474

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 195why, one step back
195

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 200–201why, one step back
200 }
201 }

More WIPMichel Weststratebefore adoptionee1c977

line 202why, one step back
202

TSDXMichel Weststratebefore adoption4c4fecc

line 203why, one step back
203 const DraftSet = (function(_super) {

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

lines 204–205why, one step back
204 invariant(_super, "Set is not polyfilled")
205

TSDXMichel Weststratebefore adoption4c4fecc

lines 206–209why, one step back
206 __extends(DraftSet, _super)
207 // Create class manually, cause #502208 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {209 this[DRAFT_STATE] = {

Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 222–224why, one step back
222 return this
223 }224 const p = DraftSet.prototype

More WIPMichel Weststratebefore adoptionee1c977

line 225why, one step back
225

TSDXMichel Weststratebefore adoption4c4fecc

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

Some notesMichel Weststratebefore adoption746dc6f

lines 229–230why, one step back
229 }
230 // enumerable: true,

TSDXMichel Weststratebefore adoption4c4fecc

line 231why, one step back
231 })

More WIPMichel Weststratebefore adoptionee1c977

line 232why, one step back
232

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 235–236why, one step back
235 assertUnrevoked(state)
236 // 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 237–238why, one step back
237 if (!state.copy_) {
238 return state.base_.has(value)

More WIPMichel Weststratebefore adoptionee1c977

line 239why, one step back
239 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 242–244why, one step back
242 return true
243 return false244 }

More WIPMichel Weststratebefore adoptionee1c977

line 245why, one step back
245

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 249–251why, one step back
249 if (state.copy_) {
250 state.copy_.add(value)251 } else if (!state.base_.has(value)) {

More WIPMichel Weststratebefore adoptionee1c977

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 255why, one step back
255 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 256–257why, one step back
256 return this
257 }

More WIPMichel Weststratebefore adoptionee1c977

line 258why, one step back
258

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

lines 262–263why, one step back
262 }
263

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 265–266why, one step back
265 assertUnrevoked(state)
266 prepareSetCopy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 268why, one step back
268 return (

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 269–271why, one step back
269 state.copy_!.delete(value) ||
270 (state.drafts_.has(value)271 ? state.copy_!.delete(state.drafts_.get(value))

TSDXMichel Weststratebefore adoption4c4fecc

lines 272–274why, one step back
272 : /* istanbul ignore next */ false)
273 )274 }

More WIPMichel Weststratebefore adoptionee1c977

line 275why, one step back
275

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 278–279why, one step back
278 assertUnrevoked(state)
279 prepareSetCopy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 282why, one step back
282 }

More WIPMichel Weststratebefore adoptionee1c977

line 283why, one step back
283

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 286–287why, one step back
286 assertUnrevoked(state)
287 prepareSetCopy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 289why, one step back
289 }

More WIPMichel Weststratebefore adoptionee1c977

line 290why, one step back
290

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 293–294why, one step back
293 assertUnrevoked(state)
294 prepareSetCopy(state)

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 296why, one step back
296 }

More WIPMichel Weststratebefore adoptionee1c977

line 297why, one step back
297

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 301why, one step back
301

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

lines 304–305why, one step back
304 }
305

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

lines 312–314why, one step back
312 }
313 }314

TSDXMichel Weststratebefore adoption4c4fecc

lines 315–317why, one step back
315 return DraftSet
316 })(Set)317

scramble plugin names, -56Michel Weststratebefore adoption037b474

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 321why, one step back
321 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 322–323why, one step back
322
323 function prepareSetCopy(state: SetState) {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

lines 329–331why, one step back
329 const draft = createProxy(state.scope_.immer_, value, state)
330 state.drafts_.set(value, draft)331 state.copy_!.add(draft)

TSDXMichel Weststratebefore adoption4c4fecc

line 332why, one step back
332 } else {

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

lines 334–338why, one step back
334 }
335 })336 }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}