The scheduler module provides a robust event management system for ComputerCraft applications. It includes utilities for handling events, managing timeouts, and running event loops.

⚠️ IMPORTANT: Many functions in this module (osEvents, asyncSleep, setTimeout, timeouts on events, etc.) require an active event loop to function. You have two main ways to use this module:

  1. Run the OS event loop with runOsEventLoop
  2. Implement your own event loop using os.pullEvent()

Key Features:

  • 🔒 Type-safe event management with TypeScript
  • ⏱️ Promise-based event waiting with timeout support
  • 🔍 Flexible event filtering
  • 🌐 Global OS event handling
  • ⚡ Async/await support
  • 🔄 Custom event systems

Common Use Cases:

  1. OS Event Management:

    • Handling ComputerCraft events (redstone, turtle, terminal)
    • Managing timeouts and intervals
    • Coordinating async operations
  2. Custom Event Systems:

    • Building robot control systems
    • Managing game state
    • Creating event-driven UIs
    • Implementing pub/sub patterns
  3. Mixed Usage:

    • Adding custom events to the OS event system
    • Building modular systems with multiple event managers
    • Creating domain-specific event subsystems
import { on, runOsEventLoop } from "@cc-ts/helpers/scheduler";

// Set up your OS event handlers
on('mouse_click', (button, x, y) => {
console.log(`Click at ${x},${y}`);
});

// Start the OS event loop
runOsEventLoop();
import { on, dispatch } from "@cc-ts/helpers/scheduler";

// Declare additional OS events
declare module "@cc-ts/helpers/types" {
interface Events {
"turtle_dig": [success: boolean, block: string];
"inventory_change": [slot: number, count: number];
}
}

// Now you can use these with the global 'on' function
on("turtle_dig", (success, block) => {
if (success) {
print(`Successfully mined ${block}`);
}
});

// And dispatch them
await dispatch("turtle_dig", true, "minecraft:diamond_ore");
// 1. Define your event types
interface GameEvents {
'score': [points: number, player: string];
'gameOver': [winner: string];
}

// 2. Create an event manager
const gameEvents = new EventManager<GameEvents>();

// 3. Set up handlers
gameEvents.on('score', (points, player) => {
console.log(`${player} scored ${points} points!`);
});

// 4. Use it in your game loop
while (gameRunning) {
// Your game logic
await gameEvents.dispatch('score', 100, 'Player1');
}

Classes

EventManager
Panic

Variables

osEvents

Functions

asyncSleep
dispatch
escalate
on
once
PANIC
runOsEventLoop
setTimeout
waitForAnyEvent
waitForEvent