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

feat: add :map() method to Signal object #23

Closed
wants to merge 1 commit into from
Closed
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
63 changes: 63 additions & 0 deletions docs/pages/docs/signal.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,69 @@ And here's the final result:

<Method name="get_value" returns="table" />

#### map

> Applies a mapping function to each value emitted by the `Signal`. The mapping function receives the current value of the `Signal` object.

<Method
name="map"
args={[
['map_fn', 'fun(value: T): R'],
]}
returns="SignalValue"
/>

Example:

```lua
local n = require 'nui-components'

local renderer = n.create_renderer {
width = 60,
height = 12,
}

local signal = n.create_signal {
name = '',
description = '',
}

local body = function()
return n.rows(
n.paragraph {
is_focusable = false,
lines = signal:map(function(val)
return {
n.line(n.text('Name: ', 'Keyword'), val.name),
n.line(n.text('Description: ', 'Keyword'), val.description),
}
end),
},
n.gap(1),
n.form(
{ id = 'form' },
n.text_input {
autofocus = true,
max_lines = 1,
border_label = 'Name',
on_change = function(val)
signal.name = val
end,
},
n.text_input {
max_lines = 1,
border_label = 'Description',
on_change = function(val)
signal.description = val
end,
}
)
)
end

renderer:render(body)
```

### Methods / Operators (Signal Value)

#### get_value
Expand Down
10 changes: 10 additions & 0 deletions lua/nui-components/signal/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ function Signal.create(object)
end
end

if key == "map" then
return function(_, ...)
return self:map(...)
end
end

if key == "get_value" then
return function(_)
return self:get_value()
Expand Down Expand Up @@ -72,4 +78,8 @@ function Signal:get_value()
return self._private.subject:get_value()
end

function Signal:map(map_fn)
return SignalValue.create(self._private.subject):map(map_fn)
end

return Signal
5 changes: 3 additions & 2 deletions lua/nui-components/signal/value.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function SignalValue.create(subject, key)
key = key,
subject = subject,
observable = subject:map(function(value)
return value[key]
return key ~= nil and value[key] or value
end),
},
}
Expand All @@ -20,7 +20,8 @@ function SignalValue.create(subject, key)
end

function SignalValue:get_value()
return self._private.subject:get_value()[self._private.key]
local val = self._private.subject:get_value()
return self._private.key ~= nil and val[self._private.key] or val
end

function SignalValue:get_observer_value()
Expand Down
Loading