Subscribe to any editor/pane changes


#1

Hello,

I am working on a package that would save the state of the workspace (at least the number/order of panes/editors) to be restored later. I’m wondering what the best event(s) to subscribe to for triggering the workspace saving would be.

I would like to update when:

  • An editor is added/removed/moved
  • An editor pane (ie tab pane) is added/removed/moved

I also want to make sure that if I close the whole window, that it doesn’t cause each editor to emit a closed event and be removed from the saved workspace.

Currently I’ve found the following events that may be useful (taken from https://gist.github.com/ardcore/9262498):

  • core:close
  • editor:attached
  • editor:path-changed
  • editor:will-be-removed
  • pane:active-item-changed
  • pane:active-item-title-changed
  • pane:attached
  • pane:became-active
  • pane:became-inactive
  • pane:before-item-destroyed
  • pane:item-added
  • pane:item-moved
  • pane:item-removed
  • pane:removed
  • pane:split-down
  • pane:split-left
  • pane:split-right
  • pane:split-up

But, this seems like more events than I need. Also, the pane events seem to have the more useful events, but I don’t know if there is an analogous method to atom.workspace.eachEditor() to make sure I subscribe to each pane.

Any suggestions for what the best events to use would be, or the best method to use to react to any changes?


#2

Random idea:

Find this from the dom itself. Use lodash to filter out unecessary info. Json serialize it. Unserialize and use for next time?


#3

After reading your post I decided to just listen for events involving the setup/teardown of windows/tabs, which should be a more efficient way of achieving what I need. Thanks for your suggestion!

Sorry for the long delay in replying. My internet went out shortly after I posted this.


#4

I’m not sure how far you are on your package, but if you’re interested, I wrote a package that does basically what you were talking about if you want to hack along. It’s called Save Session.

I ran into this issue as well, but I solved it by listening to a window event and setting a variable called onExit to true then in my window closing listener, I just make sure that onExit is false before saving the data.

    $(window).on 'beforeunload', =>
      @onExit = true

    atom.workspace.observePanes (pane) =>
      pane.onDidRemoveItem =>
        if not @onExit
          @saveBuffers()