mapset.ts
This file
Path
src/plugins/mapset.ts
304 lines · 6.7 KiB · TypeScript
Last changed by
Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
History of this fileTake it
The walk stopped after 200 commits: lines older than b7fcf7f point at it.
docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
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,docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 17–20why, one step back17 each18} from "../internal"1920export function enableMapSet() {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 21–22why, one step back21 class DraftMap extends Map {22 [DRAFT_STATE]: MapStatedocs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
line 23why, one step back23Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 24–25why, one step back24 constructor(target: AnyMap, parent?: ImmerState) {25 super()docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
line 26why, one step back26 this[DRAFT_STATE] = {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 27why, one step back27 type_: ArchType.Map,docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 28–37why, one step back28 parent_: parent,29 scope_: parent ? parent.scope_ : getCurrentScope()!,30 modified_: false,31 finalized_: false,32 copy_: undefined,33 assigned_: undefined,34 base_: target,35 draft_: this as any,36 isManual_: false,37 revoked_: falseMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 38why, one step back38 }docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 39–40why, one step back39 }40Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 41–43why, one step back41 get size(): number {42 return latest(this[DRAFT_STATE]).size43 }docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
line 44why, one step back44Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 45why, one step back45 has(key: any): boolean {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 46–48why, one step back46 return latest(this[DRAFT_STATE]).has(key)47 }48Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 49why, one step back49 set(key: any, value: any) {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 50–61why, one step back50 const state: MapState = this[DRAFT_STATE]51 assertUnrevoked(state)52 if (!latest(state).has(key) || latest(state).get(key) !== value) {53 prepareMapCopy(state)54 markChanged(state)55 state.assigned_!.set(key, true)56 state.copy_!.set(key, value)57 state.assigned_!.set(key, true)58 }59 return this60 }61Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 62why, one step back62 delete(key: any): boolean {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 63–70why, one step back63 if (!this.has(key)) {64 return false65 }6667 const state: MapState = this[DRAFT_STATE]68 assertUnrevoked(state)69 prepareMapCopy(state)70 markChanged(state)fix: immerjs#876 Ensure empty patch set for atomic set+delete on Map (#878)Julian Scheidbefore adoptione140918
lines 71–75why, one step back71 if (state.base_.has(key)) {72 state.assigned_!.set(key, false)73 } else {74 state.assigned_!.delete(key)75 }docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 76–79why, one step back76 state.copy_!.delete(key)77 return true78 }79Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 80why, one step back80 clear() {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 81–93why, one step back81 const state: MapState = this[DRAFT_STATE]82 assertUnrevoked(state)83 if (latest(state).size) {84 prepareMapCopy(state)85 markChanged(state)86 state.assigned_ = new Map()87 each(state.base_, key => {88 state.assigned_!.set(key, false)89 })90 state.copy_!.clear()91 }92 }93Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 94why, one step back94 forEach(cb: (value: any, key: any, self: any) => void, thisArg?: any) {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 95–100why, one step back95 const state: MapState = this[DRAFT_STATE]96 latest(state).forEach((_value: any, key: any, _map: any) => {97 cb.call(thisArg, this.get(key), key, this)98 })99 }100Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 101why, one step back101 get(key: any): any {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 102–111why, one step back102 const state: MapState = this[DRAFT_STATE]103 assertUnrevoked(state)104 const value = latest(state).get(key)105 if (state.finalized_ || !isDraftable(value)) {106 return value107 }108 if (value !== state.base_.get(key)) {109 return value // either already drafted or reassigned110 }111 // despite what it looks, this creates a draft only once, see above conditionMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 112why, one step back112 const draft = createProxy(value, state)docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 113–117why, one step back113 prepareMapCopy(state)114 state.copy_!.set(key, draft)115 return draft116 }117Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 118why, one step back118 keys(): IterableIterator<any> {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 119–121why, one step back119 return latest(this[DRAFT_STATE]).keys()120 }121Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 122why, one step back122 values(): IterableIterator<any> {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 123–124why, one step back123 const iterator = this.keys()124 return {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 125why, one step back125 [Symbol.iterator]: () => this.values(),docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 126–138why, one step back126 next: () => {127 const r = iterator.next()128 /* istanbul ignore next */129 if (r.done) return r130 const value = this.get(r.value)131 return {132 done: false,133 value134 }135 }136 } as any137 }138Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 139why, one step back139 entries(): IterableIterator<[any, any]> {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 140–141why, one step back140 const iterator = this.keys()141 return {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 142why, one step back142 [Symbol.iterator]: () => this.entries(),docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 143–155why, one step back143 next: () => {144 const r = iterator.next()145 /* istanbul ignore next */146 if (r.done) return r147 const value = this.get(r.value)148 return {149 done: false,150 value: [r.value, value]151 }152 }153 } as any154 }155Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 156why, one step back156 [Symbol.iterator]() {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 157–158why, one step back157 return this.entries()158 }Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 159why, one step back159 }docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 160–172why, one step back160161 function proxyMap_<T extends AnyMap>(target: T, parent?: ImmerState): T {162 // @ts-ignore163 return new DraftMap(target, parent)164 }165166 function prepareMapCopy(state: MapState) {167 if (!state.copy_) {168 state.assigned_ = new Map()169 state.copy_ = new Map(state.base_)170 }171 }172Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 173–176why, one step back173 class DraftSet extends Set {174 [DRAFT_STATE]: SetState175 constructor(target: AnySet, parent?: ImmerState) {176 super()docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
line 177why, one step back177 this[DRAFT_STATE] = {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 178why, one step back178 type_: ArchType.Set,docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 179–188why, one step back179 parent_: parent,180 scope_: parent ? parent.scope_ : getCurrentScope()!,181 modified_: false,182 finalized_: false,183 copy_: undefined,184 base_: target,185 draft_: this,186 drafts_: new Map(),187 revoked_: false,188 isManual_: falseMerge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 189why, one step back189 }docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 190–191why, one step back190 }191Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
lines 192–194why, one step back192 get size(): number {193 return latest(this[DRAFT_STATE]).size194 }docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
line 195why, one step back195Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 196why, one step back196 has(value: any): boolean {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 197–208why, one step back197 const state: SetState = this[DRAFT_STATE]198 assertUnrevoked(state)199 // bit of trickery here, to be able to recognize both the value, and the draft of its value200 if (!state.copy_) {201 return state.base_.has(value)202 }203 if (state.copy_.has(value)) return true204 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))205 return true206 return false207 }208Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 209why, one step back209 add(value: any): any {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 210–219why, one step back210 const state: SetState = this[DRAFT_STATE]211 assertUnrevoked(state)212 if (!this.has(value)) {213 prepareSetCopy(state)214 markChanged(state)215 state.copy_!.add(value)216 }217 return this218 }219Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 220why, one step back220 delete(value: any): any {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 221–236why, one step back221 if (!this.has(value)) {222 return false223 }224225 const state: SetState = this[DRAFT_STATE]226 assertUnrevoked(state)227 prepareSetCopy(state)228 markChanged(state)229 return (230 state.copy_!.delete(value) ||231 (state.drafts_.has(value)232 ? state.copy_!.delete(state.drafts_.get(value))233 : /* istanbul ignore next */ false)234 )235 }236Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 237why, one step back237 clear() {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 238–246why, one step back238 const state: SetState = this[DRAFT_STATE]239 assertUnrevoked(state)240 if (latest(state).size) {241 prepareSetCopy(state)242 markChanged(state)243 state.copy_!.clear()244 }245 }246Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 247why, one step back247 values(): IterableIterator<any> {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 248–253why, one step back248 const state: SetState = this[DRAFT_STATE]249 assertUnrevoked(state)250 prepareSetCopy(state)251 return state.copy_!.values()252 }253Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 254why, one step back254 entries(): IterableIterator<[any, any]> {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 255–260why, one step back255 const state: SetState = this[DRAFT_STATE]256 assertUnrevoked(state)257 prepareSetCopy(state)258 return state.copy_!.entries()259 }260Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 261why, one step back261 keys(): IterableIterator<any> {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 262–264why, one step back262 return this.values()263 }264Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 265why, one step back265 [Symbol.iterator]() {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 266–268why, one step back266 return this.values()267 }268Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 269why, one step back269 forEach(cb: any, thisArg?: any) {docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 270–276why, one step back270 const iterator = this.values()271 let result = iterator.next()272 while (!result.done) {273 cb.call(thisArg, result.value, result.value, this)274 result = iterator.next()275 }276 }Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 277why, one step back277 }docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 278–288why, one step back278 function proxySet_<T extends AnySet>(target: T, parent?: ImmerState): T {279 // @ts-ignore280 return new DraftSet(target, parent)281 }282283 function prepareSetCopy(state: SetState) {284 if (!state.copy_) {285 // create drafts for all entries to preserve insertion order286 state.copy_ = new Set()287 state.base_.forEach(value => {288 if (isDraftable(value)) {Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42
line 289why, one step back289 const draft = createProxy(value, state)docs: add `break` to `switch` block (#769)Greg Venechbefore adoptionb7fcf7f
lines 290–304why, one step back290 state.drafts_.set(value, draft)291 state.copy_!.add(draft)292 } else {293 state.copy_!.add(value)294 }295 })296 }297 }298299 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {300 if (state.revoked_) die(3, JSON.stringify(latest(state)))301 }302303 loadPlugin("MapSet", {proxyMap_, proxySet_})304}