Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than fe8f589 point at it.

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 1–15why, 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 isDraftable,12 createProxy,13 loadPlugin,14 markChanged,15 die,

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 16why, one step back
16 ArchType,

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 17–20why, one step back
17 each
18} from "../internal"1920export function enableMapSet() {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 21–22why, one step back
21 class DraftMap extends Map {
22 [DRAFT_STATE]: MapState

chore(comments): update comments (#727)郗银before adoptionfe8f589

line 23why, one step back
23

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 24–25why, one step back
24 constructor(target: AnyMap, parent?: ImmerState) {
25 super()

chore(comments): update comments (#727)郗银before adoptionfe8f589

line 26why, one step back
26 this[DRAFT_STATE] = {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 27why, one step back
27 type_: ArchType.Map,

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 28–37why, one step back
28 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_: false

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 38why, one step back
38 }

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 39–40why, one step back
39 }
40

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 41–43why, one step back
41 get size(): number {
42 return latest(this[DRAFT_STATE]).size43 }

chore(comments): update comments (#727)郗银before adoptionfe8f589

line 44why, one step back
44

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 45why, one step back
45 has(key: any): boolean {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 46–48why, one step back
46 return latest(this[DRAFT_STATE]).has(key)
47 }48

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 49why, one step back
49 set(key: any, value: any) {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 50–61why, one step back
50 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 }61

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 62why, one step back
62 delete(key: any): boolean {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 63–70why, one step back
63 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 back
71 if (state.base_.has(key)) {
72 state.assigned_!.set(key, false)73 } else {74 state.assigned_!.delete(key)75 }

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 76–79why, one step back
76 state.copy_!.delete(key)
77 return true78 }79

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 80why, one step back
80 clear() {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 81–93why, one step back
81 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 }93

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 94why, one step back
94 forEach(cb: (value: any, key: any, self: any) => void, thisArg?: any) {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 95–100why, one step back
95 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 }100

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 101why, one step back
101 get(key: any): any {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 102–111why, one step back
102 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 condition

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 112why, one step back
112 const draft = createProxy(value, state)

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 113–117why, one step back
113 prepareMapCopy(state)
114 state.copy_!.set(key, draft)115 return draft116 }117

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 118why, one step back
118 keys(): IterableIterator<any> {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 119–121why, one step back
119 return latest(this[DRAFT_STATE]).keys()
120 }121

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 122why, one step back
122 values(): IterableIterator<any> {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 123–124why, one step back
123 const iterator = this.keys()
124 return {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 125why, one step back
125 [Symbol.iterator]: () => this.values(),

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 126–138why, one step back
126 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 }138

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 139why, one step back
139 entries(): IterableIterator<[any, any]> {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 140–141why, one step back
140 const iterator = this.keys()
141 return {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 142why, one step back
142 [Symbol.iterator]: () => this.entries(),

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 143–155why, one step back
143 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 }155

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 156why, one step back
156 [Symbol.iterator]() {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 157–158why, one step back
157 return this.entries()
158 }

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 159why, one step back
159 }

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 160–172why, one step back
160
161 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 }172

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 173–176why, one step back
173 class DraftSet extends Set {
174 [DRAFT_STATE]: SetState175 constructor(target: AnySet, parent?: ImmerState) {176 super()

chore(comments): update comments (#727)郗银before adoptionfe8f589

line 177why, one step back
177 this[DRAFT_STATE] = {

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 178why, one step back
178 type_: ArchType.Set,

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 179–188why, one step back
179 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_: false

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 189why, one step back
189 }

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 190–191why, one step back
190 }
191

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

lines 192–194why, one step back
192 get size(): number {
193 return latest(this[DRAFT_STATE]).size194 }

chore(comments): update comments (#727)郗银before adoptionfe8f589

line 195why, one step back
195

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 196why, one step back
196 has(value: any): boolean {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 197–208why, one step back
197 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 }208

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 209why, one step back
209 add(value: any): any {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 210–219why, one step back
210 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 }219

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 220why, one step back
220 delete(value: any): any {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 221–236why, one step back
221 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 }236

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 237why, one step back
237 clear() {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 238–246why, one step back
238 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 }246

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 247why, one step back
247 values(): IterableIterator<any> {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 248–253why, one step back
248 const state: SetState = this[DRAFT_STATE]
249 assertUnrevoked(state)250 prepareSetCopy(state)251 return state.copy_!.values()252 }253

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 254why, one step back
254 entries(): IterableIterator<[any, any]> {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 255–260why, one step back
255 const state: SetState = this[DRAFT_STATE]
256 assertUnrevoked(state)257 prepareSetCopy(state)258 return state.copy_!.entries()259 }260

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 261why, one step back
261 keys(): IterableIterator<any> {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 262–264why, one step back
262 return this.values()
263 }264

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 265why, one step back
265 [Symbol.iterator]() {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 266–268why, one step back
266 return this.values()
267 }268

Merge pull request #1028 from immerjs/immer-10Michel Weststratebefore adoption2ef9a42

line 269why, one step back
269 forEach(cb: any, thisArg?: any) {

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 270–276why, one step back
270 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 back
277 }

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 278–288why, one step back
278 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 back
289 const draft = createProxy(value, state)

chore(comments): update comments (#727)郗银before adoptionfe8f589

lines 290–304why, one step back
290 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}