Skip to main content

fromEventTarget

Types

function fromEventTarget<GName extends string, GEvent extends Event>(
target: ITypedPureEventTarget<TKeyValueTuple<GName, GEvent>>,
eventName: GName,
options?: AddEventListenerOptions,
): IObservable<GEvent>

Definition

Creates an Observable that emits events of a specific type coming from the given event target.

Diagram

Example

Display the mouse position on click

const subscribe = fromEventTarget(window, 'click');

subscribe((event: MouseEvent) => {
console.log('click: ', event.clientX, '-', event.clientY);
});

Output:

click: 245-499
click: 987-123
click: 71-268
...

Shortcuts

fromSelfEventTarget

function fromSelfEventTarget<GType extends string, GEvent extends Event>(
target: IReadonlyEventTarget<Record<GType, GEvent>>,
type: GType,
options?: boolean | AddEventListenerOptions,
): IObservable<GEvent>

Same as fromEventTarget but triggers only if event.target === target. This is useful if we want to listen for events only dispatched by the target itself.