mapset.ts
This file
Path
src/plugins/mapset.ts
329 lines · 7.4 KiB · TypeScript
Last changed by
perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
History of this fileTake it
The walk stopped after 200 commits: lines older than 6555173 point at it.
fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
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 isDraftable,12 createProxy,13 loadPlugin,14 markChanged,15 die,Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 16why, one step back16 ArchType,perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 17–19why, one step back17 each,18 getValue,19 PluginMapSetfix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 20–22why, one step back20} from "../internal"2122export function enableMapSet() {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 23–24why, one step back23 class DraftMap extends Map {24 [DRAFT_STATE]: MapStatefix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 25why, one step back25Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 26–27why, one step back26 constructor(target: AnyMap, parent?: ImmerState) {27 super()fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 28why, one step back28 this[DRAFT_STATE] = {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 29why, one step back29 type_: ArchType.Map,fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 30–38why, one step back30 parent_: parent,31 scope_: parent ? parent.scope_ : getCurrentScope()!,32 modified_: false,33 finalized_: false,34 copy_: undefined,35 assigned_: undefined,36 base_: target,37 draft_: this as any,38 isManual_: false,perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 39–40why, one step back39 revoked_: false,40 callbacks_: []Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 41why, one step back41 }fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 42–43why, one step back42 }43Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 44–46why, one step back44 get size(): number {45 return latest(this[DRAFT_STATE]).size46 }fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 47why, one step back47Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 48why, one step back48 has(key: any): boolean {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 49–51why, one step back49 return latest(this[DRAFT_STATE]).has(key)50 }51Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 52why, one step back52 set(key: any, value: any) {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 53–64why, one step back53 const state: MapState = this[DRAFT_STATE]54 assertUnrevoked(state)55 if (!latest(state).has(key) || latest(state).get(key) !== value) {56 prepareMapCopy(state)57 markChanged(state)58 state.assigned_!.set(key, true)59 state.copy_!.set(key, value)60 state.assigned_!.set(key, true)61 }62 return this63 }64Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 65why, one step back65 delete(key: any): boolean {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 66–73why, one step back66 if (!this.has(key)) {67 return false68 }6970 const state: MapState = this[DRAFT_STATE]71 assertUnrevoked(state)72 prepareMapCopy(state)73 markChanged(state)fix: immerjs#876 Ensure empty patch set for atomic set+delete on Map (#878)Julian Scheidbefore adoptione140918
lines 74–78why, one step back74 if (state.base_.has(key)) {75 state.assigned_!.set(key, false)76 } else {77 state.assigned_!.delete(key)78 }fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 79–82why, one step back79 state.copy_!.delete(key)80 return true81 }82Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 83why, one step back83 clear() {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 84–96why, one step back84 const state: MapState = this[DRAFT_STATE]85 assertUnrevoked(state)86 if (latest(state).size) {87 prepareMapCopy(state)88 markChanged(state)89 state.assigned_ = new Map()90 each(state.base_, key => {91 state.assigned_!.set(key, false)92 })93 state.copy_!.clear()94 }95 }96Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 97why, one step back97 forEach(cb: (value: any, key: any, self: any) => void, thisArg?: any) {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 98–103why, one step back98 const state: MapState = this[DRAFT_STATE]99 latest(state).forEach((_value: any, key: any, _map: any) => {100 cb.call(thisArg, this.get(key), key, this)101 })102 }103Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 104why, one step back104 get(key: any): any {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 105–114why, one step back105 const state: MapState = this[DRAFT_STATE]106 assertUnrevoked(state)107 const value = latest(state).get(key)108 if (state.finalized_ || !isDraftable(value)) {109 return value110 }111 if (value !== state.base_.get(key)) {112 return value // either already drafted or reassigned113 }114 // despite what it looks, this creates a draft only once, see above conditionperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 115why, one step back115 const draft = createProxy(state.scope_, value, state, key)fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 116–120why, one step back116 prepareMapCopy(state)117 state.copy_!.set(key, draft)118 return draft119 }120Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 121why, one step back121 keys(): IterableIterator<any> {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 122–124why, one step back122 return latest(this[DRAFT_STATE]).keys()123 }124Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 125why, one step back125 values(): IterableIterator<any> {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 126–127why, one step back126 const iterator = this.keys()127 return {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 128why, one step back128 [Symbol.iterator]: () => this.values(),fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 129–141why, one step back129 next: () => {130 const r = iterator.next()131 /* istanbul ignore next */132 if (r.done) return r133 const value = this.get(r.value)134 return {135 done: false,136 value137 }138 }139 } as any140 }141Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 142why, one step back142 entries(): IterableIterator<[any, any]> {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 143–144why, one step back143 const iterator = this.keys()144 return {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 145why, one step back145 [Symbol.iterator]: () => this.entries(),fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 146–158why, one step back146 next: () => {147 const r = iterator.next()148 /* istanbul ignore next */149 if (r.done) return r150 const value = this.get(r.value)151 return {152 done: false,153 value: [r.value, value]154 }155 }156 } as any157 }158Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 159why, one step back159 [Symbol.iterator]() {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 160–161why, one step back160 return this.entries()161 }Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 162why, one step back162 }fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 163why, one step back163perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 164–167why, one step back164 function proxyMap_<T extends AnyMap>(165 target: T,166 parent?: ImmerState167 ): [T, MapState] {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 168why, one step back168 // @ts-ignoreperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 169–170why, one step back169 const map = new DraftMap(target, parent)170 return [map as any, map[DRAFT_STATE]]fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 171–179why, one step back171 }172173 function prepareMapCopy(state: MapState) {174 if (!state.copy_) {175 state.assigned_ = new Map()176 state.copy_ = new Map(state.base_)177 }178 }179Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 180–183why, one step back180 class DraftSet extends Set {181 [DRAFT_STATE]: SetState182 constructor(target: AnySet, parent?: ImmerState) {183 super()fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 184why, one step back184 this[DRAFT_STATE] = {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 185why, one step back185 type_: ArchType.Set,fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 186–194why, one step back186 parent_: parent,187 scope_: parent ? parent.scope_ : getCurrentScope()!,188 modified_: false,189 finalized_: false,190 copy_: undefined,191 base_: target,192 draft_: this,193 drafts_: new Map(),194 revoked_: false,perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 195–197why, one step back195 isManual_: false,196 assigned_: undefined,197 callbacks_: []Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 198why, one step back198 }fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 199–200why, one step back199 }200Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 201–203why, one step back201 get size(): number {202 return latest(this[DRAFT_STATE]).size203 }fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 204why, one step back204Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 205why, one step back205 has(value: any): boolean {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 206–217why, one step back206 const state: SetState = this[DRAFT_STATE]207 assertUnrevoked(state)208 // bit of trickery here, to be able to recognize both the value, and the draft of its value209 if (!state.copy_) {210 return state.base_.has(value)211 }212 if (state.copy_.has(value)) return true213 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))214 return true215 return false216 }217Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 218why, one step back218 add(value: any): any {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 219–228why, one step back219 const state: SetState = this[DRAFT_STATE]220 assertUnrevoked(state)221 if (!this.has(value)) {222 prepareSetCopy(state)223 markChanged(state)224 state.copy_!.add(value)225 }226 return this227 }228Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 229why, one step back229 delete(value: any): any {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 230–245why, one step back230 if (!this.has(value)) {231 return false232 }233234 const state: SetState = this[DRAFT_STATE]235 assertUnrevoked(state)236 prepareSetCopy(state)237 markChanged(state)238 return (239 state.copy_!.delete(value) ||240 (state.drafts_.has(value)241 ? state.copy_!.delete(state.drafts_.get(value))242 : /* istanbul ignore next */ false)243 )244 }245Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 246why, one step back246 clear() {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 247–255why, one step back247 const state: SetState = this[DRAFT_STATE]248 assertUnrevoked(state)249 if (latest(state).size) {250 prepareSetCopy(state)251 markChanged(state)252 state.copy_!.clear()253 }254 }255Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 256why, one step back256 values(): IterableIterator<any> {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 257–262why, one step back257 const state: SetState = this[DRAFT_STATE]258 assertUnrevoked(state)259 prepareSetCopy(state)260 return state.copy_!.values()261 }262Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 263why, one step back263 entries(): IterableIterator<[any, any]> {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 264–269why, one step back264 const state: SetState = this[DRAFT_STATE]265 assertUnrevoked(state)266 prepareSetCopy(state)267 return state.copy_!.entries()268 }269Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 270why, one step back270 keys(): IterableIterator<any> {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 271–273why, one step back271 return this.values()272 }273Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 274why, one step back274 [Symbol.iterator]() {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 275–277why, one step back275 return this.values()276 }277Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 278why, one step back278 forEach(cb: any, thisArg?: any) {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 279–285why, one step back279 const iterator = this.values()280 let result = iterator.next()281 while (!result.done) {282 cb.call(thisArg, result.value, result.value, this)283 result = iterator.next()284 }285 }Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 286why, one step back286 }perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 287–290why, one step back287 function proxySet_<T extends AnySet>(288 target: T,289 parent?: ImmerState290 ): [T, SetState] {fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 291why, one step back291 // @ts-ignoreperf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 292–293why, one step back292 const set = new DraftSet(target, parent)293 return [set as any, set[DRAFT_STATE]]fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 294–301why, one step back294 }295296 function prepareSetCopy(state: SetState) {297 if (!state.copy_) {298 // create drafts for all entries to preserve insertion order299 state.copy_ = new Set()300 state.base_.forEach(value => {301 if (isDraftable(value)) {perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
line 302why, one step back302 const draft = createProxy(state.scope_, value, state, value)fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
lines 303–315why, one step back303 state.drafts_.set(value, draft)304 state.copy_!.add(draft)305 } else {306 state.copy_!.add(value)307 }308 })309 }310 }311312 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {313 if (state.revoked_) die(3, JSON.stringify(latest(state)))314 }315perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202
lines 316–328why, one step back316 function fixSetContents(target: ImmerState) {317 // For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #628318 // To preserve insertion order in all cases we then clear the set319 if (target.type_ === ArchType.Set && target.copy_) {320 const copy = new Set(target.copy_)321 target.copy_.clear()322 copy.forEach(value => {323 target.copy_!.add(getValue(value))324 })325 }326 }327328 loadPlugin(PluginMapSet, {proxyMap_, proxySet_, fixSetContents})fix: #785 fix type inference for produce incorrectly inferring promise (#786)hardfistbefore adoption6555173
line 329why, one step back329}