Home

fromEvent

fromEvent is a function in RxJS that creates an observable from events emitted by a given event target. It attaches a listener for a specified event type on the target (typically via addEventListener) and emits each event object to subscribers. The listener is removed automatically when the subscription is disposed, helping to prevent memory leaks.

Usage and behavior: fromEvent(target, eventName) returns an observable that emits events of the requested type. For

Notes: fromEvent is commonly used for integrating imperative DOM events into reactive streams, enabling composition with

example,
fromEvent(document,
'click')
creates
an
observable
of
click
events
on
the
document.
This
pattern
also
works
with
other
EventTarget-compatible
objects,
such
as
elements,
window,
or
custom
objects
that
implement
the
standard
event
binding
interface.
An
optional
third
argument
can
alter
what
is
emitted:
a
selector
function
can
map
the
raw
event
to
a
custom
value,
so
fromEvent(target,
eventName,
selector)
may
emit
transformed
values.
Additionally,
an
options
argument
can
configure
addEventListener
behavior,
such
as
capture,
passive,
or
once,
for
example
fromEvent(element,
'scroll',
{
passive:
true
}).
operators
like
map,
filter,
debounceTime,
and
switchMap.
The
exact
overloads
and
supported
targets
can
vary
between
RxJS
versions,
so
consult
the
version-specific
documentation
for
precise
signatures
and
capabilities.
Overall,
fromEvent
provides
a
concise,
memory-safe
way
to
bridge
traditional
event
handling
with
reactive
programming.