All files / app-play/src worker-transferables.ts

100% Statements 22/22
100% Branches 15/15
100% Functions 2/2
100% Lines 17/17

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    3x 3x 3x 17x 14x 14x 12x 6x 6x 1x 1x 1x   5x 1x   11x     3x 3x    
/** Collect each owned output buffer once, without copying buffers or walking cyclic references twice. */
export function collectWorkerTransferables(value: unknown): ArrayBuffer[] {
  const seen = new Set<object>();
  const buffers = new Set<ArrayBuffer>();
  const visit = (item: unknown): void => {
    if (!item || typeof item !== 'object' || seen.has(item)) return;
    seen.add(item);
    if (item instanceof ArrayBuffer) buffers.add(item);
    else if (ArrayBuffer.isView(item)) {
      if (item.buffer instanceof ArrayBuffer) buffers.add(item.buffer);
    } else if (item instanceof Map) {
      for (const [key, entry] of item) {
        visit(key);
        visit(entry);
      }
    } else if (item instanceof Set) {
      for (const entry of item) visit(entry);
    } else {
      for (const entry of Object.values(item)) visit(entry);
    }
  };
  visit(value);
  return [...buffers];
}