Skip to content

Commit 972d06b

Browse files
authored
Merge pull request #739 from atom-minimap/decoration-management
Decoration Management Optimizations
2 parents 4f0e60c + ec8480a commit 972d06b

File tree

2 files changed

+74
-72
lines changed

2 files changed

+74
-72
lines changed

lib/mixins/decoration-management.js

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,41 @@ export default class DecorationManagement {
3131
* @type {Object}
3232
* @access private
3333
*/
34-
this.decorationsById = {}
34+
this.decorationsById = new Map()
3535
/**
3636
* The decorations stored in an array indexed with their marker id.
3737
* @type {Object}
3838
* @access private
3939
*/
40-
this.decorationsByMarkerId = {}
40+
this.decorationsByMarkerId = new Map()
4141
/**
4242
* The subscriptions to the markers `did-change` event indexed using the
4343
* marker id.
4444
* @type {Object}
4545
* @access private
4646
*/
47-
this.decorationMarkerChangedSubscriptions = {}
47+
this.decorationMarkerChangedSubscriptions = new Map()
4848
/**
4949
* The subscriptions to the markers `did-destroy` event indexed using the
5050
* marker id.
5151
* @type {Object}
5252
* @access private
5353
*/
54-
this.decorationMarkerDestroyedSubscriptions = {}
54+
this.decorationMarkerDestroyedSubscriptions = new Map()
5555
/**
5656
* The subscriptions to the decorations `did-change-properties` event
5757
* indexed using the decoration id.
5858
* @type {Object}
5959
* @access private
6060
*/
61-
this.decorationUpdatedSubscriptions = {}
61+
this.decorationUpdatedSubscriptions = new Map()
6262
/**
6363
* The subscriptions to the decorations `did-destroy` event indexed using
6464
* the decoration id.
6565
* @type {Object}
6666
* @access private
6767
*/
68-
this.decorationDestroyedSubscriptions = {}
68+
this.decorationDestroyedSubscriptions = new Map()
6969
}
7070

7171
/**
@@ -74,12 +74,7 @@ export default class DecorationManagement {
7474
* @return {Array<Decoration>} all the decorations in this `Minimap`
7575
*/
7676
getDecorations () {
77-
const decorations = this.decorationsById
78-
const results = []
79-
80-
for (const id in decorations) { results.push(decorations[id]) }
81-
82-
return results
77+
return this.decorationsById.values()
8378
}
8479

8580
/**
@@ -173,7 +168,7 @@ export default class DecorationManagement {
173168
* @return {Decoration} the decoration with the given id
174169
*/
175170
decorationForId (id) {
176-
return this.decorationsById[id]
171+
return this.decorationsById.get(id)
177172
}
178173

179174
/**
@@ -192,9 +187,9 @@ export default class DecorationManagement {
192187

193188
for (let i = 0, len = markers.length; i < len; i++) {
194189
const marker = markers[i]
195-
const decorations = this.decorationsByMarkerId[marker.id]
190+
const decorations = this.decorationsByMarkerId.get(marker.id)
196191

197-
if (decorations != null) {
192+
if (decorations !== undefined) {
198193
decorationsByMarkerId[marker.id] = decorations
199194
}
200195
}
@@ -235,8 +230,10 @@ export default class DecorationManagement {
235230
}
236231

237232
const cache = {}
238-
for (const id in this.decorationsById) {
239-
const decoration = this.decorationsById[id]
233+
234+
const decorations = this.decorationsById.values()
235+
for (const decoration of decorations) {
236+
240237
const range = decoration.marker.getScreenRange()
241238
const type = decoration.getProperties().type
242239

@@ -341,22 +338,22 @@ export default class DecorationManagement {
341338
decorationParams.scope = `.minimap .${cls}`
342339
}
343340

344-
if (this.decorationMarkerDestroyedSubscriptions[id] == null) {
345-
this.decorationMarkerDestroyedSubscriptions[id] =
341+
if (!this.decorationMarkerDestroyedSubscriptions.has(id)) {
342+
this.decorationMarkerDestroyedSubscriptions.set(id,
346343
marker.onDidDestroy(() => {
347344
this.removeAllDecorationsForMarker(marker)
348-
})
345+
}))
349346
}
350347

351-
if (this.decorationMarkerChangedSubscriptions[id] == null) {
352-
this.decorationMarkerChangedSubscriptions[id] =
348+
if (!this.decorationMarkerChangedSubscriptions.has(id)) {
349+
this.decorationMarkerChangedSubscriptions.set(id,
353350
marker.onDidChange((event) => {
354-
const decorations = this.decorationsByMarkerId[id]
351+
const decorations = this.decorationsByMarkerId.get(id)
355352
const screenRange = marker.getScreenRange()
356353

357354
this.invalidateDecorationForScreenRowsCache()
358355

359-
if (decorations != null) {
356+
if (decorations !== undefined) {
360357
for (let i = 0, len = decorations.length; i < len; i++) {
361358
const decoration = decorations[i]
362359
this.emitter.emit('did-change-decoration', {
@@ -393,29 +390,29 @@ export default class DecorationManagement {
393390
end: end
394391
}, 0)
395392
}
396-
})
393+
}))
397394
}
398395

399396
const decoration = new Decoration(marker, this, decorationParams)
400397

401-
if (this.decorationsByMarkerId[id] == null) {
402-
this.decorationsByMarkerId[id] = []
398+
if (!this.decorationsByMarkerId.has(id)) {
399+
this.decorationsByMarkerId.set(id, [])
403400
}
404401

405-
this.decorationsByMarkerId[id].push(decoration)
406-
this.decorationsById[decoration.id] = decoration
402+
this.decorationsByMarkerId.get(id).push(decoration)
403+
this.decorationsById.set(decoration.id, decoration)
407404

408-
if (this.decorationUpdatedSubscriptions[decoration.id] == null) {
409-
this.decorationUpdatedSubscriptions[decoration.id] =
405+
if (!this.decorationUpdatedSubscriptions.has(decoration.id)) {
406+
this.decorationUpdatedSubscriptions.set(decoration.id,
410407
decoration.onDidChangeProperties((event) => {
411408
this.emitDecorationChanges(type, decoration)
412-
})
409+
}))
413410
}
414411

415-
this.decorationDestroyedSubscriptions[decoration.id] =
412+
this.decorationDestroyedSubscriptions.set(decoration.id,
416413
decoration.onDidDestroy(() => {
417414
this.removeDecoration(decoration)
418-
})
415+
}))
419416

420417
this.emitDecorationChanges(type, decoration)
421418
this.emitter.emit('did-add-decoration', {
@@ -527,19 +524,19 @@ export default class DecorationManagement {
527524
const marker = decoration.marker
528525
let subscription
529526

530-
delete this.decorationsById[decoration.id]
527+
this.decorationsById.delete(decoration.id)
531528

532-
subscription = this.decorationUpdatedSubscriptions[decoration.id]
533-
if (subscription != null) { subscription.dispose() }
529+
subscription = this.decorationUpdatedSubscriptions.get(decoration.id)
530+
if (subscription !== undefined) { subscription.dispose() }
534531

535-
subscription = this.decorationDestroyedSubscriptions[decoration.id]
536-
if (subscription != null) { subscription.dispose() }
532+
subscription = this.decorationDestroyedSubscriptions.get(decoration.id)
533+
if (subscription !== undefined) { subscription.dispose() }
537534

538-
delete this.decorationUpdatedSubscriptions[decoration.id]
539-
delete this.decorationDestroyedSubscriptions[decoration.id]
535+
this.decorationUpdatedSubscriptions.delete(decoration.id)
536+
this.decorationDestroyedSubscriptions.delete(decoration.id)
540537

541-
const decorations = this.decorationsByMarkerId[marker.id]
542-
if (!decorations) { return }
538+
const decorations = this.decorationsByMarkerId.get(marker.id)
539+
if (decorations === undefined) { return }
543540

544541
this.emitDecorationChanges(decoration.getProperties().type, decoration)
545542

@@ -568,8 +565,8 @@ export default class DecorationManagement {
568565
removeAllDecorationsForMarker (marker) {
569566
if (marker == null) { return }
570567

571-
const decorations = this.decorationsByMarkerId[marker.id]
572-
if (!decorations) { return }
568+
const decorations = this.decorationsByMarkerId.get(marker.id)
569+
if (decorations === undefined) { return }
573570

574571
for (let i = 0, len = decorations.length; i < len; i++) {
575572
const decoration = decorations[i]
@@ -595,43 +592,48 @@ export default class DecorationManagement {
595592
removedAllMarkerDecorations (marker) {
596593
if (marker == null) { return }
597594

598-
this.decorationMarkerChangedSubscriptions[marker.id].dispose()
599-
this.decorationMarkerDestroyedSubscriptions[marker.id].dispose()
595+
this.decorationMarkerChangedSubscriptions.get(marker.id).dispose()
596+
this.decorationMarkerDestroyedSubscriptions.get(marker.id).dispose()
600597

601-
delete this.decorationsByMarkerId[marker.id]
602-
delete this.decorationMarkerChangedSubscriptions[marker.id]
603-
delete this.decorationMarkerDestroyedSubscriptions[marker.id]
598+
this.decorationsByMarkerId.delete(marker.id)
599+
this.decorationMarkerChangedSubscriptions.delete(marker.id)
600+
this.decorationMarkerDestroyedSubscriptions.delete(marker.id)
604601
}
605602

606603
/**
607604
* Removes all the decorations that was created in the current `Minimap`.
608605
*/
609606
removeAllDecorations () {
610-
for (const id in this.decorationMarkerChangedSubscriptions) {
611-
this.decorationMarkerChangedSubscriptions[id].dispose()
607+
const decorationMarkerChangedSubscriptionsValues = this.decorationMarkerChangedSubscriptions.values()
608+
for (const decoration of decorationMarkerChangedSubscriptionsValues) {
609+
decoration.dispose()
612610
}
613611

614-
for (const id in this.decorationMarkerDestroyedSubscriptions) {
615-
this.decorationMarkerDestroyedSubscriptions[id].dispose()
612+
const decorationMarkerDestroyedSubscriptionsValues = this.decorationMarkerDestroyedSubscriptions.values()
613+
for (const decoration of decorationMarkerDestroyedSubscriptionsValues) {
614+
decoration.dispose()
616615
}
617616

618-
for (const id in this.decorationUpdatedSubscriptions) {
619-
this.decorationUpdatedSubscriptions[id].dispose()
617+
const decorationUpdatedSubscriptionsValues = this.decorationUpdatedSubscriptions.values()
618+
for (const decoration of decorationUpdatedSubscriptionsValues) {
619+
decoration.dispose()
620620
}
621621

622-
for (const id in this.decorationDestroyedSubscriptions) {
623-
this.decorationDestroyedSubscriptions[id].dispose()
622+
const decorationDestroyedSubscriptionsValues = this.decorationDestroyedSubscriptions.values()
623+
for (const decoration of decorationDestroyedSubscriptionsValues) {
624+
decoration.dispose()
624625
}
625626

626-
for (const id in this.decorationsById) {
627-
this.decorationsById[id].destroy()
627+
const decorationsByIdValues = this.decorationsById.values()
628+
for (const decoration of decorationsByIdValues) {
629+
decoration.destroy()
628630
}
629631

630-
this.decorationsById = {}
631-
this.decorationsByMarkerId = {}
632-
this.decorationMarkerChangedSubscriptions = {}
633-
this.decorationMarkerDestroyedSubscriptions = {}
634-
this.decorationUpdatedSubscriptions = {}
635-
this.decorationDestroyedSubscriptions = {}
632+
this.decorationsById.clear()
633+
this.decorationsByMarkerId.clear()
634+
this.decorationMarkerChangedSubscriptions.clear()
635+
this.decorationMarkerDestroyedSubscriptions.clear()
636+
this.decorationUpdatedSubscriptions.clear()
637+
this.decorationDestroyedSubscriptions.clear()
636638
}
637639
}

spec/minimap-spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ describe('Minimap', () => {
336336
})
337337

338338
it('creates a decoration for the given marker', () => {
339-
expect(minimap.decorationsByMarkerId[marker.id]).toBeDefined()
339+
expect(minimap.decorationsByMarkerId.get(marker.id)).toBeDefined()
340340
})
341341

342342
it('creates a change corresponding to the marker range', () => {
@@ -367,7 +367,7 @@ describe('Minimap', () => {
367367
})
368368

369369
it('removes the decoration from the render view', () => {
370-
expect(minimap.decorationsByMarkerId[marker.id]).toBeUndefined()
370+
expect(minimap.decorationsByMarkerId.get(marker.id)).toBeUndefined()
371371
})
372372

373373
it('creates a change corresponding to the marker range', () => {
@@ -382,7 +382,7 @@ describe('Minimap', () => {
382382
})
383383

384384
it('removes the decoration from the render view', () => {
385-
expect(minimap.decorationsByMarkerId[marker.id]).toBeUndefined()
385+
expect(minimap.decorationsByMarkerId.get(marker.id)).toBeUndefined()
386386
})
387387

388388
it('creates a change corresponding to the marker range', () => {
@@ -397,7 +397,7 @@ describe('Minimap', () => {
397397
})
398398

399399
it('removes the decoration from the render view', () => {
400-
expect(minimap.decorationsByMarkerId[marker.id]).toBeUndefined()
400+
expect(minimap.decorationsByMarkerId.get(marker.id)).toBeUndefined()
401401
})
402402

403403
it('creates a change corresponding to the marker range', () => {
@@ -412,8 +412,8 @@ describe('Minimap', () => {
412412
})
413413

414414
it('removes all the previously added decorations', () => {
415-
expect(minimap.decorationsById).toEqual({})
416-
expect(minimap.decorationsByMarkerId).toEqual({})
415+
expect(minimap.decorationsById.size).toEqual(0)
416+
expect(minimap.decorationsByMarkerId.size).toEqual(0)
417417
})
418418

419419
it('prevents the creation of new decorations', () => {

0 commit comments

Comments
 (0)