CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-rsvp

A lightweight library that provides tools for organizing asynchronous code

Pending
Overview
Eval results
Files

configuration.mddocs/

Configuration and Events

Global configuration system and event handling for instrumentation, debugging, and custom event management in RSVP.js.

Capabilities

Configuration System

configure

Set or get global configuration options for RSVP.js behavior.

/**
 * Set or get configuration values
 * @param name - Configuration property name
 * @param value - Value to set (omit to get current value)
 * @returns Current value when getting, undefined when setting
 */
function configure(name: string, value?: any): any;

Usage Examples:

import { configure } from "rsvp";

// Enable instrumentation for debugging
configure('instrument', true);

// Set custom async scheduler
configure('async', function(callback, arg) {
  setTimeout(callback, 0, arg);
});

// Set after callback for cleanup
configure('after', function(callback) {
  process.nextTick(callback);
});

// Get current configuration values
const isInstrumented = configure('instrument'); // true/false
const asyncFn = configure('async'); // current async function

Global Event System

on

Register global event listeners for promise lifecycle events.

/**
 * Register global event listener
 * @param eventName - Name of event to listen for
 * @param callback - Function to call when event occurs
 */
function on(eventName: string, callback: Function): void;

off

Remove global event listeners.

/**
 * Remove global event listener
 * @param eventName - Name of event to stop listening for
 * @param callback - Specific callback to remove (optional)
 */
function off(eventName: string, callback?: Function): void;

Usage Examples:

import { on, off, configure } from "rsvp";

// Enable instrumentation first
configure('instrument', true);

// Listen for promise creation
on('created', function(event) {
  console.log('Promise created:', event);
});

// Listen for promise fulfillment
on('fulfilled', function(event) {
  console.log('Promise fulfilled:', event);
});

// Listen for promise rejection
on('rejected', function(event) {
  console.error('Promise rejected:', event);
});

// Listen for errors
on('error', function(reason, label) {
  console.error('Unhandled promise error:', reason, label);
});

// Remove specific listener
function myHandler(event) { /* ... */ }
on('created', myHandler);
off('created', myHandler);

// Remove all listeners for an event
off('created');

EventTarget System

EventTarget Object

Provides mixin capabilities and direct event handling for custom objects.

/**
 * EventTarget object with mixin and direct event methods
 */
const EventTarget: {
  /**
   * Add EventTarget methods to an object
   * @param object - Object to extend with event methods
   * @returns The extended object
   */
  mixin(object: Object): Object;
  
  /**
   * Register event listener
   * @param eventName - Name of event to listen for
   * @param callback - Function to call when event occurs
   */
  on(eventName: string, callback: Function): void;
  
  /**
   * Remove event listener
   * @param eventName - Name of event to stop listening for
   * @param callback - Specific callback to remove (optional)
   */
  off(eventName: string, callback?: Function): void;
  
  /**
   * Trigger event with optional data
   * @param eventName - Name of event to trigger
   * @param options - Data to pass to event listeners
   * @param label - Optional label for debugging
   */
  trigger(eventName: string, options?: any, label?: string): void;
};

Usage Examples:

import { EventTarget } from "rsvp";

// Add event capabilities to an object
const myObject = {};
EventTarget.mixin(myObject);

myObject.on('customEvent', function(data) {
  console.log('Custom event received:', data);
});

myObject.trigger('customEvent', { message: 'Hello World' });

// Add event capabilities to a prototype
function MyClass() {}
EventTarget.mixin(MyClass.prototype);

const instance1 = new MyClass();
const instance2 = new MyClass();

instance1.on('update', function(data) {
  console.log('Instance 1 updated:', data);
});

instance2.on('update', function(data) {
  console.log('Instance 2 updated:', data);
});

instance1.trigger('update', { value: 42 });
instance2.trigger('update', { value: 99 });

Instrumentation and Debugging

Browser Instrumentation

RSVP.js automatically sets up instrumentation if window.__PROMISE_INSTRUMENTATION__ exists:

<script>
  // Set up instrumentation before loading RSVP
  window.__PROMISE_INSTRUMENTATION__ = {
    created: function(event) {
      console.log('Promise created:', event);
    },
    fulfilled: function(event) {
      console.log('Promise fulfilled:', event);
    },
    rejected: function(event) {
      console.error('Promise rejected:', event);
    }
  };
</script>
<script src="rsvp.js"></script>

Custom Instrumentation

import { configure, on } from "rsvp";

// Enable instrumentation
configure('instrument', true);

// Track promise statistics
const promiseStats = {
  created: 0,
  fulfilled: 0,
  rejected: 0
};

on('created', () => promiseStats.created++);
on('fulfilled', () => promiseStats.fulfilled++);
on('rejected', () => promiseStats.rejected++);

// Log stats periodically
setInterval(() => {
  console.log('Promise stats:', promiseStats);
}, 5000);

Error Tracking

import { on, configure } from "rsvp";

configure('instrument', true);

// Global error handler for unhandled promise rejections
on('error', function(reason, label) {
  // Send to error tracking service
  errorTracker.captureException(reason, {
    tags: { source: 'rsvp-promise' },
    extra: { label: label }
  });
});

// Track specific error patterns
on('rejected', function(event) {
  if (event.detail && event.detail.reason) {
    const error = event.detail.reason;
    if (error.name === 'TimeoutError') {
      metrics.increment('promise.timeout');
    }
  }
});

Configuration Options

Available Configuration Keys

  • instrument (boolean): Enable/disable promise instrumentation
  • async (function): Custom function for scheduling async callbacks
  • after (function): Custom function for scheduling after-callbacks

Default Configuration

// RSVP.js default configuration
configure('instrument', false);
configure('async', asapFunction); // Platform-specific async scheduling
configure('after', function(callback) {
  setTimeout(callback, 0);
});

Custom Async Scheduling

import { configure } from "rsvp";

// Use setImmediate in Node.js
if (typeof setImmediate !== 'undefined') {
  configure('async', function(callback, arg) {
    setImmediate(callback, arg);
  });
}

// Use MessageChannel in web workers
if (typeof MessageChannel !== 'undefined') {
  const channel = new MessageChannel();
  const callbacks = [];
  
  channel.port1.onmessage = function() {
    const callback = callbacks.shift();
    if (callback) callback();
  };
  
  configure('async', function(callback, arg) {
    callbacks.push(() => callback(arg));
    channel.port2.postMessage(null);
  });
}

Event Types

Promise Lifecycle Events

  • created: Fired when a new promise is created
  • fulfilled: Fired when a promise is fulfilled
  • rejected: Fired when a promise is rejected
  • error: Fired for unhandled promise errors

Event Data Structure

Events typically include:

  • detail: Object with promise information
  • label: Optional debugging label
  • reason: Error reason (for rejection/error events)
  • value: Fulfillment value (for fulfilled events)

Install with Tessl CLI

npx tessl i tessl/npm-rsvp

docs

array-utilities.md

configuration.md

index.md

node-integration.md

object-utilities.md

promise.md

utilities.md

tile.json