mapset.ts
This file
Path
src/plugins/mapset.ts
340 lines · 7.8 KiB · TypeScript
Last changed by
use invariant -321 bytesMichel Weststratebefore adoptionac57da5
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–63why, 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] = {53 type: ProxyType.Map,54 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: falseMore WIPMichel Weststratebefore adoptionee1c977
line 64why, one step back64 }TSDXMichel 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–76why, 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]).size73 },74 enumerable: true,75 configurable: true76 })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
lines 82–85why, one step back82 p.set = function(key: any, value: any) {83 const state = this[DRAFT_STATE]84 assertUnrevoked(state)85 if (latest(state).get(key) !== value) {More WIPMichel Weststratebefore adoptionee1c977
line 86why, one step back86 prepareMapCopy(state)New build set. Current statsMichel Weststratebefore adoption9aec0fe
line 87why, one step back87 markChanged(state.scope.immer, state)TSDXMichel Weststratebefore adoption4c4fecc
lines 88–90why, one step back88 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 }99TSDXMichel Weststratebefore adoption4c4fecc
lines 100–102why, one step back100 const state = this[DRAFT_STATE]101 assertUnrevoked(state)102 prepareMapCopy(state)New build set. Current statsMichel Weststratebefore adoption9aec0fe
line 103why, one step back103 markChanged(state.scope.immer, state)TSDXMichel Weststratebefore adoption4c4fecc
lines 104–107why, one step back104 state.assigned!.set(key, false)105 state.copy!.delete(key)106 return true107 }More WIPMichel Weststratebefore adoptionee1c977
line 108why, one step back108TSDXMichel Weststratebefore adoption4c4fecc
lines 109–112why, one step back109 p.clear = function() {110 const state = this[DRAFT_STATE]111 assertUnrevoked(state)112 prepareMapCopy(state)New build set. Current statsMichel Weststratebefore adoption9aec0fe
line 113why, one step back113 markChanged(state.scope.immer, state)TSDXMichel Weststratebefore adoption4c4fecc
lines 114–116why, one step back114 state.assigned = new Map()115 return state.copy!.clear()116 }More WIPMichel Weststratebefore adoptionee1c977
line 117why, one step back117TSDXMichel Weststratebefore adoption4c4fecc
lines 118–126why, one step back118 p.forEach = function(119 cb: (value: any, key: any, self: any) => void,120 thisArg?: any121 ) {122 const state = this[DRAFT_STATE]123 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
lines 128–133why, one step back128 p.get = function(key: any): any {129 const state = this[DRAFT_STATE]130 assertUnrevoked(state)131 const value = latest(state).get(key)132 if (state.finalized || !isDraftable(value)) {133 return valueMore WIPMichel Weststratebefore adoptionee1c977
line 134why, one step back134 }TSDXMichel Weststratebefore adoption4c4fecc
lines 135–136why, one step back135 if (value !== state.base.get(key)) {136 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 conditionNew build set. Current statsMichel Weststratebefore adoption9aec0fe
line 139why, one step back139 const draft = createProxy(state.scope.immer, value, state)TSDXMichel Weststratebefore adoption4c4fecc
lines 140–143why, one step back140 prepareMapCopy(state)141 state.copy!.set(key, draft)142 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
lines 195–200why, one step back195 function prepareMapCopy(state: MapState) {196 if (!state.copy) {197 state.assigned = new Map()198 state.copy = new Map(state.base)199 }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–219why, one step back205 __extends(DraftSet, _super)206 // Create class manually, cause #502207 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {208 this[DRAFT_STATE] = {209 type: ProxyType.Set,210 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: falseMore WIPMichel Weststratebefore adoptionee1c977
line 220why, one step back220 }TSDXMichel 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–231why, one step back225 Object.defineProperty(p, "size", {226 get: function() {227 return latest(this[DRAFT_STATE]).size228 },229 enumerable: true,230 configurable: true231 })More WIPMichel Weststratebefore adoptionee1c977
line 232why, one step back232TSDXMichel Weststratebefore adoption4c4fecc
lines 233–238why, one step back233 p.has = function(value: any): boolean {234 const state = this[DRAFT_STATE]235 assertUnrevoked(state)236 // bit of trickery here, to be able to recognize both the value, and the draft of its value237 if (!state.copy) {238 return state.base.has(value)More WIPMichel Weststratebefore adoptionee1c977
line 239why, one step back239 }TSDXMichel Weststratebefore adoption4c4fecc
lines 240–244why, one step back240 if (state.copy.has(value)) return true241 if (state.drafts.has(value) && state.copy.has(state.drafts.get(value)))242 return true243 return false244 }More WIPMichel Weststratebefore adoptionee1c977
line 245why, one step back245TSDXMichel Weststratebefore adoption4c4fecc
lines 246–251why, one step back246 p.add = function(value: any): any {247 const state = this[DRAFT_STATE]248 assertUnrevoked(state)249 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)New build set. Current statsMichel Weststratebefore adoption9aec0fe
line 253why, one step back253 markChanged(state.scope.immer, state)TSDXMichel Weststratebefore adoption4c4fecc
line 254why, one step back254 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 }263TSDXMichel Weststratebefore adoption4c4fecc
lines 264–266why, one step back264 const state = this[DRAFT_STATE]265 assertUnrevoked(state)266 prepareSetCopy(state)New build set. Current statsMichel Weststratebefore adoption9aec0fe
line 267why, one step back267 markChanged(state.scope.immer, state)TSDXMichel Weststratebefore adoption4c4fecc
lines 268–274why, one step back268 return (269 state.copy!.delete(value) ||270 (state.drafts.has(value)271 ? state.copy!.delete(state.drafts.get(value))272 : /* istanbul ignore next */ false)273 )274 }More WIPMichel Weststratebefore adoptionee1c977
line 275why, one step back275TSDXMichel Weststratebefore adoption4c4fecc
lines 276–279why, one step back276 p.clear = function() {277 const state = this[DRAFT_STATE]278 assertUnrevoked(state)279 prepareSetCopy(state)New build set. Current statsMichel Weststratebefore adoption9aec0fe
line 280why, one step back280 markChanged(state.scope.immer, state)TSDXMichel Weststratebefore adoption4c4fecc
lines 281–282why, one step back281 return state.copy!.clear()282 }More WIPMichel Weststratebefore adoptionee1c977
line 283why, one step back283TSDXMichel Weststratebefore adoption4c4fecc
lines 284–289why, one step back284 p.values = function(): IterableIterator<any> {285 const state = this[DRAFT_STATE]286 assertUnrevoked(state)287 prepareSetCopy(state)288 return state.copy!.values()289 }More WIPMichel Weststratebefore adoptionee1c977
line 290why, one step back290TSDXMichel Weststratebefore adoption4c4fecc
lines 291–296why, one step back291 p.entries = function entries(): IterableIterator<[any, any]> {292 const state = this[DRAFT_STATE]293 assertUnrevoked(state)294 prepareSetCopy(state)295 return state.copy!.entries()296 }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–320why, one step back315 return DraftSet316 })(Set)317318 function proxySet<T extends AnySet>(target: T, parent?: ImmerState): T {319 // @ts-ignore320 return new DraftSet(target, parent)More WIPMichel Weststratebefore adoptionee1c977
line 321why, one step back321 }TSDXMichel Weststratebefore adoption4c4fecc
lines 322–340why, one step back322323 function prepareSetCopy(state: SetState) {324 if (!state.copy) {325 // create drafts for all entries to preserve insertion order326 state.copy = new Set()327 state.base.forEach(value => {328 if (isDraftable(value)) {329 const draft = createProxy(state.scope.immer, value, state)330 state.drafts.set(value, draft)331 state.copy!.add(draft)332 } else {333 state.copy!.add(value)334 }335 })336 }337 }338339 loadPlugin("mapset", {proxyMap, proxySet})340}