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

Add active hook system to settings #1983

Merged
merged 2 commits into from
Feb 7, 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
28 changes: 28 additions & 0 deletions core/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function settings:_init()
self.declarations = {}
self.stateQueue = {}
self.defaults = {}
self.hooks = {}

self:declare({
parameter = "document.language",
Expand Down Expand Up @@ -105,7 +106,16 @@ end

function settings:popState ()
if not self then return deprecator() end
local previous = self.state
self.state = table.remove(self.stateQueue)
for parameter, oldvalue in pairs(previous) do
if self.hooks[parameter] then
local newvalue = self.state[parameter]
if oldvalue ~= newvalue then
self:runHooks(parameter, newvalue)
end
end
end
end

function settings:declare (spec)
Expand All @@ -118,6 +128,10 @@ function settings:declare (spec)
return
end
self.declarations[spec.parameter] = spec
self.hooks[spec.parameter] = {}
if spec.hook then
self:registerHook(spec.parameter, spec.hook)
end
self:set(spec.parameter, spec.default, true)
end

Expand Down Expand Up @@ -202,6 +216,20 @@ function settings:set (parameter, value, makedefault, reset)
if makedefault then
self.defaults[parameter] = value
end
self:runHooks(parameter, value)
end

function settings:registerHook (parameter, func)
table.insert(self.hooks[parameter], func)
end

function settings:runHooks (parameter, value)
if self.hooks[parameter] then
for _, func in ipairs(self.hooks[parameter]) do
SU.debug("classhooks", "Running seting hook for", parameter)
func(value)
end
end
end

function settings:temporarily (func)
Expand Down
42 changes: 42 additions & 0 deletions core/settings_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
SILE = require("core.sile")

describe("The settings handler", function()

it("should trigger callbacks on set", function()
local private = "foo"
local function callback (value)
private = value .. " hooked"
end
SILE.settings:declare({
parameter = "test.callback",
type = "string",
default = "bar",
hook = callback
})
SILE.settings:set("test.callback", "baz")
assert.is.equal("baz hooked", private)
end)

it("should trigger callbacks on push/pop events", function()
local mystate = "foo"
local function callback2 (value)
mystate = value .. " stack"
end
SILE.settings:declare({
parameter = "test.callback2",
type = "string",
default = "bar",
hook = callback2
})
SILE.settings:pushState()
SILE.settings:set("test.callback2", "baz1")
SILE.settings:pushState()
SILE.settings:set("test.callback2", "baz2")
assert.is.equal("baz2 stack", mystate)
SILE.settings:popState()
assert.is.equal("baz1 stack", mystate)
SILE.settings:popState()
assert.is.equal("bar stack", mystate)
end)

end)
Loading