Module: Canopus::Workspace::AutoSavable

Defined in:
lib/canopus/workspace/auto_savable.rb

Instance Method Summary collapse

Instance Method Details

#poll_auto_save(now: Process.clock_gettime(Process::CLOCK_MONOTONIC)) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/canopus/workspace/auto_savable.rb', line 5

def poll_auto_save(now: Process.clock_gettime(Process::CLOCK_MONOTONIC))
  return false if @closed
  mode = @settings["auto_save"]
  delay = @settings["auto_save_delay"]
  current = editor&.buffer
  config = [mode, delay]
  if @auto_save_config != config
    reset_auto_save
    @auto_save_config = config
    @auto_save_focus_buffer = current
    return false unless mode == "after_delay"
  end

  case mode
  when "on_focus_change"
    previous, @auto_save_focus_buffer = @auto_save_focus_buffer, current
    return false unless previous && !previous.equal?(current)
    saved = false
    auto_save_sources(previous).each { |buffer| saved = auto_save_buffer(buffer) || saved }
    saved
  when "after_delay"
    buffers = auto_save_buffers
    @auto_save_pending ||= {}.compare_by_identity
    @auto_save_pending.delete_if { |buffer, _| !buffers.include?(buffer) }
    attempted = false
    buffers.each do |buffer|
      unless auto_save_eligible?(buffer)
        @auto_save_pending.delete(buffer)
        next
      end
      version = buffer.version
      state = @auto_save_pending[buffer]
      if !state || state[0] != version
        @auto_save_pending[buffer] = [version, now + delay / 1_000.0, false]
        next
      end
      next if state[2] || now < state[1]
      state[2] = true
      attempted = auto_save_buffer(buffer) || attempted
    end
    attempted
  else
    reset_auto_save
    @auto_save_config = config
    @auto_save_focus_buffer = current
    false
  end
end

#reset_auto_save ⇒ Object



54
55
56
57
58
# File 'lib/canopus/workspace/auto_savable.rb', line 54

def reset_auto_save
  @auto_save_pending&.clear
  @auto_save_focus_buffer = nil
  nil
end