auto-save.nvim icon indicating copy to clipboard operation
auto-save.nvim copied to clipboard

Save only after sequence of actions finished, not after each action

Open primeapple opened this issue 2 years ago • 1 comments

Problem:

When I edit text, I do it in a sequence of multiple actions, that I execute rapidly after each other. For example, I delete a word (daw), change a word (caw, after that typing and exiting insert mode), insert the deleted word two lines below. With the current implementation that would save up to 4 times (TextChanged, TextChanged, InsertLeave, TextChanged), depending on my typing speed and debounce_delay. This triggers any external service like Webpack up to 4 times, once per action.

I want it to be only triggered 1 time, once per sequence.

Idea:

Add 3 types of events: ImmediateSave, DeferSave and CancelDeferedSave

  • when DeferSave event (TextChanged, InsertLeave, ...) happens, start a timer t, which will save the file if it finishes
  • when CancelDeferedSave event (InsertEnter, TextChanged, enter, ...) happens, cancel all not yet called timers
  • when ImmediateSave events (BufferLeave, VimClose, ...) happen, save immediately

This makes it so, that actions that occur fast after another will cancel each other out and only the last action gets saved. The Timer would have to be something like 2 seconds.

Sometimes however we need to save immediately, for example when closing/leaving a buffer or closing nvim. So we need another set of events called ImmediateSave.

primeapple avatar Feb 07 '23 13:02 primeapple

Related to #61. I'd be happy with just a plain "dumb" delay for starters but this sounds like a much better solution to the problem.

okuuva avatar Feb 12 '23 14:02 okuuva