DOM ready-state helper: callbacks enqueued for
interactive(DOMContentLoaded) andcomplete(load).
Module readyState | Source packages/front/fw/src/core/readyState.js | Deps none | Worker-safe no
This component is not resolved via runtime.resolve() — it is instantiated directly by the framework bootstrap and exposed as fw.domReady. Not worker-safe: it accesses document.readyState and registers readystatechange listeners on document.
Resolve
// Not resolved via runtime — direct access from the fw object:
import fw from './fw/main.js';
const domReady = fw.domReady;
// Returns: { loaded, complete }
API
| Method | Signature | Returns |
|---|---|---|
loaded |
(fn: Function, argArray?: any[]) => void |
— (enqueues or executes immediately) |
complete |
(fn: Function, argArray?: any[]) => void |
— (enqueues or executes immediately) |
domReady.loaded(fn, argArray?)
Executes fn as soon as the DOM is interactive (document.readyState === 'interactive'). If the state is already reached, executes immediately (synchronously).
| Param | Type | Description |
|---|---|---|
fn |
Function |
Callback to execute |
argArray |
any[] |
Arguments passed to fn.apply({}, argArray) (default []) |
domReady.complete(fn, argArray?)
Executes fn when all assets are loaded (document.readyState === 'complete'). If the state is already reached, executes immediately.
Examples
DOM interactive — initialise templates
import fw from './fw/main.js';
fw.domReady.loaded(() => {
const tpl = fw.runtime.resolve('template');
tpl.init('main', { to: '#app' });
});
Full page load — with arguments
fw.domReady.complete((msg) => {
console.log(msg); // 'Page ready'
}, ['Page ready']);
Combining loaded + complete
fw.domReady.loaded(() => {
// DOM parsed — initialise rendering
fw.runtime.resolve('render').mount('#root');
});
fw.domReady.complete(() => {
// Assets loaded — start heavy resources
fw.runtime.resolve('indexedDB').open('appdb', 1);
});
Notes
- Lazy listener: no
readystatechangelistener is registered until at least one callback has been registered. If the state is alreadycompleteon the first call, no listener is registered. - Idempotency: a single
readystatechangelistener is registered, regardless of the number of registered callbacks. interactiveskip: a directloading→completetransition (cache navigation) flushes both queues in one iteration.- Execution order:
loadedqueue flushed beforecompletequeue; within a queue, registration order is preserved. - Silenced errors: exceptions inside callbacks are caught silently (
fn.applywrapped in try/catch). - Not worker-safe: depends on
document.readyStateand thereadystatechangeevent, both absent in Worker contexts.
See also
- worker-helper — spawn framework-aware workers
- Getting started guide