[Observer API ํŒŒํ—ค์น˜๊ธฐ] 6. ReportingObserver

โฐ 2024-06-30 (์ผ) 02:08:13

screener
์‹œ๋ฆฌ์ฆˆ ๋ชจ์•„๋ณด๊ธฐ
Observer API ํŒŒํ—ค์น˜๊ธฐ

6 / 6

Table of Contents

  • 1. ReportingObserver












ReportingObserver

ReportingObserver๋Š” ์›น ๋ธŒ๋ผ์šฐ์ €์˜ ์„ฑ๋Šฅ ๋ฐ ์‚ฌ์šฉ์„ฑ, ์•ˆ์ •์„ฑ ์š”์†Œ๋ฅผ ๊ฐ์ง€ํ•˜๋Š” ์˜ต์ €๋ฒ„๋‹ค. ํŽ˜์ด์ง€์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ ์„ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ชจ๋‹ˆํ„ฐ๋งํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

TYPESCRIPT

1
2
3
const ro = new ReportingObserver(callback, options);

ro.observe();

options

์˜ต์ €๋ฒ„ ์„ ์–ธ ์‹œ, options์„ ์ „๋‹ฌํ•˜์—ฌ ์„ธ๋ถ€ ์‚ฌํ•ญ์„ ์กฐ์ ˆํ•  ์ˆ˜ ์žˆ๋‹ค. ReportingObserverOptions ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค.

๋ณด๊ณ ์„œ ํƒ€์ž… (types)

์˜ต์ €๋ฒ„์—์„œ ์ œ๊ณตํ•  ๋ณด๊ณ ์„œ ํƒ€์ž…์„ ๋ฐฐ์—ด๋กœ ์ง€์ •ํ•œ๋‹ค. ์ง€์ • ๊ฐ€๋Šฅํ•œ ๊ฐ’์€ deprecation, intervention ๋‘ ๊ฐ€์ง€๋‹ค.

  • deprecation (์‚ฌ์šฉ ์ค‘๋‹จ ๋ณด๊ณ ์„œ): ๋ธŒ๋ผ์šฐ์ €์—์„œ deprecated ๋˜๊ฑฐ๋‚˜, ๋  ์˜ˆ์ •์ธ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์„ ๊ฒฝ์šฐ, ๊ด€๋ จ ๋ณด๊ณ ์„œ๋ฅผ ์ œ๊ณตํ•จ.
  • intervention (๊ฐœ์ž… ๋ณด๊ณ ์„œ): ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ํŠน์ • ๋™์ž‘์— ๊ฐœ์ž…ํ•ด ๋™์ž‘์„ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜, ์ฐจ๋‹จํ–ˆ์„ ๊ฒฝ์šฐ, ๊ด€๋ จ ๋ณด๊ณ ์„œ๋ฅผ ์ œ๊ณตํ•จ.

๊ธฐ์กด ๋ณด๊ณ ์„œ ํฌํ•จ ์—ฌ๋ถ€ (buffered)

ReportingObserver๊ฐ€ ์„ ์–ธ๋˜๊ธฐ ์ด์ „์˜ ๋ณด๊ณ ์„œ๋„ ํฌํ•จํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ์ง€์ •ํ•จ. ๊ธฐ๋ณธ์ ์œผ๋กœ false๊ฐ’์„ ๊ฐ€์ง.

callback

์ฝœ๋ฐฑ ๋ฉ”์„œ๋“œ๋กœ ๊ด€๋ จ ๋ณด๊ณ ์„œ๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค. ReportingObserverCallback ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค.

report

๋ฐœ์ƒํ•œ ๋ณด๊ณ ์„œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. Report ํƒ€์ž…์„ ๊ฐ€์ง„๋‹ค.

  • type: ๋ณด๊ณ ์„œ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. deprecation, intervention ์ค‘ ํ•˜๋‚˜๋ฅผ ๊ฐ–๋Š”๋‹ค.
  • url: ๋ณด๊ณ ์„œ ๋Œ€์ƒ URL์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ํ˜„์žฌ ํŽ˜์ด์ง€ URL์ด ๋‹ด๊ฒจ์žˆ๋‹ค.
  • body: ๋ณด๊ณ ์„œ ๋‚ด์šฉ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ฐ ๋ณด๊ณ ์„œ์— ๋”ฐ๋ผ ๋‚ด์šฉ์ด ๋ฐ”๋€๋‹ค.

observer

์˜ต์ €๋ฒ„ ์ž๊ธฐ ์ž์‹ ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ฝœ๋ฐฑ ๋ฉ”์„œ๋“œ์—์„œ ์˜ต์ €๋ฒ„์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

React์—์„œ ์ปค์Šคํ…€ ํ›…์œผ๋กœ ๊ฐ„ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ

ReportingObserver๋ฅผ ์ปค์Šคํ…€ ํ›…์„ ํ†ตํ•ด ๊ฐ„ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•ด๋ณด์ž.

TYPESCRIPT

1
2
3
4
export function useReportingObserver(): void
{
  //
}

์œ„์™€ ๊ฐ™์ด useReportingObserver ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•œ๋‹ค.

useReportingObserver๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„  ์•„๋ž˜์˜ ๋‘ ์š”์†Œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

  1. ์ฝœ๋ฐฑ ๋ฉ”์„œ๋“œ
  2. ์˜ต์…˜

PerformanceObserver์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ DOM์ด ํ•„์š”์—†๋‹ค.

TYPESCRIPT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

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
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๋กœ ํ†ตํ•œ ์˜ˆ์‹œ

CodeSandbox๋กœ ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋ฅผ ๊ตฌํ˜„ํ–ˆ๋‹ค.

๋งˆ์น˜๋ฉฐ

ReportingObserver๋Š” ์„œ๋น„์Šค์—์„œ ๋ฐœ์ƒํ•˜๊ฑฐ๋‚˜ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ž ์žฌ์ ์ธ ์˜ค๋ฅ˜๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋งํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์ €๋ฒ„๋‹ค.

์ด ์˜ต์ €๋ฒ„๋ฅผ ์ ์ ˆํžˆ ํ™œ์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž์ฒด์ ์ธ ์˜ค๋ฅ˜ ๋ชจ๋‹ˆํ„ฐ๋ง ๋ชจ๋“ˆ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.


๊ทธ๋Ÿฌ๋‚˜ ์ด๋ฏธ ์ฝ”๋“œ ํ’ˆ์งˆ ๋ฐ ์„œ๋น„์Šค ์•ˆ์ •์„ฑ์„ ๊ฒ€์ฆํ•˜๋Š” ์—ฌ๋Ÿฌ ์˜คํ”ˆ/์ƒ์šฉ ์†Œํ”„ํŠธ์›จ์–ด๊ฐ€ ์กด์žฌํ•˜๊ธฐ๋„ ํ•˜๊ณ , ๊ฒฐ์ •์ ์œผ๋กœ ReportingObserver์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์ƒ๋‹นํžˆ ๋ฏธํกํ•˜๋‹ค.

์ด ์˜ต์ €๋ฒ„ ๋˜ํ•œ PerformanceObserver์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ์ด๋Ÿฐ ๊ฒŒ ์žˆ๋‹ค ์ •๋„๋กœ๋งŒ ์งš๊ณ  ๋„˜์–ด๊ฐ€๋„, ๊ฐœ๋ฐœ์— ํฐ ์ง€์žฅ์€ ์—†์„ ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•œ๋‹ค.

์‹œ๋ฆฌ์ฆˆ๋ฅผ ๋๋งˆ์น˜๋ฉฐ

6ํŽธ์— ๊ฑธ์นœ Observer API ์‹œ๋ฆฌ์ฆˆ๊ฐ€ ์™„๊ฒฐ๋๋‹ค. ์‹ค์ œ๋กœ ๊ฐœ๋ฐœ์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ, ๋ช‡๋ช‡ Event Driven ๊ธฐ๋ฐ˜์˜ ์ฝ”๋“œ๋ฅผ Observer API๋กœ ๋Œ€์ฒดํ•˜๊ณ  ์žˆ๋‹ค. ์ปดํฌ๋„ŒํŠธ ๋ฐ ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ์— ์žˆ์–ด์„œ ๋”์šฑ ํšจ์œจ์ ์ด๊ณ , ๊ฐ„ํŽธํ•œ ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์šฉ์ดํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

ํŠนํžˆ, ์š”์ฆ˜ ๊ฐœ๋ฐœํ•˜๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๊ฐœ์ธ ํ”„๋กœ์ ํŠธ์—, ์ฃผ์š” ์˜ต์ €๋ฒ„๋ฅผ React ํ›…์œผ๋กœ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”๋ฐ, DOM ๋“ฑ๋ก ๊ท€์ฐฎ์€ ์ฝ”๋“œ๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ์ ์ด ๋งค๋ ฅ์ ์ด์˜€๋‹ค.

์•ž์œผ๋กœ ์ข…์ข… React ์ปดํฌ๋„ŒํŠธ์™€ ๊ด€๋ จ๋œ ๊ธ€์„ ์ž‘์„ฑํ•  ์˜ˆ์ •์ธ๋ฐ, ์ด์ „์— Observer์— ๊ด€๋ จ๋œ ๋‚ด์šฉ์„ ์งš๊ณ  ๋„˜์–ด๊ฐ€์•ผ ๋ช‡๋ช‡ ์ปดํฌ๋„ŒํŠธ ์„ค๋ช…์ด ๋งค๋„๋Ÿฌ์›Œ์งˆ ๊ฒƒ์ด๋ผ ํŒ๋‹จํ–ˆ๋‹ค.

์•ž์œผ๋กœ ์žˆ์„ ์ปดํฌ๋„ŒํŠธ ์„ค๋ช…๊ณผ ๋”๋ถˆ์–ด, ์ปดํฌ๋„ŒํŠธ์™€ ๊ด€๊ณ„์—†์ด ์ด ๊ธ€์„ ์ฝ๋Š” ๋ถ„๋“ค์ด, Observer API๋ฅผ ํ†ตํ•ด ๊ธฐ๋Šฅ ๊ฐœ๋ฐœ์— ๋” ๋งŽ์€ ์ธ์‚ฌ์ดํŠธ๋ฅผ ์–ป๊ธธ ๋ฐ”๋ž€๋‹ค.


๐Ÿท๏ธ ํƒœ๊ทธ
# JavaScript
# TypeScript
# React
# Observer API
# ReportingObserver

์ฝ์–ด์ฃผ์…”์„œ ๊ณ ๋งˆ์›Œ์š”!

๋„์›€์ด ๋˜์…จ๋‹ค๋ฉด, ๊ณต๊ฐ์ด๋‚˜ ๋Œ“๊ธ€์„ ๋‹ฌ์•„์ฃผ์‹œ๋Š” ๊ฑด ์–ด๋–ค๊ฐ€์š”?

๋ธ”๋กœ๊ทธ ์šด์˜์— ํฐ ํž˜์ด ๋ฉ๋‹ˆ๋‹ค.

https://hits.seeyoufarm.com/api/count/incr/badge.svg?count_bg=%23484848&icon=react.svg&icon_color=dodgerblue&title=view&title_bg=%23242424&url=https%3A%2F%2Fblog.itcode.dev%2Fposts%2F2024%2F06%2F30%2Fobserver-api-6