@exadel/esl
Version:
Exadel Smart Library (ESL) is the lightweight custom elements library that provide a set of super-flexible components
21 lines (20 loc) • 1.02 kB
JavaScript
import { onDocumentReady } from '../dom/ready';
/**
* `@ready` decorator: defers method execution until DOM is ready, then runs it in the next macrotask.
* - Wraps a void method so each call schedules execution (arguments captured) after `DOMContentLoaded`
* - If DOM already ready, still defers via `setTimeout(..., 0)` (next task boundary)
* - Multiple calls before readiness schedule multiple executions (no coalescing)
* - Return value is always `undefined` (original result not obtainable due to async defer)
* - Exceptions inside deferred callback surface asynchronously (not caught)
*
* @throws TypeError when applied to a non-method
*/
export function ready(target, propertyKey, descriptor) {
if (!descriptor || typeof descriptor.value !== 'function') {
throw new TypeError('Only class methods can be decorated via document ready decorator');
}
const fn = descriptor.value;
descriptor.value = function (...arg) {
onDocumentReady(() => fn.call(this, ...arg));
};
}