🎠The main EventEmitter class that handles all your event needs!
// Create a type-safe redstone event systeminterface RedstoneEvents { 'pulse': [side: string, strength: number]; 'off': [side: string];}const redstone = new EventEmitter<RedstoneEvents>();// Type-safe event handlingredstone.on('pulse', (side, strength) => { print(`Redstone signal on ${side}: ${strength}`);});// Clean up when doneconst cleanup = redstone.on('off', (side) => { print(`Signal lost on ${side}`);});// Later...cleanup(); // Removes the listener Copy
// Create a type-safe redstone event systeminterface RedstoneEvents { 'pulse': [side: string, strength: number]; 'off': [side: string];}const redstone = new EventEmitter<RedstoneEvents>();// Type-safe event handlingredstone.on('pulse', (side, strength) => { print(`Redstone signal on ${side}: ${strength}`);});// Clean up when doneconst cleanup = redstone.on('off', (side) => { print(`Signal lost on ${side}`);});// Later...cleanup(); // Removes the listener
A record type mapping event names to arrays of argument types
Emit an event with arguments.
The event name to emit
The arguments to pass to listeners
// Emit events with type safetyemitter.emit('turtle:error', new Error('Out of fuel!')); Copy
// Emit events with type safetyemitter.emit('turtle:error', new Error('Out of fuel!'));
Unsubscribe a listener from an event.
The event name to unsubscribe from
The callback function to remove
Subscribe to an event with a listener function.
The event name to listen for
The callback function that handles the event
A cleanup function that removes the listener when called
const cleanup = emitter.on('inventory:changed', (slot, count) => { print(`Slot ${slot} now has ${count} items`);});// Later when you're done listening:cleanup(); Copy
const cleanup = emitter.on('inventory:changed', (slot, count) => { print(`Slot ${slot} now has ${count} items`);});// Later when you're done listening:cleanup();
Subscribe to an event that automatically unsubscribes after first trigger.
The one-time callback function
A cleanup function (in case you need to remove before trigger)
// Perfect for initialization eventsemitter.once('system:ready', () => { print('System initialized!');}); Copy
// Perfect for initialization eventsemitter.once('system:ready', () => { print('System initialized!');});
Remove all listeners for an event, or all events if no event specified.
Optional
Optional event name to clear listeners for
// Clear specific eventemitter.removeAllListeners('user:input');// Clear everythingemitter.removeAllListeners(); Copy
// Clear specific eventemitter.removeAllListeners('user:input');// Clear everythingemitter.removeAllListeners();
🎠The main EventEmitter class that handles all your event needs!
Example