Type-safe asset loader class

// Default configuration with 60 second timeout
const assetLoader = new AssetLoader();

// Configuration with custom settings
const customLoader = new AssetLoader({
maxConcurrentLoads: 5,
timeout: 30000 // 30 seconds
});

// Disable timeout (wait indefinitely)
const noTimeoutLoader = new AssetLoader({
timeout: 0 // or any negative value
});

// Load promises in object format
const assets = await assetLoader.load({
environment: Rn.CubeTexture.fromUrl({
baseUrl: '/path/to/environment',
mipmapLevelNumber: 1,
isNamePosNeg: true,
hdriFormat: Rn.HdriFormat.HDR_LINEAR,
}),
specular: Rn.CubeTexture.fromUrl({
baseUrl: '/path/to/specular',
mipmapLevelNumber: 10,
isNamePosNeg: true,
hdriFormat: Rn.HdriFormat.RGBE_PNG,
}),
diffuse: Rn.CubeTexture.fromUrl({
baseUrl: '/path/to/diffuse',
mipmapLevelNumber: 1,
isNamePosNeg: true,
hdriFormat: Rn.HdriFormat.RGBE_PNG,
})
});

console.log(assets.environment); // CubeTexture
console.log(assets.specular); // CubeTexture
console.log(assets.diffuse); // CubeTexture

Constructors

  • Creates a new AssetLoader instance with the specified configuration.

    Parameters

    Returns AssetLoader

    // Default configuration
    const loader = new AssetLoader();

    // Custom configuration
    const customLoader = new AssetLoader({
    maxConcurrentLoads: 5,
    timeout: 30000
    });

Methods

  • Gets the current loading status including active and queued loads. Useful for monitoring loading progress and debugging.

    Returns {
        active: number;
        queued: number;
    }

    Object containing the number of active and queued loads

    • active: number
    • queued: number
    const status = loader.getLoadingStatus();
    console.log(`Active: ${status.active}, Queued: ${status.queued}`);
  • Loads multiple promises organized as an object with named keys. The result preserves the same structure with resolved values.

    Type Parameters

    • T extends Record<string, Promise<any>>

      Object type where values are promises

    Parameters

    • promiseObject: T

      Object containing promises to be loaded

    Returns Promise<AwaitedObject<T>>

    Promise that resolves to an object with the same keys but resolved values

    const assets = await loader.load({
    texture: loadTexture('path/to/texture.jpg'),
    model: loadModel('path/to/model.gltf'),
    audio: loadAudio('path/to/sound.mp3')
    });

    // Type-safe access to resolved values
    console.log(assets.texture); // Texture
    console.log(assets.model); // Model
    console.log(assets.audio); // AudioBuffer
  • Loads multiple promises in bulk as an array. Supports both homogeneous arrays and heterogeneous tuples with type safety.

    Type Parameters

    • T

      The type of values in the array or tuple

    Parameters

    • promises: Promise<T>[]

      Array or tuple of promises to be loaded

    Returns Promise<T[]>

    Promise that resolves to an array or tuple of resolved values with preserved types

    // Homogeneous array
    const textures = await loader.loadArray([
    loadTexture('texture1.jpg'),
    loadTexture('texture2.jpg'),
    loadTexture('texture3.jpg')
    ]);

    // Heterogeneous tuple with type safety
    const [texture, model, audio] = await loader.loadArray([
    loadTexture('texture.jpg'),
    loadModel('model.gltf'),
    loadAudio('sound.mp3')
    ] as const);
  • Type Parameters

    • T extends readonly unknown[]

    Parameters

    • promises: readonly [{
          [K in string | number | symbol]: Promise<T[K<K>]>
      }]

    Returns Promise<T>

  • Loads a single promise with queue management, timeout, and concurrency control.

    Type Parameters

    • T

      The type of value the promise resolves to

    Parameters

    • promise: Promise<T>

      The promise to be loaded

    Returns Promise<T>

    Promise that resolves to the same value as the input promise

    const texture = await loader.loadSingle(loadTexture('texture.jpg'));
    const model = await loader.loadSingle(loadModel('model.gltf'));
  • Loads multiple promises with retry capabilities organized as an object. Combines the structure of load() with the retry functionality of loadWithRetryArray().

    Type Parameters

    • T extends Record<string, Promise<any>>

      Object type where values are promises

    Parameters

    • promiseFactories: {
          [K in string | number | symbol]: (() => T[K])[]
      }

      Object where each value is an array of factory functions

    Returns Promise<AwaitedObject<T>>

    Promise that resolves to an object with the same keys but resolved values

    const assets = await loader.loadWithRetry({
    texture: [
    () => loadTexture('high-res.jpg'),
    () => loadTexture('low-res.jpg')
    ],
    model: [
    () => loadModel('detailed.gltf'),
    () => loadModel('simple.gltf')
    ]
    });

    console.log(assets.texture); // Texture (from successful factory)
    console.log(assets.model); // Model (from successful factory)
  • Loads multiple promises with retry capabilities for each one. Each promise has its own set of factory functions for retry attempts.

    Type Parameters

    • T

      The type of values the promises resolve to

    Parameters

    • promiseFactories: (() => Promise<T>)[][]

      Array where each element is an array of factory functions for one promise

    Returns Promise<T[]>

    Promise that resolves to an array of resolved values

    const assets = await loader.loadWithRetryArray([
    [
    () => loadTexture('high-res.jpg'),
    () => loadTexture('low-res.jpg')
    ],
    [
    () => loadModel('detailed.gltf'),
    () => loadModel('simple.gltf')
    ]
    ]);
  • Loads a single promise with automatic retry using fallback factories. If the first promise fails, it will try the next factory in order.

    Type Parameters

    • T

      The type of value the promise resolves to

    Parameters

    • promiseFactories: (() => Promise<T>)[]

      Array of factory functions that create promises, ordered by priority

    Returns Promise<T>

    Promise that resolves to the value from the first successful factory

    When no promise factories are provided

    const texture = await loader.loadWithRetrySingle([
    () => loadTexture('high-quality.jpg'), // Try first
    () => loadTexture('medium-quality.jpg'), // Fallback 1
    () => loadTexture('low-quality.jpg') // Fallback 2
    ]);
  • Waits until all currently active and queued loads are complete. Useful for ensuring all assets are loaded before proceeding.

    Returns Promise<void>

    Promise that resolves when all loads are complete

    // Start multiple loads
    loader.loadSingle(loadTexture('texture.jpg'));
    loader.loadSingle(loadModel('model.gltf'));

    // Wait for all to complete
    await loader.waitForAllLoads();
    console.log('All assets loaded!');