Widdershin / cycle-restart

Swap out the code in your Cycle.js apps on the fly!
MIT License
123 stars 11 forks source link

Support drivers that use startWith #30

Open Widdershin opened 8 years ago

ronag commented 8 years ago

What are the implications?

Widdershin commented 8 years ago

I have this keys driver in one of my projects:

function makeKeysDriver () {
  const keydown$ = Observable.fromEvent(document, 'keydown');
  const keyup$ = Observable.fromEvent(document, 'keyup');

  function isKey (key) {
    return (event) => {
      return event.keyCode === key;
    }
  }

  return function keysDriver () {
    return {
      isDown: (key) => {
        return Observable.merge(
          keydown$.filter(isKey(key)).map(_ => true),
          keyup$.filter(isKey(key)).map(_ => false)
        ).startWith(false)
      }
    }
  }
}

Note the startWith(false). cycle-restart subscribes to this driver, but before the application has subscribed, so the initial false value never makes it to the app. As such, I have to press all the keys I've subscribed to before a combineLatest will actually start up.

It's really a hot vs cold thing. It just so happens that the majority of drivers are hot so mostly there isn't an impact.