aboutsummaryrefslogtreecommitdiffstats
path: root/lua/mason-registry
diff options
context:
space:
mode:
authorWilliam Boman <william@redwill.se>2023-03-14 04:48:03 +0100
committerGitHub <noreply@github.com>2023-03-14 04:48:03 +0100
commitbf087883b05082a07ec2abe22e904b227b21f0d3 (patch)
tree2a024f842fb0be4ce91f325af08ecc0028299a6f /lua/mason-registry
parentfix(sources): also set .desc property when updating spec (#1095) (diff)
downloadmason-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')
-rw-r--r--lua/mason-registry/init.lua82
-rw-r--r--lua/mason-registry/sources/init.lua23
2 files changed, 91 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
diff --git a/lua/mason-registry/sources/init.lua b/lua/mason-registry/sources/init.lua
index 0e5e0d29..0739f40b 100644
--- a/lua/mason-registry/sources/init.lua
+++ b/lua/mason-registry/sources/init.lua
@@ -1,3 +1,5 @@
+local _ = require "mason-core.functional"
+
local M = {}
---@param registry_id string
@@ -72,4 +74,25 @@ function M.iter(opts)
end
end
+---@return boolean #Returns true if all sources are installed.
+function M.is_installed()
+ for source in M.iter { include_uninstalled = true } do
+ if not source:is_installed() then
+ return false
+ end
+ end
+ return true
+end
+
+---@return string # The sha256 checksum of the currently registered sources.
+function M.checksum()
+ ---@type string[]
+ local registry_ids = {}
+ for source in M.iter { include_uninstalled = true } do
+ table.insert(registry_ids, source.id)
+ end
+ local checksum = _.compose(vim.fn.sha256, _.join "", _.sort_by(_.identity))
+ return checksum(registry_ids)
+end
+
return M