|
| 1 | +local utils = require('orgmode.utils') |
| 2 | +local Promise = require('orgmode.utils.promise') |
| 3 | + |
| 4 | +---@class OrgState |
| 5 | +local OrgState = { data = {}, _ctx = { loaded = false, saved = false, curr_loader = nil, savers = 0 } } |
| 6 | + |
| 7 | +local cache_path = vim.fs.normalize(vim.fn.stdpath('cache') .. '/org-cache.json', { expand_env = false }) |
| 8 | + |
| 9 | +---Returns the current OrgState singleton |
| 10 | +---@return OrgState |
| 11 | +function OrgState.new() |
| 12 | + -- This is done so we can later iterate the 'data' |
| 13 | + -- subtable cleanly and shove it into a cache |
| 14 | + setmetatable(OrgState, { |
| 15 | + __index = function(tbl, key) |
| 16 | + return tbl.data[key] |
| 17 | + end, |
| 18 | + __newindex = function(tbl, key, value) |
| 19 | + tbl.data[key] = value |
| 20 | + end, |
| 21 | + }) |
| 22 | + local self = OrgState |
| 23 | + -- Start trying to load the state from cache as part of initializing the state |
| 24 | + self:load() |
| 25 | + return self |
| 26 | +end |
| 27 | + |
| 28 | +---Save the current state to cache |
| 29 | +---@return Promise |
| 30 | +function OrgState:save() |
| 31 | + OrgState._ctx.saved = false |
| 32 | + --- We want to ensure the state was loaded before saving. |
| 33 | + self:load() |
| 34 | + self._ctx.savers = self._ctx.savers + 1 |
| 35 | + return utils |
| 36 | + .writefile(cache_path, vim.json.encode(OrgState.data)) |
| 37 | + :next(function() |
| 38 | + self._ctx.savers = self._ctx.savers - 1 |
| 39 | + if self._ctx.savers == 0 then |
| 40 | + OrgState._ctx.saved = true |
| 41 | + end |
| 42 | + end) |
| 43 | + :catch(function(err_msg) |
| 44 | + self._ctx.savers = self._ctx.savers - 1 |
| 45 | + vim.schedule_wrap(function() |
| 46 | + utils.echo_warning('Failed to save current state! Error: ' .. err_msg) |
| 47 | + end) |
| 48 | + end) |
| 49 | +end |
| 50 | + |
| 51 | +---Synchronously save the state into cache |
| 52 | +---@param timeout? number How long to wait for the save operation |
| 53 | +function OrgState:save_sync(timeout) |
| 54 | + OrgState._ctx.saved = false |
| 55 | + self:save() |
| 56 | + vim.wait(timeout or 500, function() |
| 57 | + return OrgState._ctx.saved |
| 58 | + end, 20) |
| 59 | +end |
| 60 | + |
| 61 | +---Load the state cache into the current state |
| 62 | +---@return Promise |
| 63 | +function OrgState:load() |
| 64 | + --- If we currently have a loading operation already running, return that |
| 65 | + --- promise. This avoids a race condition of sorts as without this there's |
| 66 | + --- potential to have two OrgState:load operations occuring and whichever |
| 67 | + --- finishes last sets the state. Not desirable. |
| 68 | + if self._ctx.curr_loader ~= nil then |
| 69 | + return self._ctx.curr_loader |
| 70 | + end |
| 71 | + |
| 72 | + --- If we've already loaded the state from cache we don't need to do so again |
| 73 | + if self._ctx.loaded then |
| 74 | + return Promise.resolve(self) |
| 75 | + end |
| 76 | + |
| 77 | + self._ctx.curr_loader = utils |
| 78 | + .readfile(cache_path, { raw = true }) |
| 79 | + :next(function(data) |
| 80 | + local success, decoded = pcall(vim.json.decode, data, { |
| 81 | + luanil = { object = true, array = true }, |
| 82 | + }) |
| 83 | + self._ctx.curr_loader = nil |
| 84 | + if not success then |
| 85 | + local err_msg = vim.deepcopy(decoded) |
| 86 | + vim.schedule(function() |
| 87 | + utils.echo_warning('OrgState cache load failure, error: ' .. vim.inspect(err_msg)) |
| 88 | + -- Try to 'repair' the cache by saving the current state |
| 89 | + self:save() |
| 90 | + end) |
| 91 | + end |
| 92 | + -- Because the state cache repair happens potentially after the data has |
| 93 | + -- been added to the cache, we need to ensure the decoded table is set to |
| 94 | + -- empty if we got an error back on the json decode operation. |
| 95 | + if type(decoded) ~= 'table' then |
| 96 | + decoded = {} |
| 97 | + end |
| 98 | + |
| 99 | + -- It is possible that while the state was loading from cache values |
| 100 | + -- were saved into the state. We want to preference the newer values in |
| 101 | + -- the state and still get whatever values may not have been set in the |
| 102 | + -- interim of the load operation. |
| 103 | + self.data = vim.tbl_deep_extend('force', decoded, self.data) |
| 104 | + return self |
| 105 | + end) |
| 106 | + :catch(function(err) |
| 107 | + -- If the file didn't exist then go ahead and save |
| 108 | + -- our current cache and as a side effect create the file |
| 109 | + if type(err) == 'string' and err:match([[^ENOENT.*]]) then |
| 110 | + self:save() |
| 111 | + return self |
| 112 | + end |
| 113 | + -- If the file did exist, something is wrong. Kick this to the top |
| 114 | + error(err) |
| 115 | + end) |
| 116 | + :finally(function() |
| 117 | + self._ctx.loaded = true |
| 118 | + self._ctx.curr_loader = nil |
| 119 | + end) |
| 120 | + |
| 121 | + return self._ctx.curr_loader |
| 122 | +end |
| 123 | + |
| 124 | +---Synchronously load the state from cache if it hasn't been loaded |
| 125 | +---@param timeout? number How long to wait for the cache load before erroring |
| 126 | +---@return OrgState |
| 127 | +function OrgState:load_sync(timeout) |
| 128 | + local state |
| 129 | + local err |
| 130 | + self |
| 131 | + :load() |
| 132 | + :next(function(loaded_state) |
| 133 | + state = loaded_state |
| 134 | + end) |
| 135 | + :catch(function(reject) |
| 136 | + err = reject |
| 137 | + end) |
| 138 | + |
| 139 | + vim.wait(timeout or 500, function() |
| 140 | + return state ~= nil or err ~= nil |
| 141 | + end, 20) |
| 142 | + |
| 143 | + if err then |
| 144 | + error(err) |
| 145 | + end |
| 146 | + |
| 147 | + if err == nil and state == nil then |
| 148 | + error('Did not load OrgState in time') |
| 149 | + end |
| 150 | + |
| 151 | + return state |
| 152 | +end |
| 153 | + |
| 154 | +---Reset the current state to empty |
| 155 | +---@param overwrite? boolean Whether or not the cache should also be wiped |
| 156 | +function OrgState:wipe(overwrite) |
| 157 | + overwrite = overwrite or false |
| 158 | + |
| 159 | + self.data = {} |
| 160 | + self._ctx.curr_loader = nil |
| 161 | + self._ctx.loaded = false |
| 162 | + self._ctx.saved = false |
| 163 | + if overwrite then |
| 164 | + state:save_sync() |
| 165 | + end |
| 166 | +end |
| 167 | + |
| 168 | +return OrgState.new() |
0 commit comments