mapset.ts
This file
Path
src/plugins/mapset.ts
343 lines · 7.9 KiB · TypeScript
Last changed by
TSDXMichel Weststratebefore adoption4c4fecc
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–16why, one step back8 DRAFT_STATE,9 ProxyType,10 ImmerScope,11 latest,12 assertUnrevoked,13 iteratorSymbol,14 isDraftable,15 createProxy,16 loadPluginMore WIPMichel Weststratebefore adoptionee1c977
lines 17–18why, one step back17} from "../internal"18TSDXMichel Weststratebefore adoption4c4fecc
lines 19–24why, one step back19export function enableMapSet() {20 /* istanbul ignore next */21 var extendStatics = function(d: any, b: any): any {22 extendStatics =23 Object.setPrototypeOf ||24 ({__proto__: []} instanceof Array &&More WIPMichel Weststratebefore adoptionee1c977
line 25why, one step back25 function(d, b) {TSDXMichel Weststratebefore adoption4c4fecc
lines 26–38why, one step back26 d.__proto__ = b27 }) ||28 function(d, b) {29 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]30 }31 return extendStatics(d, b)32 }3334 // Ugly hack to resolve #502 and inherit built in Map / Set35 function __extends(d: any, b: any): any {36 extendStatics(d, b)37 function __(this: any): any {38 this.constructor = dMore WIPMichel Weststratebefore adoptionee1c977
line 39why, one step back39 }TSDXMichel Weststratebefore adoption4c4fecc
lines 40–43why, one step back40 d.prototype =41 // @ts-ignore42 ((__.prototype = b.prototype), new __())43 }More WIPMichel Weststratebefore adoptionee1c977
line 44why, one step back44TSDXMichel Weststratebefore adoption4c4fecc
lines 45–64why, one step back45 const DraftMap = (function(_super) {46 if (!_super) {47 /* istanbul ignore next */48 throw new Error("Map is not polyfilled")49 }50 __extends(DraftMap, _super)51 // Create class manually, cause #50252 function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {53 this[DRAFT_STATE] = {54 type: ProxyType.Map,55 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: falseMore WIPMichel Weststratebefore adoptionee1c977
line 65why, one step back65 }TSDXMichel 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–77why, 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]).size74 },75 enumerable: true,76 configurable: true77 })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
lines 83–86why, one step back83 p.set = function(key: any, value: any) {84 const state = this[DRAFT_STATE]85 assertUnrevoked(state)86 if (latest(state).get(key) !== value) {More WIPMichel Weststratebefore adoptionee1c977
lines 87–88why, one step back87 prepareMapCopy(state)88 state.scope.immer.markChanged(state)TSDXMichel Weststratebefore adoption4c4fecc
lines 89–91why, one step back89 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 }100TSDXMichel Weststratebefore adoption4c4fecc
lines 101–108why, one step back101 const state = this[DRAFT_STATE]102 assertUnrevoked(state)103 prepareMapCopy(state)104 state.scope.immer.markChanged(state)105 state.assigned!.set(key, false)106 state.copy!.delete(key)107 return true108 }More WIPMichel Weststratebefore adoptionee1c977
line 109why, one step back109TSDXMichel Weststratebefore adoption4c4fecc
lines 110–117why, one step back110 p.clear = function() {111 const state = this[DRAFT_STATE]112 assertUnrevoked(state)113 prepareMapCopy(state)114 state.scope.immer.markChanged(state)115 state.assigned = new Map()116 return state.copy!.clear()117 }More WIPMichel Weststratebefore adoptionee1c977
line 118why, one step back118TSDXMichel Weststratebefore adoption4c4fecc
lines 119–127why, one step back119 p.forEach = function(120 cb: (value: any, key: any, self: any) => void,121 thisArg?: any122 ) {123 const state = this[DRAFT_STATE]124 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
lines 129–134why, one step back129 p.get = function(key: any): any {130 const state = this[DRAFT_STATE]131 assertUnrevoked(state)132 const value = latest(state).get(key)133 if (state.finalized || !isDraftable(value)) {134 return valueMore WIPMichel Weststratebefore adoptionee1c977
line 135why, one step back135 }TSDXMichel Weststratebefore adoption4c4fecc
lines 136–137why, one step back136 if (value !== state.base.get(key)) {137 return value // either already drafted or reassignedMore WIPMichel Weststratebefore adoptionee1c977
line 138why, one step back138 }TSDXMichel Weststratebefore adoption4c4fecc
lines 139–144why, one step back139 // despite what it looks, this creates a draft only once, see above condition140 const draft = state.scope.immer.createProxy(value, state)141 prepareMapCopy(state)142 state.copy!.set(key, draft)143 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 back190TSDXMichel Weststratebefore adoption4c4fecc
lines 191–194why, one step back191 function proxyMap<T extends AnyMap>(target: T, parent?: ImmerState): T {192 // @ts-ignore193 return new DraftMap(target, parent)194 }More WIPMichel Weststratebefore adoptionee1c977
line 195why, one step back195TSDXMichel Weststratebefore adoption4c4fecc
lines 196–201why, one step back196 function prepareMapCopy(state: MapState) {197 if (!state.copy) {198 state.assigned = new Map()199 state.copy = new Map(state.base)200 }201 }More WIPMichel Weststratebefore adoptionee1c977
line 202why, one step back202TSDXMichel Weststratebefore adoption4c4fecc
lines 203–222why, one step back203 const DraftSet = (function(_super) {204 if (!_super) {205 /* istanbul ignore next */206 throw new Error("Set is not polyfilled")207 }208 __extends(DraftSet, _super)209 // Create class manually, cause #502210 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {211 this[DRAFT_STATE] = {212 type: ProxyType.Set,213 parent,214 scope: parent ? parent.scope : ImmerScope.current!,215 modified: false,216 finalized: false,217 copy: undefined,218 base: target,219 draft: this,220 drafts: new Map(),221 revoked: false,222 isManual: falseMore WIPMichel Weststratebefore adoptionee1c977
line 223why, one step back223 }TSDXMichel Weststratebefore adoption4c4fecc
lines 224–226why, one step back224 return this225 }226 const p = DraftSet.prototypeMore WIPMichel Weststratebefore adoptionee1c977
line 227why, one step back227TSDXMichel Weststratebefore adoption4c4fecc
lines 228–234why, one step back228 Object.defineProperty(p, "size", {229 get: function() {230 return latest(this[DRAFT_STATE]).size231 },232 enumerable: true,233 configurable: true234 })More WIPMichel Weststratebefore adoptionee1c977
line 235why, one step back235TSDXMichel Weststratebefore adoption4c4fecc
lines 236–241why, one step back236 p.has = function(value: any): boolean {237 const state = this[DRAFT_STATE]238 assertUnrevoked(state)239 // bit of trickery here, to be able to recognize both the value, and the draft of its value240 if (!state.copy) {241 return state.base.has(value)More WIPMichel Weststratebefore adoptionee1c977
line 242why, one step back242 }TSDXMichel Weststratebefore adoption4c4fecc
lines 243–247why, one step back243 if (state.copy.has(value)) return true244 if (state.drafts.has(value) && state.copy.has(state.drafts.get(value)))245 return true246 return false247 }More WIPMichel Weststratebefore adoptionee1c977
line 248why, one step back248TSDXMichel Weststratebefore adoption4c4fecc
lines 249–254why, one step back249 p.add = function(value: any): any {250 const state = this[DRAFT_STATE]251 assertUnrevoked(state)252 if (state.copy) {253 state.copy.add(value)254 } else if (!state.base.has(value)) {More WIPMichel Weststratebefore adoptionee1c977
lines 255–256why, one step back255 prepareSetCopy(state)256 state.scope.immer.markChanged(state)TSDXMichel Weststratebefore adoption4c4fecc
line 257why, one step back257 state.copy!.add(value)More WIPMichel Weststratebefore adoptionee1c977
line 258why, one step back258 }TSDXMichel Weststratebefore adoption4c4fecc
lines 259–260why, one step back259 return this260 }More WIPMichel Weststratebefore adoptionee1c977
line 261why, one step back261TSDXMichel Weststratebefore adoption4c4fecc
lines 262–264why, one step back262 p.delete = function(value: any): any {263 if (!this.has(value)) {264 return falseMore WIPMichel Weststratebefore adoptionee1c977
lines 265–266why, one step back265 }266TSDXMichel Weststratebefore adoption4c4fecc
lines 267–277why, one step back267 const state = this[DRAFT_STATE]268 assertUnrevoked(state)269 prepareSetCopy(state)270 state.scope.immer.markChanged(state)271 return (272 state.copy!.delete(value) ||273 (state.drafts.has(value)274 ? state.copy!.delete(state.drafts.get(value))275 : /* istanbul ignore next */ false)276 )277 }More WIPMichel Weststratebefore adoptionee1c977
line 278why, one step back278TSDXMichel Weststratebefore adoption4c4fecc
lines 279–285why, one step back279 p.clear = function() {280 const state = this[DRAFT_STATE]281 assertUnrevoked(state)282 prepareSetCopy(state)283 state.scope.immer.markChanged(state)284 return state.copy!.clear()285 }More WIPMichel Weststratebefore adoptionee1c977
line 286why, one step back286TSDXMichel Weststratebefore adoption4c4fecc
lines 287–292why, one step back287 p.values = function(): IterableIterator<any> {288 const state = this[DRAFT_STATE]289 assertUnrevoked(state)290 prepareSetCopy(state)291 return state.copy!.values()292 }More WIPMichel Weststratebefore adoptionee1c977
line 293why, one step back293TSDXMichel Weststratebefore adoption4c4fecc
lines 294–299why, one step back294 p.entries = function entries(): IterableIterator<[any, any]> {295 const state = this[DRAFT_STATE]296 assertUnrevoked(state)297 prepareSetCopy(state)298 return state.copy!.entries()299 }More WIPMichel Weststratebefore adoptionee1c977
line 300why, one step back300TSDXMichel Weststratebefore adoption4c4fecc
lines 301–303why, one step back301 p.keys = function(): IterableIterator<any> {302 return this.values()303 }More WIPMichel Weststratebefore adoptionee1c977
line 304why, one step back304TSDXMichel Weststratebefore adoption4c4fecc
lines 305–306why, one step back305 p[iteratorSymbol] = function() {306 return this.values()More WIPMichel Weststratebefore adoptionee1c977
lines 307–308why, one step back307 }308TSDXMichel Weststratebefore adoption4c4fecc
lines 309–314why, one step back309 p.forEach = function forEach(cb: any, thisArg?: any) {310 const iterator = this.values()311 let result = iterator.next()312 while (!result.done) {313 cb.call(thisArg, result.value, result.value, this)314 result = iterator.next()More WIPMichel Weststratebefore adoptionee1c977
lines 315–317why, one step back315 }316 }317TSDXMichel Weststratebefore adoption4c4fecc
lines 318–323why, one step back318 return DraftSet319 })(Set)320321 function proxySet<T extends AnySet>(target: T, parent?: ImmerState): T {322 // @ts-ignore323 return new DraftSet(target, parent)More WIPMichel Weststratebefore adoptionee1c977
line 324why, one step back324 }TSDXMichel Weststratebefore adoption4c4fecc
lines 325–343why, one step back325326 function prepareSetCopy(state: SetState) {327 if (!state.copy) {328 // create drafts for all entries to preserve insertion order329 state.copy = new Set()330 state.base.forEach(value => {331 if (isDraftable(value)) {332 const draft = createProxy(state.scope.immer, value, state)333 state.drafts.set(value, draft)334 state.copy!.add(draft)335 } else {336 state.copy!.add(value)337 }338 })339 }340 }341342 loadPlugin("mapset", {proxyMap, proxySet})343}