Skip to content
Myr

map.ts

This file

Path

src/map.ts

184 lines · 4.0 KiB · TypeScript

Last changed by

feat: general typing improvements for TS 3.7 and upMichel Weststratebefore adoptionf930ce1

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–98why, 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 return state.copy!.clear()97 }98

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

lines 99–102why, one step back
99 p.forEach = function(
100 cb: (value: any, key: any, self: any) => void,101 thisArg?: any102 ) {

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

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

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

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

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

lines 105–108why, one step back
105 cb.call(thisArg, this.get(key), key, this)
106 })107 }108

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

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

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

lines 110–125why, one step back
110 const state = this[DRAFT_STATE]
111 assertUnrevoked(state)112 const value = latest(state).get(key)113 if (state.finalized || !isDraftable(value)) {114 return value115 }116 if (value !== state.base.get(key)) {117 return value // either already drafted or reassigned118 }119 // despite what it looks, this creates a draft only once, see above condition120 const draft = state.scope.immer.createProxy(value, state)121 prepareCopy(state)122 state.copy!.set(key, draft)123 return draft124 }125

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

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

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

lines 127–129why, one step back
127 return latest(this[DRAFT_STATE]).keys()
128 }129

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

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

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

lines 131–146why, one step back
131 const iterator = this.keys()
132 return {133 [iteratorSymbol]: () => this.values(),134 next: () => {135 const r = iterator.next()136 /* istanbul ignore next */137 if (r.done) return r138 const value = this.get(r.value)139 return {140 done: false,141 value142 }143 }144 } as any145 }146

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

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

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

lines 148–163why, one step back
148 const iterator = this.keys()
149 return {150 [iteratorSymbol]: () => this.entries(),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 value: [r.value, value]159 }160 }161 } as any162 }163

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

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

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

lines 165–167why, one step back
165 return this.entries()
166 }167

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

lines 168–175why, one step back
168 return DraftMap
169})(Map)170171export function proxyMap<T extends AnyMap>(172 target: T,173 parent?: ImmerState174): T & {[DRAFT_STATE]: MapState} {175 // @ts-ignore

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

lines 176–184why, one step back
176 return new DraftMap(target, parent)
177}178179function prepareCopy(state: MapState) {180 if (!state.copy) {181 state.assigned = new Map()182 state.copy = new Map(state.base)183 }184}