Logical operators for streams #6509
kosich
started this conversation in
Ideas / Feature request
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello!
The idea I want to discuss is to have continuous events in Rx and use logical operators to combine them. It's pretty much like analog signal in electronics. Let me elaborate on this:
Continuous Observables
Currently, we have discrete events on streams, e.g. a marble diagram for some xhr request would look like this:
Continuous events would have a Start and optionally an End, heres a marble diagram:
Logical operators
This continuity in time would let us apply logical operators to the events, e.g:
Loading is just a NOT of xhr:
loading = ! xhr
And to filter mouse clicks during loading, we just apply an AND operator:
loading & click
Mouse press example
Another example use case is with events that form on-off pairs, like mousedown-mouseup would form a continuous mousepress events stream:
analog(mousedown, mouseup)
Imagine we have a timer and a button, and we should show the timer only when the button is pressed:
So we start with creating a regular timer:
timer(0, 1_000)
and we prolong it to make it continuous:
analog( timer(0, 1_000) )
now, to create a stream that produces a value only when mouse is pressed we apply AND operator:
mousepress & timer
Here's an online playground for this example: https://stackblitz.com/edit/kosich-current-rx
Other operators
NOT, AND, OR, XOR — are obvious candidates for operators. And we could have others that are time specific, like "filter As that start or end within Bs".
Implementation
I imagine two Observable constructors:
analog
anddiscrete
that would turn continuous events into discrete ones and vice versa. And a continuousObservable<Value>
is just an observable of on-s and off-s:Observable<On<Value> | Off>
.A naive implementation is available in the above-mentioned stackblitz example, please take a look at the
./continuous
folder: https://stackblitz.com/edit/kosich-current-rx?file=continuous%2Fcore.tsSummary
It's not an original idea: at least electronic circuits work this way. And it doesn't introduce any new capabilities, so I have doubts if it fits into the core Rx.
Though it offers a simpler model of thinking when operating on events with time-spans. An Rx newbie probably won't be able to implement examples above right away, but with logical operators — it's quite simple!
Would be happy to discuss this, please share your thoughts 🙂
Beta Was this translation helpful? Give feedback.
All reactions