Skip to content
Myr

mapset.ts

This file

Path

src/plugins/mapset.ts

341 lines · 7.9 KiB · TypeScript

Last changed by

fix: clear map creates invalid patches, fixes #663Michel Weststratebefore adoptionbacc1e0

History of this file

Take it

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 1–17why, one step back
1// 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 back
18 die,
19 each

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 20–83why, one step back
20} 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 back
84 if (!latest(state).has(key) || latest(state).get(key) !== value) {

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 85why, one step back
85 prepareMapCopy(state)

Immer 7Michel Weststratebefore adoption467ea5d

line 86why, one step back
86 markChanged(state)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 87–101why, one step back
87 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 back
102 markChanged(state)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 103–111why, one step back
103 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)111 prepareMapCopy(state)

Immer 7Michel Weststratebefore adoption467ea5d

line 112why, one step back
112 markChanged(state)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 113why, one step back
113 state.assigned_ = new Map()

fix: clear map creates invalid patches, fixes #663Michel Weststratebefore adoptionbacc1e0

lines 114–116why, one step back
114 each(state.base_, key => {
115 state.assigned_!.set(key, false)116 })

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 117–247why, one step back
117 return state.copy_!.clear()
118 }119120 p.forEach = function(121 cb: (value: any, key: any, self: any) => void,122 thisArg?: any123 ) {124 const state: MapState = this[DRAFT_STATE]125 latest(state).forEach((_value: any, key: any, _map: any) => {126 cb.call(thisArg, this.get(key), key, this)127 })128 }129130 p.get = function(key: any): any {131 const state: MapState = this[DRAFT_STATE]132 assertUnrevoked(state)133 const value = latest(state).get(key)134 if (state.finalized_ || !isDraftable(value)) {135 return value136 }137 if (value !== state.base_.get(key)) {138 return value // either already drafted or reassigned139 }140 // despite what it looks, this creates a draft only once, see above condition141 const draft = createProxy(state.scope_.immer_, value, state)142 prepareMapCopy(state)143 state.copy_!.set(key, draft)144 return draft145 }146147 p.keys = function(): IterableIterator<any> {148 return latest(this[DRAFT_STATE]).keys()149 }150151 p.values = function(): IterableIterator<any> {152 const iterator = this.keys()153 return {154 [iteratorSymbol]: () => this.values(),155 next: () => {156 const r = iterator.next()157 /* istanbul ignore next */158 if (r.done) return r159 const value = this.get(r.value)160 return {161 done: false,162 value163 }164 }165 } as any166 }167168 p.entries = function(): IterableIterator<[any, any]> {169 const iterator = this.keys()170 return {171 [iteratorSymbol]: () => this.entries(),172 next: () => {173 const r = iterator.next()174 /* istanbul ignore next */175 if (r.done) return r176 const value = this.get(r.value)177 return {178 done: false,179 value: [r.value, value]180 }181 }182 } as any183 }184185 p[iteratorSymbol] = function() {186 return this.entries()187 }188189 return DraftMap190 })(Map)191192 function proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T {193 // @ts-ignore194 return new DraftMap(target, parent)195 }196197 function prepareMapCopy(state: MapState) {198 if (!state.copy_) {199 state.assigned_ = new Map()200 state.copy_ = new Map(state.base_)201 }202 }203204 const DraftSet = (function(_super) {205 __extends(DraftSet, _super)206 // Create class manually, cause #502207 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {208 this[DRAFT_STATE] = {209 type_: ProxyTypeSet,210 parent_: parent,211 scope_: parent ? parent.scope_ : getCurrentScope()!,212 modified_: false,213 finalized_: false,214 copy_: undefined,215 base_: target,216 draft_: this,217 drafts_: new Map(),218 revoked_: false,219 isManual_: false220 } as SetState221 return this222 }223 const p = DraftSet.prototype224225 Object.defineProperty(p, "size", {226 get: function() {227 return latest(this[DRAFT_STATE]).size228 }229 // enumerable: true,230 })231232 p.has = function(value: any): boolean {233 const state: SetState = this[DRAFT_STATE]234 assertUnrevoked(state)235 // bit of trickery here, to be able to recognize both the value, and the draft of its value236 if (!state.copy_) {237 return state.base_.has(value)238 }239 if (state.copy_.has(value)) return true240 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))241 return true242 return false243 }244245 p.add = function(value: any): any {246 const state: SetState = this[DRAFT_STATE]247 assertUnrevoked(state)

fix: fixed issue where new items in Set where not recorded after iterating. Fixes #586Michel Weststratebefore adoption169db77

line 248why, one step back
248 if (!this.has(value)) {

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

line 249why, one step back
249 prepareSetCopy(state)

Immer 7Michel Weststratebefore adoption467ea5d

line 250why, one step back
250 markChanged(state)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 251–263why, one step back
251 state.copy_!.add(value)
252 }253 return this254 }255256 p.delete = function(value: any): any {257 if (!this.has(value)) {258 return false259 }260261 const state: SetState = this[DRAFT_STATE]262 assertUnrevoked(state)263 prepareSetCopy(state)

Immer 7Michel Weststratebefore adoption467ea5d

line 264why, one step back
264 markChanged(state)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 265–276why, one step back
265 return (
266 state.copy_!.delete(value) ||267 (state.drafts_.has(value)268 ? state.copy_!.delete(state.drafts_.get(value))269 : /* istanbul ignore next */ false)270 )271 }272273 p.clear = function() {274 const state: SetState = this[DRAFT_STATE]275 assertUnrevoked(state)276 prepareSetCopy(state)

Immer 7Michel Weststratebefore adoption467ea5d

line 277why, one step back
277 markChanged(state)

Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8

lines 278–341why, one step back
278 return state.copy_!.clear()
279 }280281 p.values = function(): IterableIterator<any> {282 const state: SetState = this[DRAFT_STATE]283 assertUnrevoked(state)284 prepareSetCopy(state)285 return state.copy_!.values()286 }287288 p.entries = function entries(): IterableIterator<[any, any]> {289 const state: SetState = this[DRAFT_STATE]290 assertUnrevoked(state)291 prepareSetCopy(state)292 return state.copy_!.entries()293 }294295 p.keys = function(): IterableIterator<any> {296 return this.values()297 }298299 p[iteratorSymbol] = function() {300 return this.values()301 }302303 p.forEach = function forEach(cb: any, thisArg?: any) {304 const iterator = this.values()305 let result = iterator.next()306 while (!result.done) {307 cb.call(thisArg, result.value, result.value, this)308 result = iterator.next()309 }310 }311312 return DraftSet313 })(Set)314315 function proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T {316 // @ts-ignore317 return new DraftSet(target, parent)318 }319320 function prepareSetCopy(state: SetState) {321 if (!state.copy_) {322 // create drafts for all entries to preserve insertion order323 state.copy_ = new Set()324 state.base_.forEach(value => {325 if (isDraftable(value)) {326 const draft = createProxy(state.scope_.immer_, value, state)327 state.drafts_.set(value, draft)328 state.copy_!.add(draft)329 } else {330 state.copy_!.add(value)331 }332 })333 }334 }335336 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {337 if (state.revoked_) die(3, JSON.stringify(latest(state)))338 }339340 loadPlugin("MapSet", {proxyMap_, proxySet_})341}