본문으로 건너뛰기

함수: injectThrottler()

function injectThrottler<TFn, TSelected>(
fn,
options,
selector): AngularThrottler<TFn, TSelected>;

정의 위치: angular-pacer/src/throttler/injectThrottler.ts:109

Throttler 인스턴스를 생성하고 관리하는 Angular 함수입니다.

Throttler 기능에 직접 접근할 수 있게 해 주는 저수준 함수입니다. 원하는 상태 관리 솔루션과 통합할 수 있습니다.

이 함수는 함수 호출 빈도를 제한하는 스로틀 기능을 제공하여 지정된 시간 창 안에서 최대 한 번만 실행되도록 합니다.

스로틀러는 함수를 즉시 실행하고(leading이 활성화된 경우), 대기 시간이 지날 때까지 추가 실행을 차단합니다.

상태 관리와 셀렉터

이 함수는 TanStack Store로 상태를 관리하고 이를 Angular 시그널로 래핑합니다. selector 매개변수로 어떤 상태 변경이 시그널 업데이트를 트리거할지 지정할 수 있으며, 관련 없는 상태가 변경될 때 불필요한 업데이트를 방지하여 성능을 최적화합니다.

기본적으로 반응형 상태 구독은 없습니다. 반응형 상태를 추적하려면 셀렉터 함수를 제공하여 명시적으로 활성화해야 합니다. 이를 통해 불필요한 업데이트를 방지하고 컴포넌트가 상태 변경을 추적할 시점을 완전히 제어할 수 있습니다.

사용할 수 있는 상태 속성은 다음과 같습니다.

  • canLeadingExecute: 스로틀러가 선행 에지에서 실행될 수 있는지 여부
  • canTrailingExecute: 스로틀러가 후행 에지에서 실행될 수 있는지 여부
  • executionCount: 완료된 함수 실행 횟수
  • isPending: 스로틀러가 타임아웃 후 실행되기를 기다리고 있는지 여부
  • lastArgs: 가장 최근 maybeExecute 호출에 전달된 인수
  • lastExecutionTime: 마지막 실행의 타임스탬프
  • nextExecutionTime: 다음 실행이 허용되는 시점의 타임스탬프
  • status: 현재 실행 상태('disabled' | 'idle' | 'pending')

제거 시 정리

기본적으로 컴포넌트가 제거되면 대기 중인 실행을 취소합니다. onUnmount 옵션으로 이 동작을 사용자 지정할 수 있습니다. 예를 들어 대기 중인 작업을 대신 플러시하려면 다음과 같이 설정합니다.

const throttler = injectThrottler(fn, {
wait: 100,
onUnmount: (t) => t.flush()
});

타입 매개변수

TFn

TFn extends AnyFunction

TSelected

TSelected = { }

매개변수

fn

TFn

options

AngularThrottlerOptions&lt;TFn, TSelected>

selector

(state) => TSelected

반환값

AngularThrottler&lt;TFn, TSelected>

예시

// Default behavior - no reactive state subscriptions
const throttler = injectThrottler(
(scrollY: number) => updateScrollPosition(scrollY),
{ wait: 100 }
);

// Opt-in to track isPending changes (optimized for loading states)
const throttler = injectThrottler(
(scrollY: number) => updateScrollPosition(scrollY),
{ wait: 100 },
(state) => ({ isPending: state.isPending })
);

// In an event handler
window.addEventListener('scroll', () => {
throttler.maybeExecute(window.scrollY);
});

// Access the selected state (will be empty object {} unless selector provided)
const { isPending } = throttler.state();