Skip to content

Commit

Permalink
fix: use Map for decorationsById
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Dec 29, 2020
1 parent 099dab0 commit 1fc7bc8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
28 changes: 13 additions & 15 deletions lib/mixins/decoration-management.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class DecorationManagement {
* @type {Object}
* @access private
*/
this.decorationsById = {}
this.decorationsById = new Map()
/**
* The decorations stored in an array indexed with their marker id.
* @type {Object}
Expand Down Expand Up @@ -74,12 +74,7 @@ export default class DecorationManagement {
* @return {Array<Decoration>} all the decorations in this `Minimap`
*/
getDecorations () {
const decorations = this.decorationsById
const results = []

for (const id in decorations) { results.push(decorations[id]) }

return results
return this.decorationsById.values()
}

/**
Expand Down Expand Up @@ -173,7 +168,7 @@ export default class DecorationManagement {
* @return {Decoration} the decoration with the given id
*/
decorationForId (id) {
return this.decorationsById[id]
return this.decorationsById.get(id)
}

/**
Expand Down Expand Up @@ -235,8 +230,10 @@ export default class DecorationManagement {
}

const cache = {}
for (const id in this.decorationsById) {
const decoration = this.decorationsById[id]

const decorations = this.decorationsById.values()
for (const decoration of decorations) {

const range = decoration.marker.getScreenRange()
const type = decoration.getProperties().type

Expand Down Expand Up @@ -403,7 +400,7 @@ export default class DecorationManagement {
}

this.decorationsByMarkerId[id].push(decoration)
this.decorationsById[decoration.id] = decoration
this.decorationsById.set(decoration.id, decoration)

if (this.decorationUpdatedSubscriptions[decoration.id] == null) {
this.decorationUpdatedSubscriptions[decoration.id] =
Expand Down Expand Up @@ -527,7 +524,7 @@ export default class DecorationManagement {
const marker = decoration.marker
let subscription

delete this.decorationsById[decoration.id]
this.decorationsById.delete(decoration.id)

subscription = this.decorationUpdatedSubscriptions[decoration.id]
if (subscription != null) { subscription.dispose() }
Expand Down Expand Up @@ -623,11 +620,12 @@ export default class DecorationManagement {
this.decorationDestroyedSubscriptions[id].dispose()
}

for (const id in this.decorationsById) {
this.decorationsById[id].destroy()
const decorationsByIdValues = this.decorationsById.values()
for (const decoration of decorationsByIdValues) {
decoration.destroy()
}

this.decorationsById = {}
this.decorationsById.clear()
this.decorationsByMarkerId = {}
this.decorationMarkerChangedSubscriptions = {}
this.decorationMarkerDestroyedSubscriptions = {}
Expand Down
2 changes: 1 addition & 1 deletion spec/minimap-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ describe('Minimap', () => {
})

it('removes all the previously added decorations', () => {
expect(minimap.decorationsById).toEqual({})
expect(minimap.decorationsById.size).toEqual(0)
expect(minimap.decorationsByMarkerId).toEqual({})
})

Expand Down

0 comments on commit 1fc7bc8

Please sign in to comment.