Refactor consumption code to use Etch update hooks
MODIFY
lib/get-icon-services.js
lib/get-icon-services.js
@@ -12,7 +12,6 @@ class IconServices {
this.emitter = new Emitter()
this.elementIcons = null
this.elementIconDisposables = new CompositeDisposable()
- this.elementIconViews = new Map()
this.fileIcons = DefaultFileIcons
}
@@ -33,7 +32,6 @@ class IconServices {
if (this.elementIconDisposables != null) {
this.elementIconDisposables.dispose()
}
- this.elementIconViews.clear()
if (service) { this.elementIconDisposables = new CompositeDisposable() }
this.elementIcons = service
return this.emitter.emit('did-change')
@@ -47,36 +45,22 @@ class IconServices {
}
}
- getIconClasses (view) {
- let iconClass = ''
+ updateIcon (view) {
if (this.elementIcons) {
- if (!this.elementIconViews.has(view)) {
- this.waitToAttach(view).then(() => {
- const iconDisposable = new CompositeDisposable()
- this.elementIconViews.set(view, iconDisposable)
- this.elementIconDisposables.add(iconDisposable)
- iconDisposable.add(this.elementIcons(view.refs.icon, view.filePath))
- iconDisposable.add(new Disposable(() => {
- this.elementIconViews.delete(view)
- this.elementIconDisposables.delete(view)
- }))
- })
+ if (view.refs && view.refs.icon instanceof Element) {
+ if (view.iconDisposable) {
+ view.iconDisposable.dispose()
+ this.elementIconDisposables.remove(view.iconDisposable)
+ }
+ view.iconDisposable = this.elementIcons(view.refs.icon, view.filePath)
+ this.elementIconDisposables.add(view.iconDisposable)
}
} else {
- iconClass = this.fileIcons.iconClassForPath(view.filePath, 'find-and-replace') || ''
+ let iconClass = this.fileIcons.iconClassForPath(view.filePath, 'find-and-replace') || ''
if (Array.isArray(iconClass)) {
iconClass = iconClass.join(' ')
}
- }
- return iconClass
- }
-
- async waitToAttach(view, delay = 10){
- if (view.refs && view.refs.icon instanceof Element) {
- return Promise.resolve()
- } else {
- await new Promise(done => setTimeout(done, delay))
- return this.waitToAttach(view, delay)
+ view.refs.icon.className = iconClass + ' icon'
}
}
}

MODIFY
lib/project/result-view.js
lib/project/result-view.js
@@ -30,6 +30,7 @@ class ResultView {
this.replacePattern = replacePattern;
this.previewStyle = previewStyle;
etch.initialize(this);
+ getIconServices().updateIcon(this);
}
destroy() {
@@ -75,10 +76,14 @@ class ResultView {
this.contextLineHeight = contextLineHeight;
this.leadingContextLineCount = leadingContextLineCount;
this.trailingContextLineCount = trailingContextLineCount;
- etch.update(this);
+ etch.update(this)
}
}
+ writeAfterUpdate() {
+ getIconServices().updateIcon(this);
+ }
+
render() {
let relativePath = this.filePath;
if (atom.project) {
@@ -94,8 +99,6 @@ class ResultView {
this.selectedMatchIndex === -1
);
- const iconClass = getIconServices().getIconClasses(this)
-
return (
$.li(
{
@@ -113,7 +116,7 @@ class ResultView {
$.span({className: 'disclosure-arrow'}),
$.span({
ref: 'icon',
- className: iconClass + ' icon',
+ className: 'icon',
dataset: {name: path.basename(this.filePath)}
}),
$.span({className: 'path-name bright'},

Refactor consumption code to use Etch update hooks