I’m working on a (first-time) package that listens for the current editor’s
onDidChange() method, however I’m a little stuck on how to structure the registration and disposal of this listener. Currently, I’m using the default
toggle() method from the first package creation tutorial:
module.exports = MyPackage = subscriptions: null active: false editor: null editorChanges: null activate: -> […] # Register command that toggles this view @subscriptions.add atom.commands.add 'atom-workspace', 'mypackage:toggle': => @toggle() @editor = atom.workspace.getActiveTextEditor() deactivate: -> […] toggle: -> if @active @active = false @editorChanges.dispose() else @active = true @editorChanges = @editor.onDidChange(-> console.log @editor.getCursorBufferPosition().column )
There are a few issues I’m running into. First, the use of
@editorChanges variables feels redundant, despite the fact that I need to save the return of
@editor.onDidChange() so that I can call
.dispose() on it later.
@editor is not recognized in the second to last line when trying to get the cursor position.
How do I structure this package so that I can register and dispose of a callback for the active text editor in a clean manner that allows me to read and manipulate properties of the editor itself inside the callback?