Skip to content
Eugene Ghanizadeh edited this page Oct 21, 2021 · 5 revisions
function event(node: EventTarget, name: string, options?: boolean | AddEventListenerOptions): Source<Event>

Listens to specified DOM events on given HTML element, and emits them.

HTML Code
<button>Click Me!</button>
<div></div>
import { pipe, event, scan, observe, tap } from 'streamlets'

const button = document.querySelector('button')
const div = document.querySelector('div')

pipe(
  event(button, 'click'),
  scan(c => c + 1, 0),
  tap(count => div.textContent = `Clicked ${count} times.`),
  observe
)

Try in Sandbox

💡 event() also accepts the same options as addEventListener():

pipe(
  event(window, 'scroll', { passive: true }),
  // ...
)