๐Ÿ‡ฐ๐Ÿ‡ท Learn RxJS
Ctrlk
  • ์‹œ์ž‘ํ•˜๊ธฐ
  • RxJS ๋ฐฐ์›Œ๋ณด๊ธฐ
    • RxJS ๊ฐœ๋…
    • Operator(์—ฐ์‚ฐ์ž)
    • Subjects
    • ๋ ˆ์‹œํ”ผ
      • ์˜์–ด ํƒ€์ž ์—ฐ์Šต ๊ฒŒ์ž„
      • Battleship ๊ฒŒ์ž„
      • ๋ฒ„๋ธ” ๊ฒŒ์ž„
      • ์นด๋ ˆ์ด์‹ฑ ๊ฒŒ์ž„
      • ์ ์„ ์žก์•„๋ผ!
      • ํด๋ฆญ์ขŒ ๊ฒŒ์ž„
      • ๋‚ ์•„๋ผ ์ƒˆ ๊ฒŒ์ž„
      • ๊ฒŒ์ž„ ๋ฃจํ”„
      • ๊ฐ€๋กœ ์Šคํฌ๋กค ํ‘œ์‹œ ๋ฐ”
      • HTTP ํด๋ง
      • ์ž ๊ธˆํ™”๋ฉด
      • ๋งคํŠธ๋ฆญ์Šค ๋””์ง€ํ„ธ ๋น„ ํšจ๊ณผ
      • ๊ธฐ์–ต๋ ฅ ๊ฒŒ์ž„
      • ์ง€๋ขฐ ์ฐพ๊ธฐ
      • ์ ํ”„ ๊ฒŒ์ž„
      • Progress ๋ฐ”
      • ์ €์žฅ ํ‘œ์‹œ
      • ๋˜‘๋˜‘ํ•œ ์นด์šดํ„ฐ
      • ์Šคํ†ฑ์›Œ์น˜
      • ์ŠคํŽ˜์ด์Šค ์ธ๋ฒ ์ด๋”
      • ์ƒˆ๋กœ๊ณ ์นจํ•˜๋ ค๋ฉด ์Šค์™€์ดํ”„ํ•˜์„ธ์š”
      • 2์ธ์šฉ ํƒฑํฌ ๊ฒŒ์ž„
      • ํ…ŒํŠธ๋ฆฌ์Šค
      • ๋ฏธ๋ฆฌ ์ž…๋ ฅ
      • ์ด๋ฏธ์ง€ ๋ฎ๊ธฐ ๊ฒŒ์ž„
Powered by GitBook
On this page
Edit
  1. RxJS ๋ฐฐ์›Œ๋ณด๊ธฐ
  2. ๋ ˆ์‹œํ”ผ

์Šคํ†ฑ์›Œ์น˜

By adamlubek

This recipe demonstrates RxJS implementation of Stop Watch, inspired by RxJS Advanced Patterns โ€“ Operate Heavily Dynamic UIโ€™s talk by @Michael_Hladky

Ultimate RxJS

Example Code

( StackBlitz )

Stop Watch

index.ts

index.html

Operators Used

  • fromEvent

  • interval

  • map

  • mapTo

  • merge

  • NEVER

  • noop

  • scan

  • startWith

  • switchMap

  • tap

Previous๋˜‘๋˜‘ํ•œ ์นด์šดํ„ฐNext์ŠคํŽ˜์ด์Šค ์ธ๋ฒ ์ด๋”

Last updated 3 years ago

  • Example Code
  • Operators Used
// RxJS v6+
import { fromEvent, interval, merge, noop, NEVER } from 'rxjs';
import { map, mapTo, scan, startWith, switchMap, tap } from 'rxjs/operators';

interface State {
  count: boolean;
  countup: boolean;
  speed: number;
  value: number;
  increase: number;
}

const getElem = (id: string): HTMLElement => document.getElementById(id);
const getVal = (id: string): number => parseInt(getElem(id)['value']);
const fromClick = (id: string) => fromEvent(getElem(id), 'click');
const fromClickAndMapTo = (id: string, obj: {}) =>
  fromClick(id).pipe(mapTo(obj));
const fromClickAndMap = (id: string, fn: _ => {}) =>
  fromClick(id).pipe(map(fn));
const setValue = (val: number) =>
  (getElem('counter').innerText = val.toString());

const events$ = merge(
  fromClickAndMapTo('start', { count: true }),
  fromClickAndMapTo('pause', { count: false }),
  fromClickAndMapTo('reset', { value: 0 }),
  fromClickAndMapTo('countup', { countup: true }),
  fromClickAndMapTo('countdown', { countup: false }),
  fromClickAndMap('setto', _ => ({ value: getVal('value') })),
  fromClickAndMap('setspeed', _ => ({ speed: getVal('speed') })),
  fromClickAndMap('setincrease', _ => ({ increase: getVal('increase') }))
);

const stopWatch$ = events$.pipe(
  startWith({
    count: false,
    speed: 1000,
    value: 0,
    countup: true,
    increase: 1
  }),
  scan((state: State, curr): State => ({ ...state, ...curr }), {}),
  tap((state: State) => setValue(state.value)),
  switchMap((state: State) =>
    state.count
      ? interval(state.speed).pipe(
          tap(
            _ =>
              (state.value += state.countup ? state.increase : -state.increase)
          ),
          tap(_ => setValue(state.value))
        )
      : NEVER
  )
);

stopWatch$.subscribe();
<style>
  input,
  #counter,
  #controls {
    text-align: center;
    margin: auto;
  }

  #counter {
    font-size: 50px;
  }

  #controls {
    width: 50%;
  }
</style>

<div id="counter">0</div>
<div id="controls">
  <fieldset>
    <legend>Setup</legend>
    <button id="start">start</button>
    <button id="pause">pause</button>
    <button id="reset">reset</button>
  </fieldset>
  <fieldset>
    <legend>Count</legend>
    <button id="countup">count up</button>
    <button id="countdown">count down</button>
  </fieldset>
  <fieldset>
    <legend>Set to</legend>
    <input id="value" value="0"></input>
    <br/>
    <button id="setto">set value</button>
  </fieldset>
  <fieldset>
    <legend>Speed</legend>
    <input id="speed" value="1000"></input>
    <br/>
    <button id="setspeed">set speed</button>
  </fieldset>
  <fieldset>
    <legend>Increase</legend>
    <input id="increase" value="1"></input>
    <br/>
    <button id="setincrease">set increase</button>
  </fieldset>
</div>