Skip to content

Commit

Permalink
feat(rstream): add fromDOMEvent()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 4, 2019
1 parent aa10de0 commit 6e3fec8
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/rstream/src/from/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Stream } from "../stream";
import { nextID } from "../utils/idgen";

/**
* Creates a new stream of DOM events attached to given element / event
* Creates a new stream of events attached to given element / event
* target and using given event listener options (same as supported by
* `addEventListener()`, default: false).
*
Expand All @@ -20,3 +20,24 @@ export const fromEvent = (
src.addEventListener(name, listener, opts);
return () => src.removeEventListener(name, listener, opts);
}, `event-${name}-${nextID()}`);

/**
* Same as `fromEvent`, however only supports well-known DOM event
* names. Returned stream instance will use corresponding concrete event
* type in its type signature, whereas `fromEvent` will only use the
* generic `Event`.
*
* ```
* fromDOMEvent(document.body, "mousemove"); // Stream<MouseEvent>
* fromEvent(document.body, "mousemove"); // Stream<Event>
* ```
*
* @param src
* @param name
* @param opts
*/
export const fromDOMEvent = <K extends keyof GlobalEventHandlersEventMap>(
src: EventTarget,
name: K,
opts: boolean | AddEventListenerOptions = false
): Stream<GlobalEventHandlersEventMap[K]> => fromEvent(src, name, opts);

0 comments on commit 6e3fec8

Please sign in to comment.