A lightweight library that provides tools for organizing asynchronous code
—
Global configuration system and event handling for instrumentation, debugging, and custom event management in RSVP.js.
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 functionRegister 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;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');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 });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>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);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');
}
}
});instrument (boolean): Enable/disable promise instrumentationasync (function): Custom function for scheduling async callbacksafter (function): Custom function for scheduling after-callbacks// RSVP.js default configuration
configure('instrument', false);
configure('async', asapFunction); // Platform-specific async scheduling
configure('after', function(callback) {
setTimeout(callback, 0);
});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);
});
}created: Fired when a new promise is createdfulfilled: Fired when a promise is fulfilledrejected: Fired when a promise is rejectederror: Fired for unhandled promise errorsEvents typically include:
detail: Object with promise informationlabel: Optional debugging labelreason: Error reason (for rejection/error events)value: Fulfillment value (for fulfilled events)Install with Tessl CLI
npx tessl i tessl/npm-rsvp