All files / src/components/rendering tilemapLayersScheduler.ts

89.74% Statements 70/78
75.86% Branches 22/29
100% Functions 9/9
89.18% Lines 66/74

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    2x 2x                                 2x 2x     2x 5x 5x 5x   5x 5x 5x 1x 1x 1x     4x 11x 11x 9x   11x 11x   11x               4x 4x     5x 5x     2x 10x 1x   9x     2x 9x 9x 9x       9x 4x   5x   5x 5x 5x 5x                   5x     2x 7x 7x 5x 5x     7x 7x 7x 5x                       5x   7x     2x             7x 7x 2x 2x     5x 5x     5x 5x 5x 5x     5x 3x 3x      
import type { SceneTilemapData } from "shared/lib/resources/types";
 
const WORK_BUDGET_MS = 4;
const TIME_CHECK_INTERVAL = 1024;
 
type Subscriber = (tiles: Uint32Array) => void;
 
interface FlattenJob {
  tilemap: SceneTilemapData;
  width: number;
  height: number;
  tiles: Uint32Array;
  layerIndex: number;
  cellIndex: number;
  subscribers: Set<Subscriber>;
  prioritySubscribers: Set<Subscriber>;
  queued: boolean;
  complete: boolean;
}
 
const jobsByTilemap = new WeakMap<SceneTilemapData, Map<string, FlattenJob>>();
const queue: FlattenJob[] = [];
let timer: ReturnType<typeof setTimeout> | undefined;
 
const processJobChunk = (job: FlattenJob) => {
  const size = job.width * job.height;
  const deadline = performance.now() + WORK_BUDGET_MS;
  let operations = 0;
 
  while (job.layerIndex < job.tilemap.layers.length) {
    const layer = job.tilemap.layers[job.layerIndex];
    if (!layer.visible) {
      job.layerIndex++;
      job.cellIndex = 0;
      continue;
    }
 
    while (job.cellIndex < size) {
      const tile = layer.tiles[job.cellIndex];
      if (tile) {
        job.tiles[job.cellIndex] = tile;
      }
      job.cellIndex++;
      operations++;
 
      Iif (
        operations % TIME_CHECK_INTERVAL === 0 &&
        performance.now() >= deadline
      ) {
        return false;
      }
    }
 
    job.layerIndex++;
    job.cellIndex = 0;
  }
 
  job.complete = true;
  return true;
};
 
const scheduleQueue = () => {
  if (timer !== undefined) {
    return;
  }
  timer = setTimeout(processQueue, 0);
};
 
const processQueue = () => {
  timer = undefined;
  let job = queue.shift();
  while (job && job.subscribers.size === 0) {
    job.queued = false;
    job = queue.shift();
  }
  if (!job) {
    return;
  }
  job.queued = false;
 
  if (processJobChunk(job)) {
    job.subscribers.forEach((subscriber) => subscriber(job.tiles));
    job.subscribers.clear();
    job.prioritySubscribers.clear();
  } else E{
    job.queued = true;
    if (job.prioritySubscribers.size) {
      queue.unshift(job);
    } else {
      queue.push(job);
    }
  }
 
  scheduleQueue();
};
 
const getJob = (tilemap: SceneTilemapData, width: number, height: number) => {
  let jobs = jobsByTilemap.get(tilemap);
  if (!jobs) {
    jobs = new Map();
    jobsByTilemap.set(tilemap, jobs);
  }
 
  const key = `${width}x${height}`;
  let job = jobs.get(key);
  if (!job) {
    job = {
      tilemap,
      width,
      height,
      tiles: new Uint32Array(width * height),
      layerIndex: 0,
      cellIndex: 0,
      subscribers: new Set(),
      prioritySubscribers: new Set(),
      queued: false,
      complete: false,
    };
    jobs.set(key, job);
  }
  return job;
};
 
export const scheduleFlattenTilemap = (
  tilemap: SceneTilemapData,
  width: number,
  height: number,
  subscriber: Subscriber,
  priority = false,
) => {
  const job = getJob(tilemap, width, height);
  if (job.complete) {
    const completeTimer = setTimeout(() => subscriber(job.tiles), 0);
    return () => clearTimeout(completeTimer);
  }
 
  job.subscribers.add(subscriber);
  Iif (priority) {
    job.prioritySubscribers.add(subscriber);
  }
  Eif (!job.queued) {
    job.queued = true;
    queue.unshift(job);
    scheduleQueue();
  }
 
  return () => {
    job.subscribers.delete(subscriber);
    job.prioritySubscribers.delete(subscriber);
  };
};