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 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–14why, one step back8 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 back15 loadPlugin,Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 16–18why, one step back16 markChanged,17 ProxyTypeMap,18 ProxyTypeSetMore WIPMichel Weststratebefore adoptionee1c977
line 19why, one step back19} from "../internal"use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 20why, one step back20import invariant from "tiny-invariant"More WIPMichel Weststratebefore adoptionee1c977
line 21why, one step back21TSDXMichel Weststratebefore adoption4c4fecc
lines 22–27why, one step back22export 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 back28 function(d, b) {TSDXMichel Weststratebefore adoption4c4fecc
lines 29–41why, one step back29 d.__proto__ = b30 }) ||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 = dMore WIPMichel Weststratebefore adoptionee1c977
line 42why, one step back42 }TSDXMichel Weststratebefore adoption4c4fecc
lines 43–46why, one step back43 d.prototype =44 // @ts-ignore45 ((__.prototype = b.prototype), new __())46 }More WIPMichel Weststratebefore adoptionee1c977
line 47why, one step back47TSDXMichel Weststratebefore adoption4c4fecc
line 48why, one step back48 const DraftMap = (function(_super) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
line 49why, one step back49 invariant(_super, "Map is not polyfilled")TSDXMichel Weststratebefore adoption4c4fecc
lines 50–53why, one step back50 __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 back54 type_: ProxyTypeMap,Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 55–65why, one step back55 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 MapStateTSDXMichel Weststratebefore adoption4c4fecc
line 66why, one step back66 return thisMore WIPMichel Weststratebefore adoptionee1c977
line 67why, one step back67 }TSDXMichel Weststratebefore adoption4c4fecc
line 68why, one step back68 const p = DraftMap.prototypeMore WIPMichel Weststratebefore adoptionee1c977
line 69why, one step back69TSDXMichel Weststratebefore adoption4c4fecc
lines 70–73why, one step back70 // TODO: smaller build size if we create a util for Object.defineProperty71 Object.defineProperty(p, "size", {72 get: function() {73 return latest(this[DRAFT_STATE]).sizeSome notesMichel Weststratebefore adoption746dc6f
lines 74–76why, one step back74 }75 // enumerable: false,76 // configurable: trueTSDXMichel Weststratebefore adoption4c4fecc
line 77why, one step back77 })More WIPMichel Weststratebefore adoptionee1c977
line 78why, one step back78TSDXMichel Weststratebefore adoption4c4fecc
lines 79–81why, one step back79 p.has = function(key: any): boolean {80 return latest(this[DRAFT_STATE]).has(key)81 }More WIPMichel Weststratebefore adoptionee1c977
line 82why, one step back82TSDXMichel Weststratebefore adoption4c4fecc
line 83why, one step back83 p.set = function(key: any, value: any) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 84why, one step back84 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 85–86why, one step back85 assertUnrevoked(state)86 if (latest(state).get(key) !== value) {More WIPMichel Weststratebefore adoptionee1c977
line 87why, one step back87 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 88–91why, one step back88 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 back92 }TSDXMichel Weststratebefore adoption4c4fecc
lines 93–94why, one step back93 return this94 }More WIPMichel Weststratebefore adoptionee1c977
line 95why, one step back95TSDXMichel Weststratebefore adoption4c4fecc
lines 96–98why, one step back96 p.delete = function(key: any): boolean {97 if (!this.has(key)) {98 return falseMore WIPMichel Weststratebefore adoptionee1c977
lines 99–100why, one step back99 }100Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 101why, one step back101 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 102–103why, one step back102 assertUnrevoked(state)103 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 104–106why, one step back104 markChanged(state.scope_.immer_, state)105 state.assigned_!.set(key, false)106 state.copy_!.delete(key)TSDXMichel Weststratebefore adoption4c4fecc
lines 107–108why, one step back107 return true108 }More WIPMichel Weststratebefore adoptionee1c977
line 109why, one step back109TSDXMichel Weststratebefore adoption4c4fecc
line 110why, one step back110 p.clear = function() {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 111why, one step back111 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 112–113why, one step back112 assertUnrevoked(state)113 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 114–116why, one step back114 markChanged(state.scope_.immer_, state)115 state.assigned_ = new Map()116 return state.copy_!.clear()TSDXMichel Weststratebefore adoption4c4fecc
line 117why, one step back117 }More WIPMichel Weststratebefore adoptionee1c977
line 118why, one step back118TSDXMichel Weststratebefore adoption4c4fecc
lines 119–122why, one step back119 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 back123 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 124–127why, one step back124 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 back128TSDXMichel Weststratebefore adoption4c4fecc
line 129why, one step back129 p.get = function(key: any): any {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 130why, one step back130 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 131–132why, one step back131 assertUnrevoked(state)132 const value = latest(state).get(key)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 133why, one step back133 if (state.finalized_ || !isDraftable(value)) {TSDXMichel Weststratebefore adoption4c4fecc
line 134why, one step back134 return valueMore WIPMichel Weststratebefore adoptionee1c977
line 135why, one step back135 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 136why, one step back136 if (value !== state.base_.get(key)) {TSDXMichel Weststratebefore adoption4c4fecc
line 137why, one step back137 return value // either already drafted or reassignedMore WIPMichel Weststratebefore adoptionee1c977
line 138why, one step back138 }TSDXMichel Weststratebefore adoption4c4fecc
line 139why, one step back139 // despite what it looks, this creates a draft only once, see above conditionMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 140why, one step back140 const draft = createProxy(state.scope_.immer_, value, state)TSDXMichel Weststratebefore adoption4c4fecc
line 141why, one step back141 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 142why, one step back142 state.copy_!.set(key, draft)TSDXMichel Weststratebefore adoption4c4fecc
lines 143–144why, one step back143 return draft144 }More WIPMichel Weststratebefore adoptionee1c977
line 145why, one step back145TSDXMichel Weststratebefore adoption4c4fecc
lines 146–148why, one step back146 p.keys = function(): IterableIterator<any> {147 return latest(this[DRAFT_STATE]).keys()148 }More WIPMichel Weststratebefore adoptionee1c977
line 149why, one step back149TSDXMichel Weststratebefore adoption4c4fecc
lines 150–165why, one step back150 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 back166TSDXMichel Weststratebefore adoption4c4fecc
lines 167–181why, one step back167 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 anyMore WIPMichel Weststratebefore adoptionee1c977
lines 182–183why, one step back182 }183TSDXMichel Weststratebefore adoption4c4fecc
lines 184–185why, one step back184 p[iteratorSymbol] = function() {185 return this.entries()More WIPMichel Weststratebefore adoptionee1c977
lines 186–187why, one step back186 }187TSDXMichel Weststratebefore adoption4c4fecc
lines 188–189why, one step back188 return DraftMap189 })(Map)More WIPMichel Weststratebefore adoptionee1c977
line 190why, one step back190scramble plugin names, -56Michel Weststratebefore adoption037b474
line 191why, one step back191 function proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T {TSDXMichel Weststratebefore adoption4c4fecc
lines 192–194why, one step back192 // @ts-ignore193 return new DraftMap(target, parent)194 }More WIPMichel Weststratebefore adoptionee1c977
line 195why, one step back195TSDXMichel Weststratebefore adoption4c4fecc
line 196why, one step back196 function prepareMapCopy(state: MapState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 197–199why, one step back197 if (!state.copy_) {198 state.assigned_ = new Map()199 state.copy_ = new Map(state.base_)TSDXMichel Weststratebefore adoption4c4fecc
lines 200–201why, one step back200 }201 }More WIPMichel Weststratebefore adoptionee1c977
line 202why, one step back202TSDXMichel Weststratebefore adoption4c4fecc
line 203why, one step back203 const DraftSet = (function(_super) {use invariant -321 bytesMichel Weststratebefore adoptionac57da5
lines 204–205why, one step back204 invariant(_super, "Set is not polyfilled")205TSDXMichel Weststratebefore adoption4c4fecc
lines 206–209why, one step back206 __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 back210 type_: ProxyTypeSet,Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 211–221why, one step back211 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 SetStateTSDXMichel Weststratebefore adoption4c4fecc
lines 222–224why, one step back222 return this223 }224 const p = DraftSet.prototypeMore WIPMichel Weststratebefore adoptionee1c977
line 225why, one step back225TSDXMichel Weststratebefore adoption4c4fecc
lines 226–228why, one step back226 Object.defineProperty(p, "size", {227 get: function() {228 return latest(this[DRAFT_STATE]).sizeSome notesMichel Weststratebefore adoption746dc6f
lines 229–230why, one step back229 }230 // enumerable: true,TSDXMichel Weststratebefore adoption4c4fecc
line 231why, one step back231 })More WIPMichel Weststratebefore adoptionee1c977
line 232why, one step back232TSDXMichel Weststratebefore adoption4c4fecc
line 233why, one step back233 p.has = function(value: any): boolean {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 234why, one step back234 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 235–236why, one step back235 assertUnrevoked(state)236 // bit of trickery here, to be able to recognize both the value, and the draft of its valueMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 237–238why, one step back237 if (!state.copy_) {238 return state.base_.has(value)More WIPMichel Weststratebefore adoptionee1c977
line 239why, one step back239 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 240–241why, one step back240 if (state.copy_.has(value)) return true241 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))TSDXMichel Weststratebefore adoption4c4fecc
lines 242–244why, one step back242 return true243 return false244 }More WIPMichel Weststratebefore adoptionee1c977
line 245why, one step back245TSDXMichel Weststratebefore adoption4c4fecc
line 246why, one step back246 p.add = function(value: any): any {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 247why, one step back247 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
line 248why, one step back248 assertUnrevoked(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 249–251why, one step back249 if (state.copy_) {250 state.copy_.add(value)251 } else if (!state.base_.has(value)) {More WIPMichel Weststratebefore adoptionee1c977
line 252why, one step back252 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 253–254why, one step back253 markChanged(state.scope_.immer_, state)254 state.copy_!.add(value)More WIPMichel Weststratebefore adoptionee1c977
line 255why, one step back255 }TSDXMichel Weststratebefore adoption4c4fecc
lines 256–257why, one step back256 return this257 }More WIPMichel Weststratebefore adoptionee1c977
line 258why, one step back258TSDXMichel Weststratebefore adoption4c4fecc
lines 259–261why, one step back259 p.delete = function(value: any): any {260 if (!this.has(value)) {261 return falseMore WIPMichel Weststratebefore adoptionee1c977
lines 262–263why, one step back262 }263Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 264why, one step back264 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 265–266why, one step back265 assertUnrevoked(state)266 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 267why, one step back267 markChanged(state.scope_.immer_, state)TSDXMichel Weststratebefore adoption4c4fecc
line 268why, one step back268 return (Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 269–271why, one step back269 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 back272 : /* istanbul ignore next */ false)273 )274 }More WIPMichel Weststratebefore adoptionee1c977
line 275why, one step back275TSDXMichel Weststratebefore adoption4c4fecc
line 276why, one step back276 p.clear = function() {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 277why, one step back277 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 278–279why, one step back278 assertUnrevoked(state)279 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 280–281why, one step back280 markChanged(state.scope_.immer_, state)281 return state.copy_!.clear()TSDXMichel Weststratebefore adoption4c4fecc
line 282why, one step back282 }More WIPMichel Weststratebefore adoptionee1c977
line 283why, one step back283TSDXMichel Weststratebefore adoption4c4fecc
line 284why, one step back284 p.values = function(): IterableIterator<any> {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 285why, one step back285 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 286–287why, one step back286 assertUnrevoked(state)287 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 288why, one step back288 return state.copy_!.values()TSDXMichel Weststratebefore adoption4c4fecc
line 289why, one step back289 }More WIPMichel Weststratebefore adoptionee1c977
line 290why, one step back290TSDXMichel Weststratebefore adoption4c4fecc
line 291why, one step back291 p.entries = function entries(): IterableIterator<[any, any]> {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 292why, one step back292 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 293–294why, one step back293 assertUnrevoked(state)294 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 295why, one step back295 return state.copy_!.entries()TSDXMichel Weststratebefore adoption4c4fecc
line 296why, one step back296 }More WIPMichel Weststratebefore adoptionee1c977
line 297why, one step back297TSDXMichel Weststratebefore adoption4c4fecc
lines 298–300why, one step back298 p.keys = function(): IterableIterator<any> {299 return this.values()300 }More WIPMichel Weststratebefore adoptionee1c977
line 301why, one step back301TSDXMichel Weststratebefore adoption4c4fecc
lines 302–303why, one step back302 p[iteratorSymbol] = function() {303 return this.values()More WIPMichel Weststratebefore adoptionee1c977
lines 304–305why, one step back304 }305TSDXMichel Weststratebefore adoption4c4fecc
lines 306–311why, one step back306 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 back312 }313 }314TSDXMichel Weststratebefore adoption4c4fecc
lines 315–317why, one step back315 return DraftSet316 })(Set)317scramble plugin names, -56Michel Weststratebefore adoption037b474
line 318why, one step back318 function proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T {TSDXMichel Weststratebefore adoption4c4fecc
lines 319–320why, one step back319 // @ts-ignore320 return new DraftSet(target, parent)More WIPMichel Weststratebefore adoptionee1c977
line 321why, one step back321 }TSDXMichel Weststratebefore adoption4c4fecc
lines 322–323why, one step back322323 function prepareSetCopy(state: SetState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 324why, one step back324 if (!state.copy_) {TSDXMichel Weststratebefore adoption4c4fecc
line 325why, one step back325 // create drafts for all entries to preserve insertion orderMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 326–327why, one step back326 state.copy_ = new Set()327 state.base_.forEach(value => {TSDXMichel Weststratebefore adoption4c4fecc
line 328why, one step back328 if (isDraftable(value)) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 329–331why, one step back329 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 back332 } else {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 333why, one step back333 state.copy_!.add(value)TSDXMichel Weststratebefore adoption4c4fecc
lines 334–338why, one step back334 }335 })336 }337 }338scramble plugin names, -56Michel Weststratebefore adoption037b474
line 339why, one step back339 loadPlugin("mapset", {proxyMap_, proxySet_})TSDXMichel Weststratebefore adoption4c4fecc
line 340why, one step back340}