All files / lib/core/reactive scope.js

96.74% Statements 119/123
84.21% Branches 16/19
77.77% Functions 7/9
96.74% Lines 119/123

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 124404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 671x 671x 671x 671x 671x 671x 671x 404x 404x 404x 404x 404x 404x 404x 404x 11x     11x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 11x 404x 404x 404x 404x 404x 404x 195x 195x 195x 195x 195x 8x 8x 195x 404x 404x 404x 404x 404x 404x 404x 1x 1x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 404x 238x 238x 238x 238x 238x 238x 238x 238x 404x 404x 404x 404x 404x 404x 404x 33x     33x 22x 22x 7x 5x 5x 22x 22x 11x 33x  
/**
 * @file scope.js
 * @description Disposal scopes for Avenx-JS.
 *
 * A disposal scope is the owner of every teardown callback created while it is
 * active. Components run their lifecycle hooks and event handlers inside their
 * own scope, so anything that registers a subscription during that window (most
 * notably `bridge.on(...)`) is released automatically when the component
 * unmounts. This mirrors how `$watch` watchers are already collected in
 * `AvenxComponent._watchers` and torn down in `__performTeardown()`.
 */
 
/**
 * The scope that currently owns newly created teardown callbacks.
 * @type {DisposalScope|null}
 */
let activeScope = null;
 
/**
 * Collects teardown callbacks and releases them together.
 */
export class DisposalScope {
  /**
   * @param {string} [name] - Debug label used in diagnostics.
   */
  constructor(name = 'scope') {
    /** @type {string} */
    this.name = name;
    /** @type {Set<Function>} */
    this.disposers = new Set();
    /** @type {boolean} */
    this.disposed = false;
  }
 
  /**
   * Registers a teardown callback with this scope.
   * Disposing the scope runs it; running it earlier removes it from the scope.
   * @param {Function} disposer - The teardown callback.
   * @returns {Function} A wrapper that runs the teardown at most once.
   */
  add(disposer) {
    if (typeof disposer !== 'function') {
      return () => {};
    }
    if (this.disposed) {
      disposer();
      return () => {};
    }
 
    let released = false;
    const release = () => {
      if (released) return;
      released = true;
      this.disposers.delete(release);
      disposer();
    };
 
    this.disposers.add(release);
    return release;
  }
 
  /**
   * Runs every registered teardown callback and empties the scope.
   * Safe to call more than once.
   */
  dispose() {
    this.disposed = true;
    // Copy first: a disposer removes itself from the set while running.
    const pending = [...this.disposers];
    this.disposers.clear();
    for (const release of pending) {
      release();
    }
  }
}
 
/**
 * Returns the scope that currently owns new teardown callbacks.
 * @returns {DisposalScope|null} The active scope, or null outside of one.
 */
export function getScope() {
  return activeScope;
}
 
/**
 * Runs a function with the given scope active, restoring the previous scope
 * afterwards. Passing `null` deliberately detaches ownership, which is how
 * long-lived work (such as a bridge `setup()`) avoids being torn down by
 * whichever component happened to touch it first.
 * @template T
 * @param {DisposalScope|null} scope - The scope to activate.
 * @param {function(): T} fn - The function to run.
 * @returns {T} Whatever `fn` returned.
 */
export function runInScope(scope, fn) {
  const previous = activeScope;
  activeScope = scope;
  try {
    return fn();
  } finally {
    activeScope = previous;
  }
}
 
/**
 * Registers a teardown callback with the active scope, if there is one.
 * @param {Function} disposer - The teardown callback.
 * @returns {Function} A release function that runs the teardown at most once.
 */
export function onScopeDispose(disposer) {
  if (typeof disposer !== 'function') {
    return () => {};
  }
  if (!activeScope) {
    let released = false;
    return () => {
      if (released) return;
      released = true;
      disposer();
    };
  }
  return activeScope.add(disposer);
}