Skip to content

Commit d0dda93

Browse files
committed
feat: slot are always HOF
The slot's`slot` function callback needs to be a Higher-Order function to manage correctly and consistently the signal parameters. Before this change, the API was broken when the user wanted to pass `slot_params` to a signal with parameters. (e.g., the `screen` `"request::desktop_decoration"` signal passes the screen instance as a parameter; when used in combination with `slot_params`, `slot_param` overwrites the parameter.) Bonus point: every slot is by default a pseudo constructor function that has to return the actual signal callback. It makes it obvious to the user how to use the `slot_params` to manage external dependencies and configurations for the actual signal callback implementation.
1 parent d4add4f commit d0dda93

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

spec/slot_spec.lua

+53-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ local slot = require "awesome-slot"
22

33
local function new_target()
44
return {
5-
connect_signal = spy.new(function() end),
6-
disconnect_signal = spy.new(function() end),
5+
signal = nil, -- Only need to bind 1 signal in the tests
6+
connect_signal = function(self, _signal_name, signal_callback)
7+
self.signal = signal_callback
8+
end,
9+
disconnect_signal = function()
10+
-- Unimplemented
11+
end,
12+
emit_signal = function(self, _signal_name, ...)
13+
self.signal(...)
14+
end,
715
}
816
end
917

@@ -121,4 +129,47 @@ describe("Awesome-slot", function()
121129

122130
assert.is_not_nil(s.id)
123131
end)
132+
133+
it("should manage slot parameters", function()
134+
local target = new_target()
135+
local signal_name = "signal"
136+
local params = { key = "value" }
137+
local callback = spy.new(function(p)
138+
return function()
139+
assert.same(params, p)
140+
end
141+
end)
142+
143+
slot {
144+
target = target,
145+
signal = signal_name,
146+
slot = callback,
147+
slot_params = params,
148+
connect = true,
149+
}
150+
151+
target:emit_signal(signal_name)
152+
assert.spy(callback).called()
153+
end)
154+
155+
it("should retrieve signal parameters", function()
156+
local target = new_target()
157+
local signal_name = "signal"
158+
local callback = spy.new(function()
159+
return function(a, b, c)
160+
assert.equal(a, 1)
161+
assert.equal(b, 2)
162+
assert.equal(c, 3)
163+
end
164+
end)
165+
166+
slot {
167+
target = target,
168+
signal = signal_name,
169+
slot = callback,
170+
connect = true,
171+
}
172+
173+
target:emit_signal(signal_name, 1, 2, 3)
174+
end)
124175
end)

src/awesome-slot/init.lua

+2-8
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,8 @@ function awesome_slot.create(params)
120120
slot.target = params.target
121121
slot.signal = params.signal
122122
slot.connected = false
123-
124-
if params.slot_params then
125-
slot.params = params.slot_params
126-
slot.callback = function()
127-
params.slot(slot.params)
128-
end
129-
else
130-
slot.callback = params.slot
123+
slot.callback = function(...)
124+
params.slot(params.slot_params)(...)
131125
end
132126

133127
-- Insert the new slot into the slots list

0 commit comments

Comments
 (0)