import { Asset } from "../../core/asset"; import { GlobalContext } from "../../core/global"; import { AssetImporterPlugin, AssetsPack } from "./types"; export * from "./types"; interface Importers { [key: string]: AssetImporterPlugin } // const importers = { // img: TextureImporter, // }; export class AssetsImporter { private importers: T; constructor(importers: T) { this.importers = importers; } async url(url: string, ctx = GlobalContext()) { const buffer = await fetch(url).then(r => r.arrayBuffer()); return await this.buffer(buffer, ctx); } async blob(blob: Blob, ctx = GlobalContext()) { const buffer = await blob.arrayBuffer(); return await this.buffer(buffer, ctx); } async buffer(buffer: ArrayBuffer, ctx = GlobalContext()): Promise> { const bufImporters: BufferImporter = {} as any; for (const key in this.importers) { bufImporters[key] = (options?) => this.importers[key].import(buffer, options, ctx) as any; } return bufImporters; } } type BufferImporter = { [key in keyof T]: (options?: Parameters["1"]) => ReturnType; }; // type BufferImporter = { [key in keyof typeof importers]: (options: AssetImportOptions) => Promise }; // function createBufferImporter(buffer: ArrayBuffer, ctx = GlobalContext()): BufferImporter // { // const wrapper = {} as any; // for (const importer in importers) // { // wrapper[importer] = (options?: AssetImportOptions) => importers[importer as keyof typeof importers].import(buffer, options, ctx); // } // return wrapper; // } // export const AssetsImporter = { // importers: importers, // async url(url: string, ctx = GlobalContext()) // { // const buffer = await fetch(url).then(r => r.arrayBuffer()); // return createBufferImporter(buffer, ctx); // }, // async buffer(buffer: ArrayBuffer, ctx = GlobalContext()) // { // return createBufferImporter(buffer, ctx); // } // };