ํฐ์คํ ๋ฆฌ ๋ทฐ
https://choi95.tistory.com/115
axios Intercept requests_API ์์ฒญ ์ฒ๋ฆฌ ๋์ UI ์์ ๋ณด์ฌ ์ฃผ๊ธฐ
๊ธฐ์กด ์ฝ๋ $searchInput.addEventListener('keyup', async function(event) { $loadingIndicator.style.visibility = 'visible'; //loading UI ์น ์์ ๋๋๋ง await searchKeyword(event.target.value, searchKey..
choi95.tistory.com
์ด์ ํฌ์คํ ์์ API ์์ฒญ ์ฒ๋ฆฌ ๋์์ loading UI๋ฅผ ๋๋๋งํด์ฃผ๊ณ API ์์ฒญ์ด ๋๋๋ฉด ๋ค์ ์ฌ๋ผ์ง๋๋ก axios Intercept requests์ await๋ฅผ ์ด์ฉํ ๋น๋๊ธฐ ์ฒ๋ฆฌ๋ฅผ ํตํด ํด๋น ๊ธฐ๋ฅ์ ๊ตฌํํ์๋ค.
ํ์ง๋ง ์ด์ธ์๋ rxjs operator ์ค tap์ ์ด์ฉํด์๋ ํด๋น ๊ธฐ๋ฅ์ ๊ตฌํํ ์ ์์์ ํ์ธํ์๋ค.
rxjs tap operator
https://rxjs.dev/api/operators/tap
RxJS
rxjs.dev
Tap is designed to allow the developer a designated place to perform side effects. While you could perform side-effects inside of a map or a mergeMap, that would make their mapping functions impure, which isn't always a big deal, but will make it so you can't do things like memoize those functions. The tap operator is designed solely for such side-effects to help you remove side-effects from other operations.
For any notification, next, error, or complete, tap will call the appropriate callback you have provided to it, via a function reference, or a partial observer, then pass that notification down the stream.
tap operator๋ sequential ํ๊ฒ ๋ฐ์ํ๋ ๋ฐ์ดํฐ ์คํธ๋ฆผ์ ํ๋ฆ์ ๋ฐฉํดํ์ง ์์ ์ฑ ๋ถ์์ ์ธ ์ธก๋ฉด์์ ๋์์ ์ผ๋ก ์ผ์ด๋๊ธฐ ๋๋ฌธ์ ๋ค์ํ ์ํฉ์์์ ์ฌ๊ฑด์ ์ฒ๋ฆฌํ ์ ์๋ค.
const inputStream = fromEvent($searchInput, "input").pipe(
map((event) => event.target.value),
debounceTime(500),
distinctUntilChanged(),
tap(() => ($loadingIndicator.style.visibility = 'visible')),
switchMap((query) =>
ajax(`${API_URL}?q=${query}`, {method: 'GET'}).pipe(
map(({response}) => response)
)
),
tap(() => ($loadingIndicator.style.visibility = 'hidden'))
);
https://rxjs.dev/api/operators/switchMap
RxJS
rxjs.dev
Returns an Observable that emits items based on applying a function that you supply to each item emitted by the source Observable, where that function returns an (so-called "inner") Observable. Each time it observes one of these inner Observables, the output Observable begins emitting the items emitted by that inner Observable. When a new inner Observable is emitted, switchMap stops emitting items from the earlier-emitted inner Observable and begins emitting items from the new one. It continues to behave like this for subsequent inner Observables.
์ฐธ๊ณ ๋ก switchMap operator๋ ๊ตฌ๋ ์ค์ด๋ Observable์ด ๋๋๊ธฐ ์ ์ ์๋ก์ด Observable๋ฅผ ๊ตฌ๋ ํ๋ฉด ์ด์ ์ ๊ตฌ๋ ํ๊ณ ์Observable์ ๊ตฌ๋ ์ทจ์ํ๊ณ ๋ค์ Observable์ ๊ตฌ๋ ํ๋ค.
switchMap์ ๊ฒฝ์ฐ๋ ์๋ฒ์ ์์ฒญ์ ํ๋ค๊ฐ ์ทจ์ํ๊ณ ๋ค๋ฅธ ์์ฒญ์ ๋ณด๋ด์ผ ํ๋ ์ํฉ์ฒ๋ผ ์งํ ์ค์ธ ์์ ์ ์ทจ์ํ๋ ๊ฒฝ์ฐ์ ์ ํฉํ๋ค.
๊ด๋ จ ์ฌํญ
RxJS debounceTime operator๋ฅผ ํตํด Event request ์ค์ด๊ธฐ
๊ฒ์์ด๋ฅผ ์ ๋ ฅํ๋ ๋์์ ์ํฐํค, ๊ฒ์ ๋ฒํผ์ ๋๋ฅผ ํ์ ์์ด ๊ฒฐ๊ณผ๊ฐ์ ์ฆ์ ๋ฐ์ํ ์ ์๋๋ก ๋ค์๊ณผ ๊ฐ์ด ๊ตฌํํ์๋ค. const dispatchEvent = debounce((targetText) => { this.callback(targetText); }, thi..
choi95.tistory.com
'JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ณ์ UI (0) | 2021.08.28 |
---|---|
๋ฐฑ์ค node.JS ์ ์ถ๋ ฅ ๊ด๋ฆฌ (0) | 2021.08.22 |
axios Intercept requests_API ์์ฒญ ์ฒ๋ฆฌ ๋์ UI ์์ ๋ณด์ฌ ์ฃผ๊ธฐ (0) | 2021.08.03 |
formData์ ๊ฐ์ ์ฐธ์กฐ ๋ฐ ์ฌ์ฉ (0) | 2021.07.30 |
CustomEvent๋ก ํน์ ์ด๋ฒคํธ ๊ธฐ๋ฅ ๋ถ๋ฆฌ (0) | 2021.07.28 |