mapset.ts
This file
Path
src/plugins/mapset.ts
345 lines · 8.0 KiB · TypeScript
Last changed by
fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–17why, one step back1// types only!2import {3 ImmerState,4 AnyMap,5 AnySet,6 MapState,7 SetState,8 DRAFT_STATE,9 getCurrentScope,10 latest,11 iteratorSymbol,12 isDraftable,13 createProxy,14 loadPlugin,15 markChanged,16 ProxyTypeMap,17 ProxyTypeSet,fix: clear map creates invalid patches, fixes #663Michel Weststratebefore adoptionbacc1e0
lines 18–19why, one step back18 die,19 eachMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 20–83why, one step back20} from "../internal"2122export function enableMapSet() {23 /* istanbul ignore next */24 var extendStatics = function(d: any, b: any): any {25 extendStatics =26 Object.setPrototypeOf ||27 ({__proto__: []} instanceof Array &&28 function(d, b) {29 d.__proto__ = b30 }) ||31 function(d, b) {32 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]33 }34 return extendStatics(d, b)35 }3637 // Ugly hack to resolve #502 and inherit built in Map / Set38 function __extends(d: any, b: any): any {39 extendStatics(d, b)40 function __(this: any): any {41 this.constructor = d42 }43 d.prototype =44 // @ts-ignore45 ((__.prototype = b.prototype), new __())46 }4748 const DraftMap = (function(_super) {49 __extends(DraftMap, _super)50 // Create class manually, cause #50251 function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {52 this[DRAFT_STATE] = {53 type_: ProxyTypeMap,54 parent_: parent,55 scope_: parent ? parent.scope_ : getCurrentScope()!,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_: false64 } as MapState65 return this66 }67 const p = DraftMap.prototype6869 Object.defineProperty(p, "size", {70 get: function() {71 return latest(this[DRAFT_STATE]).size72 }73 // enumerable: false,74 // configurable: true75 })7677 p.has = function(key: any): boolean {78 return latest(this[DRAFT_STATE]).has(key)79 }8081 p.set = function(key: any, value: any) {82 const state: MapState = this[DRAFT_STATE]83 assertUnrevoked(state)fix: new map keys were not added if value is undefinedMichel Weststratebefore adoption4a1bd65
line 84why, one step back84 if (!latest(state).has(key) || latest(state).get(key) !== value) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 85why, one step back85 prepareMapCopy(state)Immer 7Michel Weststratebefore adoption467ea5d
line 86why, one step back86 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 87–101why, one step back87 state.assigned_!.set(key, true)88 state.copy_!.set(key, value)89 state.assigned_!.set(key, true)90 }91 return this92 }9394 p.delete = function(key: any): boolean {95 if (!this.has(key)) {96 return false97 }9899 const state: MapState = this[DRAFT_STATE]100 assertUnrevoked(state)101 prepareMapCopy(state)Immer 7Michel Weststratebefore adoption467ea5d
line 102why, one step back102 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 103–110why, one step back103 state.assigned_!.set(key, false)104 state.copy_!.delete(key)105 return true106 }107108 p.clear = function() {109 const state: MapState = this[DRAFT_STATE]110 assertUnrevoked(state)fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 111–119why, one step back111 if (latest(state).size) {112 prepareMapCopy(state)113 markChanged(state)114 state.assigned_ = new Map()115 each(state.base_, key => {116 state.assigned_!.set(key, false)117 })118 state.copy_!.clear()119 }Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 120–249why, one step back120 }121122 p.forEach = function(123 cb: (value: any, key: any, self: any) => void,124 thisArg?: any125 ) {126 const state: MapState = this[DRAFT_STATE]127 latest(state).forEach((_value: any, key: any, _map: any) => {128 cb.call(thisArg, this.get(key), key, this)129 })130 }131132 p.get = function(key: any): any {133 const state: MapState = this[DRAFT_STATE]134 assertUnrevoked(state)135 const value = latest(state).get(key)136 if (state.finalized_ || !isDraftable(value)) {137 return value138 }139 if (value !== state.base_.get(key)) {140 return value // either already drafted or reassigned141 }142 // despite what it looks, this creates a draft only once, see above condition143 const draft = createProxy(state.scope_.immer_, value, state)144 prepareMapCopy(state)145 state.copy_!.set(key, draft)146 return draft147 }148149 p.keys = function(): IterableIterator<any> {150 return latest(this[DRAFT_STATE]).keys()151 }152153 p.values = function(): IterableIterator<any> {154 const iterator = this.keys()155 return {156 [iteratorSymbol]: () => this.values(),157 next: () => {158 const r = iterator.next()159 /* istanbul ignore next */160 if (r.done) return r161 const value = this.get(r.value)162 return {163 done: false,164 value165 }166 }167 } as any168 }169170 p.entries = function(): IterableIterator<[any, any]> {171 const iterator = this.keys()172 return {173 [iteratorSymbol]: () => this.entries(),174 next: () => {175 const r = iterator.next()176 /* istanbul ignore next */177 if (r.done) return r178 const value = this.get(r.value)179 return {180 done: false,181 value: [r.value, value]182 }183 }184 } as any185 }186187 p[iteratorSymbol] = function() {188 return this.entries()189 }190191 return DraftMap192 })(Map)193194 function proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T {195 // @ts-ignore196 return new DraftMap(target, parent)197 }198199 function prepareMapCopy(state: MapState) {200 if (!state.copy_) {201 state.assigned_ = new Map()202 state.copy_ = new Map(state.base_)203 }204 }205206 const DraftSet = (function(_super) {207 __extends(DraftSet, _super)208 // Create class manually, cause #502209 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {210 this[DRAFT_STATE] = {211 type_: ProxyTypeSet,212 parent_: parent,213 scope_: parent ? parent.scope_ : getCurrentScope()!,214 modified_: false,215 finalized_: false,216 copy_: undefined,217 base_: target,218 draft_: this,219 drafts_: new Map(),220 revoked_: false,221 isManual_: false222 } as SetState223 return this224 }225 const p = DraftSet.prototype226227 Object.defineProperty(p, "size", {228 get: function() {229 return latest(this[DRAFT_STATE]).size230 }231 // enumerable: true,232 })233234 p.has = function(value: any): boolean {235 const state: SetState = this[DRAFT_STATE]236 assertUnrevoked(state)237 // bit of trickery here, to be able to recognize both the value, and the draft of its value238 if (!state.copy_) {239 return state.base_.has(value)240 }241 if (state.copy_.has(value)) return true242 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))243 return true244 return false245 }246247 p.add = function(value: any): any {248 const state: SetState = this[DRAFT_STATE]249 assertUnrevoked(state)fix: fixed issue where new items in Set where not recorded after iterating. Fixes #586Michel Weststratebefore adoption169db77
line 250why, one step back250 if (!this.has(value)) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 251why, one step back251 prepareSetCopy(state)Immer 7Michel Weststratebefore adoption467ea5d
line 252why, one step back252 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 253–265why, one step back253 state.copy_!.add(value)254 }255 return this256 }257258 p.delete = function(value: any): any {259 if (!this.has(value)) {260 return false261 }262263 const state: SetState = this[DRAFT_STATE]264 assertUnrevoked(state)265 prepareSetCopy(state)Immer 7Michel Weststratebefore adoption467ea5d
line 266why, one step back266 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 267–277why, one step back267 return (268 state.copy_!.delete(value) ||269 (state.drafts_.has(value)270 ? state.copy_!.delete(state.drafts_.get(value))271 : /* istanbul ignore next */ false)272 )273 }274275 p.clear = function() {276 const state: SetState = this[DRAFT_STATE]277 assertUnrevoked(state)fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 278–282why, one step back278 if (latest(state).size) {279 prepareSetCopy(state)280 markChanged(state)281 state.copy_!.clear()282 }Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 283–345why, one step back283 }284285 p.values = function(): IterableIterator<any> {286 const state: SetState = this[DRAFT_STATE]287 assertUnrevoked(state)288 prepareSetCopy(state)289 return state.copy_!.values()290 }291292 p.entries = function entries(): IterableIterator<[any, any]> {293 const state: SetState = this[DRAFT_STATE]294 assertUnrevoked(state)295 prepareSetCopy(state)296 return state.copy_!.entries()297 }298299 p.keys = function(): IterableIterator<any> {300 return this.values()301 }302303 p[iteratorSymbol] = function() {304 return this.values()305 }306307 p.forEach = function forEach(cb: any, thisArg?: any) {308 const iterator = this.values()309 let result = iterator.next()310 while (!result.done) {311 cb.call(thisArg, result.value, result.value, this)312 result = iterator.next()313 }314 }315316 return DraftSet317 })(Set)318319 function proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T {320 // @ts-ignore321 return new DraftSet(target, parent)322 }323324 function prepareSetCopy(state: SetState) {325 if (!state.copy_) {326 // create drafts for all entries to preserve insertion order327 state.copy_ = new Set()328 state.base_.forEach(value => {329 if (isDraftable(value)) {330 const draft = createProxy(state.scope_.immer_, value, state)331 state.drafts_.set(value, draft)332 state.copy_!.add(draft)333 } else {334 state.copy_!.add(value)335 }336 })337 }338 }339340 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {341 if (state.revoked_) die(3, JSON.stringify(latest(state)))342 }343344 loadPlugin("MapSet", {proxyMap_, proxySet_})345}