본문으로 건너뛰기

함수: injectAsyncThrottler()

function injectAsyncThrottler<TFn, TSelected>(
fn,
options,
selector): AngularAsyncThrottler<TFn, TSelected>;

정의 위치: angular-pacer/src/async-throttler/injectAsyncThrottler.ts:98

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

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

이 함수는 Promise 지원, 오류 처리, 재시도 기능 및 중단을 갖춘 비동기 기능을 제공합니다.

스로틀러는 지정된 대기 시간 안에 함수를 최대 한 번 실행합니다.

상태 관리와 셀렉터

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

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

제거 시 정리

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

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

onUnmount에서 flush를 사용할 때는 컴포넌트가 이미 제거되었을 수 있으므로 콜백을 안전하게 보호해야 합니다.

타입 매개변수

TFn

TFn extends AnyAsyncFunction

TSelected

TSelected = { }

매개변수

fn

TFn

options

AngularAsyncThrottlerOptions&lt;TFn, TSelected>

selector

(state) => TSelected

반환값

AngularAsyncThrottler&lt;TFn, TSelected>

예시

// Default behavior - no reactive state subscriptions
const throttler = injectAsyncThrottler(
async (data: Data) => {
const response = await fetch('/api/update', {
method: 'POST',
body: JSON.stringify(data)
});
return response.json();
},
{ wait: 1000 }
);

// In an event handler
const handleUpdate = async (data: Data) => {
const result = await throttler.maybeExecute(data);
console.log('Update result:', result);
};