Skip to content

Commit 5259a0c

Browse files
committed
feat: add :map() method to Signal object
closes #19
1 parent 0af84e5 commit 5259a0c

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

docs/pages/docs/signal.mdx

+63
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,69 @@ And here's the final result:
186186
187187
<Method name="get_value" returns="table" />
188188

189+
#### map
190+
191+
> Applies a mapping function to each value emitted by the `Signal`. The mapping function receives the current value of the `Signal` object.
192+
193+
<Method
194+
name="map"
195+
args={[
196+
['map_fn', 'fun(value: T): R'],
197+
]}
198+
returns="SignalValue"
199+
/>
200+
201+
Example:
202+
203+
```lua
204+
local n = require 'nui-components'
205+
206+
local renderer = n.create_renderer {
207+
width = 60,
208+
height = 12,
209+
}
210+
211+
local signal = n.create_signal {
212+
name = '',
213+
description = '',
214+
}
215+
216+
local body = function()
217+
return n.rows(
218+
n.paragraph {
219+
is_focusable = false,
220+
lines = signal:map(function(val)
221+
return {
222+
n.line(n.text('Name: ', 'Keyword'), val.name),
223+
n.line(n.text('Description: ', 'Keyword'), val.description),
224+
}
225+
end),
226+
},
227+
n.gap(1),
228+
n.form(
229+
{ id = 'form' },
230+
n.text_input {
231+
autofocus = true,
232+
max_lines = 1,
233+
border_label = 'Name',
234+
on_change = function(val)
235+
signal.name = val
236+
end,
237+
},
238+
n.text_input {
239+
max_lines = 1,
240+
border_label = 'Description',
241+
on_change = function(val)
242+
signal.description = val
243+
end,
244+
}
245+
)
246+
)
247+
end
248+
249+
renderer:render(body)
250+
```
251+
189252
### Methods / Operators (Signal Value)
190253

191254
#### get_value

lua/nui-components/signal/init.lua

+10
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ function Signal.create(object)
3030
end
3131
end
3232

33+
if key == "map" then
34+
return function(_, ...)
35+
return self:map(...)
36+
end
37+
end
38+
3339
if key == "get_value" then
3440
return function(_)
3541
return self:get_value()
@@ -72,4 +78,8 @@ function Signal:get_value()
7278
return self._private.subject:get_value()
7379
end
7480

81+
function Signal:map(map_fn)
82+
return SignalValue.create(self._private.subject):map(map_fn)
83+
end
84+
7585
return Signal

lua/nui-components/signal/value.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function SignalValue.create(subject, key)
1111
key = key,
1212
subject = subject,
1313
observable = subject:map(function(value)
14-
return value[key]
14+
return key ~= nil and value[key] or value
1515
end),
1616
},
1717
}
@@ -20,7 +20,8 @@ function SignalValue.create(subject, key)
2020
end
2121

2222
function SignalValue:get_value()
23-
return self._private.subject:get_value()[self._private.key]
23+
local val = self._private.subject:get_value()
24+
return self._private.key ~= nil and val[self._private.key] or val
2425
end
2526

2627
function SignalValue:get_observer_value()

0 commit comments

Comments
 (0)