Skip to content

Commit

Permalink
refactor: Adapt our base class fork to SILE 0.15
Browse files Browse the repository at this point in the history
See SILE PR 2095
  • Loading branch information
Omikhleia authored and Didier Willis committed Aug 30, 2024
1 parent d9c1215 commit ce86c32
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions silex/classes/base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class.type = "class"
class._name = "base"

class._initialized = false
class.deferredLegacyInit = {}
class.deferredInit = {}
class.pageTemplate = { frames = {}, firstContentFrame = nil }
class.defaultFrameset = {}
Expand Down Expand Up @@ -73,6 +72,7 @@ function class:_init (options)
end

function class:_post_init ()
SILE.documentState.documentClass = self
self._initialized = true
for i, func in ipairs(self.deferredInit) do
func(self)
Expand Down Expand Up @@ -222,13 +222,22 @@ function class:initPackage (pack, options)
end
end

function class:registerLegacyPostinit (func, options)
if self._initialized then return func(self, options) end
table.insert(self.deferredLegacyInit, function (_)
func(self, options)
end)
end

--- Register a callback function to be executed after the class initialization has completed.
-- Sometimes a class or package may want to run things after the class has been fully initialized. This can be useful
-- for setting document settings after packages and all their dependencies have been loaded. For example a package might
-- want to trigger something to happen after all frames have been defined, but the package itself doesn't know if it is
-- being loaded before or after the document options are processed, frame masters have been setup, etc. Rather than
-- relying on the user to load the package after these events, the package can use this callback to deffer the action
-- until those things can be reasonable expected to have completed.
--
-- Functions in the deferred initialization queue are run on a first-set first-run basis.
--
-- Note the typesetter will *not* have been instantiated yet, so is not appropriate to try to output content at this
-- point. Injecting content to be processed at the start of a document should be done with preambles. The inputter
-- *will* be instantiated at this point, so adding a new preamble is viable.
-- If the class has already been initialized the callback function will be run immediately.
-- @tparam function func Callback function accepting up to two arguments.
-- @tparam[opt] table options Additional table passed as a second argument to the callback.
function class:registerPostinit (func, options)
if self._initialized then return func(self, options) end
table.insert(self.deferredInit, function (_)
Expand Down

0 comments on commit ce86c32

Please sign in to comment.