Skip to content
Myr

mapset.ts

This file

Path

src/plugins/mapset.ts

354 lines · 7.9 KiB · TypeScript

Last changed by

fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036

History of this file

Take it

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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,

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 17–18why, one step back
17 each,
18 getValue,

fix: Fix broken array patching and ensure all values in draft Maps/Sets are finalized (#1201)Mark Eriksonbefore adoptionb208d58

lines 19–20why, one step back
19 PluginMapSet,
20 handleCrossReference

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 21–22why, one step back
21} from "../internal"
22

fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036

lines 23–31why, one step back
23declare global {
24 // `Iterator.from` was added in ES2025.25 var Iterator:26 | undefined27 | {28 from<T, TReturn>(iterable: Iterator<T, TReturn>): IterableIterator<T>29 }30}31

chore: added unit test for #897Michel Weststratebefore adoption607b994

line 32why, one step back
32export function enableMapSet() {

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

line 35why, one step back
35

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 40–48why, one step back
40 parent_: parent,
41 scope_: parent ? parent.scope_ : getCurrentScope()!,42 modified_: false,43 finalized_: false,44 copy_: undefined,45 assigned_: undefined,46 base_: target,47 draft_: this as any,48 isManual_: false,

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 49–50why, one step back
49 revoked_: false,
50 callbacks_: []

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

line 51why, one step back
51 }

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 52–53why, one step back
52 }
53

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

lines 54–56why, one step back
54 get size(): number {
55 return latest(this[DRAFT_STATE]).size56 }

chore: added unit test for #897Michel Weststratebefore adoption607b994

line 57why, one step back
57

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 63–70why, one step back
63 const state: MapState = this[DRAFT_STATE]
64 assertUnrevoked(state)65 if (!latest(state).has(key) || latest(state).get(key) !== value) {66 prepareMapCopy(state)67 markChanged(state)68 state.assigned_!.set(key, true)69 state.copy_!.set(key, value)70 state.assigned_!.set(key, true)

fix: Fix broken array patching and ensure all values in draft Maps/Sets are finalized (#1201)Mark Eriksonbefore adoptionb208d58

line 71why, one step back
71 handleCrossReference(state, key, value)

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 72–75why, one step back
72 }
73 return this74 }75

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 77–93why, one step back
77 if (!this.has(key)) {
78 return false79 }8081 const state: MapState = this[DRAFT_STATE]82 assertUnrevoked(state)83 prepareMapCopy(state)84 markChanged(state)85 if (state.base_.has(key)) {86 state.assigned_!.set(key, false)87 } else {88 state.assigned_!.delete(key)89 }90 state.copy_!.delete(key)91 return true92 }93

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 95–107why, one step back
95 const state: MapState = this[DRAFT_STATE]
96 assertUnrevoked(state)97 if (latest(state).size) {98 prepareMapCopy(state)99 markChanged(state)100 state.assigned_ = new Map()101 each(state.base_, key => {102 state.assigned_!.set(key, false)103 })104 state.copy_!.clear()105 }106 }107

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 109–114why, one step back
109 const state: MapState = this[DRAFT_STATE]
110 latest(state).forEach((_value: any, key: any, _map: any) => {111 cb.call(thisArg, this.get(key), key, this)112 })113 }114

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 116–125why, one step back
116 const state: MapState = this[DRAFT_STATE]
117 assertUnrevoked(state)118 const value = latest(state).get(key)119 if (state.finalized_ || !isDraftable(value)) {120 return value121 }122 if (value !== state.base_.get(key)) {123 return value // either already drafted or reassigned124 }125 // despite what it looks, this creates a draft only once, see above condition

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 126why, one step back
126 const draft = createProxy(state.scope_, value, state, key)

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 127–131why, one step back
127 prepareMapCopy(state)
128 state.copy_!.set(key, draft)129 return draft130 }131

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 133–135why, one step back
133 return latest(this[DRAFT_STATE]).keys()
134 }135

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

line 137why, one step back
137 const iterator = this.keys()

fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036

line 138why, one step back
138 return iteratorFrom({

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 139–148why, one step back
139 next: () => {
140 const r = iterator.next()141 /* istanbul ignore next */142 if (r.done) return r143 const value = this.get(r.value)144 return {145 done: false,146 value147 }148 }

fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036

line 149why, one step back
149 })

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 150–151why, one step back
150 }
151

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

line 153why, one step back
153 const iterator = this.keys()

fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036

line 154why, one step back
154 return iteratorFrom({

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 155–164why, one step back
155 next: () => {
156 const r = iterator.next()157 /* istanbul ignore next */158 if (r.done) return r159 const value = this.get(r.value)160 return {161 done: false,162 value: [r.value, value]163 }164 }

fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036

line 165why, one step back
165 })

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 166–167why, one step back
166 }
167

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

fix: improve DraftMap.{entries,values}() compatibility#1228 (#1228)Marcel Jackwerthbefore adoption858d036

lines 171–185why, one step back
171 }
172173 function iteratorFrom<T, TReturn>(174 iterable: Iterator<T, TReturn>175 ): IterableIterator<T> {176 if (typeof Iterator !== "undefined") {177 return Iterator.from(iterable)178 }179180 const iterator: IterableIterator<T> = {181 ...iterable,182 [Symbol.iterator]: () => iterator183 }184185 return iterator

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

line 186why, one step back
186 }

chore: added unit test for #897Michel Weststratebefore adoption607b994

line 187why, one step back
187

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 188–191why, one step back
188 function proxyMap_<T extends AnyMap>(
189 target: T,190 parent?: ImmerState191 ): [T, MapState] {

chore: added unit test for #897Michel Weststratebefore adoption607b994

line 192why, one step back
192 // @ts-ignore

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 193–194why, one step back
193 const map = new DraftMap(target, parent)
194 return [map as any, map[DRAFT_STATE]]

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 195–203why, one step back
195 }
196197 function prepareMapCopy(state: MapState) {198 if (!state.copy_) {199 state.assigned_ = new Map()200 state.copy_ = new Map(state.base_)201 }202 }203

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

lines 204–207why, one step back
204 class DraftSet extends Set {
205 [DRAFT_STATE]: SetState206 constructor(target: AnySet, parent?: ImmerState) {207 super()

chore: added unit test for #897Michel Weststratebefore adoption607b994

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

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 210–218why, one step back
210 parent_: parent,
211 scope_: parent ? parent.scope_ : getCurrentScope()!,212 modified_: false,213 finalized_: false,214 copy_: undefined,215 base_: target,216 draft_: this,217 drafts_: new Map(),218 revoked_: false,

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 219–221why, one step back
219 isManual_: false,
220 assigned_: undefined,221 callbacks_: []

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

line 222why, one step back
222 }

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 223–224why, one step back
223 }
224

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

lines 225–227why, one step back
225 get size(): number {
226 return latest(this[DRAFT_STATE]).size227 }

chore: added unit test for #897Michel Weststratebefore adoption607b994

line 228why, one step back
228

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 230–241why, one step back
230 const state: SetState = this[DRAFT_STATE]
231 assertUnrevoked(state)232 // bit of trickery here, to be able to recognize both the value, and the draft of its value233 if (!state.copy_) {234 return state.base_.has(value)235 }236 if (state.copy_.has(value)) return true237 if (state.drafts_.has(value) && state.copy_.has(state.drafts_.get(value)))238 return true239 return false240 }241

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 243–248why, one step back
243 const state: SetState = this[DRAFT_STATE]
244 assertUnrevoked(state)245 if (!this.has(value)) {246 prepareSetCopy(state)247 markChanged(state)248 state.copy_!.add(value)

fix: Fix broken array patching and ensure all values in draft Maps/Sets are finalized (#1201)Mark Eriksonbefore adoptionb208d58

line 249why, one step back
249 handleCrossReference(state, value, value)

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 250–253why, one step back
250 }
251 return this252 }253

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 255–270why, one step back
255 if (!this.has(value)) {
256 return false257 }258259 const state: SetState = this[DRAFT_STATE]260 assertUnrevoked(state)261 prepareSetCopy(state)262 markChanged(state)263 return (264 state.copy_!.delete(value) ||265 (state.drafts_.has(value)266 ? state.copy_!.delete(state.drafts_.get(value))267 : /* istanbul ignore next */ false)268 )269 }270

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 272–280why, one step back
272 const state: SetState = this[DRAFT_STATE]
273 assertUnrevoked(state)274 if (latest(state).size) {275 prepareSetCopy(state)276 markChanged(state)277 state.copy_!.clear()278 }279 }280

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 282–287why, one step back
282 const state: SetState = this[DRAFT_STATE]
283 assertUnrevoked(state)284 prepareSetCopy(state)285 return state.copy_!.values()286 }287

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 289–294why, one step back
289 const state: SetState = this[DRAFT_STATE]
290 assertUnrevoked(state)291 prepareSetCopy(state)292 return state.copy_!.entries()293 }294

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 296–298why, one step back
296 return this.values()
297 }298

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 300–302why, one step back
300 return this.values()
301 }302

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

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

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 304–310why, one step back
304 const iterator = this.values()
305 let result = iterator.next()306 while (!result.done) {307 cb.call(thisArg, result.value, result.value, this)308 result = iterator.next()309 }310 }

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

line 311why, one step back
311 }

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 312–315why, one step back
312 function proxySet_<T extends AnySet>(
313 target: T,314 parent?: ImmerState315 ): [T, SetState] {

chore: added unit test for #897Michel Weststratebefore adoption607b994

line 316why, one step back
316 // @ts-ignore

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 317–318why, one step back
317 const set = new DraftSet(target, parent)
318 return [set as any, set[DRAFT_STATE]]

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 319–326why, one step back
319 }
320321 function prepareSetCopy(state: SetState) {322 if (!state.copy_) {323 // create drafts for all entries to preserve insertion order324 state.copy_ = new Set()325 state.base_.forEach(value => {326 if (isDraftable(value)) {

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

line 327why, one step back
327 const draft = createProxy(state.scope_, value, state, value)

chore: added unit test for #897Michel Weststratebefore adoption607b994

lines 328–340why, one step back
328 state.drafts_.set(value, draft)
329 state.copy_!.add(draft)330 } else {331 state.copy_!.add(value)332 }333 })334 }335 }336337 function assertUnrevoked(state: any /*ES5State | MapState | SetState*/) {338 if (state.revoked_) die(3, JSON.stringify(latest(state)))339 }340

perf: Rewrite finalization system to use a callback approach instead of tree traversal (#1183)Mark Eriksonbefore adoptiond6c1202

lines 341–353why, one step back
341 function fixSetContents(target: ImmerState) {
342 // For sets we clone before iterating, otherwise we can get in endless loop due to modifying during iteration, see #628343 // To preserve insertion order in all cases we then clear the set344 if (target.type_ === ArchType.Set && target.copy_) {345 const copy = new Set(target.copy_)346 target.copy_.clear()347 copy.forEach(value => {348 target.copy_!.add(getValue(value))349 })350 }351 }352353 loadPlugin(PluginMapSet, {proxyMap_, proxySet_, fixSetContents})

chore: added unit test for #897Michel Weststratebefore adoption607b994

line 354why, one step back
354}