Skip to content

Commit 4300a6b

Browse files
authored
Merge pull request #27 from grapp-dev/feature/add-new-operators
feat: add `debounce` and `start_with` operators
2 parents d9b6b7c + 91a3aa8 commit 4300a6b

4 files changed

Lines changed: 46 additions & 0 deletions

File tree

docs/pages/docs/signal.mdx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,18 @@ And here's the final result:
224224
225225
<Method name="negate" returns="SignalValue" />
226226

227+
#### debounce
228+
229+
> Delays an operation until a specified time has passed without further input.
230+
231+
<Method
232+
name="debounce"
233+
args={[
234+
['ms', 'number'],
235+
]}
236+
returns="SignalValue"
237+
/>
238+
227239
#### tap
228240

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

301+
#### start_with
302+
303+
> Emits a default value immediately and synchronously after subscribing.
304+
305+
<Method
306+
name="start_with"
307+
args={[
308+
['default_value', 'T'],
309+
]}
310+
returns="SignalValue"
311+
/>
312+
289313
#### observe
290314

291315
> Subscribes an observer function to the observable. The observer function is called every time a new value is emitted by the observable.

lua/nui-components/rx/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ require("nui-components.rx.operators.scan")
1212
require("nui-components.rx.operators.skip")
1313
require("nui-components.rx.operators.combine_latest")
1414
require("nui-components.rx.operators.debounce")
15+
require("nui-components.rx.operators.start_with")
1516

1617
return Rx
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
local Observable = require("nui-components.rx.observable")
2+
local fn = require("nui-components.utils.fn")
3+
4+
function Observable:start_with(...)
5+
local values = fn.pack(...)
6+
7+
return Observable.create(function(observer)
8+
observer:on_next(fn.unpack(values))
9+
return self:subscribe(observer)
10+
end)
11+
end

lua/nui-components/signal/value.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ function SignalValue:skip(n)
5858
return self
5959
end
6060

61+
function SignalValue:debounce(ms)
62+
self._private.observable = self._private.observable:debounce(ms)
63+
return self
64+
end
65+
66+
function SignalValue:start_with(value)
67+
self._private.observable = self._private.observable:start_with(value)
68+
return self
69+
end
70+
6171
function SignalValue:combine_latest(...)
6272
local signal_values = { ... }
6373

0 commit comments

Comments
 (0)