ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

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์˜ ๊ฒฝ์šฐ๋Š” ์„œ๋ฒ„์— ์š”์ฒญ์„ ํ–ˆ๋‹ค๊ฐ€ ์ทจ์†Œํ•˜๊ณ  ๋‹ค๋ฅธ ์š”์ฒญ์„ ๋ณด๋‚ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ฒ˜๋Ÿผ ์ง„ํ–‰ ์ค‘์ธ ์ž‘์—…์„ ์ทจ์†Œํ•˜๋Š” ๊ฒฝ์šฐ์— ์ ํ•ฉํ•˜๋‹ค.

 

๊ด€๋ จ ์‚ฌํ•ญ

https://choi95.tistory.com/89

 

RxJS debounceTime operator๋ฅผ ํ†ตํ•ด Event request ์ค„์ด๊ธฐ

๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋™์‹œ์— ์—”ํ„ฐํ‚ค, ๊ฒ€์ƒ‰ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ํ•„์š” ์—†์ด ๊ฒฐ๊ณผ๊ฐ’์„ ์ฆ‰์‹œ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•˜์˜€๋‹ค. const dispatchEvent = debounce((targetText) => { this.callback(targetText); }, thi..

choi95.tistory.com

 

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ