Skip to main content

Scheduler

The scheduler module helps you prioritize work and avoid blocking the main thread. It wraps the Scheduler API with automatic fallbacks.

scheduleTask

Run a function with a specified priority:

js
import { scheduleTask } from 'weblisk/core/scheduler.js';

// High priority — user is waiting
const result = await scheduleTask(() => processInput(data), 'user-blocking');

// Normal priority — visible but not blocking
await scheduleTask(() => renderChart(data), 'user-visible');

// Low priority — background work
await scheduleTask(() => preloadImages(), 'background');

Priority levels:

| Priority | Use Case | Fallback |

|----------|----------|----------|

| user-blocking | Input handling, critical UI | queueMicrotask |

| user-visible | Rendering visible content | requestAnimationFrame |

| background | Prefetching, analytics | requestIdleCallback |

batchUpdates

Batch multiple signal updates into a single animation frame:

js
import { batchUpdates } from 'weblisk/core/scheduler.js';

await batchUpdates(() => {
  setName('Alice');
  setAge(30);
  setRole('Admin');
  // Effects run once after all three updates
});

rafThrottle

Throttle a function to run at most once per animation frame:

js
import { rafThrottle } from 'weblisk/core/scheduler.js';

const onScroll = rafThrottle(() => {
  parallax.style.transform = `translateY(${scrollY * 0.5}px)`;
});

window.addEventListener('scroll', onScroll, { passive: true });

idleDebounce

Debounce a function to run when the browser is idle:

js
import { idleDebounce } from 'weblisk/core/scheduler.js';

const saveProgress = idleDebounce(() => {
  localStorage.setItem('draft', editor.value);
}, 2000); // 2s timeout

editor.addEventListener('input', saveProgress);