 
   beforeRemove: ->
     @unsubscribeFromBuffer()
-
-    @constructor.instances--
-    if @constructor.instances is 0
-      @constructor.task.terminate()
-      @constructor.task = null
+    @constructor.destroyTask()
 
   unsubscribeFromBuffer: ->
     @destroyViews()


  spellCheckCurrentGrammar: ->
    grammar = @editorView.getEditor().getGrammar().scopeName
    _.contains atom.config.get('spell-check.grammars'), grammar

  destroyViews: ->
    while view = @views.shift()

  @content: ->
    @div class: 'spell-check'

  @createTask: ->
    @task ?= new Task(require.resolve('./spell-check-handler'))
    if @activeViews?
      @activeViews++
    else
      @activeViews = 1

  @destroyTask: ->
    @activeViews--
    if @activeViews is 0
      @task?.terminate()
      @task = null

  initialize: (@editorView) ->
    @views = []
    @constructor.createTask()

    @subscribe editorView, 'editor:path-changed', @subscribeToBuffer
    @subscribe editorView, 'editor:grammar-changed', @subscribeToBuffer
