camspiers/snap

Per directory cache

TyberiusPrime opened this issue · 1 comments

I'm trying to write a per directory cache,
but for some arcane reason that I can't fathom, it reads my outside variable
just fine, but does not store changes to it...
Now my lua is very basic,
but maybe somebody can spot right away what I'm doing wrong?

It starts with cache length = 1
(from the example entry),
then stores, and the cache length goes up to two,
but the next time I start it,
I'm down to 1 again :(.
This is straight in my init.lua.

my_cache_cache = {"shu"} -- just so I see whether it's reading the outside variable
local function my_cache(dir)
  local tbl = snap.get("common.tbl")
  local snap = require("snap")
  local dir = dir or vim.fn.getcwd()
  local function my_cache_inner(producer)
    local function _2_(request)
      coroutine.yield({dir}) -- debug: so we know where we are
      if my_cache_cache[dir] == nil then
        coroutine.yield({"cache length before storing " .. tablelength(my_cache_cache)})
        my_cache_cache[dir] = {"hello", "world"}
        coroutine.yield({"after storing" .. tablelength(my_cache_cache)})
      end
    end
    return _2_
  end
  return my_cache_inner
end
my_producer = snap.get("consumer.fzf")(my_cache()(snap.get("producer.ripgrep.file")))

--my_producer = snap.get("consumer.fzf")(snap.get("consumer.cache")(snap.get("producer.ripgrep.file").args(rg_args)))


snap.register.map(
  {"n"},
  {"<leader>f"},
  function()
    snap.run {
      producer = my_producer,
      select = snap.get "select.file".select,
      multiselect = snap.get "select.file".multiselect
      --	  views = {snap.get'preview.file'}
    }
  end
)

Related: #70