Skip to content

Instantly share code, notes, and snippets.

@m1m1s1ku
Last active September 6, 2022 08:29
Show Gist options
  • Save m1m1s1ku/00672d1e771c556c2e4c13f7c79c69ed to your computer and use it in GitHub Desktop.
Save m1m1s1ku/00672d1e771c556c2e4c13f7c79c69ed to your computer and use it in GitHub Desktop.
Konami RxJS 7
const table: {[key: string]: number} = {
ArrowUp: 38,
ArrowDown: 40,
ArrowLeft: 37,
ArrowRight: 39,
KeyB: 66,
// Azerty compat
KeyQ: 65,
KeyA: 65,
};
const knownSequence = from([38, 38, 40, 40, 37, 39, 37, 39, 66, 65]);
const x$ = fromEvent<KeyboardEvent>(document, 'keyup').pipe(
map((e) => table[e.code] ? table[e.code] : -1),
skipWhile((k) => k !== 38),
bufferCount(10, 1),
mergeMap((x) => {
return from(x).pipe(sequenceEqual(knownSequence));
}),
filter((sequenceEqual) => sequenceEqual),
switchMap((_) => of('konami'))
);
await firstValueFrom(x$);
// Do x thing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment