Skip to content
Myr

mapset.ts

This file

Path

src/plugins/mapset.ts

339 lines · 8.0 KiB · TypeScript

Last changed by

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

History of this file

Take it

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

lines 1–339why, 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)85 markChanged(state.scope_.immer_, state)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)101 markChanged(state.scope_.immer_, state)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)111 markChanged(state.scope_.immer_, state)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)244 if (state.copy_) {245 state.copy_.add(value)246 } else if (!state.base_.has(value)) {247 prepareSetCopy(state)248 markChanged(state.scope_.immer_, state)249 state.copy_!.add(value)250 }251 return this252 }253254 p.delete = function(value: any): any {255 if (!this.has(value)) {256 return false257 }258259 const state: SetState = this[DRAFT_STATE]260 assertUnrevoked(state)261 prepareSetCopy(state)262 markChanged(state.scope_.immer_, state)263 return (264 state.copy_!.delete(value) ||265 (state.drafts_.has(value)266 ? state.copy_!.delete(state.drafts_.get(value))267 : /* istanbul ignore next */ false)268 )269 }270271 p.clear = function() {272 const state: SetState = this[DRAFT_STATE]273 assertUnrevoked(state)274 prepareSetCopy(state)275 markChanged(state.scope_.immer_, state)276 return state.copy_!.clear()277 }278279 p.values = function(): IterableIterator<any> {280 const state: SetState = this[DRAFT_STATE]281 assertUnrevoked(state)282 prepareSetCopy(state)283 return state.copy_!.values()284 }285286 p.entries = function entries(): IterableIterator<[any, any]> {287 const state: SetState = this[DRAFT_STATE]288 assertUnrevoked(state)289 prepareSetCopy(state)290 return state.copy_!.entries()291 }292293 p.keys = function(): IterableIterator<any> {294 return this.values()295 }296297 p[iteratorSymbol] = function() {298 return this.values()299 }300301 p.forEach = function forEach(cb: any, thisArg?: any) {302 const iterator = this.values()303 let result = iterator.next()304 while (!result.done) {305 cb.call(thisArg, result.value, result.value, this)306 result = iterator.next()307 }308 }309310 return DraftSet311 })(Set)312313 function proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T {314 // @ts-ignore315 return new DraftSet(target, parent)316 }317318 function prepareSetCopy(state: SetState) {319 if (!state.copy_) {320 // create drafts for all entries to preserve insertion order321 state.copy_ = new Set()322 state.base_.forEach(value => {323 if (isDraftable(value)) {324 const draft = createProxy(state.scope_.immer_, value, state)325 state.drafts_.set(value, draft)326 state.copy_!.add(draft)327 } else {328 state.copy_!.add(value)329 }330 })331 }332 }333334 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {335 if (state.revoked_) die(3, JSON.stringify(latest(state)))336 }337338 loadPlugin("MapSet", {proxyMap_, proxySet_})339}