In Javascript and using Kefir, I'd like to capture all key inputs until ENTER
is hit. So far, I've managed to do this using bufferWhile
like
var inputValues = Kefir
.fromEvents(document.querySelector('body'), 'keydown')
.bufferWhile(event => event.keyCode!=13);
var result = inputValues.toProperty(() => "");
result
.onValue(x => elm.innerHTML = x.slice(0,-1).map(y => String.fromCharCode(y.keyCode)).join(''))
.onError(() => elm.innerHTML = "?");
but initially I wanted to use a regular scan
as in
var inputValues = Kefir
.fromEvents(document.querySelector('body'), 'keydown')
.scan((acc, y) => acc.concat(y), "");
but then how do I:
- Output the accumulator when
ENTER
is hit? - Restart the accumulator to start a new keystroke sequence?
essentially, how do you compose bufferWhile
using scan
and a single stream? The answer doesn't have to be specifically about Kefir though, any FRP pseudo-code will do.