mapset.ts
This file
Path
src/plugins/mapset.ts
350 lines · 8.2 KiB · TypeScript
Last changed by
More WIPMichel Weststratebefore adoptionee1c977
History of this fileTake it
More WIPMichel Weststratebefore adoptionee1c977
lines 1–350why, one step back1// types only!2import {3 ImmerState,4 AnyMap,5 AnySet,6 MapState,7 SetState,8 Utilities9} from "../internal"1011import {__loadPlugin} from "../../"1213__loadPlugin(14 "mapset",15 // @ts-ignore16 ({17 DRAFT_STATE,18 ProxyType,19 ImmerScope,20 latest,21 assertUnrevoked,22 iteratorSymbol,23 isDraftable,24 createProxy25 }: Utilities) => {26 /* istanbul ignore next */27 var extendStatics = function(d: any, b: any): any {28 extendStatics =29 Object.setPrototypeOf ||30 ({__proto__: []} instanceof Array &&31 function(d, b) {32 d.__proto__ = b33 }) ||34 function(d, b) {35 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]36 }37 return extendStatics(d, b)38 }3940 // Ugly hack to resolve #502 and inherit built in Map / Set41 function __extends(d: any, b: any): any {42 extendStatics(d, b)43 function __(this: any): any {44 this.constructor = d45 }46 d.prototype =47 // @ts-ignore48 ((__.prototype = b.prototype), new __())49 }5051 const DraftMap = (function(_super) {52 if (!_super) {53 /* istanbul ignore next */54 throw new Error("Map is not polyfilled")55 }56 __extends(DraftMap, _super)57 // Create class manually, cause #50258 function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {59 this[DRAFT_STATE] = {60 type: ProxyType.Map,61 parent,62 scope: parent ? parent.scope : ImmerScope.current!,63 modified: false,64 finalized: false,65 copy: undefined,66 assigned: undefined,67 base: target,68 draft: this as any,69 isManual: false,70 revoked: false71 }72 return this73 }74 const p = DraftMap.prototype7576 // TODO: smaller build size if we create a util for Object.defineProperty77 Object.defineProperty(p, "size", {78 get: function() {79 return latest(this[DRAFT_STATE]).size80 },81 enumerable: true,82 configurable: true83 })8485 p.has = function(key: any): boolean {86 return latest(this[DRAFT_STATE]).has(key)87 }8889 p.set = function(key: any, value: any) {90 const state = this[DRAFT_STATE]91 assertUnrevoked(state)92 if (latest(state).get(key) !== value) {93 prepareMapCopy(state)94 state.scope.immer.markChanged(state)95 state.assigned!.set(key, true)96 state.copy!.set(key, value)97 state.assigned!.set(key, true)98 }99 return this100 }101102 p.delete = function(key: any): boolean {103 if (!this.has(key)) {104 return false105 }106107 const state = this[DRAFT_STATE]108 assertUnrevoked(state)109 prepareMapCopy(state)110 state.scope.immer.markChanged(state)111 state.assigned!.set(key, false)112 state.copy!.delete(key)113 return true114 }115116 p.clear = function() {117 const state = this[DRAFT_STATE]118 assertUnrevoked(state)119 prepareMapCopy(state)120 state.scope.immer.markChanged(state)121 state.assigned = new Map()122 return state.copy!.clear()123 }124125 p.forEach = function(126 cb: (value: any, key: any, self: any) => void,127 thisArg?: any128 ) {129 const state = this[DRAFT_STATE]130 latest(state).forEach((_value: any, key: any, _map: any) => {131 cb.call(thisArg, this.get(key), key, this)132 })133 }134135 p.get = function(key: any): any {136 const state = this[DRAFT_STATE]137 assertUnrevoked(state)138 const value = latest(state).get(key)139 if (state.finalized || !isDraftable(value)) {140 return value141 }142 if (value !== state.base.get(key)) {143 return value // either already drafted or reassigned144 }145 // despite what it looks, this creates a draft only once, see above condition146 const draft = state.scope.immer.createProxy(value, state)147 prepareMapCopy(state)148 state.copy!.set(key, draft)149 return draft150 }151152 p.keys = function(): IterableIterator<any> {153 return latest(this[DRAFT_STATE]).keys()154 }155156 p.values = function(): IterableIterator<any> {157 const iterator = this.keys()158 return {159 [iteratorSymbol]: () => this.values(),160 next: () => {161 const r = iterator.next()162 /* istanbul ignore next */163 if (r.done) return r164 const value = this.get(r.value)165 return {166 done: false,167 value168 }169 }170 } as any171 }172173 p.entries = function(): IterableIterator<[any, any]> {174 const iterator = this.keys()175 return {176 [iteratorSymbol]: () => this.entries(),177 next: () => {178 const r = iterator.next()179 /* istanbul ignore next */180 if (r.done) return r181 const value = this.get(r.value)182 return {183 done: false,184 value: [r.value, value]185 }186 }187 } as any188 }189190 p[iteratorSymbol] = function() {191 return this.entries()192 }193194 return DraftMap195 })(Map)196197 function proxyMap<T extends AnyMap>(target: T, parent?: ImmerState): T {198 // @ts-ignore199 return new DraftMap(target, parent)200 }201202 function prepareMapCopy(state: MapState) {203 if (!state.copy) {204 state.assigned = new Map()205 state.copy = new Map(state.base)206 }207 }208209 const DraftSet = (function(_super) {210 if (!_super) {211 /* istanbul ignore next */212 throw new Error("Set is not polyfilled")213 }214 __extends(DraftSet, _super)215 // Create class manually, cause #502216 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {217 this[DRAFT_STATE] = {218 type: ProxyType.Set,219 parent,220 scope: parent ? parent.scope : ImmerScope.current!,221 modified: false,222 finalized: false,223 copy: undefined,224 base: target,225 draft: this,226 drafts: new Map(),227 revoked: false,228 isManual: false229 }230 return this231 }232 const p = DraftSet.prototype233234 Object.defineProperty(p, "size", {235 get: function() {236 return latest(this[DRAFT_STATE]).size237 },238 enumerable: true,239 configurable: true240 })241242 p.has = function(value: any): boolean {243 const state = this[DRAFT_STATE]244 assertUnrevoked(state)245 // bit of trickery here, to be able to recognize both the value, and the draft of its value246 if (!state.copy) {247 return state.base.has(value)248 }249 if (state.copy.has(value)) return true250 if (state.drafts.has(value) && state.copy.has(state.drafts.get(value)))251 return true252 return false253 }254255 p.add = function(value: any): any {256 const state = this[DRAFT_STATE]257 assertUnrevoked(state)258 if (state.copy) {259 state.copy.add(value)260 } else if (!state.base.has(value)) {261 prepareSetCopy(state)262 state.scope.immer.markChanged(state)263 state.copy!.add(value)264 }265 return this266 }267268 p.delete = function(value: any): any {269 if (!this.has(value)) {270 return false271 }272273 const state = this[DRAFT_STATE]274 assertUnrevoked(state)275 prepareSetCopy(state)276 state.scope.immer.markChanged(state)277 return (278 state.copy!.delete(value) ||279 (state.drafts.has(value)280 ? state.copy!.delete(state.drafts.get(value))281 : /* istanbul ignore next */ false)282 )283 }284285 p.clear = function() {286 const state = this[DRAFT_STATE]287 assertUnrevoked(state)288 prepareSetCopy(state)289 state.scope.immer.markChanged(state)290 return state.copy!.clear()291 }292293 p.values = function(): IterableIterator<any> {294 const state = this[DRAFT_STATE]295 assertUnrevoked(state)296 prepareSetCopy(state)297 return state.copy!.values()298 }299300 p.entries = function entries(): IterableIterator<[any, any]> {301 const state = this[DRAFT_STATE]302 assertUnrevoked(state)303 prepareSetCopy(state)304 return state.copy!.entries()305 }306307 p.keys = function(): IterableIterator<any> {308 return this.values()309 }310311 p[iteratorSymbol] = function() {312 return this.values()313 }314315 p.forEach = function forEach(cb: any, thisArg?: any) {316 const iterator = this.values()317 let result = iterator.next()318 while (!result.done) {319 cb.call(thisArg, result.value, result.value, this)320 result = iterator.next()321 }322 }323324 return DraftSet325 })(Set)326327 function proxySet<T extends AnySet>(target: T, parent?: ImmerState): T {328 // @ts-ignore329 return new DraftSet(target, parent)330 }331332 function prepareSetCopy(state: SetState) {333 if (!state.copy) {334 // create drafts for all entries to preserve insertion order335 state.copy = new Set()336 state.base.forEach(value => {337 if (isDraftable(value)) {338 const draft = createProxy(state.scope.immer, value, state)339 state.drafts.set(value, draft)340 state.copy!.add(draft)341 } else {342 state.copy!.add(value)343 }344 })345 }346 }347348 return {proxyMap, proxySet}349 }350)