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


๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๋™์‹œ์— ์—”ํ„ฐํ‚ค, ๊ฒ€์ƒ‰ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅผ ํ•„์š” ์—†์ด ๊ฒฐ๊ณผ๊ฐ’์„ ์ฆ‰์‹œ ๋ฐ˜์˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

const dispatchEvent = debounce((targetText) => {
            this.callback(targetText);
        }, this.delayTime);
        
this.textinputElement.addEventListener('keyup', (event) => {
	dispatchEvent(event.target.value);
});

keyup ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ Input ์š”์†Œ์— ๋“ฑ๋กํ•ด์ฃผ๊ณ  ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ๊ด€๋ จ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ๋„๋ก ํ•˜์˜€๋‹ค.

 

RxJS debounce operator

์œ„์™€ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ RxJS๋ฅผ ํ†ตํ•ด ๋ฆฌํŒฉํ† ๋งํ•˜์˜€๋‹ค.ํ˜„ ์†Œ์Šค ์ƒ์—์„œ๋Š” ์ฒ˜๋ฆฌํ•ด์•ผ ๋  ๋น„๋™๊ธฐ ์ž‘์—…์ด ๋งŽ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํฌ๊ฒŒ ์ƒ๊ด€์€ ์—†์ง€๋งŒ, ์ข€ ๋” ๋ช…ํ™•ํ•˜๊ณ  ํšจ์œจ์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋„๋ก ๋‹ค์ˆ˜์˜ ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•  ๊ฒฝ์šฐ์—๋Š” RxJS๋ฅผ ์‚ฌ์šฉํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

RxJS

  • RxJS๋Š” Reactive Extensions For JavaScrpt ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • RxJS๋Š” ์ด๋ฒคํŠธ ์ŠคํŠธ๋ฆผ์„ Observable ์ด๋ผ๋Š” ๊ฐ์ฒด๋กœ ํ‘œํ˜„ํ•œ ํ›„ ๋น„๋™๊ธฐ ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜์˜ ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑ์„ ์šฉ์ด์ผ€ ํ•จ

 Observable

  • ์‹œ๊ฐ„์„ ์ถ•์œผ๋กœ ์—ฐ์†์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ์ปฌ๋ ‰์…˜์„ ํ‘œํ˜„ํ•œ ๊ฐ์ฒด
  • ์ŠคํŠธ๋ฆผ(stream): ํŠน์ • ๊ฐ์ฒด๋ฅผ ๊ด€์ฐฐํ•˜๋Š” Observer์—๊ฒŒ ์—ฌ๋Ÿฌ ์ด๋ฒคํŠธ๋‚˜ ๊ฐ’์„ ๋ณด๋‚ด๋Š” ์—ญํ• ์„ ํ•จ

Observer

  • Observable์— ์˜ํ•ด ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์†Œ๋น„ํ•˜๋Š” ์ฃผ์ฒด
  • ๊ตฌ๋…(Subscription): Observable์ด ๋ฐฐ์ถœํ•˜๋Š” ํ•˜๋‚˜ ๋˜๋Š” ์—ฐ์†๋œ ํ•ญ๋ชฉ์— ๋ฐ˜์‘
  • ๋™์‹œ์„ฑ ์—ฐ์‚ฐ์„ ๊ฐ€๋Šฅ์ผ€ ํ•จ

Operator

  • Observable๋ฅผ ์ƒ์„ฑ ๋ฐ ์กฐ์ž‘ํ•˜๋Š” ํ•จ์ˆ˜
  • Observable๋ฅผ ์ƒ์„ฑ ๋ฐ ๊ฐ๊ฐ์˜ Observable์„ ์—ฐ๊ฒฐํ•˜๊ธฐ๋„ ํ•จ
  • ๋„ํŠธ์ฒด์ด๋‹: Operator๋Š” ํ˜„์žฌ Observable ์ธ์Šคํ„ด์Šค ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ญ์ƒ ์ƒˆ๋กœ์šด Observable ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜

์ด์ „์—๋Š” Input ํƒœ๊ทธ์— ์‹œ๊ฐ„์„ ์ •ํ•ด์„œ ์ง€์ •๋œ ์‹œ๊ฐ„ ๋‚ด์— ์—ฌ๋Ÿฌ ๋ฒˆ ๊ฒ€์ƒ‰์„ ํ•˜์—ฌ value๊ฐ€ ๋ณ€๊ฒฝ๋˜๋”๋ผ๋„, debounce ํ•จ์ˆ˜ ๋‚ด์— setTimeout์„ ํ†ตํ•ด ์˜๋„์ ์œผ๋กœ ๋”œ๋ ˆ์ด ์‹œ๊ฐ„์„ ๊ฑธ์–ด์ฃผ์–ด ์š”์ฒญ์„ ์ง€์—ฐ์‹œํ‚ค๋„๋ก ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

 

์ด๋ฅผ ๋Œ€์‹ ์— ์ด๋ฒˆ์—๋Š” RxJS operator์ธ debounceTime์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

debounceTime์€ output ์‚ฌ์ด์—์„œ ํŠน์ • ์‹œ๊ฐ„๋ณด๋‹ค ์ ์€ ์‹œ๊ฐ„์— ๋ฐฐ์ถœ๋œ ๊ฐ’๋“ค์„ ๋ฒ„๋ฆฐ๋‹ค.
const eventSource = fromEvent(this.textinputElement, 'keyup')
                .debounceTime(this.delayTime); //์ง€์ •๋œ ์‹œ๊ฐ„ ๋‚ด์—์„  observer์—์„œ ๊ฐ์ง€๋ฅผ ํ•˜๋”๋ผ๋„ ํ•ด๋‹น ๊ฐ’์„ ๋ฒ„๋ฆผ
            
         this.subscription = eventSource.subscribe((event) => {
                dispatchEvent(event.target.value);
        })

 

ํ˜„ ์†Œ์Šค ์ฝ”๋“œ ์ƒ์—์„œ๋Š” ๊ตฌํ˜„์ด ์•ˆ๋˜์–ด ์žˆ์ง€๋งŒ Input ํ•„๋“œ์˜ auto-complete๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” RxJS opserator ์ค‘์— distinctUntilChanged๊ฐ€ ์žˆ๋‹ค.  

https://www.learnrxjs.io/learn-rxjs/operators/filtering/distinctuntilchanged

 

distinctUntilChanged

 

www.learnrxjs.io

Only emit when the current value is different than the last.

 

distinctUntilChanged๋Š” ํ˜„์žฌ์˜ ๊ฐ’์ด ๋งˆ์ง€๋ง‰ ์ž…๋ ฅํ•œ ๊ฐ’๊ณผ ๋‹ค๋ฅผ ๋•Œ๋งŒ ๊ฐ’์„ ๋‚ด๋ณด๋‚ธ๋‹ค.

distinctUntilChanged๋Š” ์ด์ „์˜ value์™€ ํ˜„์žฌ value๊ฐ€ ๋‹ค๋ฅผ ๊ฒฝ์šฐ์—๋งŒ ํ•ด๋‹น ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ํ•ด์ฃผ๋Š” ์˜คํผ๋ ˆ์ดํ„ฐ์ธ๋ฐ, ์—ฌ๊ธฐ์„œ ์œ ์˜ํ•  ์ ์ด ์žˆ๋‹ค.

์šฐ๋ฆฌ๋Š” ๋ณดํ†ต Input ์š”์†Œ ๋‚ด์— ์žˆ๋Š” value ํ”„๋กœํผํ‹ฐ๋ฅผ ํ†ตํ•ด์„œ ํ‚ค์›Œ๋“œ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ• ๋•Œ ๋งˆ๋‹ค value ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋ฐ”์ธ๋”ฉ๋˜์–ด ์žˆ๋Š” event object_์ „์ฒด ์ด๋ฒคํŠธ์˜ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ๊ฐ’(์ฃผ์†Œ๊ฐ’)์€ ๋งค๋ฒˆ ๋‹ฌ๋ผ์ง€๊ธฐ ๋•Œ๋ฌธ์— distinctUntilChanged๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์—†๋‹ค(Observable์—์„œ ๋ฐฐ์ถœ๋˜๋Š” ๊ฐ’์€ event object ์ „์ฒด์ด๋‹ค).

 

์œ„์™€ ๊ฐ™์€ ์ฐธ์กฐ๊ฐ’์— ์˜ํ•œ ๋น„๊ต ๋Œ€์‹ ์— ์‹ค์ œ ๊ฐ’์— ์˜ํ•œ ๋น„๊ต๋ฅผ ์œ„ํ•ด์„œ map operator ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

observable
.map(event => event.target.value) //ํ˜„์žฌ ์ด๋ฒคํŠธ ๊ฐ์ฒด์˜ ์‹ค์ œ ๊ฐ’์œผ๋กœ
.debounceTime(delayTime)
.distinctUntilChanged()

https://rxjs.dev/api/operators/map

 

RxJS

 

rxjs.dev

Similar to the well known Array.prototype.map function, this operator applies a projection to each value and emits that projection in the output Observable.

 

map operator๋Š” ๋‹ค๋ฅธ API๋“ค๊ณผ ๋™์ผํ•˜๊ฒŒ ๋ฐœ์ƒํ•˜๋Š” ์š”์†Œ๋“ค์„ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•ด ์ค€๋‹ค.

๋ฐœ์ƒํ•˜๋Š” ๊ฐ’์„ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ ๋ฐ ๋Œ€์ฒดํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ์— ์ ํ•ฉํ•˜๋‹ค. 

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
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
๊ธ€ ๋ณด๊ด€ํ•จ