-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rstream): add support for event listener opts in
fromEvent()
- Loading branch information
1 parent
9ffa346
commit d5ac264
Showing
1 changed file
with
13 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++}`); | ||
} |