Skip to content
Myr

map.ts

This file

Path

src/map.ts

187 lines · 4.0 KiB · TypeScript

Last changed by

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

History of this file

Take it

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 1–14why, one step back
1import {
2 __extends,3 ImmerBaseState,4 ProxyType,5 AnyMap,6 Drafted,7 ImmerState,8 DRAFT_STATE,9 ImmerScope,10 latest,11 assertUnrevoked,12 isDraftable,13 iteratorSymbol14} from "./internal"

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 15–24why, one step back
15
16export interface MapState extends ImmerBaseState {17 type: ProxyType.Map18 copy: AnyMap | undefined19 assigned: Map<any, boolean> | undefined20 base: AnyMap21 revoked: boolean22 draft: Drafted<AnyMap, MapState>23}24

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 25–32why, one step back
25const DraftMap = (function(_super) {
26 if (!_super) {27 /* istanbul ignore next */28 throw new Error("Map is not polyfilled")29 }30 __extends(DraftMap, _super)31 // Create class manually, cause #50232 function DraftMap(this: any, target: AnyMap, parent?: ImmerState): any {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 33–41why, one step back
33 this[DRAFT_STATE] = {
34 type: ProxyType.Map,35 parent,36 scope: parent ? parent.scope : ImmerScope.current!,37 modified: false,38 finalized: false,39 copy: undefined,40 assigned: undefined,41 base: target,

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 42why, one step back
42 draft: this as any,

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 43–45why, one step back
43 isManual: false,
44 revoked: false45 }

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 46why, one step back
46 return this

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 47why, one step back
47 }

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 48why, one step back
48 const p = DraftMap.prototype

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 49why, one step back
49

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 50–57why, one step back
50 // TODO: smaller build size if we create a util for Object.defineProperty
51 Object.defineProperty(p, "size", {52 get: function() {53 return latest(this[DRAFT_STATE]).size54 },55 enumerable: true,56 configurable: true57 })

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 58why, one step back
58

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 59why, one step back
59 p.has = function(key: any): boolean {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 60–62why, one step back
60 return latest(this[DRAFT_STATE]).has(key)
61 }62

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 63why, one step back
63 p.set = function(key: any, value: any) {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 64–75why, one step back
64 const state = this[DRAFT_STATE]
65 assertUnrevoked(state)66 if (latest(state).get(key) !== value) {67 prepareCopy(state)68 state.scope.immer.markChanged(state)69 state.assigned!.set(key, true)70 state.copy!.set(key, value)71 state.assigned!.set(key, true)72 }73 return this74 }75

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 76why, one step back
76 p.delete = function(key: any): boolean {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 77–89why, one step back
77 if (!this.has(key)) {
78 return false79 }8081 const state = this[DRAFT_STATE]82 assertUnrevoked(state)83 prepareCopy(state)84 state.scope.immer.markChanged(state)85 state.assigned!.set(key, false)86 state.copy!.delete(key)87 return true88 }89

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 90why, one step back
90 p.clear = function() {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 91–101why, one step back
91 const state = this[DRAFT_STATE]
92 assertUnrevoked(state)93 prepareCopy(state)94 state.scope.immer.markChanged(state)95 state.assigned = new Map()96 for (const key of latest(state).keys()) {97 state.assigned.set(key, false)98 }99 return state.copy!.clear()100 }101

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 102–105why, one step back
102 p.forEach = function(
103 cb: (value: any, key: any, self: any) => void,104 thisArg?: any105 ) {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

line 106why, one step back
106 const state = this[DRAFT_STATE]

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 107why, one step back
107 latest(state).forEach((_value: any, key: any, _map: any) => {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 108–111why, one step back
108 cb.call(thisArg, this.get(key), key, this)
109 })110 }111

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 112why, one step back
112 p.get = function(key: any): any {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 113–128why, one step back
113 const state = this[DRAFT_STATE]
114 assertUnrevoked(state)115 const value = latest(state).get(key)116 if (state.finalized || !isDraftable(value)) {117 return value118 }119 if (value !== state.base.get(key)) {120 return value // either already drafted or reassigned121 }122 // despite what it looks, this creates a draft only once, see above condition123 const draft = state.scope.immer.createProxy(value, state)124 prepareCopy(state)125 state.copy!.set(key, draft)126 return draft127 }128

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 129why, one step back
129 p.keys = function(): IterableIterator<any> {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 130–132why, one step back
130 return latest(this[DRAFT_STATE]).keys()
131 }132

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 133why, one step back
133 p.values = function(): IterableIterator<any> {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 134–149why, one step back
134 const iterator = this.keys()
135 return {136 [iteratorSymbol]: () => this.values(),137 next: () => {138 const r = iterator.next()139 /* istanbul ignore next */140 if (r.done) return r141 const value = this.get(r.value)142 return {143 done: false,144 value145 }146 }147 } as any148 }149

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 150why, one step back
150 p.entries = function(): IterableIterator<[any, any]> {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 151–166why, one step back
151 const iterator = this.keys()
152 return {153 [iteratorSymbol]: () => this.entries(),154 next: () => {155 const r = iterator.next()156 /* istanbul ignore next */157 if (r.done) return r158 const value = this.get(r.value)159 return {160 done: false,161 value: [r.value, value]162 }163 }164 } as any165 }166

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

line 167why, one step back
167 p[iteratorSymbol] = function() {

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 168–170why, one step back
168 return this.entries()
169 }170

fix: Map and Set instantions won't work on certain environments, fixes #502Michel Weststratebefore adoption9f4711a

lines 171–178why, one step back
171 return DraftMap
172})(Map)173174export function proxyMap<T extends AnyMap>(175 target: T,176 parent?: ImmerState177): T & {[DRAFT_STATE]: MapState} {178 // @ts-ignore

feat: rewrote Map and Set implementation, code base cleanupMichel Weststratebefore adoption1913fe8

lines 179–187why, one step back
179 return new DraftMap(target, parent)
180}181182function prepareCopy(state: MapState) {183 if (!state.copy) {184 state.assigned = new Map()185 state.copy = new Map(state.base)186 }187}