mapset.ts
This file
Path
src/plugins/mapset.ts
339 lines · 8.0 KiB · TypeScript
Last changed by
unclassed scopeMichel Weststratebefore adoption7a38af9
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
line 8why, one step back8 DRAFT_STATE,unclassed scopeMichel Weststratebefore adoption7a38af9
line 9why, one step back9 getCurrentScope,TSDXMichel Weststratebefore adoption4c4fecc
lines 10–13why, one step back10 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
line 53why, one step back53 parent_: parent,unclassed scopeMichel Weststratebefore adoption7a38af9
line 54why, one step back54 scope_: parent ? parent.scope_ : getCurrentScope()!,Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 55–63why, one step back55 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–70why, one step back68 Object.defineProperty(p, "size", {69 get: function() {70 return latest(this[DRAFT_STATE]).sizeSome notesMichel Weststratebefore adoption746dc6f
lines 71–73why, one step back71 }72 // enumerable: false,73 // configurable: trueTSDXMichel Weststratebefore adoption4c4fecc
line 74why, one step back74 })More WIPMichel Weststratebefore adoptionee1c977
line 75why, one step back75TSDXMichel Weststratebefore adoption4c4fecc
lines 76–78why, one step back76 p.has = function(key: any): boolean {77 return latest(this[DRAFT_STATE]).has(key)78 }More WIPMichel Weststratebefore adoptionee1c977
line 79why, one step back79TSDXMichel Weststratebefore adoption4c4fecc
line 80why, one step back80 p.set = function(key: any, value: any) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 81why, one step back81 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 82–83why, one step back82 assertUnrevoked(state)83 if (latest(state).get(key) !== value) {More WIPMichel Weststratebefore adoptionee1c977
line 84why, one step back84 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 85–88why, one step back85 markChanged(state.scope_.immer_, state)86 state.assigned_!.set(key, true)87 state.copy_!.set(key, value)88 state.assigned_!.set(key, true)More WIPMichel Weststratebefore adoptionee1c977
line 89why, one step back89 }TSDXMichel Weststratebefore adoption4c4fecc
lines 90–91why, one step back90 return this91 }More WIPMichel Weststratebefore adoptionee1c977
line 92why, one step back92TSDXMichel Weststratebefore adoption4c4fecc
lines 93–95why, one step back93 p.delete = function(key: any): boolean {94 if (!this.has(key)) {95 return falseMore WIPMichel Weststratebefore adoptionee1c977
lines 96–97why, one step back96 }97Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 98why, one step back98 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 99–100why, one step back99 assertUnrevoked(state)100 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 101–103why, one step back101 markChanged(state.scope_.immer_, state)102 state.assigned_!.set(key, false)103 state.copy_!.delete(key)TSDXMichel Weststratebefore adoption4c4fecc
lines 104–105why, one step back104 return true105 }More WIPMichel Weststratebefore adoptionee1c977
line 106why, one step back106TSDXMichel Weststratebefore adoption4c4fecc
line 107why, one step back107 p.clear = function() {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 108why, one step back108 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 109–110why, one step back109 assertUnrevoked(state)110 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 111–113why, one step back111 markChanged(state.scope_.immer_, state)112 state.assigned_ = new Map()113 return state.copy_!.clear()TSDXMichel Weststratebefore adoption4c4fecc
line 114why, one step back114 }More WIPMichel Weststratebefore adoptionee1c977
line 115why, one step back115TSDXMichel Weststratebefore adoption4c4fecc
lines 116–119why, one step back116 p.forEach = function(117 cb: (value: any, key: any, self: any) => void,118 thisArg?: any119 ) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 120why, one step back120 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 121–124why, one step back121 latest(state).forEach((_value: any, key: any, _map: any) => {122 cb.call(thisArg, this.get(key), key, this)123 })124 }More WIPMichel Weststratebefore adoptionee1c977
line 125why, one step back125TSDXMichel Weststratebefore adoption4c4fecc
line 126why, one step back126 p.get = function(key: any): any {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 127why, one step back127 const state: MapState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 128–129why, one step back128 assertUnrevoked(state)129 const value = latest(state).get(key)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 130why, one step back130 if (state.finalized_ || !isDraftable(value)) {TSDXMichel Weststratebefore adoption4c4fecc
line 131why, one step back131 return valueMore WIPMichel Weststratebefore adoptionee1c977
line 132why, one step back132 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 133why, one step back133 if (value !== state.base_.get(key)) {TSDXMichel Weststratebefore adoption4c4fecc
line 134why, one step back134 return value // either already drafted or reassignedMore WIPMichel Weststratebefore adoptionee1c977
line 135why, one step back135 }TSDXMichel Weststratebefore adoption4c4fecc
line 136why, one step back136 // despite what it looks, this creates a draft only once, see above conditionMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 137why, one step back137 const draft = createProxy(state.scope_.immer_, value, state)TSDXMichel Weststratebefore adoption4c4fecc
line 138why, one step back138 prepareMapCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 139why, one step back139 state.copy_!.set(key, draft)TSDXMichel Weststratebefore adoption4c4fecc
lines 140–141why, one step back140 return draft141 }More WIPMichel Weststratebefore adoptionee1c977
line 142why, one step back142TSDXMichel Weststratebefore adoption4c4fecc
lines 143–145why, one step back143 p.keys = function(): IterableIterator<any> {144 return latest(this[DRAFT_STATE]).keys()145 }More WIPMichel Weststratebefore adoptionee1c977
line 146why, one step back146TSDXMichel Weststratebefore adoption4c4fecc
lines 147–162why, one step back147 p.values = function(): IterableIterator<any> {148 const iterator = this.keys()149 return {150 [iteratorSymbol]: () => this.values(),151 next: () => {152 const r = iterator.next()153 /* istanbul ignore next */154 if (r.done) return r155 const value = this.get(r.value)156 return {157 done: false,158 value159 }160 }161 } as any162 }More WIPMichel Weststratebefore adoptionee1c977
line 163why, one step back163TSDXMichel Weststratebefore adoption4c4fecc
lines 164–178why, one step back164 p.entries = function(): IterableIterator<[any, any]> {165 const iterator = this.keys()166 return {167 [iteratorSymbol]: () => this.entries(),168 next: () => {169 const r = iterator.next()170 /* istanbul ignore next */171 if (r.done) return r172 const value = this.get(r.value)173 return {174 done: false,175 value: [r.value, value]176 }177 }178 } as anyMore WIPMichel Weststratebefore adoptionee1c977
lines 179–180why, one step back179 }180TSDXMichel Weststratebefore adoption4c4fecc
lines 181–182why, one step back181 p[iteratorSymbol] = function() {182 return this.entries()More WIPMichel Weststratebefore adoptionee1c977
lines 183–184why, one step back183 }184TSDXMichel Weststratebefore adoption4c4fecc
lines 185–186why, one step back185 return DraftMap186 })(Map)More WIPMichel Weststratebefore adoptionee1c977
line 187why, one step back187scramble plugin names, -56Michel Weststratebefore adoption037b474
line 188why, one step back188 function proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T {TSDXMichel Weststratebefore adoption4c4fecc
lines 189–191why, one step back189 // @ts-ignore190 return new DraftMap(target, parent)191 }More WIPMichel Weststratebefore adoptionee1c977
line 192why, one step back192TSDXMichel Weststratebefore adoption4c4fecc
line 193why, one step back193 function prepareMapCopy(state: MapState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 194–196why, one step back194 if (!state.copy_) {195 state.assigned_ = new Map()196 state.copy_ = new Map(state.base_)TSDXMichel Weststratebefore adoption4c4fecc
lines 197–198why, one step back197 }198 }More WIPMichel Weststratebefore adoptionee1c977
line 199why, one step back199TSDXMichel Weststratebefore adoption4c4fecc
lines 200–204why, one step back200 const DraftSet = (function(_super) {201 __extends(DraftSet, _super)202 // Create class manually, cause #502203 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {204 this[DRAFT_STATE] = {Killed enumerations -145 (!)Michel Weststratebefore adoptionfdc78db
line 205why, one step back205 type_: ProxyTypeSet,Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 206why, one step back206 parent_: parent,unclassed scopeMichel Weststratebefore adoption7a38af9
line 207why, one step back207 scope_: parent ? parent.scope_ : getCurrentScope()!,Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 208–216why, one step back208 modified_: false,209 finalized_: false,210 copy_: undefined,211 base_: target,212 draft_: this,213 drafts_: new Map(),214 revoked_: false,215 isManual_: false216 } as SetStateTSDXMichel Weststratebefore adoption4c4fecc
lines 217–219why, one step back217 return this218 }219 const p = DraftSet.prototypeMore WIPMichel Weststratebefore adoptionee1c977
line 220why, one step back220TSDXMichel Weststratebefore adoption4c4fecc
lines 221–223why, one step back221 Object.defineProperty(p, "size", {222 get: function() {223 return latest(this[DRAFT_STATE]).sizeSome notesMichel Weststratebefore adoption746dc6f
lines 224–225why, one step back224 }225 // enumerable: true,TSDXMichel Weststratebefore adoption4c4fecc
line 226why, one step back226 })More WIPMichel Weststratebefore adoptionee1c977
line 227why, one step back227TSDXMichel Weststratebefore adoption4c4fecc
line 228why, one step back228 p.has = function(value: any): boolean {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 229why, one step back229 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 230–231why, one step back230 assertUnrevoked(state)231 // bit of trickery here, to be able to recognize both the value, and the draft of its valueMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 232–233why, one step back232 if (!state.copy_) {233 return state.base_.has(value)More WIPMichel Weststratebefore adoptionee1c977
line 234why, one step back234 }Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 235–236why, one step back235 if (state.copy_.has(value)) return true236 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))TSDXMichel Weststratebefore adoption4c4fecc
lines 237–239why, one step back237 return true238 return false239 }More WIPMichel Weststratebefore adoptionee1c977
line 240why, one step back240TSDXMichel Weststratebefore adoption4c4fecc
line 241why, one step back241 p.add = function(value: any): any {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 242why, one step back242 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
line 243why, one step back243 assertUnrevoked(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 244–246why, one step back244 if (state.copy_) {245 state.copy_.add(value)246 } else if (!state.base_.has(value)) {More WIPMichel Weststratebefore adoptionee1c977
line 247why, one step back247 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 248–249why, one step back248 markChanged(state.scope_.immer_, state)249 state.copy_!.add(value)More WIPMichel Weststratebefore adoptionee1c977
line 250why, one step back250 }TSDXMichel Weststratebefore adoption4c4fecc
lines 251–252why, one step back251 return this252 }More WIPMichel Weststratebefore adoptionee1c977
line 253why, one step back253TSDXMichel Weststratebefore adoption4c4fecc
lines 254–256why, one step back254 p.delete = function(value: any): any {255 if (!this.has(value)) {256 return falseMore WIPMichel Weststratebefore adoptionee1c977
lines 257–258why, one step back257 }258Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 259why, one step back259 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 260–261why, one step back260 assertUnrevoked(state)261 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 262why, one step back262 markChanged(state.scope_.immer_, state)TSDXMichel Weststratebefore adoption4c4fecc
line 263why, one step back263 return (Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 264–266why, one step back264 state.copy_!.delete(value) ||265 (state.drafts_.has(value)266 ? state.copy_!.delete(state.drafts_.get(value))TSDXMichel Weststratebefore adoption4c4fecc
lines 267–269why, one step back267 : /* istanbul ignore next */ false)268 )269 }More WIPMichel Weststratebefore adoptionee1c977
line 270why, one step back270TSDXMichel Weststratebefore adoption4c4fecc
line 271why, one step back271 p.clear = function() {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 272why, one step back272 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 273–274why, one step back273 assertUnrevoked(state)274 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 275–276why, one step back275 markChanged(state.scope_.immer_, state)276 return state.copy_!.clear()TSDXMichel Weststratebefore adoption4c4fecc
line 277why, one step back277 }More WIPMichel Weststratebefore adoptionee1c977
line 278why, one step back278TSDXMichel Weststratebefore adoption4c4fecc
line 279why, one step back279 p.values = function(): IterableIterator<any> {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 280why, one step back280 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 281–282why, one step back281 assertUnrevoked(state)282 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 283why, one step back283 return state.copy_!.values()TSDXMichel Weststratebefore adoption4c4fecc
line 284why, one step back284 }More WIPMichel Weststratebefore adoptionee1c977
line 285why, one step back285TSDXMichel Weststratebefore adoption4c4fecc
line 286why, one step back286 p.entries = function entries(): IterableIterator<[any, any]> {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 287why, one step back287 const state: SetState = this[DRAFT_STATE]TSDXMichel Weststratebefore adoption4c4fecc
lines 288–289why, one step back288 assertUnrevoked(state)289 prepareSetCopy(state)Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 290why, one step back290 return state.copy_!.entries()TSDXMichel Weststratebefore adoption4c4fecc
line 291why, one step back291 }More WIPMichel Weststratebefore adoptionee1c977
line 292why, one step back292TSDXMichel Weststratebefore adoption4c4fecc
lines 293–295why, one step back293 p.keys = function(): IterableIterator<any> {294 return this.values()295 }More WIPMichel Weststratebefore adoptionee1c977
line 296why, one step back296TSDXMichel Weststratebefore adoption4c4fecc
lines 297–298why, one step back297 p[iteratorSymbol] = function() {298 return this.values()More WIPMichel Weststratebefore adoptionee1c977
lines 299–300why, one step back299 }300TSDXMichel Weststratebefore adoption4c4fecc
lines 301–306why, one step back301 p.forEach = function forEach(cb: any, thisArg?: any) {302 const iterator = this.values()303 let result = iterator.next()304 while (!result.done) {305 cb.call(thisArg, result.value, result.value, this)306 result = iterator.next()More WIPMichel Weststratebefore adoptionee1c977
lines 307–309why, one step back307 }308 }309TSDXMichel Weststratebefore adoption4c4fecc
lines 310–312why, one step back310 return DraftSet311 })(Set)312scramble plugin names, -56Michel Weststratebefore adoption037b474
line 313why, one step back313 function proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T {TSDXMichel Weststratebefore adoption4c4fecc
lines 314–315why, one step back314 // @ts-ignore315 return new DraftSet(target, parent)More WIPMichel Weststratebefore adoptionee1c977
line 316why, one step back316 }TSDXMichel Weststratebefore adoption4c4fecc
lines 317–318why, one step back317318 function prepareSetCopy(state: SetState) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 319why, one step back319 if (!state.copy_) {TSDXMichel Weststratebefore adoption4c4fecc
line 320why, one step back320 // create drafts for all entries to preserve insertion orderMangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 321–322why, one step back321 state.copy_ = new Set()322 state.base_.forEach(value => {TSDXMichel Weststratebefore adoption4c4fecc
line 323why, one step back323 if (isDraftable(value)) {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
lines 324–326why, one step back324 const draft = createProxy(state.scope_.immer_, value, state)325 state.drafts_.set(value, draft)326 state.copy_!.add(draft)TSDXMichel Weststratebefore adoption4c4fecc
line 327why, one step back327 } else {Mangle properties, -114 bytesMichel Weststratebefore adoptionbfa7ddd
line 328why, one step back328 state.copy_!.add(value)TSDXMichel Weststratebefore adoption4c4fecc
lines 329–331why, one step back329 }330 })331 }Preserve error codes in productionMichel Weststratebefore adoptiond005fab
lines 332–335why, one step back332 }333334 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {335 if (state.revoked_) die(3, JSON.stringify(latest(state)))TSDXMichel Weststratebefore adoption4c4fecc
lines 336–337why, one step back336 }337Tests for pluginsMichel Weststratebefore adoptiondb16520
line 338why, one step back338 loadPlugin("MapSet", {proxyMap_, proxySet_})TSDXMichel Weststratebefore adoption4c4fecc
line 339why, one step back339}