Skip to content

Commit

Permalink
feat(rstream): add support for event listener opts in fromEvent()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jul 4, 2018
1 parent 9ffa346 commit d5ac264
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/rstream/src/from/event.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { Stream } from "../stream";

export function fromEvent(src: EventTarget, id: string) {
/**
* Creates a new stream of DOM events attached to given element / event
* target and using given event listener options (same as supported by
* `addEventListener()`, default: false).
*
* @param src event target
* @param name event name
* @param opts listener opts
*/
export function fromEvent(src: EventTarget, name: string, opts: boolean | AddEventListenerOptions = false) {
return new Stream<Event>((stream) => {
let listener = (e) => stream.next(e);
src.addEventListener(id, listener);
return () => src.removeEventListener(id, listener);
}, `event-${id}-${Stream.NEXT_ID++}`);
src.addEventListener(name, listener, opts);
return () => src.removeEventListener(name, listener, opts);
}, `event-${name}-${Stream.NEXT_ID++}`);
}

0 comments on commit d5ac264

Please sign in to comment.