Skip to content
Myr

mapset.ts

This file

Path

src/plugins/mapset.ts

343 lines · 7.9 KiB · TypeScript

Last changed by

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

lines 17–18why, one step back
17} from "../internal"
18

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 39why, one step back
39 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 40–43why, one step back
40 d.prototype =
41 // @ts-ignore42 ((__.prototype = b.prototype), new __())43 }

More WIPMichel Weststratebefore adoptionee1c977

line 44why, one step back
44

TSDXMichel Weststratebefore adoption4c4fecc

lines 45–64why, one step back
45 const DraftMap = (function(_super) {
46 if (!_super) {47 /* istanbul ignore next */48 throw new Error("Map is not polyfilled")49 }50 __extends(DraftMap, _super)51 // Create class manually, cause #50252 function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {53 this[DRAFT_STATE] = {54 type: ProxyType.Map,55 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: false

More WIPMichel Weststratebefore adoptionee1c977

line 65why, one step back
65 }

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–77why, 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]).size74 },75 enumerable: true,76 configurable: true77 })

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

lines 83–86why, one step back
83 p.set = function(key: any, value: any) {
84 const state = this[DRAFT_STATE]85 assertUnrevoked(state)86 if (latest(state).get(key) !== value) {

More WIPMichel Weststratebefore adoptionee1c977

lines 87–88why, one step back
87 prepareMapCopy(state)
88 state.scope.immer.markChanged(state)

TSDXMichel Weststratebefore adoption4c4fecc

lines 89–91why, one step back
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

TSDXMichel Weststratebefore adoption4c4fecc

lines 101–108why, one step back
101 const state = this[DRAFT_STATE]
102 assertUnrevoked(state)103 prepareMapCopy(state)104 state.scope.immer.markChanged(state)105 state.assigned!.set(key, false)106 state.copy!.delete(key)107 return true108 }

More WIPMichel Weststratebefore adoptionee1c977

line 109why, one step back
109

TSDXMichel Weststratebefore adoption4c4fecc

lines 110–117why, one step back
110 p.clear = function() {
111 const state = this[DRAFT_STATE]112 assertUnrevoked(state)113 prepareMapCopy(state)114 state.scope.immer.markChanged(state)115 state.assigned = new Map()116 return state.copy!.clear()117 }

More WIPMichel Weststratebefore adoptionee1c977

line 118why, one step back
118

TSDXMichel Weststratebefore adoption4c4fecc

lines 119–127why, one step back
119 p.forEach = function(
120 cb: (value: any, key: any, self: any) => void,121 thisArg?: any122 ) {123 const state = this[DRAFT_STATE]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

lines 129–134why, one step back
129 p.get = function(key: any): any {
130 const state = this[DRAFT_STATE]131 assertUnrevoked(state)132 const value = latest(state).get(key)133 if (state.finalized || !isDraftable(value)) {134 return value

More WIPMichel Weststratebefore adoptionee1c977

line 135why, one step back
135 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 136–137why, one step back
136 if (value !== state.base.get(key)) {
137 return value // either already drafted or reassigned

More WIPMichel Weststratebefore adoptionee1c977

line 138why, one step back
138 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 139–144why, one step back
139 // despite what it looks, this creates a draft only once, see above condition
140 const draft = state.scope.immer.createProxy(value, state)141 prepareMapCopy(state)142 state.copy!.set(key, draft)143 return draft144 }

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 195why, one step back
195

TSDXMichel Weststratebefore adoption4c4fecc

lines 196–201why, one step back
196 function prepareMapCopy(state: MapState) {
197 if (!state.copy) {198 state.assigned = new Map()199 state.copy = new Map(state.base)200 }201 }

More WIPMichel Weststratebefore adoptionee1c977

line 202why, one step back
202

TSDXMichel Weststratebefore adoption4c4fecc

lines 203–222why, one step back
203 const DraftSet = (function(_super) {
204 if (!_super) {205 /* istanbul ignore next */206 throw new Error("Set is not polyfilled")207 }208 __extends(DraftSet, _super)209 // Create class manually, cause #502210 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {211 this[DRAFT_STATE] = {212 type: ProxyType.Set,213 parent,214 scope: parent ? parent.scope : ImmerScope.current!,215 modified: false,216 finalized: false,217 copy: undefined,218 base: target,219 draft: this,220 drafts: new Map(),221 revoked: false,222 isManual: false

More WIPMichel Weststratebefore adoptionee1c977

line 223why, one step back
223 }

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 227why, one step back
227

TSDXMichel Weststratebefore adoption4c4fecc

lines 228–234why, one step back
228 Object.defineProperty(p, "size", {
229 get: function() {230 return latest(this[DRAFT_STATE]).size231 },232 enumerable: true,233 configurable: true234 })

More WIPMichel Weststratebefore adoptionee1c977

line 235why, one step back
235

TSDXMichel Weststratebefore adoption4c4fecc

lines 236–241why, one step back
236 p.has = function(value: any): boolean {
237 const state = this[DRAFT_STATE]238 assertUnrevoked(state)239 // bit of trickery here, to be able to recognize both the value, and the draft of its value240 if (!state.copy) {241 return state.base.has(value)

More WIPMichel Weststratebefore adoptionee1c977

line 242why, one step back
242 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 243–247why, one step back
243 if (state.copy.has(value)) return true
244 if (state.drafts.has(value) && state.copy.has(state.drafts.get(value)))245 return true246 return false247 }

More WIPMichel Weststratebefore adoptionee1c977

line 248why, one step back
248

TSDXMichel Weststratebefore adoption4c4fecc

lines 249–254why, one step back
249 p.add = function(value: any): any {
250 const state = this[DRAFT_STATE]251 assertUnrevoked(state)252 if (state.copy) {253 state.copy.add(value)254 } else if (!state.base.has(value)) {

More WIPMichel Weststratebefore adoptionee1c977

lines 255–256why, one step back
255 prepareSetCopy(state)
256 state.scope.immer.markChanged(state)

TSDXMichel Weststratebefore adoption4c4fecc

line 257why, one step back
257 state.copy!.add(value)

More WIPMichel Weststratebefore adoptionee1c977

line 258why, one step back
258 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 259–260why, one step back
259 return this
260 }

More WIPMichel Weststratebefore adoptionee1c977

line 261why, one step back
261

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

lines 265–266why, one step back
265 }
266

TSDXMichel Weststratebefore adoption4c4fecc

lines 267–277why, one step back
267 const state = this[DRAFT_STATE]
268 assertUnrevoked(state)269 prepareSetCopy(state)270 state.scope.immer.markChanged(state)271 return (272 state.copy!.delete(value) ||273 (state.drafts.has(value)274 ? state.copy!.delete(state.drafts.get(value))275 : /* istanbul ignore next */ false)276 )277 }

More WIPMichel Weststratebefore adoptionee1c977

line 278why, one step back
278

TSDXMichel Weststratebefore adoption4c4fecc

lines 279–285why, one step back
279 p.clear = function() {
280 const state = this[DRAFT_STATE]281 assertUnrevoked(state)282 prepareSetCopy(state)283 state.scope.immer.markChanged(state)284 return state.copy!.clear()285 }

More WIPMichel Weststratebefore adoptionee1c977

line 286why, one step back
286

TSDXMichel Weststratebefore adoption4c4fecc

lines 287–292why, one step back
287 p.values = function(): IterableIterator<any> {
288 const state = this[DRAFT_STATE]289 assertUnrevoked(state)290 prepareSetCopy(state)291 return state.copy!.values()292 }

More WIPMichel Weststratebefore adoptionee1c977

line 293why, one step back
293

TSDXMichel Weststratebefore adoption4c4fecc

lines 294–299why, one step back
294 p.entries = function entries(): IterableIterator<[any, any]> {
295 const state = this[DRAFT_STATE]296 assertUnrevoked(state)297 prepareSetCopy(state)298 return state.copy!.entries()299 }

More WIPMichel Weststratebefore adoptionee1c977

line 300why, one step back
300

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

line 304why, one step back
304

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

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

TSDXMichel Weststratebefore adoption4c4fecc

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

More WIPMichel Weststratebefore adoptionee1c977

lines 315–317why, one step back
315 }
316 }317

TSDXMichel Weststratebefore adoption4c4fecc

lines 318–323why, one step back
318 return DraftSet
319 })(Set)320321 function proxySet<T extends AnySet>(target: T, parent?: ImmerState): T {322 // @ts-ignore323 return new DraftSet(target, parent)

More WIPMichel Weststratebefore adoptionee1c977

line 324why, one step back
324 }

TSDXMichel Weststratebefore adoption4c4fecc

lines 325–343why, one step back
325
326 function prepareSetCopy(state: SetState) {327 if (!state.copy) {328 // create drafts for all entries to preserve insertion order329 state.copy = new Set()330 state.base.forEach(value => {331 if (isDraftable(value)) {332 const draft = createProxy(state.scope.immer, value, state)333 state.drafts.set(value, draft)334 state.copy!.add(draft)335 } else {336 state.copy!.add(value)337 }338 })339 }340 }341342 loadPlugin("mapset", {proxyMap, proxySet})343}