Skip to content

Instantly share code, notes, and snippets.

@Sawtaytoes
Created April 30, 2019 00:03
Show Gist options
  • Save Sawtaytoes/d4955f87aa746cefc7c3f8589f1d7905 to your computer and use it in GitHub Desktop.
Save Sawtaytoes/d4955f87aa746cefc7c3f8589f1d7905 to your computer and use it in GitHub Desktop.
const codeSequenceEpic = (
action$,
) => (
action$
.pipe(
ofType('START_CODE_SEQUENCE_LISTENER'),
mergeMap(({
codeSequence,
}) => (
action$
.pipe(
takeUntil(
action$
.pipe(
ofType('STOP_CODE_SEQUENCE_LISTENER'),
filter(({
codeSequence: stopSequence,
}) => (
stopSequence === codeSequence
)),
)
),
ofType('ON_KEYPRESS'),
mergeMap(({
pressedKey: firstPressedKey,
}) => (
action$
.pipe(
takeUntil(
action$
.pipe(
ofType('STOP_CODE_SEQUENCE_LISTENER'),
filter(({
codeSequence: stopSequence,
}) => (
stopSequence === codeSequence
)),
)
),
ofType('ON_KEYPRESS'),
pluck('pressedKey'),
startWith(firstPressedKey),
scan(
(
{ codeSequenceIndex },
pressedKey,
) => ({
codeSequenceIndex: codeSequenceIndex + 1,
done: codeSequenceIndex === codeSequence.length - 1,
isMatching: pressedKey === codeSequence[codeSequenceIndex],
}),
{
codeSequenceIndex: 0,
}
),
takeWhile(({
isMatching,
}) => (
isMatching
)),
filter(({
done,
}) => (
done
)),
mapTo([
{
codeSequence,
type: 'STOP_CODE_SEQUENCE_LISTENER',
},
{
codeSequence,
type: 'ACTIVATED_CODE_SEQUENCE',
},
]),
mergeAll(),
)
)),
)
)),
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment