Skip to content

Commit

Permalink
.dispose() all the disposables
Browse files Browse the repository at this point in the history
- Reloading the package (e.g. with https://github.com/cakecatz/atom-hot-package-loader) would cause all of the commands to fire N times, once for each reload
- This applies `CompositeDisposable` on all `.on*` and `.add*` calls to properly dispose all disposables and fix that leakage
  • Loading branch information
jdanbrown committed Apr 25, 2018
1 parent 890c741 commit 63083da
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
11 changes: 6 additions & 5 deletions lib/file-view.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{CompositeDisposable} = require 'atom'
{$$} = require 'atom-space-pen-views'
SymbolsView = require './symbols-view'

Expand All @@ -6,16 +7,16 @@ class FileView extends SymbolsView
initialize: ->
super

@editorsSubscription = atom.workspace.observeTextEditors (editor) =>
disposable = editor.onDidSave =>
@disposables = new CompositeDisposable()

@disposables.add atom.workspace.observeTextEditors (editor) =>
@disposables.add editor.onDidSave =>
f = editor.getPath()
return unless atom.project.contains(f)
@ctagsCache.generateTags(f, true)

editor.onDidDestroy -> disposable.dispose()

destroy: ->
@editorsSubscription.dispose()
@disposables.dispose()
super

viewForItem: ({lineNumber, name, file, pattern}) ->
Expand Down
20 changes: 9 additions & 11 deletions lib/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $ = null

MouseEventWhichDict = {"left click": 1, "middle click": 2, "right click": 3}
module.exports =
disposable: null
disposables: new CompositeDisposable()

config:
disableComplete:
Expand Down Expand Up @@ -50,26 +50,26 @@ module.exports =
@ctagsCache.activate()

@ctagsCache.initTags(atom.project.getPaths(), atom.config.get('atom-ctags.autoBuildTagsWhenActive'))
@disposable = atom.project.onDidChangePaths (paths)=>
@disposables.add atom.project.onDidChangePaths (paths)=>
@ctagsCache.initTags(paths, atom.config.get('atom-ctags.autoBuildTagsWhenActive'))

atom.commands.add 'atom-workspace', 'atom-ctags:rebuild', (e, cmdArgs)=>
@disposables.add atom.commands.add 'atom-workspace', 'atom-ctags:rebuild', (e, cmdArgs)=>
console.error "rebuild: ", e
@ctagsCache.cmdArgs = cmdArgs if Array.isArray(cmdArgs)
@createFileView().rebuild(true)
if t
clearTimeout(t)
t = null

atom.commands.add 'atom-workspace', 'atom-ctags:toggle-project-symbols', =>
@disposables.add atom.commands.add 'atom-workspace', 'atom-ctags:toggle-project-symbols', =>
@createFileView().toggleAll()

atom.commands.add 'atom-text-editor',
@disposables.add atom.commands.add 'atom-text-editor',
'atom-ctags:toggle-file-symbols': => @createFileView().toggle()
'atom-ctags:go-to-declaration': => @createFileView().goto()
'atom-ctags:return-from-declaration': => @createGoBackView().toggle()

atom.workspace.observeTextEditors (editor) =>
@disposables.add atom.workspace.observeTextEditors (editor) =>
editorView = atom.views.getView(editor)
{$} = require 'atom-space-pen-views' unless $
$(editorView).on 'mousedown', (event) =>
Expand All @@ -85,22 +85,19 @@ module.exports =
atom-ctags replaces and enhances the symbols-view package.
Therefore, symbols-view has been disabled."

atom.config.observe 'atom-ctags.disableComplete', =>
@disposables.add atom.config.observe 'atom-ctags.disableComplete', =>
return unless @provider
@provider.disabled = atom.config.get('atom-ctags.disableComplete')

initExtraTagsTime = null
atom.config.observe 'atom-ctags.extraTagFiles', =>
@disposables.add atom.config.observe 'atom-ctags.extraTagFiles', =>
clearTimeout initExtraTagsTime if initExtraTagsTime
initExtraTagsTime = setTimeout((=>
@ctagsCache.initExtraTags(atom.config.get('atom-ctags.extraTagFiles').split(" "))
initExtraTagsTime = null
), 1000)

deactivate: ->
if @disposable?
@disposable.dispose()
@disposable = null

if @fileView?
@fileView.destroy()
Expand All @@ -119,6 +116,7 @@ module.exports =
@goBackView = null

@ctagsCache.deactivate()
@disposables.dispose()

createFileView: ->
unless @fileView?
Expand Down

0 comments on commit 63083da

Please sign in to comment.