 HISTORY_URI = 'atom://git/view-history'
 CHANGES_URI = 'atom://git/view-changes'
 
+changesView = null
+historyView = null
+serializedState = {}
+
 module.exports = GitExperiment =
   subscriptions: null
-  historyView: null
-  changesView: null
   state: null
 
   activate: (@state) ->


createHistoryView = (state) ->
 HistoryView ?= require './history/history-view'
 view = new HistoryView
 view.initialize(state)
 view

createChangesView = (state) ->
  ChangesView ?= require './changes/changes-view'
  view = new ChangesView
  view.initialize(state)
  view

atom.deserializers.add
  name: 'GitHistoryView'

    process.nextTick =>
      @subscriptions.add atom.workspace.onWillDestroyPaneItem (pane) =>
        switch pane.item
          when historyView
            serializedState.history = historyView.serialize()
          when changesView
            serializedState.changes = changesView.serialize()

      @subscriptions.add atom.workspace.registerOpener (filePath) =>
        switch filePath
          when HISTORY_URI
            historyView or= if @state.history?
              console.log 'deserialized'
              atom.deserializers.deserialize(@state.history)
            else
              console.log 'created'
              createHistoryView(uri: HISTORY_URI)
          when CHANGES_URI
            changesView or= if @state.changes?
              atom.deserializers.deserialize(@state.changes)
            else
              createChangesView(uri: CHANGES_URI)

  serialize: ->
    serializedState

  deactivate: ->
    @subscriptions?.dispose()

  openHistoryView: ->
    atom.workspace.open(HISTORY_URI)
