Skip to content

Commit

Permalink
Merge pull request #129 from CogentRedTester/cache-refactor2
Browse files Browse the repository at this point in the history
Cache refactor 2
  • Loading branch information
CogentRedTester authored Mar 7, 2025
2 parents 369222a + 630a55c commit 7201158
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 220 deletions.
6 changes: 5 additions & 1 deletion docs/addons.md
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,10 @@ Clears the directory cache. Use this if you are modifying the contents of direct
than the current one to ensure that their contents will be rescanned when next opened.
An an array of directory strings is passed to the function only those directories
will be cleared from the cache.
The cache is cleared asynchronously, it may not, and probably will not, have been cleared
when the function returns. This may change in the future.

The cache is implemented as an [internal parser](../modules/parsers/cache.lua).

#### `fb.coroutine.assert(err?: string): coroutine`

Expand Down Expand Up @@ -648,7 +652,7 @@ any additional arguments. The (not yet started) coroutine is returned by the fun

#### `fb.rescan(): coroutine`

Rescans the current directory. Equivalent to Ctrl+r without the cache refresh for any other directory.
Rescans the current directory. Equivalent to Ctrl+r.
Returns the coroutine of the upcoming parse operation. The parse is queued and run when the script thread next goes idle,
allowing one to store this value and use it to identify the triggered parse operation.

Expand Down
3 changes: 2 additions & 1 deletion docs/file_browser.conf
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ normalise_backslash=auto
# enable if it takes a long time to load directories.
# May cause 'ghost' files to be shown that no-longer exist or
# fail to show files that have recently been created.
# Use Ctrl+r to forcibly clear the cache when enabled.
# Reloading the directory with Ctrl+r will never use the cache.
# Use Ctrl+Shift+r to forcibly clear the cache.
cache=no

# this option reverses the behaviour of the alt+ENTER keybind
Expand Down
10 changes: 6 additions & 4 deletions modules/apis/fb.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ local fb_utils = require 'modules.utils'
local ass = require 'modules.ass'
local directory_movement = require 'modules.navigation.directory-movement'
local scanning = require 'modules.navigation.scanning'
local cache = require 'modules.cache'
local controls = require 'modules.controls'

---@class FbAPI: fb_utils
Expand All @@ -22,21 +21,24 @@ fb.browse_directory = controls.browse_directory
---Clears the directory cache.
---@return thread
function fb.rescan()
cache:clear({g.state.directory})
return scanning.rescan()
end

---@async
---@return thread
function fb.rescan_await()
cache:clear({g.state.directory})
local co = scanning.rescan(nil, fb_utils.coroutine.callback())
coroutine.yield()
return co
end

---@param directories? string[]
function fb.clear_cache(directories)
cache:clear(directories)
if directories then
mp.commandv('script-message-to', mp.get_script_name(), 'cache/clear', utils.format_json(directories))
else
mp.commandv('script-message-to', mp.get_script_name(), 'cache/clear')
end
end

---A wrapper around scan_directory for addon API.
Expand Down
182 changes: 0 additions & 182 deletions modules/cache.lua

This file was deleted.

5 changes: 4 additions & 1 deletion modules/defs/mp/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ function mp.get_property_native(name, def) end
---@return string|nil
function mp.get_script_directory() end

---@return string
function mp.get_script_name() end

---@param name string
---@param type 'native'|'bool'|'string'|'number'
---@param fn fun(name: string, v: unknown)
Expand All @@ -112,7 +115,7 @@ function mp.observe_property(name, type, fn) end
function mp.register_event(name, fn) end

---@param name string
---@param fn function
---@param fn fun(...: string)
function mp.register_script_message(name, fn) end

---@param name string
Expand Down
2 changes: 1 addition & 1 deletion modules/defs/mp/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function utils.join_path(p1, p2) end

---@param str string
---@param trail? boolean
---@return table? t
---@return (table|unknown[])? t
---@return string? err # error
---@return string trail # trailing characters
function utils.parse_json(str, trail) end
Expand Down
3 changes: 1 addition & 2 deletions modules/keybinds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ local controls = require 'modules.controls'
local movement = require 'modules.navigation.directory-movement'
local scanning = require 'modules.navigation.scanning'
local cursor = require 'modules.navigation.cursor'
local cache = require 'modules.cache'

g.state.keybinds = {
{'ENTER', 'play', function() playlist.add_files('replace', false) end},
Expand All @@ -33,7 +32,7 @@ g.state.keybinds = {
{'Shift+PGUP', 'list_top', function() cursor.scroll(-math.huge) end},
{'HOME', 'goto_current', movement.goto_current_dir},
{'Shift+HOME', 'goto_root', movement.goto_root},
{'Ctrl+r', 'reload', function() cache:clear(); scanning.rescan() end},
{'Ctrl+r', 'reload', scanning.rescan},
{'s', 'select_mode', cursor.toggle_select_mode},
{'S', 'select_item', cursor.toggle_selection},
{'Ctrl+a', 'select_all', cursor.select_all}
Expand Down
4 changes: 0 additions & 4 deletions modules/navigation/directory-movement.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ local utils = require 'mp.utils'
local o = require 'modules.options'
local g = require 'modules.globals'
local ass = require 'modules.ass'
local cache = require 'modules.cache'
local scanning = require 'modules.navigation.scanning'
local fb_utils = require 'modules.utils'

Expand Down Expand Up @@ -35,9 +34,6 @@ end

--the base function for moving to a directory
function directory_movement.goto_directory(directory, moving_adjacent)
-- update cache to the lastest state values before changing the current directory
cache:add_current_state()

local current = g.state.list[g.state.selected]
g.state.directory = directory

Expand Down
22 changes: 1 addition & 21 deletions modules/navigation/scanning.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ local utils = require 'mp.utils'

local g = require 'modules.globals'
local fb_utils = require 'modules.utils'
local cache = require 'modules.cache'
local cursor = require 'modules.navigation.cursor'
local ass = require 'modules.ass'

local parse_state_API = require 'modules.apis.parse-state'

local function clear_non_adjacent_state()
g.state.directory_label = nil
cache:clear_traversal_stack()
end

---parses the given directory or defers to the next parser if nil is returned
Expand Down Expand Up @@ -105,13 +103,6 @@ local function update_list(moving_adjacent)
g.state.selected = 1
g.state.selection = {}

--loads the current directry from the cache to save loading time
if cache:in_cache(g.state.directory) then
msg.verbose('found directory in cache')
cache:apply(g.state.directory)
g.state.prev_directory = g.state.directory
return
end
local directory = g.state.directory
local list, opts = parse_directory(g.state.directory, { source = "browser" })

Expand All @@ -124,13 +115,7 @@ local function update_list(moving_adjacent)
end

--apply fallbacks if the scan failed
if not list and cache:in_cache(g.state.prev_directory) then
--switches settings back to the previously opened directory
--to the user it will be like the directory never changed
msg.warn("could not read directory", g.state.directory)
cache:apply(g.state.prev_directory)
return
elseif not list then
if not list then
--opens the root instead
msg.warn("could not read directory", g.state.directory, "redirecting to root")
list, opts = parse_directory("", { source = "browser" })
Expand Down Expand Up @@ -189,11 +174,6 @@ local function rescan(moving_adjacent, cb)
update_list(moving_adjacent)
if g.state.empty_text == "~" then g.state.empty_text = "empty directory" end

cache:append_history()
if type(moving_adjacent) == 'number' and moving_adjacent < 0 then cache:pop()
else cache:push() end
if not cache.traversal_stack[1] then cache:push() end

ass.update_ass()
if cb then fb_utils.coroutine.run(cb) end
end)
Expand Down
3 changes: 1 addition & 2 deletions modules/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ local o = {
--a directory cache to improve directory reading time,
--enable if it takes a long time to load directories.
--may cause 'ghost' files to be shown that no-longer exist or
--fail to show files that have recently been created. Use Ctrl+r to
--forcibly clear the cache when enabled.
--fail to show files that have recently been created.
cache = false,

--this option reverses the behaviour of the alt+ENTER keybind
Expand Down
Loading

0 comments on commit 7201158

Please sign in to comment.