Skip to content

Commit 4922ac8

Browse files
authored
Merge pull request #794 from atom-minimap/type-safe-decorations-api
2 parents 47cb108 + 930ba15 commit 4922ac8

File tree

6 files changed

+4593
-2554
lines changed

6 files changed

+4593
-2554
lines changed

babel.config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"presets": ["babel-preset-atomic"],
3+
"plugins": [],
4+
"exclude": "node_modules/**",
5+
"sourceMap": "inline"
6+
}

lib/decoration.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ export default class Decoration {
8888
return
8989
}
9090

91-
this.markerDestroyDisposable.dispose()
92-
this.markerDestroyDisposable = null
91+
this.markerDestroyDisposable?.dispose?.()
9392
this.destroyed = true
9493
this.emitter.emit("did-destroy")
9594
this.emitter.dispose()
@@ -114,6 +113,9 @@ export default class Decoration {
114113
* @return {Disposable} a disposable to stop listening to the event
115114
*/
116115
onDidChangeProperties(callback) {
116+
if (this.destroyed) {
117+
return
118+
}
117119
return this.emitter.on("did-change-properties", callback)
118120
}
119121

@@ -125,6 +127,9 @@ export default class Decoration {
125127
* @return {Disposable} a disposable to stop listening to the event
126128
*/
127129
onDidDestroy(callback) {
130+
if (this.destroyed) {
131+
return
132+
}
128133
return this.emitter.on("did-destroy", callback)
129134
}
130135

lib/minimap.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
"use strict"
22

3-
import { Emitter, CompositeDisposable } from "atom"
3+
import { Emitter, CompositeDisposable, Disposable } from "atom"
4+
import Decoration from "./decoration"
45
import StableAdapter from "./adapters/stable-adapter"
56
import { editorsMinimaps } from "./main"
67

78
let nextModelId = 1
89

10+
// returned in the decorations API when minimap is destoryed
11+
const disposedDisposable = new Disposable()
12+
disposedDisposable.dispose()
13+
const markerMock = {
14+
onDidDestroy: () => disposedDisposable,
15+
getScreenRange: () => new Range(),
16+
}
17+
const dummyDecoration = new Decoration(markerMock, null, {})
18+
919
/**
1020
* The Minimap class is the underlying model of a <MinimapElement>.
1121
* Most manipulations of the minimap is done through the model.
@@ -1108,60 +1118,52 @@ export default class Minimap {
11081118

11091119
/**
11101120
* get the DecorationManagement API for the current minimapElement
1111-
* @return {DecorationManagement}
1121+
* @return {DecorationManagement | undefined}
11121122
*/
11131123
getDecorationManagement() {
11141124
if (!this.DecorationManagement) {
1115-
if (this.minimapElement) {
1125+
if (this.minimapElement?.DecorationManagement) {
11161126
this.DecorationManagement = this.minimapElement.DecorationManagement
1117-
} else {
1118-
// TODO: find why this becomes null: https://github.com/atom-minimap/minimap/issues/766
1119-
throw new Error(`getDecorationManagement failed.
1120-
this.DecorationManagement: ${JSON.stringify(this.DecorationManagement)}
1121-
this.minimapElement: ${JSON.stringify(this.minimapElement)}
1122-
this.textEditor: ${this.getTextEditor()}
1123-
this: ${JSON.stringify(this)}
1124-
`)
11251127
}
11261128
}
11271129
return this.DecorationManagement
11281130
}
11291131

11301132
// Decoration API duplicated for backward compatibility in the service
11311133
getDecorations() {
1132-
return this.getDecorationManagement().getDecorations()
1134+
return this.getDecorationManagement()?.getDecorations() ?? []
11331135
}
11341136
onDidAddDecoration(...args) {
1135-
return this.getDecorationManagement().onDidAddDecoration(...args)
1137+
return this.getDecorationManagement()?.onDidAddDecoration(...args) ?? disposedDisposable
11361138
}
11371139
onDidRemoveDecoration(...args) {
1138-
return this.getDecorationManagement().onDidRemoveDecoration(...args)
1140+
return this.getDecorationManagement()?.onDidRemoveDecoration(...args) ?? disposedDisposable
11391141
}
11401142
onDidChangeDecorationRange(...args) {
1141-
return this.getDecorationManagement().onDidChangeDecorationRange(...args)
1143+
return this.getDecorationManagement()?.onDidChangeDecorationRange(...args) ?? disposedDisposable
11421144
}
11431145
onDidUpdateDecoration(...args) {
1144-
return this.getDecorationManagement().onDidUpdateDecoration(...args)
1146+
return this.getDecorationManagement()?.onDidUpdateDecoration(...args) ?? disposedDisposable
11451147
}
11461148
decorationForId(...args) {
1147-
return this.getDecorationManagement().decorationForId(...args)
1149+
return this.getDecorationManagement()?.decorationForId(...args) ?? dummyDecoration
11481150
}
11491151
decorationsForScreenRowRange(...args) {
1150-
return this.getDecorationManagement().decorationsForScreenRowRange(...args)
1152+
return this.getDecorationManagement()?.decorationsForScreenRowRange(...args) ?? dummyDecoration
11511153
}
11521154
decorationsByTypeThenRows() {
1153-
return this.getDecorationManagement().decorationsByTypeThenRows()
1155+
return this.getDecorationManagement()?.decorationsByTypeThenRows() ?? dummyDecoration
11541156
}
11551157
decorateMarker(...args) {
1156-
return this.getDecorationManagement().decorateMarker(...args)
1158+
return this.getDecorationManagement()?.decorateMarker(...args) ?? dummyDecoration
11571159
}
11581160
removeDecoration(...args) {
1159-
return this.getDecorationManagement().removeDecoration(...args)
1161+
return this.getDecorationManagement()?.removeDecoration(...args)
11601162
}
11611163
removeAllDecorationsForMarker(...args) {
1162-
return this.getDecorationManagement().removeAllDecorationsForMarker(...args)
1164+
return this.getDecorationManagement()?.removeAllDecorationsForMarker(...args)
11631165
}
11641166
removeAllDecorations() {
1165-
return this.getDecorationManagement().removeAllDecorations()
1167+
return this.getDecorationManagement()?.removeAllDecorations()
11661168
}
11671169
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"devDependencies": {
6969
"@types/atom": "^1.40.10",
7070
"@types/jasmine": "3.6.9",
71+
"babel-preset-atomic": "^4.1.0",
7172
"build-commit": "^0.1.4",
7273
"cross-env": "^7.0.3",
7374
"esdoc": "^1.1.0",
@@ -77,8 +78,7 @@
7778
"jasmine-expect": "^5.0.0",
7879
"npm-check-updates": "latest",
7980
"prettier-config-atomic": "^1.0.1",
80-
"rollup": "2.44.0",
81-
"rollup-plugin-atomic": "^2.1.2",
81+
"rollup-plugin-atomic": "^2.3.1",
8282
"shx": "^0.3.3",
8383
"underscore-plus": "^1.7.0"
8484
},

0 commit comments

Comments
 (0)