Autoload

Automatic data loading and initialization utilities.

Features

Auto Detection

Automatic file format detection and parsing

Lazy Loading

On-demand loading of large datasets

Configuration

interface AutoloadConfig {
  source: string | File;           // Data source
  format?: string;                 // Optional format override
  options?: {                      // Loading options
    chunkSize?: number;           // Size of data chunks
    maxMemory?: number;           // Maximum memory usage
    cache?: boolean;              // Enable caching
  };
  callbacks?: {                    // Event callbacks
    onProgress?: (progress: number) => void;
    onComplete?: (data: any) => void;
    onError?: (error: Error) => void;
  };
}

Methods

load

Load data from source

async function load(config: AutoloadConfig): Promise<any> {
  const format = config.format || detectFormat(config.source);
  const parser = getParser(format);
  
  try {
    const data = await parser.parse(config.source, config.options);
    config.callbacks?.onComplete?.(data);
    return data;
  } catch (error) {
    config.callbacks?.onError?.(error);
    throw error;
  }
}

detectFormat

Detect file format

function detectFormat(source: string | File): string {
  if (typeof source === 'string') {
    const ext = source.split('.').pop().toLowerCase();
    return formatMap[ext] || 'unknown';
  }
  
  const type = source.type;
  return mimeMap[type] || 'unknown';
}

getParser

Get appropriate parser

function getParser(format: string): Parser {
  const parser = parserMap[format];
  if (!parser) {
    throw new Error(`No parser available for format: ${format}`);
  }
  return parser;
}

Example

// Configure autoloader
const autoloader = new Autoloader({
  source: 'protein.pdb',
  options: {
    chunkSize: 1024 * 1024,  // 1MB chunks
    maxMemory: 512 * 1024 * 1024,  // 512MB max
    cache: true
  },
  callbacks: {
    onProgress: (progress) => {
      console.log(`Loading: ${progress}%`);
    },
    onComplete: (data) => {
      console.log('Loading complete');
      viewer.load(data);
    },
    onError: (error) => {
      console.error('Loading failed:', error);
    }
  }
});

// Load data
try {
  const data = await autoloader.load();
  // Process loaded data
} catch (error) {
  // Handle error
}

Related Topics

Learn more about utilities: