       console.error "Error serializing package '#{@name}'", e.stack
 
   deactivate: ->
+    @unsubscribeFromActivationEvents()
     syntax.removeGrammar(grammar) for grammar in @grammars
     syntax.removeProperties(path) for [path] in @scopedProperties
     keymap.remove(path) for [path] in @keymaps

    syntax.addGrammar(grammar) for grammar in @grammars
    syntax.addProperties(path, selector, properties) for [path, selector, properties] in @scopedProperties

    if @deferActivation and not immediate
      @subscribeToActivationEvents()
    else
      @activateNow()


  subscribeToActivationEvents: () ->
    return unless @metadata.activationEvents?
    if _.isArray(@metadata.activationEvents)
      rootView.command(event, @handleActivationEvent) for event in @metadata.activationEvents
    else
      rootView.command(event, selector, @handleActivationEvent) for event, selector of @metadata.activationEvents

  handleActivationEvent: (event) =>
    bubblePathEventHandlers = @disableEventHandlersOnBubblePath(event)
    @activateNow()
    $(event.target).trigger(event)
    @restoreEventHandlersOnBubblePath(bubblePathEventHandlers)
    @unsubscribeFromActivationEvents()

  unsubscribeFromActivationEvents: ->
    if _.isArray(@metadata.activationEvents)
      rootView.off(event, @handleActivationEvent) for event in @metadata.activationEvents
    else
      rootView.off(event, selector, @handleActivationEvent) for event, selector of @metadata.activationEvents

  disableEventHandlersOnBubblePath: (event) ->
    bubblePathEventHandlers = []
