@oxog/delay
Version:
A comprehensive, zero-dependency delay/timeout utility library with advanced timing features
146 lines • 5.3 kB
JavaScript
import { createBasicDelay, msDelay, secondsDelay, minutesDelay, hoursDelay, daysDelay, } from './core/delay.js';
import { createCancellableDelay } from './core/cancellable.js';
import { forDelay, untilDelay, whileDelay } from './core/parser.js';
import { retryDelay } from './core/retry.js';
import { createRepeatDelay } from './core/repeat.js';
import { createBatchScheduler, preciseDelay } from './core/scheduler.js';
import { addJitter, randomBetween } from './utils/random.js';
import { throttle, debounce } from './utils/throttle-debounce.js';
import { raceWithTimeout, createTimeoutPromise, minimumDelay } from './utils/promise.js';
import { nextFrame, idle } from './utils/browser.js';
import { PluginManager } from './plugins/plugin-manager.js';
class DelayImplementation {
constructor() {
this.pluginManager = new PluginManager();
this.pluginManager.setDelayInstance(this);
}
// Main delay function - will be added as property
delay(ms, options) {
return createBasicDelay(ms, options);
}
// Time unit methods
ms(milliseconds, options) {
return msDelay(milliseconds, options);
}
seconds(seconds, options) {
return secondsDelay(seconds, options);
}
minutes(minutes, options) {
return minutesDelay(minutes, options);
}
hours(hours, options) {
return hoursDelay(hours, options);
}
days(days, options) {
return daysDelay(days, options);
}
// Human-friendly syntax
for(timeString, options) {
return forDelay(timeString, options);
}
until(target, options) {
return untilDelay(target, options);
}
while(predicate, options) {
return whileDelay(predicate, options);
}
// Cancellable delays
cancellable(ms, options) {
return createCancellableDelay(ms, options);
}
// Retry mechanism
retry(fn, options) {
return retryDelay(fn, options);
}
// Repeating delays
repeat(fn, interval) {
return createRepeatDelay(fn, interval);
}
// Randomization
random(ms, options = {}) {
const { jitter = 0.1 } = options;
const randomMs = addJitter(ms, jitter);
return createBasicDelay(randomMs);
}
between(min, max) {
const randomMs = randomBetween(min, max);
return createBasicDelay(randomMs);
}
// Performance features
precise(ms) {
return preciseDelay(ms);
}
batch(options) {
return createBatchScheduler(options);
}
// Promise utilities
race(promises, timeout, timeoutError) {
return raceWithTimeout(promises, timeout, timeoutError);
}
timeout(ms, error) {
return createTimeoutPromise(ms, error);
}
minimum(promise, ms) {
return minimumDelay(promise, ms);
}
// Utility methods
throttle(fn, ms, options) {
return throttle(fn, ms, options);
}
debounce(fn, ms, options) {
return debounce(fn, ms, options);
}
nextFrame() {
return nextFrame();
}
idle(options) {
return idle(options);
}
// Plugin system
use(plugin) {
this.pluginManager.register(plugin);
}
}
// Create a function that can be called directly but also has methods
function createDelayInstance() {
const instance = new DelayImplementation();
// Create a callable function
const delay = (ms, options) => {
return instance.delay(ms, options);
};
// Copy all methods to the function, excluding the internal delay method
const propertiesToCopy = Object.getOwnPropertyNames(Object.getPrototypeOf(instance))
.filter(name => name !== 'constructor' && name !== 'delay');
propertiesToCopy.forEach(name => {
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(instance), name);
if (descriptor && typeof descriptor.value === 'function') {
delay[name] = descriptor.value.bind(instance);
}
});
// Also copy any instance properties
Object.keys(instance).forEach(key => {
if (key !== 'pluginManager') {
delay[key] = instance[key];
}
});
return delay;
}
// Create the default instance
const delay = createDelayInstance();
// Export both the instance and types
export default delay;
export { delay };
// Export error types
export { DelayError, DelayErrorCode } from './types/index.js';
// Export utility functions for advanced use cases
export { createBasicDelay } from './core/delay.js';
export { createCancellableDelay } from './core/cancellable.js';
export { retryDelay } from './core/retry.js';
export { createRepeatDelay } from './core/repeat.js';
export { createBatchScheduler, preciseDelay } from './core/scheduler.js';
export { throttle, debounce } from './utils/throttle-debounce.js';
export { addJitter, randomBetween, calculateBackoffDelay } from './utils/random.js';
export { parseTimeString, parseTimeUntil, convertToMs, getHighResolutionTime, } from './utils/time.js';
export { nextFrame, idle, waitForDOMReady, waitForWindowLoad, getEnvironmentCapabilities, } from './utils/browser.js';
export { PluginManager, createLoggingPlugin, createMetricsPlugin, createDebugPlugin, } from './plugins/plugin-manager.js';
//# sourceMappingURL=index.js.map