or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli.mdconfiguration.mdevent-system.mdfile-operations.mdindex.mdtask-system.mdtemplating.md
tile.json

event-system.mddocs/

Event System

EventEmitter2-based event system with wildcard support for task lifecycle and custom events. Provides comprehensive event handling capabilities for build process automation and plugin communication.

Capabilities

Event Registration

Register event listeners for task lifecycle events and custom application events.

/**
 * Register event listener with optional wildcard support
 * @param {string} event - Event name (supports wildcards like 'task.*')
 * @param {function} listener - Event handler function
 * @returns {EventEmitter2} Event emitter instance for chaining
 */
grunt.event.on = function(event, listener) {};

/**
 * Register one-time event listener that removes itself after firing
 * @param {string} event - Event name (supports wildcards)
 * @param {function} listener - Event handler function
 * @returns {EventEmitter2} Event emitter instance for chaining
 */
grunt.event.once = function(event, listener) {};

/**
 * Register multiple event listeners at once
 * @param {object} listeners - Object mapping event names to handler functions
 * @returns {EventEmitter2} Event emitter instance for chaining
 */
grunt.event.many = function(listeners) {};

Usage Examples:

// Listen to specific task events
grunt.event.on('task.start', function(taskName) {
  console.log('Task started:', taskName);
});

grunt.event.on('task.end', function(taskName) {
  console.log('Task completed:', taskName);
});

// Listen to all task events using wildcards
grunt.event.on('task.*', function(event, taskName) {
  console.log('Task event:', event, 'for task:', taskName);
});

// One-time listener
grunt.event.once('build.complete', function() {
  console.log('Build process finished');
});

// Multiple listeners
grunt.event.many({
  'file.changed': function(filepath) {
    console.log('File changed:', filepath);
  },
  'file.deleted': function(filepath) {
    console.log('File deleted:', filepath);
  }
});

Event Emission

Emit events to trigger registered listeners.

/**
 * Emit event with optional arguments
 * @param {string} event - Event name to emit
 * @param {...*} args - Arguments to pass to event listeners
 * @returns {boolean} True if event had listeners, false otherwise
 */
grunt.event.emit = function(event, ...args) {};

/**
 * Emit event asynchronously
 * @param {string} event - Event name to emit
 * @param {...*} args - Arguments to pass to event listeners
 * @returns {EventEmitter2} Event emitter instance for chaining
 */
grunt.event.emitAsync = function(event, ...args) {};

Usage Examples:

// Custom task that emits events
grunt.registerTask('deploy', function() {
  grunt.event.emit('deploy.start', 'production');
  
  // Deployment logic here
  
  if (deploymentSuccessful) {
    grunt.event.emit('deploy.success', 'production');
  } else {
    grunt.event.emit('deploy.error', 'production', errorMessage);
  }
  
  grunt.event.emit('deploy.end', 'production');
});

// Emit custom events
grunt.event.emit('file.processed', 'src/app.js', processedContent);
grunt.event.emit('build.progress', 75); // 75% complete

Event Removal

Remove event listeners to prevent memory leaks and control event flow.

/**
 * Remove specific event listener
 * @param {string} event - Event name
 * @param {function} [listener] - Specific listener to remove (removes all if omitted)
 * @returns {EventEmitter2} Event emitter instance for chaining
 */
grunt.event.off = function(event, listener) {};

/**
 * Remove all listeners for an event
 * @param {string} [event] - Event name (removes all events if omitted)
 * @returns {EventEmitter2} Event emitter instance for chaining
 */
grunt.event.removeAllListeners = function(event) {};

Usage Examples:

// Store listener reference for later removal
const taskListener = function(taskName) {
  console.log('Task event:', taskName);
};

grunt.event.on('task.start', taskListener);

// Remove specific listener
grunt.event.off('task.start', taskListener);

// Remove all listeners for an event
grunt.event.removeAllListeners('task.start');

// Remove all listeners for all events
grunt.event.removeAllListeners();

Event Querying

Query event listener information and event system state.

/**
 * Get array of listeners for an event
 * @param {string} event - Event name
 * @returns {function[]} Array of listener functions
 */
grunt.event.listeners = function(event) {};

/**
 * Check if event has any listeners
 * @param {string} event - Event name
 * @returns {boolean} True if event has listeners
 */
grunt.event.hasListeners = function(event) {};

/**
 * Get maximum number of listeners for any event
 * @returns {number} Maximum listener count
 */
grunt.event.getMaxListeners = function() {};

/**
 * Set maximum number of listeners per event
 * @param {number} max - Maximum listener count
 * @returns {EventEmitter2} Event emitter instance for chaining
 */
grunt.event.setMaxListeners = function(max) {};

Usage Examples:

// Check if event has listeners before emitting
if (grunt.event.hasListeners('custom.event')) {
  grunt.event.emit('custom.event', data);
}

// Get listener count
const listeners = grunt.event.listeners('task.start');
console.log('Task start listeners:', listeners.length);

// Increase max listeners if needed
grunt.event.setMaxListeners(20);

Built-in Task Events

Grunt automatically emits various events during task execution that you can listen to.

Task Lifecycle Events:

// Task execution events
grunt.event.on('task.start', function(taskName) {
  // Task is about to start
});

grunt.event.on('task.end', function(taskName, success) {
  // Task has completed (success = true/false)
});

grunt.event.on('task.error', function(taskName, error) {
  // Task encountered an error
});

// Queue management events
grunt.event.on('queue.start', function() {
  // Task queue execution started
});

grunt.event.on('queue.end', function() {
  // All queued tasks completed
});

File Operation Events:

// File system events (if watching is enabled)
grunt.event.on('file.changed', function(filepath, stat) {
  console.log('File changed:', filepath);
});

grunt.event.on('file.added', function(filepath, stat) {
  console.log('File added:', filepath);
});

grunt.event.on('file.deleted', function(filepath) {
  console.log('File deleted:', filepath);
});

Advanced Event Patterns

Complex event handling patterns and best practices.

Event Namespacing:

// Use namespaced events for better organization
grunt.event.on('build.stage.compile', function(stage) {
  console.log('Compilation stage:', stage);
});

grunt.event.on('build.stage.test', function(results) {
  console.log('Test results:', results);
});

grunt.event.on('build.stage.deploy', function(environment) {
  console.log('Deploying to:', environment);
});

// Listen to all build stages
grunt.event.on('build.stage.*', function(event, data) {
  console.log('Build stage event:', event, data);
});

Error Handling in Events:

// Robust error handling for event listeners
grunt.event.on('task.process', function(data) {
  try {
    processTaskData(data);
  } catch (error) {
    grunt.event.emit('task.process.error', error, data);
    grunt.fail.warn('Task processing failed: ' + error.message);
  }
});

// Listen for processing errors
grunt.event.on('task.process.error', function(error, data) {
  grunt.log.error('Processing error:', error.message);
  // Additional error handling logic
});

Event-Driven Task Coordination:

// Use events to coordinate between tasks
grunt.registerTask('compile', function() {
  const done = this.async();
  
  // Emit compilation start event
  grunt.event.emit('compilation.start');
  
  performCompilation()
    .then(function(result) {
      grunt.event.emit('compilation.success', result);
      done();
    })
    .catch(function(error) {
      grunt.event.emit('compilation.error', error);
      done(false);
    });
});

// Other tasks can listen and react
grunt.event.on('compilation.success', function(result) {
  // Trigger next step in build process
  grunt.task.run('optimize');
});

grunt.event.on('compilation.error', function(error) {
  // Handle compilation failure
  grunt.task.clearQueue();
  grunt.fail.fatal('Compilation failed: ' + error.message);
});

Event System Configuration

Configure event system behavior and performance characteristics.

// Increase max listeners for complex workflows
grunt.event.setMaxListeners(50);

// Configure event emitter options (if using EventEmitter2 directly)
const customEmitter = new grunt.event.constructor({
  wildcard: true,
  delimiter: '.',
  maxListeners: 100,
  verboseMemoryLeak: true
});

This event system provides the foundation for building reactive, event-driven build processes and enables powerful plugin communication patterns in Grunt-based workflows.