diff options
| author | William Boman <william@redwill.se> | 2022-07-08 18:34:38 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-08 18:34:38 +0200 |
| commit | 976aa4fbee8a070f362cab6f6ec84e9251a90cf9 (patch) | |
| tree | 5e8d9c9c59444a25c7801b8f39763c4ba6e1f76d /lua/mason-core/ui/state.lua | |
| parent | feat: add gotests, gomodifytags, impl (#28) (diff) | |
| download | mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.gz mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.bz2 mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.lz mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.xz mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.tar.zst mason-976aa4fbee8a070f362cab6f6ec84e9251a90cf9.zip | |
refactor: add mason-schemas and mason-core modules (#29)
* refactor: add mason-schemas and move generated filetype map to mason-lspconfig
* refactor: add mason-core module
Diffstat (limited to 'lua/mason-core/ui/state.lua')
| -rw-r--r-- | lua/mason-core/ui/state.lua | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lua/mason-core/ui/state.lua b/lua/mason-core/ui/state.lua new file mode 100644 index 00000000..9d7bcdda --- /dev/null +++ b/lua/mason-core/ui/state.lua @@ -0,0 +1,24 @@ +local M = {} + +---@generic T : table +---@param initial_state T +---@param subscriber fun(state: T) +function M.create_state_container(initial_state, subscriber) + -- we do deepcopy to make sure instances of state containers doesn't mutate the initial state + local state = vim.deepcopy(initial_state) + local has_unsubscribed = false + + ---@param mutate_fn fun(current_state: table) + return function(mutate_fn) + mutate_fn(state) + if not has_unsubscribed then + subscriber(state) + end + end, function() + return state + end, function(val) + has_unsubscribed = val + end +end + +return M |
