diff options
| author | William Boman <william@redwill.se> | 2023-03-14 04:48:03 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-14 04:48:03 +0100 |
| commit | bf087883b05082a07ec2abe22e904b227b21f0d3 (patch) | |
| tree | 2a024f842fb0be4ce91f325af08ecc0028299a6f /lua/mason-registry/init.lua | |
| parent | fix(sources): also set .desc property when updating spec (#1095) (diff) | |
| download | mason-bf087883b05082a07ec2abe22e904b227b21f0d3.tar mason-bf087883b05082a07ec2abe22e904b227b21f0d3.tar.gz mason-bf087883b05082a07ec2abe22e904b227b21f0d3.tar.bz2 mason-bf087883b05082a07ec2abe22e904b227b21f0d3.tar.lz mason-bf087883b05082a07ec2abe22e904b227b21f0d3.tar.xz mason-bf087883b05082a07ec2abe22e904b227b21f0d3.tar.zst mason-bf087883b05082a07ec2abe22e904b227b21f0d3.zip | |
feat: add registry.refresh() method (#1096)
Diffstat (limited to 'lua/mason-registry/init.lua')
| -rw-r--r-- | lua/mason-registry/init.lua | 82 |
1 files changed, 68 insertions, 14 deletions
diff --git a/lua/mason-registry/init.lua b/lua/mason-registry/init.lua index 4568c54d..19a24d60 100644 --- a/lua/mason-registry/init.lua +++ b/lua/mason-registry/init.lua @@ -7,6 +7,7 @@ local path = require "mason-core.path" local sources = require "mason-registry.sources" ---@class RegistrySource +---@field id string ---@field get_package fun(self: RegistrySource, pkg_name: string): Package? ---@field get_all_package_names fun(self: RegistrySource): string[] ---@field get_display_name fun(self: RegistrySource): string @@ -118,12 +119,33 @@ function M.get_all_packages() return get_packages(M.get_all_package_names()) end ----@param cb fun(success: boolean, err: any?) -function M.update(cb) +local STATE_FILE = path.concat { vim.fn.stdpath "cache", "mason-registry-update" } + +---@param time integer +local function get_store_age(time) + local checksum = sources.checksum() + if fs.sync.file_exists(STATE_FILE) then + local parse_state_file = + _.compose(_.evolve { timestamp = tonumber }, _.zip_table { "checksum", "timestamp" }, _.split "\n") + local state = parse_state_file(fs.sync.read_file(STATE_FILE)) + if checksum == state.checksum then + return math.abs(time - state.timestamp) + end + end + return time +end + +---@async +---@param time integer +local function update_store_timestamp(time) + fs.async.write_file(STATE_FILE, _.join("\n", { sources.checksum(), tostring(time) })) +end + +---@param callback? fun(success: boolean, updated_registries: RegistrySource[]) +function M.update(callback) local a = require "mason-core.async" local Result = require "mason-core.result" - - a.run(function() + return a.run(function() return Result.try(function(try) local updated_sources = {} for source in sources.iter { include_uninstalled = true } do @@ -135,23 +157,55 @@ function M.update(cb) end) end return updated_sources + end):on_success(function(updated_sources) + if #updated_sources > 0 then + M:emit("update", updated_sources) + end end) - end, function(success, sources_or_err) - if not success then - cb(success, sources_or_err) + end, function(success, result) + if not callback then return end - sources_or_err - :on_success(function(updated_sources) - if #updated_sources > 0 then - M:emit("update", updated_sources) - end - cb(true, updated_sources) + if not success then + return callback(false, result) + end + result + :on_success(function(value) + callback(true, value) end) :on_failure(function(err) - cb(false, err) + callback(false, err) end) end) end +local REGISTRY_STORE_TTL = 86400 -- 24 hrs + +---@param cb? fun() +function M.refresh(cb) + local a = require "mason-core.async" + + ---@async + local function refresh() + if vim.in_fast_event() then + a.scheduler() + end + local is_outdated = get_store_age(os.time()) > REGISTRY_STORE_TTL + if is_outdated or not sources.is_installed() then + if a.wait(M.update) then + if vim.in_fast_event() then + a.scheduler() + end + update_store_timestamp(os.time()) + end + end + end + + if not cb then + a.run_blocking(refresh) + else + a.run(refresh, cb) + end +end + return M |
