Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store instantiated packages not just module source #1979

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions classes/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,32 @@ function class.declareSettings (_)
end

function class:loadPackage (packname, options, reload)
local pack = require(("packages.%s"):format(packname))
if type(pack) == "table" and pack.type == "package" then -- new package
self.packages[pack._name] = pack(options, reload)
local pack
-- Allow loading by injecting whole packages as-is, otherwise try to load it with the usual packages path.
if type(packname) == "table" then
pack, packname = packname, packname._name
else
pack = require(("packages.%s"):format(packname))
if pack._name ~= packname then
SU.error(("Loaded module name '%s' does not match requested name '%s'"):format(pack._name, packname))
end
end
SILE.packages[packname] = pack
if type(pack) == "table" and pack.type == "package" then -- current package api
if self.packages[packname] then
-- If the same package name has been loaded before, we might be loading a modified version of the same package or
-- we might be re-loading the same package, or we might just be doubling up work because somebody called us twice.
-- The package itself should take care of the difference between load and reload based on the reload flag here,
-- but in addition to that we also want to avoid creating a new instance. We want to run the intitializer from the
-- (possibly different) new module, but not create a new instance ID and loose any connections it made.
-- To do this we add a create function that returns the current instance. This brings along the _initialized flag
-- and of course anything else already setup and running.
local current_instance = self.packages[packname]
pack._create = function () return current_instance end
pack(options, true)
else
self.packages[packname] = pack(options, reload)
end
else -- legacy package
self:initPackage(pack, options)
end
Expand Down
6 changes: 3 additions & 3 deletions core/sile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ local function suggest_luarocks (module)
)
end

SILE.use = function (module, options)
SILE.use = function (module, options, reload)
local status, pack
if type(module) == "string" then
status, pack = pcall(require, module)
Expand Down Expand Up @@ -211,9 +211,9 @@ SILE.use = function (module, options)
SILE.pagebuilders[name] = pack
SILE.pagebuilder = pack(options)
elseif pack.type == "package" then
SILE.packages[name] = pack
SILE.packages[pack._name] = pack
if class then
pack(options)
class:loadPackage(pack, options, reload)
else
table.insert(SILE.input.preambles, { pack = pack, options = options })
end
Expand Down
3 changes: 2 additions & 1 deletion inputters/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ local function process_ambles (ambles)
local options = {}
if amble.pack then amble, options = amble.pack, amble.options end
if amble.type == "package" then
amble(options)
local class = SILE.documentState.documentClass
class:loadPackage(amble.pack, options)
else
SILE.documentState.documentClass:initPackage(amble, options)
end
Expand Down
Loading