mapset.ts
This file
Path
src/plugins/mapset.ts
339 lines · 8.0 KiB · TypeScript
Last changed by
Some notesMichel Weststratebefore adoption746dc6f
History of this fileTake it
More WIPMichel Weststratebefore adoptionee1c977
lines 1–7why, one step back1// types only!2import {3 ImmerState,4 AnyMap,5 AnySet,6 MapState,7 SetState,TSDXMichel Weststratebefore adoption4c4fecc
lines 8–15why, one step back8 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 back16 loadPlugin,17 markChangedMore WIPMichel Weststratebefore adoptionee1c977
line 18why, one step back18} from "../internal"use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 19why, one step back19import invariant from "tiny-invariant"More WIPMichel Weststratebefore adoptionee1c977
line 20why, one step back20TSDXMichel Weststratebefore adoption4c4fecc
lines 21–26why, one step back21export 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 back27 function(d, b) {TSDXMichel Weststratebefore adoption4c4fecc
lines 28–40why, one step back28 d.__proto__ = b29 }) ||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 = dMore WIPMichel Weststratebefore adoptionee1c977
line 41why, one step back41 }TSDXMichel Weststratebefore adoption4c4fecc
lines 42–45why, one step back42 d.prototype =43 // @ts-ignore44 ((__.prototype = b.prototype), new __())45 }More WIPMichel Weststratebefore adoptionee1c977
line 46why, one step back46TSDXMichel Weststratebefore adoption4c4fecc
line 47why, one step back47 const DraftMap = (function(_super) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 48why, one step back48 invariant(_super, "Map is not polyfilled")TSDXMichel Weststratebefore adoption4c4fecc
lines 49–52why, one step back49 __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 back53 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 MapStateTSDXMichel Weststratebefore adoption4c4fecc
line 65why, one step back65 return thisMore WIPMichel Weststratebefore adoptionee1c977
line 66why, one step back66 }TSDXMichel Weststratebefore adoption4c4fecc
line 67why, one step back67 const p = DraftMap.prototypeMore WIPMichel Weststratebefore adoptionee1c977
line 68why, one step back68TSDXMichel Weststratebefore adoption4c4fecc
lines 69–72why, one step back69 // TODO: smaller build size if we create a util for Object.defineProperty70 Object.defineProperty(p, "size", {71 get: function() {72 return latest(this[DRAFT_STATE]).sizeSome notesMichel Weststratebefore adoption746dc6f
lines 73–75why, one step back73 }74 // enumerable: false,75 // configurable: trueTSDXMichel Weststratebefore adoption4c4fecc
line 76why, one step back76 })More WIPMichel Weststratebefore adoptionee1c977
line 77why, one step back77TSDXMichel Weststratebefore adoption4c4fecc
lines 78–80why, one step back78 p.has = function(key: any): boolean {79 return latest(this[DRAFT_STATE]).has(key)80 }More WIPMichel Weststratebefore adoptionee1c977
line 81why, one step back81TSDXMichel Weststratebefore adoption4c4fecc
line 82why, one step back82 p.set = function(key: any, value: any) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 83why, one step back83 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 84–85why, one step back84 assertUnrevoked(state)85 if (latest(state).get(key) !== value) {More WIPMichel Weststratebefore adoptionee1c977
line 86why, one step back86 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 87–90why, one step back87 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 back91 }TSDXMichel Weststratebefore adoption4c4fecc
lines 92–93why, one step back92 return this93 }More WIPMichel Weststratebefore adoptionee1c977
line 94why, one step back94TSDXMichel Weststratebefore adoption4c4fecc
lines 95–97why, one step back95 p.delete = function(key: any): boolean {96 if (!this.has(key)) {97 return falseMore WIPMichel Weststratebefore adoptionee1c977
lines 98–99why, one step back98 }99Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 100why, one step back100 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 101–102why, one step back101 assertUnrevoked(state)102 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 103–105why, one step back103 markChanged(state.scope_.immer_, state)104 state.assigned_!.set(key, false)105 state.copy_!.delete(key)TSDXMichel Weststratebefore adoption4c4fecc
lines 106–107why, one step back106 return true107 }More WIPMichel Weststratebefore adoptionee1c977
line 108why, one step back108TSDXMichel Weststratebefore adoption4c4fecc
line 109why, one step back109 p.clear = function() {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 110why, one step back110 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 111–112why, one step back111 assertUnrevoked(state)112 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 113–115why, one step back113 markChanged(state.scope_.immer_, state)114 state.assigned_ = new Map()115 return state.copy_!.clear()TSDXMichel Weststratebefore adoption4c4fecc
line 116why, one step back116 }More WIPMichel Weststratebefore adoptionee1c977
line 117why, one step back117TSDXMichel Weststratebefore adoption4c4fecc
lines 118–121why, one step back118 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 back122 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 123–126why, one step back123 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 back127TSDXMichel Weststratebefore adoption4c4fecc
line 128why, one step back128 p.get = function(key: any): any {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 129why, one step back129 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 130–131why, one step back130 assertUnrevoked(state)131 const value = latest(state).get(key)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 132why, one step back132 if (state.finalized_ || !isDraftable(value)) {TSDXMichel Weststratebefore adoption4c4fecc
line 133why, one step back133 return valueMore WIPMichel Weststratebefore adoptionee1c977
line 134why, one step back134 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 135why, one step back135 if (value !== state.base_.get(key)) {TSDXMichel Weststratebefore adoption4c4fecc
line 136why, one step back136 return value // either already drafted or reassignedMore WIPMichel Weststratebefore adoptionee1c977
line 137why, one step back137 }TSDXMichel Weststratebefore adoption4c4fecc
line 138why, one step back138 // despite what it looks, this creates a draft only once, see above conditionMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 139why, one step back139 const draft = createProxy(state.scope_.immer_, value, state)TSDXMichel Weststratebefore adoption4c4fecc
line 140why, one step back140 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 141why, one step back141 state.copy_!.set(key, draft)TSDXMichel Weststratebefore adoption4c4fecc
lines 142–143why, one step back142 return draft143 }More WIPMichel Weststratebefore adoptionee1c977
line 144why, one step back144TSDXMichel Weststratebefore adoption4c4fecc
lines 145–147why, one step back145 p.keys = function(): IterableIterator<any> {146 return latest(this[DRAFT_STATE]).keys()147 }More WIPMichel Weststratebefore adoptionee1c977
line 148why, one step back148TSDXMichel Weststratebefore adoption4c4fecc
lines 149–164why, one step back149 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 back165TSDXMichel Weststratebefore adoption4c4fecc
lines 166–180why, one step back166 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 anyMore WIPMichel Weststratebefore adoptionee1c977
lines 181–182why, one step back181 }182TSDXMichel Weststratebefore adoption4c4fecc
lines 183–184why, one step back183 p[iteratorSymbol] = function() {184 return this.entries()More WIPMichel Weststratebefore adoptionee1c977
lines 185–186why, one step back185 }186TSDXMichel Weststratebefore adoption4c4fecc
lines 187–188why, one step back187 return DraftMap188 })(Map)More WIPMichel Weststratebefore adoptionee1c977
line 189why, one step back189TSDXMichel Weststratebefore adoption4c4fecc
lines 190–193why, one step back190 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 back194TSDXMichel Weststratebefore adoption4c4fecc
line 195why, one step back195 function prepareMapCopy(state: MapState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 196–198why, one step back196 if (!state.copy_) {197 state.assigned_ = new Map()198 state.copy_ = new Map(state.base_)TSDXMichel Weststratebefore adoption4c4fecc
lines 199–200why, one step back199 }200 }More WIPMichel Weststratebefore adoptionee1c977
line 201why, one step back201TSDXMichel Weststratebefore adoption4c4fecc
line 202why, one step back202 const DraftSet = (function(_super) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
lines 203–204why, one step back203 invariant(_super, "Set is not polyfilled")204TSDXMichel Weststratebefore adoption4c4fecc
lines 205–208why, one step back205 __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 back209 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 SetStateTSDXMichel Weststratebefore adoption4c4fecc
lines 221–223why, one step back221 return this222 }223 const p = DraftSet.prototypeMore WIPMichel Weststratebefore adoptionee1c977
line 224why, one step back224TSDXMichel Weststratebefore adoption4c4fecc
lines 225–227why, one step back225 Object.defineProperty(p, "size", {226 get: function() {227 return latest(this[DRAFT_STATE]).sizeSome notesMichel Weststratebefore adoption746dc6f
lines 228–229why, one step back228 }229 // enumerable: true,TSDXMichel Weststratebefore adoption4c4fecc
line 230why, one step back230 })More WIPMichel Weststratebefore adoptionee1c977
line 231why, one step back231TSDXMichel Weststratebefore adoption4c4fecc
line 232why, one step back232 p.has = function(value: any): boolean {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 233why, one step back233 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 234–235why, one step back234 assertUnrevoked(state)235 // bit of trickery here, to be able to recognize both the value, and the draft of its valueMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 236–237why, one step back236 if (!state.copy_) {237 return state.base_.has(value)More WIPMichel Weststratebefore adoptionee1c977
line 238why, one step back238 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 239–240why, one step back239 if (state.copy_.has(value)) return true240 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))TSDXMichel Weststratebefore adoption4c4fecc
lines 241–243why, one step back241 return true242 return false243 }More WIPMichel Weststratebefore adoptionee1c977
line 244why, one step back244TSDXMichel Weststratebefore adoption4c4fecc
line 245why, one step back245 p.add = function(value: any): any {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 246why, one step back246 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
line 247why, one step back247 assertUnrevoked(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 248–250why, one step back248 if (state.copy_) {249 state.copy_.add(value)250 } else if (!state.base_.has(value)) {More WIPMichel Weststratebefore adoptionee1c977
line 251why, one step back251 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 252–253why, one step back252 markChanged(state.scope_.immer_, state)253 state.copy_!.add(value)More WIPMichel Weststratebefore adoptionee1c977
line 254why, one step back254 }TSDXMichel Weststratebefore adoption4c4fecc
lines 255–256why, one step back255 return this256 }More WIPMichel Weststratebefore adoptionee1c977
line 257why, one step back257TSDXMichel Weststratebefore adoption4c4fecc
lines 258–260why, one step back258 p.delete = function(value: any): any {259 if (!this.has(value)) {260 return falseMore WIPMichel Weststratebefore adoptionee1c977
lines 261–262why, one step back261 }262Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 263why, one step back263 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 264–265why, one step back264 assertUnrevoked(state)265 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 266why, one step back266 markChanged(state.scope_.immer_, state)TSDXMichel Weststratebefore adoption4c4fecc
line 267why, one step back267 return (Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 268–270why, one step back268 state.copy_!.delete(value) ||269 (state.drafts_.has(value)270 ? state.copy_!.delete(state.drafts_.get(value))TSDXMichel Weststratebefore adoption4c4fecc
lines 271–273why, one step back271 : /* istanbul ignore next */ false)272 )273 }More WIPMichel Weststratebefore adoptionee1c977
line 274why, one step back274TSDXMichel Weststratebefore adoption4c4fecc
line 275why, one step back275 p.clear = function() {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 276why, one step back276 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 277–278why, one step back277 assertUnrevoked(state)278 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 279–280why, one step back279 markChanged(state.scope_.immer_, state)280 return state.copy_!.clear()TSDXMichel Weststratebefore adoption4c4fecc
line 281why, one step back281 }More WIPMichel Weststratebefore adoptionee1c977
line 282why, one step back282TSDXMichel Weststratebefore adoption4c4fecc
line 283why, one step back283 p.values = function(): IterableIterator<any> {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 284why, one step back284 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 285–286why, one step back285 assertUnrevoked(state)286 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 287why, one step back287 return state.copy_!.values()TSDXMichel Weststratebefore adoption4c4fecc
line 288why, one step back288 }More WIPMichel Weststratebefore adoptionee1c977
line 289why, one step back289TSDXMichel Weststratebefore adoption4c4fecc
line 290why, one step back290 p.entries = function entries(): IterableIterator<[any, any]> {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 291why, one step back291 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 292–293why, one step back292 assertUnrevoked(state)293 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 294why, one step back294 return state.copy_!.entries()TSDXMichel Weststratebefore adoption4c4fecc
line 295why, one step back295 }More WIPMichel Weststratebefore adoptionee1c977
line 296why, one step back296TSDXMichel Weststratebefore adoption4c4fecc
lines 297–299why, one step back297 p.keys = function(): IterableIterator<any> {298 return this.values()299 }More WIPMichel Weststratebefore adoptionee1c977
line 300why, one step back300TSDXMichel Weststratebefore adoption4c4fecc
lines 301–302why, one step back301 p[iteratorSymbol] = function() {302 return this.values()More WIPMichel Weststratebefore adoptionee1c977
lines 303–304why, one step back303 }304TSDXMichel Weststratebefore adoption4c4fecc
lines 305–310why, one step back305 p.forEach = function forEach(cb: any, thisArg?: any) {306 const iterator = this.values()307 let result = iterator.next()308 while (!result.done) {309 cb.call(thisArg, result.value, result.value, this)310 result = iterator.next()More WIPMichel Weststratebefore adoptionee1c977
lines 311–313why, one step back311 }312 }313TSDXMichel Weststratebefore adoption4c4fecc
lines 314–319why, one step back314 return DraftSet315 })(Set)316317 function proxySet<T extends AnySet>(target: T, parent?: ImmerState): T {318 // @ts-ignore319 return new DraftSet(target, parent)More WIPMichel Weststratebefore adoptionee1c977
line 320why, one step back320 }TSDXMichel Weststratebefore adoption4c4fecc
lines 321–322why, one step back321322 function prepareSetCopy(state: SetState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 323why, one step back323 if (!state.copy_) {TSDXMichel Weststratebefore adoption4c4fecc
line 324why, one step back324 // create drafts for all entries to preserve insertion orderMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 325–326why, one step back325 state.copy_ = new Set()326 state.base_.forEach(value => {TSDXMichel Weststratebefore adoption4c4fecc
line 327why, one step back327 if (isDraftable(value)) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 328–330why, one step back328 const draft = createProxy(state.scope_.immer_, value, state)329 state.drafts_.set(value, draft)330 state.copy_!.add(draft)TSDXMichel Weststratebefore adoption4c4fecc
line 331why, one step back331 } else {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 332why, one step back332 state.copy_!.add(value)TSDXMichel Weststratebefore adoption4c4fecc
lines 333–339why, one step back333 }334 })335 }336 }337338 loadPlugin("mapset", {proxyMap, proxySet})339}