Skip to content
Myr

mapset.ts

This file

Path

src/plugins/mapset.ts

340 lines · 8.0 KiB · TypeScript

Last changed by

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

New build set. Current statsMichel Weststratebefore adoption9aec0fe

lines 16–17why, one step back
16 loadPlugin,
17 markChanged

More WIPMichel Weststratebefore adoptionee1c977

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

More WIPMichel Weststratebefore adoptionee1c977

line 20why, one step back
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

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 65why, one step back
65 return this

More WIPMichel Weststratebefore adoptionee1c977

line 66why, one step back
66 }

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 68why, one step back
68

TSDXMichel Weststratebefore adoption4c4fecc

lines 69–76why, one step back
69 // TODO: smaller build size if we create a util for Object.defineProperty
70 Object.defineProperty(p, "size", {71 get: function() {72 return latest(this[DRAFT_STATE]).size73 },74 enumerable: true,75 configurable: true76 })

More WIPMichel Weststratebefore adoptionee1c977

line 77why, one step back
77

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 81why, one step back
81

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

More WIPMichel Weststratebefore adoptionee1c977

line 91why, one step back
91 }

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 94why, one step back
94

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 108why, one step back
108

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 116why, one step back
116 }

More WIPMichel Weststratebefore adoptionee1c977

line 117why, one step back
117

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 127why, one step back
127

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

line 133why, one step back
133 return value

More WIPMichel Weststratebefore adoptionee1c977

line 134why, one step back
134 }

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 137why, one step back
137 }

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 144why, one step back
144

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 148why, one step back
148

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 165why, one step back
165

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 189why, one step back
189

TSDXMichel Weststratebefore adoption4c4fecc

lines 190–193why, one step back
190 function proxyMap<T extends AnyMap>(target: T, parent?: ImmerState): T {
191 // @ts-ignore192 return new DraftMap(target, parent)193 }

More WIPMichel Weststratebefore adoptionee1c977

line 194why, one step back
194

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 201why, one step back
201

TSDXMichel Weststratebefore adoption4c4fecc

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

use invariant -321 bytesMichel Weststratebefore adoptionac57da5

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

TSDXMichel Weststratebefore adoption4c4fecc

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

Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 224why, one step back
224

TSDXMichel Weststratebefore adoption4c4fecc

lines 225–231why, one step back
225 Object.defineProperty(p, "size", {
226 get: function() {227 return latest(this[DRAFT_STATE]).size228 },229 enumerable: true,230 configurable: true231 })

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–320why, one step back
315 return DraftSet
316 })(Set)317318 function proxySet<T extends AnySet>(target: T, parent?: ImmerState): T {319 // @ts-ignore320 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–340why, one step back
334 }
335 })336 }337 }338339 loadPlugin("mapset", {proxyMap, proxySet})340}