mapset.ts
This file
Path
src/plugins/mapset.ts
344 lines · 7.9 KiB · TypeScript
Last changed by
chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
History of this fileTake it
Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 1–15why, 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,chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 16why, one step back16 ProxyType,fix: clear map creates invalid patches, fixes #663Michel Weststratebefore adoptionbacc1e0
lines 17–18why, one step back17 die,18 eachMerge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 19–51why, one step back19} from "../internal"2021export function enableMapSet() {22 /* istanbul ignore next */23 var extendStatics = function(d: any, b: any): any {24 extendStatics =25 Object.setPrototypeOf ||26 ({__proto__: []} instanceof Array &&27 function(d, b) {28 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 = d41 }42 d.prototype =43 // @ts-ignore44 ((__.prototype = b.prototype), new __())45 }4647 const DraftMap = (function(_super) {48 __extends(DraftMap, _super)49 // Create class manually, cause #50250 function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {51 this[DRAFT_STATE] = {chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 52why, one step back52 type_: ProxyType.Map,Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 53–82why, one step back53 parent_: parent,54 scope_: parent ? parent.scope_ : getCurrentScope()!,55 modified_: false,56 finalized_: false,57 copy_: undefined,58 assigned_: undefined,59 base_: target,60 draft_: this as any,61 isManual_: false,62 revoked_: false63 } as MapState64 return this65 }66 const p = DraftMap.prototype6768 Object.defineProperty(p, "size", {69 get: function() {70 return latest(this[DRAFT_STATE]).size71 }72 // enumerable: false,73 // configurable: true74 })7576 p.has = function(key: any): boolean {77 return latest(this[DRAFT_STATE]).has(key)78 }7980 p.set = function(key: any, value: any) {81 const state: MapState = this[DRAFT_STATE]82 assertUnrevoked(state)fix: new map keys were not added if value is undefinedMichel Weststratebefore adoption4a1bd65
line 83why, one step back83 if (!latest(state).has(key) || latest(state).get(key) !== value) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 84why, one step back84 prepareMapCopy(state)Immer 7Michel Weststratebefore adoption467ea5d
line 85why, one step back85 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 86–100why, one step back86 state.assigned_!.set(key, true)87 state.copy_!.set(key, value)88 state.assigned_!.set(key, true)89 }90 return this91 }9293 p.delete = function(key: any): boolean {94 if (!this.has(key)) {95 return false96 }9798 const state: MapState = this[DRAFT_STATE]99 assertUnrevoked(state)100 prepareMapCopy(state)Immer 7Michel Weststratebefore adoption467ea5d
line 101why, one step back101 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 102–109why, one step back102 state.assigned_!.set(key, false)103 state.copy_!.delete(key)104 return true105 }106107 p.clear = function() {108 const state: MapState = this[DRAFT_STATE]109 assertUnrevoked(state)fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 110–118why, one step back110 if (latest(state).size) {111 prepareMapCopy(state)112 markChanged(state)113 state.assigned_ = new Map()114 each(state.base_, key => {115 state.assigned_!.set(key, false)116 })117 state.copy_!.clear()118 }Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 119–209why, one step back119 }120121 p.forEach = function(122 cb: (value: any, key: any, self: any) => void,123 thisArg?: any124 ) {125 const state: MapState = this[DRAFT_STATE]126 latest(state).forEach((_value: any, key: any, _map: any) => {127 cb.call(thisArg, this.get(key), key, this)128 })129 }130131 p.get = function(key: any): any {132 const state: MapState = this[DRAFT_STATE]133 assertUnrevoked(state)134 const value = latest(state).get(key)135 if (state.finalized_ || !isDraftable(value)) {136 return value137 }138 if (value !== state.base_.get(key)) {139 return value // either already drafted or reassigned140 }141 // despite what it looks, this creates a draft only once, see above condition142 const draft = createProxy(state.scope_.immer_, value, state)143 prepareMapCopy(state)144 state.copy_!.set(key, draft)145 return draft146 }147148 p.keys = function(): IterableIterator<any> {149 return latest(this[DRAFT_STATE]).keys()150 }151152 p.values = function(): IterableIterator<any> {153 const iterator = this.keys()154 return {155 [iteratorSymbol]: () => this.values(),156 next: () => {157 const r = iterator.next()158 /* istanbul ignore next */159 if (r.done) return r160 const value = this.get(r.value)161 return {162 done: false,163 value164 }165 }166 } as any167 }168169 p.entries = function(): IterableIterator<[any, any]> {170 const iterator = this.keys()171 return {172 [iteratorSymbol]: () => this.entries(),173 next: () => {174 const r = iterator.next()175 /* istanbul ignore next */176 if (r.done) return r177 const value = this.get(r.value)178 return {179 done: false,180 value: [r.value, value]181 }182 }183 } as any184 }185186 p[iteratorSymbol] = function() {187 return this.entries()188 }189190 return DraftMap191 })(Map)192193 function proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T {194 // @ts-ignore195 return new DraftMap(target, parent)196 }197198 function prepareMapCopy(state: MapState) {199 if (!state.copy_) {200 state.assigned_ = new Map()201 state.copy_ = new Map(state.base_)202 }203 }204205 const DraftSet = (function(_super) {206 __extends(DraftSet, _super)207 // Create class manually, cause #502208 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {209 this[DRAFT_STATE] = {chore: reduce size by using TS const enum (#812)houfengbefore adoptionb1fac9d
line 210why, one step back210 type_: ProxyType.Set,Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 211–248why, one step back211 parent_: parent,212 scope_: parent ? parent.scope_ : getCurrentScope()!,213 modified_: false,214 finalized_: false,215 copy_: undefined,216 base_: target,217 draft_: this,218 drafts_: new Map(),219 revoked_: false,220 isManual_: false221 } as SetState222 return this223 }224 const p = DraftSet.prototype225226 Object.defineProperty(p, "size", {227 get: function() {228 return latest(this[DRAFT_STATE]).size229 }230 // enumerable: true,231 })232233 p.has = function(value: any): boolean {234 const state: SetState = 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)239 }240 if (state.copy_.has(value)) return true241 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))242 return true243 return false244 }245246 p.add = function(value: any): any {247 const state: SetState = this[DRAFT_STATE]248 assertUnrevoked(state)fix: fixed issue where new items in Set where not recorded after iterating. Fixes #586Michel Weststratebefore adoption169db77
line 249why, one step back249 if (!this.has(value)) {Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
line 250why, one step back250 prepareSetCopy(state)Immer 7Michel Weststratebefore adoption467ea5d
line 251why, one step back251 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 252–264why, one step back252 state.copy_!.add(value)253 }254 return this255 }256257 p.delete = function(value: any): any {258 if (!this.has(value)) {259 return false260 }261262 const state: SetState = this[DRAFT_STATE]263 assertUnrevoked(state)264 prepareSetCopy(state)Immer 7Michel Weststratebefore adoption467ea5d
line 265why, one step back265 markChanged(state)Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 266–276why, one step back266 return (267 state.copy_!.delete(value) ||268 (state.drafts_.has(value)269 ? state.copy_!.delete(state.drafts_.get(value))270 : /* istanbul ignore next */ false)271 )272 }273274 p.clear = function() {275 const state: SetState = this[DRAFT_STATE]276 assertUnrevoked(state)fix: Clearing empty Set&Map should be noop (#682). Fixes #680Björgvin Ragnarssonbefore adoption33a305b
lines 277–281why, one step back277 if (latest(state).size) {278 prepareSetCopy(state)279 markChanged(state)280 state.copy_!.clear()281 }Merge pull request #536 from immerjs/multi-bundleMichel Weststratebefore adoption0d87fc8
lines 282–344why, one step back282 }283284 p.values = function(): IterableIterator<any> {285 const state: SetState = this[DRAFT_STATE]286 assertUnrevoked(state)287 prepareSetCopy(state)288 return state.copy_!.values()289 }290291 p.entries = function entries(): IterableIterator<[any, any]> {292 const state: SetState = this[DRAFT_STATE]293 assertUnrevoked(state)294 prepareSetCopy(state)295 return state.copy_!.entries()296 }297298 p.keys = function(): IterableIterator<any> {299 return this.values()300 }301302 p[iteratorSymbol] = function() {303 return this.values()304 }305306 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()312 }313 }314315 return DraftSet316 })(Set)317318 function proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T {319 // @ts-ignore320 return new DraftSet(target, parent)321 }322323 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 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {340 if (state.revoked_) die(3, JSON.stringify(latest(state)))341 }342343 loadPlugin("MapSet", {proxyMap_, proxySet_})344}