Skip to content

Commit

Permalink
fixes #23; add deactivate handler and properly handle disposables
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhanlon committed Jan 20, 2016
1 parent 5d1a7b3 commit e918290
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/less-than-slash.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@
module.exports =
emptyTags: []

disposable: {}

config:
emptyTags:
type: "string"
default: "!doctype, br, hr, img, input, link, meta, area, base, col, command, embed, keygen, param, source, track, wbr"

deactivate: (state) ->
@disposable[key].dispose() for key in Object.keys @disposable

activate: (state) ->
# Register config change handler to update the empty tags list
atom.config.observe "less-than-slash.emptyTags", (value) =>
@emptyTags = (tag.toLowerCase() for tag in value.split(/\s*[\s,|]+\s*/))

atom.workspace.observeTextEditors (editor) =>
@disposable._root = atom.workspace.observeTextEditors (editor) =>
buffer = editor.getBuffer()
buffer.onDidChange (event) =>
@disposable[buffer.id] = buffer.onDidChange (event) =>
if event.newText == "/"
# Ignore it if its right at the start of a line
if event.newRange.start.column > 0
Expand All @@ -37,6 +42,10 @@ module.exports =
event.newRange.end.row, event.newRange.end.column - 2
], textToInsert

buffer.onDidDestroy (event) =>
@disposable[buffer.id].dispose()
delete @disposable[buffer.id]

# Takes functions that provide the data so we can lazily collect them
onSlash: (getCheckText, getText) ->
checkText = getCheckText()
Expand Down

4 comments on commit e918290

@jerone
Copy link

@jerone jerone commented on e918290 Jan 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the CompositeDisposable class for disposing: http://blog.atom.io/2014/09/16/new-event-subscription-api.html

@mrhanlon
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't familiar with it. I haven't been following Atom development much lately, just providing support for this plugin as needed.

@jerone
Copy link

@jerone jerone commented on e918290 Jan 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current solution works too. CompositeDisposable is just an easy to use wrapper around a Set.

@mrhanlon
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like a much cleaner way to handle it. I'm going to have to deal with the disposables again probably when merging #24, so it probably makes sense to update to using CompositeDisposable then. Thanks!

Please sign in to comment.