All files / lib/core/renderer deferManager.js

88.43% Statements 260/294
75.92% Branches 41/54
87.5% Functions 14/16
88.43% Lines 260/294

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295256x 256x 256x 256x 256x 256x 256x 256x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 577x 7x 7x 577x 577x 577x 577x 577x 577x 577x 577x 1042x 1042x 1042x 1042x     1042x 1042x 1042x 1042x 1042x 5x 1042x 1042x 577x 577x 577x 577x 577x 577x 577x 577x 577x 5x 5x 5x 5x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 4x 5x 5x 5x 5x 577x 577x 577x 577x 577x 577x 577x 577x 577x 5x 5x 5x 5x 1x 5x   4x 1x 4x 1x 3x 2x 2x 5x 577x 577x 577x 577x 577x 577x 577x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 577x 577x 577x 577x 577x 577x 577x 577x 1x 1x 1x 1x 1x 1x 1x 577x 577x 577x 577x 577x 577x 577x 577x 577x 2x 2x 2x 2x 2x 2x 1x 1x 2x     2x 577x 577x 577x 577x 577x 577x 577x 1x                 1x 1x 1x 1x 1x 1x 1x 577x 577x 577x 577x 577x 577x 577x                                       577x 577x 577x 577x 577x 577x 577x 4x 4x 4x 4x 4x 4x 3x 3x 3x 4x 4x 4x 4x 4x 4x 4x 4x 4x 21x 14x 3x 3x 14x 14x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 577x 577x 577x 577x 577x 577x 577x 3x 3x 577x 577x 577x 577x 577x     577x 256x 256x 256x 256x 256x 256x 7x 7x 7x 7x  
import { DomPatcher } from './domPatch.js';
import { deriveScope } from '../reactive/scopeProxy.js';
import { unescapeTemplateMarkers } from '../utils/templateUtils.js';
 
/**
 * Manages deferred loading (<@defer>) of DOM subtrees and component instances.
 */
export class DeferManager {
  /** @type {WeakSet<Element>} */
  #initialized = new WeakSet();
 
  /** @type {WeakSet<Element>} */
  #loaded = new WeakSet();
 
  /** @type {WeakMap<Element, Function>} */
  #cleanups = new WeakMap();
 
  /**
   * @param {DynamicEvaluator} evaluator - Expression evaluator.
   * @param {TemplateRenderer} renderer - Template renderer.
   * @param {EventBinder} [eventBinder] - Event binder.
   * @param {string} [componentName] - Parent component name.
   */
  constructor(evaluator, renderer, eventBinder, componentName) {
    this.evaluator = evaluator;
    this.renderer = renderer;
    this.eventBinder = eventBinder;
    this.componentName = componentName || 'AnonymousComponent';
    this.patcher = new DomPatcher();
  }
 
  /**
   * Checks if a container element has already loaded its deferred content.
   * @param {Element} el
   * @returns {boolean}
   */
  isLoaded(el) {
    return this.#loaded.has(el);
  }
 
  /**
   * Processes all [data-ax-defer] container elements within a root element.
   * @param {Element} root - Root container or component element.
   * @param {object} scope - Evaluation scope.
   * @param {object} state - Component state.
   */
  process(root, scope, state) {
    if (!root) return;
 
    const containers = [];
    if (root.matches && root.matches('[data-ax-defer]')) {
      containers.push(root);
    }
    if (root.querySelectorAll) {
      root.querySelectorAll('[data-ax-defer]').forEach((el) => containers.push(el));
    }
 
    containers.forEach((container) => {
      this.#processContainer(container, scope, state);
    });
  }
 
  /**
   * Processes an individual [data-ax-defer] container element.
   * @param {Element} container
   * @param {object} scope
   * @param {object} state
   * #private
   */
  #processContainer(container, scope, state) {
    if (this.#loaded.has(container)) return;
 
    // Initialize placeholder content if not done yet
    if (!this.#initialized.has(container)) {
      this.#initialized.add(container);
      const placeholderTpl = container.querySelector('template[data-ax-defer-placeholder]');
      if (placeholderTpl && container.children.length === container.querySelectorAll('template').length) {
        const placeholderContent = unescapeTemplate(placeholderTpl.innerHTML);
        const renderedPlaceholder = this.renderer.render(placeholderContent, scope);
        const tempDiv = document.createElement('div');
        tempDiv.innerHTML = renderedPlaceholder;
        while (tempDiv.firstChild) {
          container.insertBefore(tempDiv.firstChild, placeholderTpl);
        }
      }
    }
 
    const whenAttr = (container.getAttribute('data-ax-defer-when') || 'idle').trim();
    this.setupTrigger(container, whenAttr, scope, state);
  }
 
  /**
   * Sets up trigger conditions based on whenAttr.
   * @param {Element} container
   * @param {string} whenAttr
   * @param {object} scope
   * @param {object} state
   */
  setupTrigger(container, whenAttr, scope, state) {
    if (this.#loaded.has(container)) return;
 
    const normalizedWhen = whenAttr.toLowerCase();
    if (normalizedWhen === 'idle') {
      this.#setupIdleTrigger(container, scope);
    } else if (normalizedWhen === 'visible') {
      this.#setupVisibleTrigger(container, scope);
    } else if (normalizedWhen === 'interaction' || normalizedWhen === 'hover' || normalizedWhen === 'click') {
      this.#setupInteractionTrigger(container, scope);
    } else if (normalizedWhen.startsWith('timer(') || normalizedWhen.endsWith('ms')) {
      this.#setupTimerTrigger(container, whenAttr, scope);
    } else {
      this.#setupExpressionTrigger(container, whenAttr, scope, state);
    }
  }
 
  /**
   * Triggers deferred loading on user interaction (click or mouseenter).
   * @param {Element} container - Target container element.
   * @param {object} scope - Evaluation scope.
   */
  #setupInteractionTrigger(container, scope) {
    const handler = () => {
      container.removeEventListener('click', handler);
      container.removeEventListener('mouseenter', handler);
      this.loadDeferredContent(container, scope);
    };
    container.addEventListener('click', handler);
    container.addEventListener('mouseenter', handler);
    this.registerCleanup(container, () => {
      container.removeEventListener('click', handler);
      container.removeEventListener('mouseenter', handler);
    });
  }
 
  /**
   * Triggers deferred loading after a timer delay in ms (e.g. timer(1000) or 1000ms).
   * @param {Element} container - Target container element.
   * @param {string} whenAttr - Raw trigger attribute value.
   * @param {object} scope - Evaluation scope.
   */
  #setupTimerTrigger(container, whenAttr, scope) {
    const match = whenAttr.match(/(\d+)/);
    const delay = match ? parseInt(match[1], 10) : 1000;
    const timer = setTimeout(() => {
      this.loadDeferredContent(container, scope);
    }, delay);
    this.registerCleanup(container, () => clearTimeout(timer));
  }
 
  /**
   * Triggers deferred loading when a reactive condition expression evaluates to true.
   * @param {Element} container - Target container element.
   * @param {string} whenAttr - Expression string to evaluate.
   * @param {object} scope - Evaluation scope.
   * @param {object} state - Component state.
   */
  #setupExpressionTrigger(container, whenAttr, scope, state) {
    try {
      const evalScope = deriveScope(scope, { state });
      const evalResult = this.evaluator
        ? this.evaluator.evaluateExpression(whenAttr, evalScope, state)
        : Boolean(state && state[whenAttr]);
      if (evalResult) {
        this.loadDeferredContent(container, scope);
      }
    } catch {
      // Evaluation failed, ignore
    }
  }
 
  /**
   * Triggers deferred loading during browser idle time (requestIdleCallback).
   * @param {Element} container - Target container element.
   * @param {object} scope - Evaluation scope.
   */
  #setupIdleTrigger(container, scope) {
    if (typeof window !== 'undefined' && 'requestIdleCallback' in window) {
      const handle = window.requestIdleCallback(() => {
        this.loadDeferredContent(container, scope);
      });
      this.registerCleanup(container, () => {
        if ('cancelIdleCallback' in window) {
          window.cancelIdleCallback(handle);
        }
      });
    } else {
      const timer = setTimeout(() => {
        this.loadDeferredContent(container, scope);
      }, 1);
      this.registerCleanup(container, () => clearTimeout(timer));
    }
  }
 
  /**
   * Triggers deferred loading when element becomes visible in viewport (IntersectionObserver).
   * @param {Element} container - Target container element.
   * @param {object} scope - Evaluation scope.
   */
  #setupVisibleTrigger(container, scope) {
    if (typeof window !== 'undefined' && 'IntersectionObserver' in window) {
      const observer = new IntersectionObserver(
        (entries) => {
          entries.forEach((entry) => {
            if (entry.isIntersecting) {
              observer.disconnect();
              this.loadDeferredContent(container, scope);
            }
          });
        },
        { threshold: 0.1 }
      );
      observer.observe(container);
      this.registerCleanup(container, () => observer.disconnect());
    } else {
      // Fallback if IntersectionObserver is not available
      this.loadDeferredContent(container, scope);
    }
  }
 
  /**
   * Swaps placeholder with deferred content.
   * @param {Element} container - Target container element.
   * @param {object} scope - Evaluation scope.
   */
  loadDeferredContent(container, scope) {
    if (this.#loaded.has(container)) return;
    this.#loaded.add(container);
 
    // Run cleanup if any trigger listener was set
    const cleanup = this.#cleanups.get(container);
    if (cleanup) {
      cleanup();
      this.#cleanups.delete(container);
    }
 
    const contentTpl = container.querySelector('template[data-ax-defer-content]');
    if (!contentTpl) return;
 
    const unescapedContent = unescapeTemplate(contentTpl.innerHTML);
    const renderedHtml = this.renderer.render(unescapedContent, scope);
 
    // Clear placeholder children (keep templates)
    Array.from(container.childNodes).forEach((child) => {
      if (child.nodeName !== 'TEMPLATE') {
        if (this.eventBinder && child.nodeType === 1) {
          this.eventBinder.unbind(child);
        }
        container.removeChild(child);
      }
    });
 
    // Mount newly rendered deferred content
    const tempDiv = document.createElement('div');
    tempDiv.innerHTML = renderedHtml;
 
    const fragment = document.createDocumentFragment();
    while (tempDiv.firstChild) {
      fragment.appendChild(tempDiv.firstChild);
    }
 
    container.insertBefore(fragment, contentTpl);
 
    // Bind events if eventBinder is available
    if (this.eventBinder) {
      this.eventBinder.bind(container, scope);
    }
  }
 
  /**
   * Registers a cleanup function for a container.
   * @param {Element} container
   * @param {Function} cleanupFn
   */
  registerCleanup(container, cleanupFn) {
    this.#cleanups.set(container, cleanupFn);
  }
 
  /**
   * Destroys and cleans up all observers and listeners.
   */
  destroy() {
    // Cleanup handlers
  }
}
 
/**
 * Restores escaped template expressions from {% %} back to {{ }}.
 * @param {string} html
 * @returns {string}
 */
function unescapeTemplate(html) {
  if (!html) return '';
  return unescapeTemplateMarkers(html);
}