Skip to content

Commit

Permalink
Merge pull request #27 from grapp-dev/feature/add-new-operators
Browse files Browse the repository at this point in the history
feat: add `debounce` and `start_with` operators
  • Loading branch information
mobily authored Apr 15, 2024
2 parents d9b6b7c + 91a3aa8 commit 4300a6b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
24 changes: 24 additions & 0 deletions docs/pages/docs/signal.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ And here's the final result:
<Method name="negate" returns="SignalValue" />

#### debounce

> Delays an operation until a specified time has passed without further input.
<Method
name="debounce"
args={[
['ms', 'number'],
]}
returns="SignalValue"
/>

#### tap

> Executes a function with each value emitted by the observable.
Expand Down Expand Up @@ -286,6 +298,18 @@ And here's the final result:
returns="SignalValue"
/>

#### start_with

> Emits a default value immediately and synchronously after subscribing.
<Method
name="start_with"
args={[
['default_value', 'T'],
]}
returns="SignalValue"
/>

#### observe

> Subscribes an observer function to the observable. The observer function is called every time a new value is emitted by the observable.
Expand Down
1 change: 1 addition & 0 deletions lua/nui-components/rx/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ require("nui-components.rx.operators.scan")
require("nui-components.rx.operators.skip")
require("nui-components.rx.operators.combine_latest")
require("nui-components.rx.operators.debounce")
require("nui-components.rx.operators.start_with")

return Rx
11 changes: 11 additions & 0 deletions lua/nui-components/rx/operators/start_with.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local Observable = require("nui-components.rx.observable")
local fn = require("nui-components.utils.fn")

function Observable:start_with(...)
local values = fn.pack(...)

return Observable.create(function(observer)
observer:on_next(fn.unpack(values))
return self:subscribe(observer)
end)
end
10 changes: 10 additions & 0 deletions lua/nui-components/signal/value.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ function SignalValue:skip(n)
return self
end

function SignalValue:debounce(ms)
self._private.observable = self._private.observable:debounce(ms)
return self
end

function SignalValue:start_with(value)
self._private.observable = self._private.observable:start_with(value)
return self
end

function SignalValue:combine_latest(...)
local signal_values = { ... }

Expand Down

0 comments on commit 4300a6b

Please sign in to comment.