Skip to content

Commit 77d2f40

Browse files
authored
fix: Optimizations (#39)
1 parent f29b657 commit 77d2f40

File tree

3 files changed

+56
-59
lines changed

3 files changed

+56
-59
lines changed

lib/minimap-find-and-replace-binding.js

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class MinimapFindAndReplaceBinding {
99
this.fnrVisible = fnrVisible
1010
this.editor = this.minimap.getTextEditor()
1111
this.subscriptions = new CompositeDisposable()
12-
this.decorationsByMarkerId = {}
13-
this.subscriptionsByMarkerId = {}
12+
this.decorationsByMarkerId = new Map()
13+
this.subscriptionsByMarkerId = new Map()
1414

1515
this.layer = this.fnrAPI.resultsMarkerLayerForTextEditor(this.editor)
1616

@@ -23,20 +23,22 @@ class MinimapFindAndReplaceBinding {
2323
this.discoverMarkers()
2424
}
2525

26-
destroy () {
27-
let id
28-
for (id in this.subscriptionsByMarkerId) {
29-
const sub = this.subscriptionsByMarkerId[id]; sub.dispose()
26+
disposeMarkerSubsctiotions () {
27+
const markerSubscriptions = this.subscriptionsByMarkerId.values()
28+
for (const markerSubscription of markerSubscriptions) {
29+
markerSubscription.dispose()
3030
}
31-
for (id in this.decorationsByMarkerId) {
32-
const decoration = this.decorationsByMarkerId[id]; decoration.destroy()
31+
const decorationSubscriotions = this.decorationsByMarkerId.values()
32+
for (const decorationSubscriotion of decorationSubscriotions) {
33+
decorationSubscriotion.destroy()
3334
}
35+
}
3436

37+
destroy () {
3538
this.subscriptions.dispose()
39+
this.clear()
3640
this.minimap = null
3741
this.editor = null
38-
this.decorationsByMarkerId = {}
39-
this.subscriptionsByMarkerId = {}
4042
}
4143

4244
changeVisible (visible) {
@@ -49,23 +51,15 @@ class MinimapFindAndReplaceBinding {
4951
}
5052

5153
clear () {
52-
let id
53-
for (id in this.subscriptionsByMarkerId) {
54-
const sub = this.subscriptionsByMarkerId[id]
55-
sub.dispose()
56-
delete this.subscriptionsByMarkerId[id]
57-
}
58-
59-
for (id in this.decorationsByMarkerId) {
60-
const decoration = this.decorationsByMarkerId[id]
61-
decoration.destroy()
62-
delete this.decorationsByMarkerId[id]
63-
}
54+
this.disposeMarkerSubsctiotions()
55+
this.decorationsByMarkerId.clear()
56+
this.subscriptionsByMarkerId.clear()
6457
}
6558

6659
discoverMarkers () {
6760
setImmediate(() => {
68-
for (const marker of this.layer.getMarkers()) {
61+
const markers = this.layer.getMarkers()
62+
for (const marker of markers) {
6963
this.createDecoration(marker)
7064
}
7165
})
@@ -75,7 +69,7 @@ class MinimapFindAndReplaceBinding {
7569
if (!this.findViewIsVisible()) {
7670
return
7771
}
78-
if (this.decorationsByMarkerId[marker.id]) {
72+
if (this.decorationsByMarkerId.has(marker.id)) {
7973
return
8074
}
8175

@@ -89,15 +83,14 @@ class MinimapFindAndReplaceBinding {
8983
return
9084
}
9185

92-
const {
93-
id,
94-
} = marker
95-
this.decorationsByMarkerId[id] = decoration
96-
this.subscriptionsByMarkerId[id] = decoration.onDidDestroy(() => {
97-
this.subscriptionsByMarkerId[id].dispose()
98-
delete this.decorationsByMarkerId[id]
99-
delete this.subscriptionsByMarkerId[id]
100-
})
86+
const { id } = marker
87+
this.decorationsByMarkerId.set(id, decoration)
88+
this.subscriptionsByMarkerId.set(id,
89+
decoration.onDidDestroy(() => {
90+
this.subscriptionsByMarkerId.get(id).dispose()
91+
this.subscriptionsByMarkerId.delete(id)
92+
}),
93+
)
10194
}
10295

10396
findViewIsVisible () {

lib/minimap-find-and-replace.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ module.exports = {
88
return this.active
99
},
1010

11-
activate (state) {
11+
activate () {
1212
this.active = false
1313
this.fnrVisible = false
14-
this.bindingsById = {}
15-
this.subscriptionsById = {}
14+
this.bindingsById = new Map()
15+
this.subscriptionsById = new Map()
1616
this.subscriptions = new CompositeDisposable()
1717
require('atom-package-deps').install('minimap-find-and-replace')
1818
},
@@ -46,23 +46,23 @@ module.exports = {
4646
MinimapFindAndReplaceBinding = require('./minimap-find-and-replace-binding')
4747
}
4848

49-
const {
50-
id,
51-
} = minimap
49+
const { id } = minimap
5250
const binding = new MinimapFindAndReplaceBinding(minimap, fnr, this.fnrVisible)
53-
this.bindingsById[id] = binding
54-
55-
this.subscriptionsById[id] = minimap.onDidDestroy(() => {
56-
if (this.subscriptionsById[id]) {
57-
this.subscriptionsById[id].dispose()
58-
}
59-
if (this.bindingsById[id]) {
60-
this.bindingsById[id].destroy()
61-
}
62-
63-
delete this.bindingsById[id]
64-
delete this.subscriptionsById[id]
65-
})
51+
this.bindingsById.set(id, binding)
52+
53+
this.subscriptionsById.set(id,
54+
minimap.onDidDestroy(() => {
55+
if (this.subscriptionsById.has(id)) {
56+
this.subscriptionsById.get(id).dispose()
57+
}
58+
if (this.bindingsById.has(id)) {
59+
this.bindingsById.get(id).destroy()
60+
}
61+
62+
this.bindingsById.delete(id)
63+
this.subscriptionsById.delete(id)
64+
}),
65+
)
6666
}))
6767
})
6868
},
@@ -87,25 +87,28 @@ module.exports = {
8787

8888
changeVisible (visible) {
8989
this.fnrVisible = visible
90-
for (const id in this.bindingsById) {
91-
this.bindingsById[id].changeVisible(visible)
90+
const bindings = this.bindingsById.values()
91+
for (const binding of bindings) {
92+
binding.changeVisible(visible)
9293
}
9394
},
9495

9596
deactivatePlugin () {
96-
let id
9797
if (!this.active) {
9898
return
9999
}
100100

101101
this.active = false
102102
this.subscriptions.dispose()
103103

104-
for (id in this.subscriptionsById) {
105-
const sub = this.subscriptionsById[id]; sub.dispose()
104+
const subscriptions = this.subscriptionsById.values()
105+
for (const subscription of subscriptions) {
106+
subscription.dispose()
106107
}
107-
for (id in this.bindingsById) {
108-
const binding = this.bindingsById[id]; binding.destroy()
108+
109+
const bindings = this.bindingsById.values()
110+
for (const binding of bindings) {
111+
binding.destroy()
109112
}
110113

111114
this.bindingsById = {}

styles/minimap-find-and-replace.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
@import "syntax-variables";
77

88
.minimap .search-result {
9+
contain: layout size paint style;
910
background: @syntax-result-marker-color-selected;
1011
}

0 commit comments

Comments
 (0)