[Observer API ํํค์น๊ธฐ] 6. ReportingObserver
[Observer API ํํค์น๊ธฐ] 6. ReportingObserver
ReportingObserver๋ ์น ๋ธ๋ผ์ฐ์ ์ ์ฑ๋ฅ ๋ฐ ์ฌ์ฉ์ฑ, ์์ ์ฑ ์์๋ฅผ ๊ฐ์งํ๋ ์ต์ ๋ฒ๋ค. ํ์ด์ง์์ ๋ฐ์ํ ์ ์๋ ๋ฌธ์ ์ ์ ์ค์๊ฐ์ผ๋ก ๋ชจ๋ํฐ๋งํ ๋ ์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์๋ค.
TYPESCRIPT
const ro = new ReportingObserver(callback, options); ro.observe();
์ต์ ๋ฒ ์ ์ธ ์, options์ ์ ๋ฌํ์ฌ ์ธ๋ถ ์ฌํญ์ ์กฐ์ ํ ์ ์๋ค. ReportingObserverOptions ํ์ ์ ๊ฐ์ง๋ค.
์ต์ ๋ฒ์์ ์ ๊ณตํ ๋ณด๊ณ ์ ํ์ ์ ๋ฐฐ์ด๋ก ์ง์ ํ๋ค. ์ง์ ๊ฐ๋ฅํ ๊ฐ์ deprecation, intervention ๋ ๊ฐ์ง๋ค.
- deprecation (์ฌ์ฉ ์ค๋จ ๋ณด๊ณ ์): ๋ธ๋ผ์ฐ์ ์์ deprecated ๋๊ฑฐ๋, ๋ ์์ ์ธ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๊ณ ์์ ๊ฒฝ์ฐ, ๊ด๋ จ ๋ณด๊ณ ์๋ฅผ ์ ๊ณตํจ.
- intervention (๊ฐ์ ๋ณด๊ณ ์): ๋ธ๋ผ์ฐ์ ๊ฐ ํน์ ๋์์ ๊ฐ์ ํด ๋์์ ์์ ํ๊ฑฐ๋, ์ฐจ๋จํ์ ๊ฒฝ์ฐ, ๊ด๋ จ ๋ณด๊ณ ์๋ฅผ ์ ๊ณตํจ.
ReportingObserver๊ฐ ์ ์ธ๋๊ธฐ ์ด์ ์ ๋ณด๊ณ ์๋ ํฌํจํ๋์ง ์ฌ๋ถ๋ฅผ ์ง์ ํจ. ๊ธฐ๋ณธ์ ์ผ๋ก false๊ฐ์ ๊ฐ์ง.
์ฝ๋ฐฑ ๋ฉ์๋๋ก ๊ด๋ จ ๋ณด๊ณ ์๋ฅผ ๋ค๋ฃฐ ์ ์๋ค. ReportingObserverCallback ํ์ ์ ๊ฐ์ง๋ค.
๋ฐ์ํ ๋ณด๊ณ ์ ๋ฐฐ์ด์ ๋ฐํํ๋ค. Report ํ์ ์ ๊ฐ์ง๋ค.
- type: ๋ณด๊ณ ์ ํ์ ์ ๋ฐํํ๋ค. deprecation, intervention ์ค ํ๋๋ฅผ ๊ฐ๋๋ค.
- url: ๋ณด๊ณ ์ ๋์ URL์ ๋ฐํํ๋ค. ์ผ๋ฐ์ ์ผ๋ก ํ์ฌ ํ์ด์ง URL์ด ๋ด๊ฒจ์๋ค.
- body: ๋ณด๊ณ ์ ๋ด์ฉ์ ๋ฐํํ๋ค. ๊ฐ ๋ณด๊ณ ์์ ๋ฐ๋ผ ๋ด์ฉ์ด ๋ฐ๋๋ค.
์ต์ ๋ฒ ์๊ธฐ ์์ ์ ๋ฐํํ๋ค. ์ด๋ฅผ ํตํด ์ฝ๋ฐฑ ๋ฉ์๋์์ ์ต์ ๋ฒ์ ์ ๊ทผํ ์ ์๋ค.
ReportingObserver๋ฅผ ์ปค์คํ ํ ์ ํตํด ๊ฐํธํ๊ฒ ์ฌ์ฉํด๋ณด์.
TYPESCRIPT
export function useReportingObserver(): void { // }
์์ ๊ฐ์ด useReportingObserver ๋ฉ์๋๋ฅผ ์ ์ํ๋ค.
useReportingObserver๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์ ์๋์ ๋ ์์๊ฐ ํ์ํ๋ค.
- ์ฝ๋ฐฑ ๋ฉ์๋
- ์ต์
PerformanceObserver์ ๋ง์ฐฌ๊ฐ์ง๋ก DOM์ด ํ์์๋ค.
TYPESCRIPT
import { useEffect } from "react"; export type UseReportingObserverCallback = (entry: Report) => void; /** * ReportingObserver ์ ์ฉ ํ ๋ฉ์๋ * * @param {UseReportingObserverCallback} callback: ์ฝ๋ฐฑ ๋ฉ์๋ * @param {PerformanceObserverInit} options: ์ต์ */ export function useReportingObserver( callback: UseReportingObserverCallback, options?: ReportingObserverOptions ): void { useEffect(() => { const ro = new ReportingObserver((reports) => { reports.forEach(callback); }, options); }, [callback, options]); }
ReportingObserver๋ฅผ ์ด๊ธฐํํ์ฌ ro ๋ณ์๋ก ํ ๋นํ๋ค.
ro.observe() ๋ฉ์๋๋ฅผ ํตํด ํ๊ทธ๋ฅผ ๋ฑ๋กํ ์ ์๋ค.
TYPESCRIPT
import { useEffect } from "react"; export type UseReportingObserverCallback = (entry: Report) => void; /** * ReportingObserver ์ ์ฉ ํ ๋ฉ์๋ * * @param {UseReportingObserverCallback} callback: ์ฝ๋ฐฑ ๋ฉ์๋ * @param {PerformanceObserverInit} options: ์ต์ */ export function useReportingObserver( callback: UseReportingObserverCallback, options?: ReportingObserverOptions ): void { useEffect(() => { const ro = new ReportingObserver((reports) => { reports.forEach(callback); }, options); ro.observe(); }, [callback, options]); }
๋ง์ง๋ง์ผ๋ก, ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง ๋ ๋๋ง๋ค ReportingObserver์ ๋ฑ๋ก์ด ์ค์ฒฉ๋์ง ์๋๋ก, ์ด๊ธฐํ ์ฝ๋๋ฅผ ๋ฃ์ด์ค๋ค.
ro.disconnect() ๋ฉ์๋๋ฅผ ํตํด ReportingObserver๋ฅผ ์ ๊ฑฐํ ์ ์๋ค.
TYPESCRIPT
import { useEffect } from "react"; export type UseReportingObserverCallback = (entry: Report) => void; /** * ReportingObserver ์ ์ฉ ํ ๋ฉ์๋ * * @param {UseReportingObserverCallback} callback: ์ฝ๋ฐฑ ๋ฉ์๋ * @param {PerformanceObserverInit} options: ์ต์ */ export function useReportingObserver( callback: UseReportingObserverCallback, options?: ReportingObserverOptions ): void { useEffect(() => { const ro = new ReportingObserver((reports) => { reports.forEach(callback); }, options); ro.observe(); return () => { ro.disconnect(); }; }, [callback, options]); }
์ ์ฒด ์ฝ๋๋ ์์ ๊ฐ๋ค.
CodeSandbox๋ก ๊ฐ๋จํ ์์๋ฅผ ๊ตฌํํ๋ค.
ReportingObserver๋ ์๋น์ค์์ ๋ฐ์ํ๊ฑฐ๋ ๋ฐ์ํ ์ ์๋ ์ ์ฌ์ ์ธ ์ค๋ฅ๋ฅผ ๋ชจ๋ํฐ๋งํ ์ ์๋ ์ต์ ๋ฒ๋ค.
์ด ์ต์ ๋ฒ๋ฅผ ์ ์ ํ ํ์ฉํ๋ฉด ๊ฐ๋จํ๊ฒ ์์ฒด์ ์ธ ์ค๋ฅ ๋ชจ๋ํฐ๋ง ๋ชจ๋์ ๊ตฌํํ ์ ์์ ๊ฒ์ด๋ค.
๊ทธ๋ฌ๋ ์ด๋ฏธ ์ฝ๋ ํ์ง ๋ฐ ์๋น์ค ์์ ์ฑ์ ๊ฒ์ฆํ๋ ์ฌ๋ฌ ์คํ/์์ฉ ์ํํธ์จ์ด๊ฐ ์กด์ฌํ๊ธฐ๋ ํ๊ณ , ๊ฒฐ์ ์ ์ผ๋ก ReportingObserver์ ๋ํ ์ ๋ณด๊ฐ ์๋นํ ๋ฏธํกํ๋ค.
์ด ์ต์ ๋ฒ ๋ํ PerformanceObserver์ ๋ง์ฐฌ๊ฐ์ง๋ก, ์ด๋ฐ ๊ฒ ์๋ค ์ ๋๋ก๋ง ์ง๊ณ ๋์ด๊ฐ๋, ๊ฐ๋ฐ์ ํฐ ์ง์ฅ์ ์์ ๊ฒ์ด๋ผ ์๊ฐํ๋ค.
6ํธ์ ๊ฑธ์น Observer API ์๋ฆฌ์ฆ๊ฐ ์๊ฒฐ๋๋ค. ์ค์ ๋ก ๊ฐ๋ฐ์ ์งํํ๋ฉด์, ๋ช๋ช Event Driven ๊ธฐ๋ฐ์ ์ฝ๋๋ฅผ Observer API๋ก ๋์ฒดํ๊ณ ์๋ค. ์ปดํฌ๋ํธ ๋ฐ ๊ธฐ๋ฅ ๊ฐ๋ฐ์ ์์ด์ ๋์ฑ ํจ์จ์ ์ด๊ณ , ๊ฐํธํ ๋ก์ง์ ๊ตฌํํ๊ธฐ ์ฉ์ดํ๋ ๊ฒ ๊ฐ๋ค.
ํนํ, ์์ฆ ๊ฐ๋ฐํ๋ ๋๋ถ๋ถ์ ๊ฐ์ธ ํ๋ก์ ํธ์, ์ฃผ์ ์ต์ ๋ฒ๋ฅผ React ํ ์ผ๋ก ๋ง๋ค์ด ์ฌ์ฉํ๊ณ ์๋๋ฐ, DOM ๋ฑ๋ก ๊ท์ฐฎ์ ์ฝ๋๋ฅผ ๊ฐ๋จํ๊ฒ ์ค์ผ ์ ์๋ ์ ์ด ๋งค๋ ฅ์ ์ด์๋ค.
์์ผ๋ก ์ข ์ข React ์ปดํฌ๋ํธ์ ๊ด๋ จ๋ ๊ธ์ ์์ฑํ ์์ ์ธ๋ฐ, ์ด์ ์ Observer์ ๊ด๋ จ๋ ๋ด์ฉ์ ์ง๊ณ ๋์ด๊ฐ์ผ ๋ช๋ช ์ปดํฌ๋ํธ ์ค๋ช ์ด ๋งค๋๋ฌ์์ง ๊ฒ์ด๋ผ ํ๋จํ๋ค.
์์ผ๋ก ์์ ์ปดํฌ๋ํธ ์ค๋ช ๊ณผ ๋๋ถ์ด, ์ปดํฌ๋ํธ์ ๊ด๊ณ์์ด ์ด ๊ธ์ ์ฝ๋ ๋ถ๋ค์ด, Observer API๋ฅผ ํตํด ๊ธฐ๋ฅ ๊ฐ๋ฐ์ ๋ ๋ง์ ์ธ์ฌ์ดํธ๋ฅผ ์ป๊ธธ ๋ฐ๋๋ค.