본문으로 건너뛰기

함수: createBatcher()

function createBatcher<TValue, TSelected>(
fn,
options,
selector): SolidBatcher<TValue, TSelected>;

정의 위치: solid-pacer/src/batcher/createBatcher.ts:155

항목 배치를 관리하는 Solid 호환 Batcher 인스턴스를 생성하고 모든 상태 속성을 Solid 시그널로 노출합니다.

기능:

  • 제공된 fn 함수를 사용한 항목 배치 처리
  • 구성 가능한 배치 크기 및 대기 시간
  • getShouldExecute를 통한 사용자 지정 배치 처리 로직
  • 배치 작업 모니터링을 위한 이벤트 콜백
  • 모든 상태 속성(항목, 횟수 등)은 반응성을 위해 Solid 시그널로 노출됩니다.

배처는 다음 조건에 따라 항목을 모아 배치로 처리합니다.

  • 최대 배치 크기
  • 시간 기반 배칭(X밀리초 후 처리)
  • getShouldExecute를 통한 사용자 지정 배치 처리 로직

상태 관리와 셀렉터

훅은 반응형 상태 관리에 TanStack Store를 사용합니다. 다음 두 가지 방식으로 상태 변경을 구독할 수 있습니다. 구독 방식은 다음과 같습니다.

1. batcher.Subscribe 컴포넌트 사용(컴포넌트 트리 구독에 권장)

Subscribe 컴포넌트를 사용하면 컴포넌트 트리 깊은 곳에서 상태 변경을 구독하면서도 훅에 셀렉터를 전달할 필요가 없습니다. 자식 컴포넌트에서 상태를 구독하려는 경우에 적합합니다.

2. selector 매개변수 사용(훅 수준 구독)

selector 매개변수로 반응형 업데이트를 트리거할 상태 변경을 지정할 수 있으며, 훅 수준에서 관련 없는 상태가 변경될 때 불필요한 업데이트를 방지하여 성능을 최적화합니다.

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

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

  • executionCount: 완료된 배치 실행 횟수
  • isRunning: 배처가 현재 실행 중인지 여부(중지되지 않음)
  • items: 현재 배칭을 위해 큐에 대기 중인 항목 배열
  • totalItemsProcessed: 모든 배치에서 처리된 개별 항목의 총수

언마운트 동작

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

const batcher = createBatcher(fn, {
maxSize: 10,
wait: 2000,
onUnmount: (b) => b.flush()
});

사용 예시:

// Default behavior - no reactive state subscriptions
const batcher = createBatcher(
(items) => {
// Process batch of items
console.log('Processing batch:', items);
},
{
maxSize: 5,
wait: 2000,
onExecute: (batcher) => console.log('Batch executed'),
getShouldExecute: (items) => items.length >= 3
}
);

// Opt-in to track items or isRunning changes (optimized for UI updates)
const batcher = createBatcher(
(items) => console.log('Processing batch:', items),
{ maxSize: 5, wait: 2000 },
(state) => ({ items: state.items, isRunning: state.isRunning })
);

// Opt-in to track execution metrics changes (optimized for tracking progress)
const batcher = createBatcher(
(items) => console.log('Processing batch:', items),
{ maxSize: 5, wait: 2000 },
(state) => ({
executionCount: state.executionCount,
totalItemsProcessed: state.totalItemsProcessed
})
);

// Add items to batch
batcher.addItem('task1');
batcher.addItem('task2');

// Control the batcher
batcher.stop(); // Pause processing
batcher.start(); // Resume processing

// Access the selected state (will be empty object {} unless selector provided)
const { items, isRunning } = batcher.state();

타입 매개변수

TValue

TValue

TSelected

TSelected = { }

매개변수

fn

(items) => void

options

SolidBatcherOptions&lt;TValue, TSelected> = {}

selector

(state) => TSelected

반환값

SolidBatcher&lt;TValue, TSelected>