mapset.ts
This file
Path
src/plugins/mapset.ts
340 lines · 8.0 KiB · TypeScript
Last changed by
Preserve error codes in productionMichel Weststratebefore adoptiond005fab
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–13why, one step back8 DRAFT_STATE,9 ImmerScope,10 latest,11 iteratorSymbol,12 isDraftable,13 createProxy,New build set. Current statsMichel Weststratebefore adoption9aec0fe
line 14why, one step back14 loadPlugin,Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
lines 15–16why, one step back15 markChanged,16 ProxyTypeMap,Preserve error codes in productionMichel Weststratebefore adoptiond005fab
lines 17–18why, one step back17 ProxyTypeSet,18 dieMore WIPMichel Weststratebefore adoptionee1c977
lines 19–20why, one step back19} from "../internal"20TSDXMichel 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
lines 47–51why, one step back47 const DraftMap = (function(_super) {48 __extends(DraftMap, _super)49 // Create class manually, cause #50250 function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {51 this[DRAFT_STATE] = {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 52why, one step back52 type_: ProxyTypeMap,Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 53–63why, one step back53 parent_: parent,54 scope_: parent ? parent.scope_ : ImmerScope.current_!,55 modified_: false,56 finalized_: false,57 copy_: undefined,58 assigned_: undefined,59 base_: target,60 draft_: this as any,61 isManual_: false,62 revoked_: false63 } as MapStateTSDXMichel Weststratebefore adoption4c4fecc
line 64why, one step back64 return thisMore WIPMichel Weststratebefore adoptionee1c977
line 65why, one step back65 }TSDXMichel Weststratebefore adoption4c4fecc
line 66why, one step back66 const p = DraftMap.prototypeMore WIPMichel Weststratebefore adoptionee1c977
line 67why, one step back67TSDXMichel Weststratebefore adoption4c4fecc
lines 68–71why, one step back68 // TODO: smaller build size if we create a util for Object.defineProperty69 Object.defineProperty(p, "size", {70 get: function() {71 return latest(this[DRAFT_STATE]).sizeSome notesMichel Weststratebefore adoption746dc6f
lines 72–74why, one step back72 }73 // enumerable: false,74 // configurable: trueTSDXMichel Weststratebefore adoption4c4fecc
line 75why, one step back75 })More WIPMichel Weststratebefore adoptionee1c977
line 76why, one step back76TSDXMichel Weststratebefore adoption4c4fecc
lines 77–79why, one step back77 p.has = function(key: any): boolean {78 return latest(this[DRAFT_STATE]).has(key)79 }More WIPMichel Weststratebefore adoptionee1c977
line 80why, one step back80TSDXMichel Weststratebefore adoption4c4fecc
line 81why, one step back81 p.set = function(key: any, value: any) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 82why, one step back82 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 83–84why, one step back83 assertUnrevoked(state)84 if (latest(state).get(key) !== value) {More WIPMichel Weststratebefore adoptionee1c977
line 85why, one step back85 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 86–89why, one step back86 markChanged(state.scope_.immer_, state)87 state.assigned_!.set(key, true)88 state.copy_!.set(key, value)89 state.assigned_!.set(key, true)More WIPMichel Weststratebefore adoptionee1c977
line 90why, one step back90 }TSDXMichel Weststratebefore adoption4c4fecc
lines 91–92why, one step back91 return this92 }More WIPMichel Weststratebefore adoptionee1c977
line 93why, one step back93TSDXMichel Weststratebefore adoption4c4fecc
lines 94–96why, one step back94 p.delete = function(key: any): boolean {95 if (!this.has(key)) {96 return falseMore WIPMichel Weststratebefore adoptionee1c977
lines 97–98why, one step back97 }98Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 99why, one step back99 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 100–101why, one step back100 assertUnrevoked(state)101 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 102–104why, one step back102 markChanged(state.scope_.immer_, state)103 state.assigned_!.set(key, false)104 state.copy_!.delete(key)TSDXMichel Weststratebefore adoption4c4fecc
lines 105–106why, one step back105 return true106 }More WIPMichel Weststratebefore adoptionee1c977
line 107why, one step back107TSDXMichel Weststratebefore adoption4c4fecc
line 108why, one step back108 p.clear = function() {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 109why, one step back109 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 110–111why, one step back110 assertUnrevoked(state)111 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 112–114why, one step back112 markChanged(state.scope_.immer_, state)113 state.assigned_ = new Map()114 return state.copy_!.clear()TSDXMichel Weststratebefore adoption4c4fecc
line 115why, one step back115 }More WIPMichel Weststratebefore adoptionee1c977
line 116why, one step back116TSDXMichel Weststratebefore adoption4c4fecc
lines 117–120why, one step back117 p.forEach = function(118 cb: (value: any, key: any, self: any) => void,119 thisArg?: any120 ) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 121why, one step back121 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 122–125why, one step back122 latest(state).forEach((_value: any, key: any, _map: any) => {123 cb.call(thisArg, this.get(key), key, this)124 })125 }More WIPMichel Weststratebefore adoptionee1c977
line 126why, one step back126TSDXMichel Weststratebefore adoption4c4fecc
line 127why, one step back127 p.get = function(key: any): any {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 128why, one step back128 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 129–130why, one step back129 assertUnrevoked(state)130 const value = latest(state).get(key)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 131why, one step back131 if (state.finalized_ || !isDraftable(value)) {TSDXMichel Weststratebefore adoption4c4fecc
line 132why, one step back132 return valueMore WIPMichel Weststratebefore adoptionee1c977
line 133why, one step back133 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 134why, one step back134 if (value !== state.base_.get(key)) {TSDXMichel Weststratebefore adoption4c4fecc
line 135why, one step back135 return value // either already drafted or reassignedMore WIPMichel Weststratebefore adoptionee1c977
line 136why, one step back136 }TSDXMichel Weststratebefore adoption4c4fecc
line 137why, one step back137 // despite what it looks, this creates a draft only once, see above conditionMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 138why, one step back138 const draft = createProxy(state.scope_.immer_, value, state)TSDXMichel Weststratebefore adoption4c4fecc
line 139why, one step back139 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 140why, one step back140 state.copy_!.set(key, draft)TSDXMichel Weststratebefore adoption4c4fecc
lines 141–142why, one step back141 return draft142 }More WIPMichel Weststratebefore adoptionee1c977
line 143why, one step back143TSDXMichel Weststratebefore adoption4c4fecc
lines 144–146why, one step back144 p.keys = function(): IterableIterator<any> {145 return latest(this[DRAFT_STATE]).keys()146 }More WIPMichel Weststratebefore adoptionee1c977
line 147why, one step back147TSDXMichel Weststratebefore adoption4c4fecc
lines 148–163why, one step back148 p.values = function(): IterableIterator<any> {149 const iterator = this.keys()150 return {151 [iteratorSymbol]: () => this.values(),152 next: () => {153 const r = iterator.next()154 /* istanbul ignore next */155 if (r.done) return r156 const value = this.get(r.value)157 return {158 done: false,159 value160 }161 }162 } as any163 }More WIPMichel Weststratebefore adoptionee1c977
line 164why, one step back164TSDXMichel Weststratebefore adoption4c4fecc
lines 165–179why, one step back165 p.entries = function(): IterableIterator<[any, any]> {166 const iterator = this.keys()167 return {168 [iteratorSymbol]: () => this.entries(),169 next: () => {170 const r = iterator.next()171 /* istanbul ignore next */172 if (r.done) return r173 const value = this.get(r.value)174 return {175 done: false,176 value: [r.value, value]177 }178 }179 } as anyMore WIPMichel Weststratebefore adoptionee1c977
lines 180–181why, one step back180 }181TSDXMichel Weststratebefore adoption4c4fecc
lines 182–183why, one step back182 p[iteratorSymbol] = function() {183 return this.entries()More WIPMichel Weststratebefore adoptionee1c977
lines 184–185why, one step back184 }185TSDXMichel Weststratebefore adoption4c4fecc
lines 186–187why, one step back186 return DraftMap187 })(Map)More WIPMichel Weststratebefore adoptionee1c977
line 188why, one step back188scramble plugin names, -56Michel Weststratebefore adoption037b474
line 189why, one step back189 function proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T {TSDXMichel Weststratebefore adoption4c4fecc
lines 190–192why, one step back190 // @ts-ignore191 return new DraftMap(target, parent)192 }More WIPMichel Weststratebefore adoptionee1c977
line 193why, one step back193TSDXMichel Weststratebefore adoption4c4fecc
line 194why, one step back194 function prepareMapCopy(state: MapState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 195–197why, one step back195 if (!state.copy_) {196 state.assigned_ = new Map()197 state.copy_ = new Map(state.base_)TSDXMichel Weststratebefore adoption4c4fecc
lines 198–199why, one step back198 }199 }More WIPMichel Weststratebefore adoptionee1c977
line 200why, one step back200TSDXMichel Weststratebefore adoption4c4fecc
lines 201–205why, one step back201 const DraftSet = (function(_super) {202 __extends(DraftSet, _super)203 // Create class manually, cause #502204 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {205 this[DRAFT_STATE] = {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 206why, one step back206 type_: ProxyTypeSet,Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 207–217why, one step back207 parent_: parent,208 scope_: parent ? parent.scope_ : ImmerScope.current_!,209 modified_: false,210 finalized_: false,211 copy_: undefined,212 base_: target,213 draft_: this,214 drafts_: new Map(),215 revoked_: false,216 isManual_: false217 } as SetStateTSDXMichel Weststratebefore adoption4c4fecc
lines 218–220why, one step back218 return this219 }220 const p = DraftSet.prototypeMore WIPMichel Weststratebefore adoptionee1c977
line 221why, one step back221TSDXMichel Weststratebefore adoption4c4fecc
lines 222–224why, one step back222 Object.defineProperty(p, "size", {223 get: function() {224 return latest(this[DRAFT_STATE]).sizeSome notesMichel Weststratebefore adoption746dc6f
lines 225–226why, one step back225 }226 // enumerable: true,TSDXMichel Weststratebefore adoption4c4fecc
line 227why, one step back227 })More WIPMichel Weststratebefore adoptionee1c977
line 228why, one step back228TSDXMichel Weststratebefore adoption4c4fecc
line 229why, one step back229 p.has = function(value: any): boolean {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 230why, one step back230 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 231–232why, one step back231 assertUnrevoked(state)232 // bit of trickery here, to be able to recognize both the value, and the draft of its valueMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 233–234why, one step back233 if (!state.copy_) {234 return state.base_.has(value)More WIPMichel Weststratebefore adoptionee1c977
line 235why, one step back235 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 236–237why, one step back236 if (state.copy_.has(value)) return true237 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))TSDXMichel Weststratebefore adoption4c4fecc
lines 238–240why, one step back238 return true239 return false240 }More WIPMichel Weststratebefore adoptionee1c977
line 241why, one step back241TSDXMichel Weststratebefore adoption4c4fecc
line 242why, one step back242 p.add = function(value: any): any {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 243why, one step back243 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
line 244why, one step back244 assertUnrevoked(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 245–247why, one step back245 if (state.copy_) {246 state.copy_.add(value)247 } else if (!state.base_.has(value)) {More WIPMichel Weststratebefore adoptionee1c977
line 248why, one step back248 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 249–250why, one step back249 markChanged(state.scope_.immer_, state)250 state.copy_!.add(value)More WIPMichel Weststratebefore adoptionee1c977
line 251why, one step back251 }TSDXMichel Weststratebefore adoption4c4fecc
lines 252–253why, one step back252 return this253 }More WIPMichel Weststratebefore adoptionee1c977
line 254why, one step back254TSDXMichel Weststratebefore adoption4c4fecc
lines 255–257why, one step back255 p.delete = function(value: any): any {256 if (!this.has(value)) {257 return falseMore WIPMichel Weststratebefore adoptionee1c977
lines 258–259why, one step back258 }259Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 260why, one step back260 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 261–262why, one step back261 assertUnrevoked(state)262 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 263why, one step back263 markChanged(state.scope_.immer_, state)TSDXMichel Weststratebefore adoption4c4fecc
line 264why, one step back264 return (Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 265–267why, one step back265 state.copy_!.delete(value) ||266 (state.drafts_.has(value)267 ? state.copy_!.delete(state.drafts_.get(value))TSDXMichel Weststratebefore adoption4c4fecc
lines 268–270why, one step back268 : /* istanbul ignore next */ false)269 )270 }More WIPMichel Weststratebefore adoptionee1c977
line 271why, one step back271TSDXMichel Weststratebefore adoption4c4fecc
line 272why, one step back272 p.clear = function() {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 273why, one step back273 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 274–275why, one step back274 assertUnrevoked(state)275 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 276–277why, one step back276 markChanged(state.scope_.immer_, state)277 return state.copy_!.clear()TSDXMichel Weststratebefore adoption4c4fecc
line 278why, one step back278 }More WIPMichel Weststratebefore adoptionee1c977
line 279why, one step back279TSDXMichel Weststratebefore adoption4c4fecc
line 280why, one step back280 p.values = function(): IterableIterator<any> {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 281why, one step back281 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 282–283why, one step back282 assertUnrevoked(state)283 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 284why, one step back284 return state.copy_!.values()TSDXMichel Weststratebefore adoption4c4fecc
line 285why, one step back285 }More WIPMichel Weststratebefore adoptionee1c977
line 286why, one step back286TSDXMichel Weststratebefore adoption4c4fecc
line 287why, one step back287 p.entries = function entries(): IterableIterator<[any, any]> {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 288why, one step back288 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 289–290why, one step back289 assertUnrevoked(state)290 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 291why, one step back291 return state.copy_!.entries()TSDXMichel Weststratebefore adoption4c4fecc
line 292why, one step back292 }More WIPMichel Weststratebefore adoptionee1c977
line 293why, one step back293TSDXMichel Weststratebefore adoption4c4fecc
lines 294–296why, one step back294 p.keys = function(): IterableIterator<any> {295 return this.values()296 }More WIPMichel Weststratebefore adoptionee1c977
line 297why, one step back297TSDXMichel Weststratebefore adoption4c4fecc
lines 298–299why, one step back298 p[iteratorSymbol] = function() {299 return this.values()More WIPMichel Weststratebefore adoptionee1c977
lines 300–301why, one step back300 }301TSDXMichel Weststratebefore adoption4c4fecc
lines 302–307why, one step back302 p.forEach = function forEach(cb: any, thisArg?: any) {303 const iterator = this.values()304 let result = iterator.next()305 while (!result.done) {306 cb.call(thisArg, result.value, result.value, this)307 result = iterator.next()More WIPMichel Weststratebefore adoptionee1c977
lines 308–310why, one step back308 }309 }310TSDXMichel Weststratebefore adoption4c4fecc
lines 311–313why, one step back311 return DraftSet312 })(Set)313scramble plugin names, -56Michel Weststratebefore adoption037b474
line 314why, one step back314 function proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T {TSDXMichel Weststratebefore adoption4c4fecc
lines 315–316why, one step back315 // @ts-ignore316 return new DraftSet(target, parent)More WIPMichel Weststratebefore adoptionee1c977
line 317why, one step back317 }TSDXMichel Weststratebefore adoption4c4fecc
lines 318–319why, one step back318319 function prepareSetCopy(state: SetState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 320why, one step back320 if (!state.copy_) {TSDXMichel Weststratebefore adoption4c4fecc
line 321why, one step back321 // create drafts for all entries to preserve insertion orderMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 322–323why, one step back322 state.copy_ = new Set()323 state.base_.forEach(value => {TSDXMichel Weststratebefore adoption4c4fecc
line 324why, one step back324 if (isDraftable(value)) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 325–327why, one step back325 const draft = createProxy(state.scope_.immer_, value, state)326 state.drafts_.set(value, draft)327 state.copy_!.add(draft)TSDXMichel Weststratebefore adoption4c4fecc
line 328why, one step back328 } else {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 329why, one step back329 state.copy_!.add(value)TSDXMichel Weststratebefore adoption4c4fecc
lines 330–332why, one step back330 }331 })332 }Preserve error codes in productionMichel Weststratebefore adoptiond005fab
lines 333–336why, one step back333 }334335 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {336 if (state.revoked_) die(3, JSON.stringify(latest(state)))TSDXMichel Weststratebefore adoption4c4fecc
lines 337–338why, one step back337 }338scramble plugin names, -56Michel Weststratebefore adoption037b474
line 339why, one step back339 loadPlugin("mapset", {proxyMap_, proxySet_})TSDXMichel Weststratebefore adoption4c4fecc
line 340why, one step back340}