All files / packages/core/src/loaders fillNearbyFrames.ts

0% Statements 0/22
0% Branches 0/11
0% Functions 0/1
0% Lines 0/22

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                                                                                                           
import { ImageLoadListener } from '../types';
import { ImageQualityStatus } from '../enums';
 
/** Actually fills the nearby frames from the given frame */
export function fillNearbyFrames(
  listener: ImageLoadListener,
  imageQualityStatusMap: Map<string, ImageQualityStatus>,
  request,
  image,
  options
) {
  if (!request?.nearbyRequests?.length) {
    return;
  }
 
  const {
    arrayBuffer,
    offset: srcOffset,
    type,
    length: frameLength,
  } = options.targetBuffer;
  if (!arrayBuffer || srcOffset === undefined || !type) {
    return;
  }
  const scalarData = new Float32Array(arrayBuffer);
  const bytesPerPixel = scalarData.byteLength / scalarData.length;
  const offset = options.targetBuffer.offset / bytesPerPixel; // in bytes
 
  // since set is based on the underlying type,
  // we need to divide the offset bytes by the byte type
  const src = scalarData.slice(offset, offset + frameLength);
 
  for (const nearbyItem of request.nearbyRequests) {
    try {
      const { itemId: targetId, imageQualityStatus } = nearbyItem;
      const targetStatus = imageQualityStatusMap.get(targetId);
      if (targetStatus !== undefined && targetStatus >= imageQualityStatus) {
        continue;
      }
      const targetOptions = listener.getLoaderImageOptions(targetId);
      const { offset: targetOffset } = targetOptions.targetBuffer as any;
      scalarData.set(src, targetOffset / bytesPerPixel);
      const nearbyImage = {
        ...image,
        imageQualityStatus,
      };
      listener.successCallback(targetId, nearbyImage);
      imageQualityStatusMap.set(targetId, imageQualityStatus);
    } catch (e) {
      console.log("Couldn't fill nearby item ", nearbyItem.itemId, e);
    }
  }
}