Last active
March 23, 2017 14:16
-
-
Save nicoespeon/b02d3a83c237a4119d4b631ed2ba75f4 to your computer and use it in GitHub Desktop.
Blog - Using Observables to make our app work with barcode scanners
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ENTER_KEY_CODE = 13 | |
const MAX_INTERVAL_BETWEEN_EVENTS_IN_MS = 50 | |
const keyCode$ = Rx.Observable.fromEvent(document, "keypress") | |
.pluck('keyCode') | |
const keyCodesBuffer$ = keyCode$ | |
.buffer(keyCode$.debounce(MAX_INTERVAL_BETWEEN_EVENTS_IN_MS)) | |
.filter(isFromScan) | |
function isFromScan(keyCodes) { | |
return keyCodes.length > 1 && keyCodes[keyCodes.length - 1] === ENTER_KEY_CODE | |
} | |
function fillInputWith(keyCodes) { | |
// … | |
} | |
keyCodesBuffer$.subscribe(fillInputWith) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment