Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
- name: Run StyLua check
uses: JohnnyMorganz/stylua-action@76fd70c03e6340ceaf673366712db9b20560b402 # v5.0.0
with:
Expand Down Expand Up @@ -49,7 +49,7 @@ jobs:
- nightly
steps:
- name: Checkout
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
- name: Set up Neovim
uses: rhysd/action-setup-vim@febef33995d6649302e9d88dda81e071b68f16a7 # v1.6.1
with:
Expand Down
13 changes: 2 additions & 11 deletions lua/peekstack/persist/sessions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,10 @@ end
---@return integer
function M.resolve_root_winid(root_winid)
if root_winid and type(root_winid) == "number" and vim.api.nvim_win_is_valid(root_winid) then
return root_winid
return stack.get_root_winid(root_winid)
end

local winid = vim.api.nvim_get_current_win()
local bufnr = vim.api.nvim_win_get_buf(winid)
if vim.bo[bufnr].filetype == "peekstack-stack" then
local ok, stack_root_winid = pcall(vim.api.nvim_win_get_var, winid, "peekstack_root_winid")
if ok and type(stack_root_winid) == "number" and vim.api.nvim_win_is_valid(stack_root_winid) then
return stack_root_winid
end
end

return winid
return stack.get_root_winid(vim.api.nvim_get_current_win())
end

---Collect the current stack items (truncated to `persist.max_items`).
Expand Down
13 changes: 11 additions & 2 deletions lua/peekstack/persist/store.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ local notify = require("peekstack.util.notify")

local M = {}

local write_counter = 0

---@param path string
---@return string
local function next_tmp_path(path)
write_counter = write_counter + 1
return string.format("%s.%d.%d.tmp", path, vim.uv.hrtime(), write_counter)
end

---@param path string
---@return boolean
local function ensure_parent_dir(path)
Expand Down Expand Up @@ -110,7 +119,7 @@ function M.write(scope, data, opts)
return
end

local tmp_path = path .. ".tmp"
local tmp_path = next_tmp_path(path)
vim.uv.fs_open(tmp_path, "w", 438, function(open_err, fd)
if open_err or not fd then
vim.schedule(function()
Expand Down Expand Up @@ -159,7 +168,7 @@ function M.write_sync(scope, data)
return false
end

local tmp_path = path .. ".tmp"
local tmp_path = next_tmp_path(path)
local fd = vim.uv.fs_open(tmp_path, "w", 438)
if not fd then
notify.warn("Failed to write session data: " .. path)
Expand Down
47 changes: 47 additions & 0 deletions tests/persist_store_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,53 @@ describe("peekstack.persist.store", function()
assert.same(data, store.read_sync(test_scope))
end)

it("allows overlapping async writes to the same scope", function()
local first = {
version = 2,
sessions = {
first = {
items = {},
meta = { created_at = 1, updated_at = 1 },
},
},
}
local second = {
version = 2,
sessions = {
second = {
items = {},
meta = { created_at = 2, updated_at = 2 },
},
},
}

local done = 0
local successes = {}
store.write(test_scope, first, {
on_done = function(ok)
done = done + 1
successes[#successes + 1] = ok
end,
})
store.write(test_scope, second, {
on_done = function(ok)
done = done + 1
successes[#successes + 1] = ok
end,
})

local ok = vim.wait(wait_timeout_ms, function()
return done == 2
end, wait_interval_ms)
assert.is_true(ok, "Timed out waiting for overlapping store writes")
assert.is_true(successes[1])
assert.is_true(successes[2])

local result = read_and_wait(test_scope)
assert.equals(2, result.version)
assert.is_true(result.sessions.first ~= nil or result.sessions.second ~= nil)
end)

it("write_sync returns false when payload cannot be encoded", function()
local ok = store.write_sync(test_scope, {
version = 2,
Expand Down