import {LoaderImage, LoaderImageStatus, LoaderImageRequest, LoaderImageResponse} from "./Loaders-Image"; import {S, Maybe, Either} from "../external/sanctuary/Sanctuary"; import {isNil} from "../utils/Utils"; export interface LoaderTextureRequest extends LoaderImageRequest { mapper: (target: HTMLImageElement) => WebGLTexture; } export interface LoaderTextureResponse extends LoaderImageResponse { texture: WebGLTexture; } export const LOADER_TYPE_TEXTURE = "texture"; export type LoaderTextureStatus = Maybe | Either; export type LoaderTextureCallback = (resp:Either) => void; //use loadTexture for promise-version, this one is meant to be callback/polled export class LoaderTexture { private _status:Either; public __loaderType = LOADER_TYPE_TEXTURE; constructor(_req:Partial, callbackAfterLoad?:LoaderTextureCallback) { const req = Object.assign({}, _req, {__loaderType: LOADER_TYPE_TEXTURE}) const ldr = new LoaderImage(req, imgResp => { this._status = imgResp.isLeft ? S.Left({ //TODO: change this to map or chain, convert to Right first then to left __loaderType: LOADER_TYPE_TEXTURE, errorEvt: imgResp.value.errorEvt }) : S.map(imgResp => ({ img: imgResp.img, texture: req.mapper(imgResp.img), __loaderType: LOADER_TYPE_TEXTURE })) (imgResp) if(callbackAfterLoad !== undefined) { callbackAfterLoad(this._status); } }) } public get status():LoaderTextureStatus { return isNil(this._status) ? S.Nothing : this._status } } export const loadTexturePromise = (req:Partial):Promise> => new Promise>(resolve => new LoaderTexture(req, resolve)); export const loadTextureFactory = (req:Partial, callback?:LoaderTextureCallback):LoaderTexture => new LoaderTexture(req, callback) export const isLoaderTexture = (arg:any): arg is LoaderTexture => arg.__loaderType === LOADER_TYPE_TEXTURE;