Skip to content
Myr

mapset.ts

This file

Path

src/plugins/mapset.ts

337 lines · 7.8 KiB · TypeScript

Last changed by

Immer 7Michel Weststratebefore adoption467ea5d

History of this file

Take it

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

lines 1–84why, 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,18 die19} 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] = {52 type_: ProxyTypeMap,53 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)83 if (latest(state).get(key) !== value) {84 prepareMapCopy(state)

Immer 7Michel Weststratebefore adoption467ea5d

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

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

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

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

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

Immer 7Michel Weststratebefore adoption467ea5d

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

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

lines 112–243why, one step back
112 state.assigned_ = new Map()
113 return state.copy_!.clear()114 }115116 p.forEach = function(117 cb: (value: any, key: any, self: any) => void,118 thisArg?: any119 ) {120 const state: MapState = this[DRAFT_STATE]121 latest(state).forEach((_value: any, key: any, _map: any) => {122 cb.call(thisArg, this.get(key), key, this)123 })124 }125126 p.get = function(key: any): any {127 const state: MapState = this[DRAFT_STATE]128 assertUnrevoked(state)129 const value = latest(state).get(key)130 if (state.finalized_ || !isDraftable(value)) {131 return value132 }133 if (value !== state.base_.get(key)) {134 return value // either already drafted or reassigned135 }136 // despite what it looks, this creates a draft only once, see above condition137 const draft = createProxy(state.scope_.immer_, value, state)138 prepareMapCopy(state)139 state.copy_!.set(key, draft)140 return draft141 }142143 p.keys = function(): IterableIterator<any> {144 return latest(this[DRAFT_STATE]).keys()145 }146147 p.values = function(): IterableIterator<any> {148 const iterator = this.keys()149 return {150 [iteratorSymbol]: () => this.values(),151 next: () => {152 const r = iterator.next()153 /* istanbul ignore next */154 if (r.done) return r155 const value = this.get(r.value)156 return {157 done: false,158 value159 }160 }161 } as any162 }163164 p.entries = function(): IterableIterator<[any, any]> {165 const iterator = this.keys()166 return {167 [iteratorSymbol]: () => this.entries(),168 next: () => {169 const r = iterator.next()170 /* istanbul ignore next */171 if (r.done) return r172 const value = this.get(r.value)173 return {174 done: false,175 value: [r.value, value]176 }177 }178 } as any179 }180181 p[iteratorSymbol] = function() {182 return this.entries()183 }184185 return DraftMap186 })(Map)187188 function proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T {189 // @ts-ignore190 return new DraftMap(target, parent)191 }192193 function prepareMapCopy(state: MapState) {194 if (!state.copy_) {195 state.assigned_ = new Map()196 state.copy_ = new Map(state.base_)197 }198 }199200 const DraftSet = (function(_super) {201 __extends(DraftSet, _super)202 // Create class manually, cause #502203 function DraftSet(this: any, target: AnySet, parent?: ImmerState) {204 this[DRAFT_STATE] = {205 type_: ProxyTypeSet,206 parent_: parent,207 scope_: parent ? parent.scope_ : getCurrentScope()!,208 modified_: false,209 finalized_: false,210 copy_: undefined,211 base_: target,212 draft_: this,213 drafts_: new Map(),214 revoked_: false,215 isManual_: false216 } as SetState217 return this218 }219 const p = DraftSet.prototype220221 Object.defineProperty(p, "size", {222 get: function() {223 return latest(this[DRAFT_STATE]).size224 }225 // enumerable: true,226 })227228 p.has = function(value: any): boolean {229 const state: SetState = this[DRAFT_STATE]230 assertUnrevoked(state)231 // bit of trickery here, to be able to recognize both the value, and the draft of its value232 if (!state.copy_) {233 return state.base_.has(value)234 }235 if (state.copy_.has(value)) return true236 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))237 return true238 return false239 }240241 p.add = function(value: any): any {242 const state: SetState = this[DRAFT_STATE]243 assertUnrevoked(state)

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

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

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

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

Immer 7Michel Weststratebefore adoption467ea5d

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

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

lines 247–259why, one step back
247 state.copy_!.add(value)
248 }249 return this250 }251252 p.delete = function(value: any): any {253 if (!this.has(value)) {254 return false255 }256257 const state: SetState = this[DRAFT_STATE]258 assertUnrevoked(state)259 prepareSetCopy(state)

Immer 7Michel Weststratebefore adoption467ea5d

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

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

lines 261–272why, one step back
261 return (
262 state.copy_!.delete(value) ||263 (state.drafts_.has(value)264 ? state.copy_!.delete(state.drafts_.get(value))265 : /* istanbul ignore next */ false)266 )267 }268269 p.clear = function() {270 const state: SetState = this[DRAFT_STATE]271 assertUnrevoked(state)272 prepareSetCopy(state)

Immer 7Michel Weststratebefore adoption467ea5d

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

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

lines 274–337why, one step back
274 return state.copy_!.clear()
275 }276277 p.values = function(): IterableIterator<any> {278 const state: SetState = this[DRAFT_STATE]279 assertUnrevoked(state)280 prepareSetCopy(state)281 return state.copy_!.values()282 }283284 p.entries = function entries(): IterableIterator<[any, any]> {285 const state: SetState = this[DRAFT_STATE]286 assertUnrevoked(state)287 prepareSetCopy(state)288 return state.copy_!.entries()289 }290291 p.keys = function(): IterableIterator<any> {292 return this.values()293 }294295 p[iteratorSymbol] = function() {296 return this.values()297 }298299 p.forEach = function forEach(cb: any, thisArg?: any) {300 const iterator = this.values()301 let result = iterator.next()302 while (!result.done) {303 cb.call(thisArg, result.value, result.value, this)304 result = iterator.next()305 }306 }307308 return DraftSet309 })(Set)310311 function proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T {312 // @ts-ignore313 return new DraftSet(target, parent)314 }315316 function prepareSetCopy(state: SetState) {317 if (!state.copy_) {318 // create drafts for all entries to preserve insertion order319 state.copy_ = new Set()320 state.base_.forEach(value => {321 if (isDraftable(value)) {322 const draft = createProxy(state.scope_.immer_, value, state)323 state.drafts_.set(value, draft)324 state.copy_!.add(draft)325 } else {326 state.copy_!.add(value)327 }328 })329 }330 }331332 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {333 if (state.revoked_) die(3, JSON.stringify(latest(state)))334 }335336 loadPlugin("MapSet", {proxyMap_, proxySet_})337}