@sixbell-telco/sdk
Version:
A collection of reusable components designed for use in Sixbell Telco Angular projects
85 lines (82 loc) • 2.67 kB
JavaScript
/**
* Async utility helpers for reliable DOM synchronization and timing control.
* These utilities provide browser-native timing mechanisms that are more reliable
* than hardcoded setTimeout values across different environments and platforms.
*/
class SyncUtils {
/**
* Wait for next microtask (more reliable than setTimeout(0))
* Useful for ensuring state updates are processed before DOM operations
*/
static nextTick() {
return new Promise((resolve) => queueMicrotask(resolve));
}
/**
* Wait for next animation frame (better for DOM updates)
* Ideal for synchronizing with browser rendering cycles
*/
static nextFrame() {
return new Promise((resolve) => requestAnimationFrame(() => resolve()));
}
/**
* Wait for DOM element to be available
* Useful for web components or dynamically created elements
*/
static async waitForElement(selector, maxAttempts = 10) {
for (let i = 0; i < maxAttempts; i++) {
const element = document.querySelector(selector);
if (element)
return element;
await this.nextFrame();
}
return null;
}
/**
* Wait for a condition to be met with timeout
* Generic utility for polling-based waiting
*/
static async waitForCondition(condition, maxAttempts = 20, delayStrategy = 'tick') {
for (let i = 0; i < maxAttempts; i++) {
const result = await condition();
if (result)
return true;
if (delayStrategy === 'frame') {
await this.nextFrame();
}
else {
await this.nextTick();
}
}
return false;
}
/**
* Debounce utility for preventing rapid successive calls
* Returns a debounced version of the provided function
*/
static debounce(func, wait) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = globalThis.setTimeout(() => func(...args), wait);
};
}
/**
* Throttle utility for limiting function execution frequency
* Returns a throttled version of the provided function
*/
static throttle(func, limit) {
let inThrottle = false;
return (...args) => {
if (!inThrottle) {
func(...args);
inThrottle = true;
setTimeout(() => (inThrottle = false), limit);
}
};
}
}
/**
* Generated bundle index. Do not edit.
*/
export { SyncUtils };
//# sourceMappingURL=sixbell-telco-sdk-utils-sync.mjs.map