afterTicks
afterTicks creates a reusable Minecraft tick scheduler for debounce.
Import
import { afterTicks } from '@bedrock-core/observable/minecraft';
import type { DebounceScheduler } from '@bedrock-core/observable';
The /minecraft entry imports both @minecraft/server and @minecraft/server-ui; declare both modules in the behavior pack manifest.
Signature
afterTicks(ticks: number): DebounceScheduler
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
ticks * | number | — | A non-negative safe integer specifying the delay in Minecraft ticks |
Invalid delays throw RangeError when afterTicks is called, before a timer is scheduled.
Returns
type DebounceScheduler = (callback: () => void) => Unsubscribe;
Creating the scheduler starts no timer. Each invocation schedules a separate system.runTimeout and returns a cancellation function that calls system.clearRun with that invocation's run ID.
Usage
import { debounce, observable } from '@bedrock-core/observable';
import { afterTicks } from '@bedrock-core/observable/minecraft';
const query = observable('');
const settledQuery = debounce(query, afterTicks(5));
settledQuery.subscribe(next => console.warn(next));
query.set('stone');
query.set('stone bricks');
Each change restarts the five-tick wait. The debounced observable publishes only the latest value after the quiet period. Call settledQuery.dispose() when its owner ends to cancel pending delivery and release the source subscription.
Examples
Reuse a delay
const settle = afterTicks(10);
const settledPrice = debounce(price, settle);
const settledStock = debounce(stock, settle);
Each observable owns its timer; resetting or disposing one does not cancel the other.
Notes
- Tick timing follows Minecraft's
system.runTimeoutsemantics. A zero delay means the current tick or as soon as possible. - The main
@bedrock-core/observableentry remains independent of Minecraft. Tick scheduling is exposed through/minecraft.