DEV Community

artydev
artydev

Posted on

Reacting to streams

We have now our basic stream, how can we react on it ?
Always with simplicity in mind, here is a solution among others.


function Stream (value) {
  let storedvalue = value
  let mappers = []
  function stream (newvalue) {
    if (arguments.length) {
      mappers.map(f => f(newvalue))
      storedvalue = newvalue
    }
    return storedvalue
  }
  stream.map = function (f) {
    mappers.push(f)
  }
  return stream
}

s = Stream()

document.addEventListener("mousemove", s)

s.map(t => divmouse.innerHTML = (`<h2>(${t.clientX}, ${t.clientY})</h2>`)) 

You can test it here reactions

Top comments (1)

Collapse
 
artydev profile image
artydev

Hy Adam,
I am very glad it helped you, that is exactly why I post ;-)
Regards