WICG / observable

Observable API proposal
https://wicg.github.io/observable/
Other
569 stars 13 forks source link

TC39 iterator helpers? #1

Closed benlesh closed 1 year ago

benlesh commented 1 year ago

With the TC39 (Currently Stage 3) proposal for iterator helpers: https://github.com/tc39/proposal-iterator-helpers, this addition becomes much, much more useful. Take a drag and drop, for example:

element.on('mousedown').flatMap(e => {
  const { left, top } = element.getBoundingClientRect()
  const { clientX: startX, clientY: startY } = e;
  return document.on('mousemove')
    .takeUntil(document.on('mouseup'))
    .map(e => [
      left + e.clientX - startX,
      top + e.clientY - startY
    ])
})
.subscribe(([updateX, updateY]) => {
  element.style.transform = `translate3d(${updateX)px, ${updateY}px, 0)`;
})
domfarolino commented 1 year ago

Check out the Operators & combinators section and let me know what you think as I've made a reference to that proposal there. Maybe that is enough to "close" this issue, since as we discussed elsewhere, resolving this would essentially entail implementing those operators independently on Observable.prototype (not actually implementing the Iterator interface or anything).

domfarolino commented 1 year ago

Going to close this for now but please re-open if there is more we can do here.