Skip to content

Commit

Permalink
add: natural management of update arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Aire-One committed Sep 29, 2021
1 parent 0d1b43e commit a39d528
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
22 changes: 9 additions & 13 deletions lib/wibox/widget/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ local template = {
queued_updates = {}
}

function template:_do_update_now(args)
if type(self.update_callback) == 'function' then
self:update_callback(args)
function template:_do_update_now()
if type(self.update_callback) == "function" then
self:update_callback(self.update_args)
end
self.update_args = nil
template.queued_updates[self] = false
end

Expand All @@ -37,23 +38,19 @@ end
-- is called multiple times during the same GLib event loop, only the first call
-- will be run.
-- All arguments are passed to the queued `update_callback` call.
-- @treturn boolean Returns `true` if the update_callback have been queued to be
-- run at the end of the event loop. Returns `false` if there is already an update
-- in the queue (in this case, this new update request is discared).
function template:update(args)
if type(args) == "table" then
self.update_args = gtable.crush(gtable.clone(self.update_args or {}, false), args)
end

if not template.queued_updates[self] then
local update_args = gtable.crush(gtable.clone(self.update_args, false), args or {})
gtimer.delayed_call(
function()
self:_do_update_now(update_args)
self:_do_update_now()
end
)
template.queued_updates[self] = true

return true
end

return false
end

--- Create a new `wibox.widget.template` instance.
Expand All @@ -71,7 +68,6 @@ function template.new(args)
local widget = wbase.make_widget_from_value(widget_template)

widget.update_callback = args.update_callback
widget.update_args = args.update_args or {}

gtable.crush(widget, template, true)

Expand Down
25 changes: 20 additions & 5 deletions spec/wibox/widget/template_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,36 @@ describe("wibox.widget.template", function()

it("update parameters", function()
local spied_update_callback = spy.new(function() end)
local args_structure = { foo = "string" }
local update_args = { foo = "bar" }
local args = { foo = "string" }

widget.update_args = args_structure
widget.update_callback = function(...) spied_update_callback(...) end

widget:update(update_args)
widget:update(args)

gtimer.run_delayed_calls_now()

assert.spy(spied_update_callback).was.called_with(
match.is_ref(widget),
match.is_same_table_struture(widget.update_args)
match.is_same(args)
)
end)

it("crush update parameters", function()
local spied_update_callback = spy.new(function() end)

widget.update_callback = function(...) spied_update_callback(...) end

widget:update { foo = "bar" }
widget:update { bar = 10 }

gtimer.run_delayed_calls_now()

assert.spy(spied_update_callback).was.called_with(
match.is_ref(widget),
match.is_same { foo = "bar", bar = 10 }
)
end)

end)
end)

Expand Down

0 comments on commit a39d528

Please # to comment.