@armniko/assets
Advanced tools
| export declare enum AssetType { | ||
| Image = 0, | ||
| Font = 1, | ||
| Audio = 2, | ||
| Json = 3 | ||
| } |
| export declare class HttpError extends Error { | ||
| response: Response; | ||
| constructor(response: Response); | ||
| } |
| export declare class InvalidJsonError extends Error { | ||
| response: Response; | ||
| body: string; | ||
| constructor(response: Response, body: string); | ||
| } |
| export declare class MediaLoadError extends Error { | ||
| mediaError: MediaError; | ||
| constructor(mediaError: MediaError); | ||
| } |
| import { Loader } from './loader'; | ||
| import { LoadableAsset } from '../core/loadable-asset'; | ||
| import { AssetType } from '../enums'; | ||
| export declare class JsonLoader extends Loader<AssetType.Json> { | ||
| load<T>(loadableAsset: LoadableAsset, onSuccess: (json: T) => void, onError: (error: Error) => void): void; | ||
| } |
| import { Progress } from './progress'; | ||
| import { AssetsBatchOptions } from '../interfaces/assets-batch-options.interface'; | ||
| import { BatchAsset } from '../types'; | ||
| import { BatchAsset, AssetsBatchOptions } from '../types'; | ||
| export declare class AssetsBatch { | ||
@@ -5,0 +4,0 @@ private options; |
@@ -7,3 +7,5 @@ import { AssetsBatch } from './assets-batch'; | ||
| audio(name: string): HTMLAudioElement | undefined; | ||
| json<T>(name: string): T | undefined; | ||
| asset<T>(name: string): HTMLImageElement | HTMLAudioElement | string | T | undefined; | ||
| fetch(batch: AssetsBatch): void; | ||
| } |
@@ -1,2 +0,2 @@ | ||
| import { AssetType } from '../enums/asset-type'; | ||
| import { AssetType } from '../enums'; | ||
| import { BatchAsset } from '../types'; | ||
@@ -3,0 +3,0 @@ export declare class LoadableAsset { |
| import { Progress } from '../core/progress'; | ||
| import { AssetRecord } from '../interfaces/asset-record.interface'; | ||
| import { BatchAsset } from '../types'; | ||
| import { BatchAsset, AssetRecord } from '../types'; | ||
| export declare abstract class Fetcher { | ||
@@ -5,0 +4,0 @@ protected _total: number; |
+7
-5
@@ -1,5 +0,7 @@ | ||
| export * from './core/assets'; | ||
| export * from './core/assets-batch'; | ||
| export * from './core/progress'; | ||
| export * from './interfaces/assets-batch-options.interface'; | ||
| export type { BatchAsset } from './types'; | ||
| export { Assets } from './core/assets'; | ||
| export { AssetsBatch } from './core/assets-batch'; | ||
| export { Progress } from './core/progress'; | ||
| export { type HttpError } from './libraries/http-error'; | ||
| export { type InvalidJsonError } from './libraries/invalid-json-error'; | ||
| export { type MediaLoadError } from './libraries/media-load-error'; | ||
| export type { BatchAsset, AssetsBatchOptions } from './types'; |
+1
-231
@@ -1,231 +0,1 @@ | ||
| class l { | ||
| } | ||
| class c extends l { | ||
| load(t, s, o) { | ||
| const e = new Image(); | ||
| e.crossOrigin = "Anonymous", e.onload = () => { | ||
| s(e); | ||
| }, e.onerror = (n) => { | ||
| typeof n == "string" && o(new Error(n)); | ||
| }, e.src = t.url(); | ||
| } | ||
| } | ||
| class d { | ||
| _total; | ||
| _loaded; | ||
| _failed; | ||
| constructor(t, s, o) { | ||
| this._total = t, this._loaded = s, this._failed = o; | ||
| } | ||
| total() { | ||
| return this._total; | ||
| } | ||
| loaded() { | ||
| return this._loaded; | ||
| } | ||
| failed() { | ||
| return this._failed; | ||
| } | ||
| isCompleted() { | ||
| return this.loaded() + this.failed() >= this.total(); | ||
| } | ||
| percents() { | ||
| return Math.floor(100 / this.total() * (this.loaded() + this.failed())); | ||
| } | ||
| } | ||
| var r = /* @__PURE__ */ ((a) => (a[a.Image = 0] = "Image", a[a.Font = 1] = "Font", a[a.Audio = 2] = "Audio", a))(r || {}); | ||
| class h { | ||
| _batchAsset; | ||
| constructor(t) { | ||
| this._batchAsset = t; | ||
| } | ||
| url() { | ||
| return typeof this._batchAsset == "string" ? this._batchAsset : Object.values(this._batchAsset)[0]; | ||
| } | ||
| name() { | ||
| return typeof this._batchAsset == "string" ? this.urlLastSegment().split(".")[0] : Object.keys(this._batchAsset)[0]; | ||
| } | ||
| type() { | ||
| const t = this.urlLastSegment().split(".").pop(); | ||
| let s; | ||
| switch (t) { | ||
| case "woff2": | ||
| case "woff": | ||
| case "ttf": | ||
| case "otf": | ||
| s = r.Font; | ||
| break; | ||
| case "png": | ||
| case "webp": | ||
| case "jpg": | ||
| case "jpeg": | ||
| case "svg": | ||
| case "gif": | ||
| s = r.Image; | ||
| break; | ||
| case "webm": | ||
| case "mp3": | ||
| case "ogg": | ||
| case "m4a": | ||
| case "aac": | ||
| case "wav": | ||
| s = r.Audio; | ||
| break; | ||
| default: | ||
| s = void 0; | ||
| break; | ||
| } | ||
| return s; | ||
| } | ||
| urlLastSegment() { | ||
| return this.url().split("?")[0].split("/").filter((t) => t !== "").pop() || ""; | ||
| } | ||
| } | ||
| class u extends l { | ||
| load(t, s, o) { | ||
| const e = new FontFace(t.name(), "url(" + t.url() + ")"); | ||
| document.fonts.add(e), e.load().then(() => { | ||
| s(t.name()); | ||
| }).catch((n) => { | ||
| o(n); | ||
| }); | ||
| } | ||
| } | ||
| class _ extends l { | ||
| load(t, s, o) { | ||
| const e = new Audio(t.url()); | ||
| e.oncanplaythrough = () => { | ||
| s(e); | ||
| }, e.onerror = () => { | ||
| const n = e.error ? e.error.message : "Failed to load audio!"; | ||
| o(new Error(n)); | ||
| }, e.load(); | ||
| } | ||
| } | ||
| class p { | ||
| _total = 0; | ||
| _loaded = 0; | ||
| _failed = 0; | ||
| _batchAssets = []; | ||
| _onLoad; | ||
| _onComplete; | ||
| _onError; | ||
| constructor(t, s, o, e) { | ||
| this._batchAssets = t, this._total = this._batchAssets.length, this._onLoad = s, this._onComplete = o, this._onError = e; | ||
| } | ||
| loadAsset(t, s, o) { | ||
| const e = new h(t), n = this.loader(e); | ||
| n ? n.load( | ||
| e, | ||
| (i) => { | ||
| s({ | ||
| name: e.name(), | ||
| url: e.url(), | ||
| asset: i, | ||
| type: e.type() | ||
| }); | ||
| }, | ||
| (i) => o(e.url(), i) | ||
| ) : o(e.url(), new Error("Cant find loader for `" + e.url() + "`")); | ||
| } | ||
| progress() { | ||
| return new d(this._total, this._loaded, this._failed); | ||
| } | ||
| loader(t) { | ||
| let s; | ||
| switch (t.type()) { | ||
| case r.Font: | ||
| s = new u(); | ||
| break; | ||
| case r.Image: | ||
| s = new c(); | ||
| break; | ||
| case r.Audio: | ||
| s = new _(); | ||
| break; | ||
| } | ||
| return s; | ||
| } | ||
| } | ||
| class f extends p { | ||
| fetch() { | ||
| this._batchAssets.forEach((t) => { | ||
| this.loadAsset( | ||
| t, | ||
| this.onLoadAssetSuccessCallback.bind(this), | ||
| this.onLoadAssetErrorCallback.bind(this) | ||
| ); | ||
| }); | ||
| } | ||
| onLoadAssetSuccessCallback(t) { | ||
| this._loaded++; | ||
| const s = this.progress(); | ||
| this._onLoad(t, s), this.checkAndCompleteBatch(s); | ||
| } | ||
| onLoadAssetErrorCallback(t, s) { | ||
| this._failed++; | ||
| const o = this.progress(); | ||
| this._onError(t, s), this.checkAndCompleteBatch(o); | ||
| } | ||
| checkAndCompleteBatch(t) { | ||
| t.isCompleted() && this._onComplete(t); | ||
| } | ||
| } | ||
| class m { | ||
| _store = { | ||
| [r.Font]: {}, | ||
| [r.Image]: {}, | ||
| [r.Audio]: {} | ||
| }; | ||
| font(t) { | ||
| return this._store[r.Font][t]?.asset; | ||
| } | ||
| image(t) { | ||
| return this._store[r.Image][t]?.asset; | ||
| } | ||
| audio(t) { | ||
| return this._store[r.Audio][t]?.asset; | ||
| } | ||
| fetch(t) { | ||
| new f( | ||
| t.assets(), | ||
| (s, o) => { | ||
| const e = this._store[s.type]; | ||
| e[s.name] = s, t.loaded(s.name, o); | ||
| }, | ||
| (s) => { | ||
| t.completed(s); | ||
| }, | ||
| (s, o) => { | ||
| t.error(s, o); | ||
| } | ||
| ).fetch(); | ||
| } | ||
| } | ||
| class g { | ||
| constructor(t) { | ||
| this.options = t, t.assets = this.uniqueAssets(t.assets); | ||
| } | ||
| assets() { | ||
| return this.options.assets; | ||
| } | ||
| loaded(t, s) { | ||
| this.options.onLoad && this.options.onLoad(t, s); | ||
| } | ||
| completed(t) { | ||
| this.options.onComplete && this.options.onComplete(t); | ||
| } | ||
| error(t, s) { | ||
| this.options.onError && this.options.onError(t, s); | ||
| } | ||
| uniqueAssets(t) { | ||
| return t.filter( | ||
| (s, o, e) => o === e.indexOf(s) | ||
| ); | ||
| } | ||
| } | ||
| export { | ||
| m as Assets, | ||
| g as AssetsBatch, | ||
| d as Progress | ||
| }; | ||
| var t=class{},s=class extends t{load(t,s,e){const o=new Image;o.crossOrigin="Anonymous",o.onload=()=>{s(o)},o.onerror=t=>{e(new Error("string"==typeof t?t:"Failed to load image!"))},o.src=t.url()}},e=class{_total;_loaded;_failed;constructor(t,s,e){this._total=t,this._loaded=s,this._failed=e}total(){return this._total}loaded(){return this._loaded}failed(){return this._failed}isCompleted(){return this.loaded()+this.failed()>=this.total()}percents(){return Math.floor(100/this.total()*(this.loaded()+this.failed()))}},o=/* @__PURE__ */function(t){return t[t.Image=0]="Image",t[t.Font=1]="Font",t[t.Audio=2]="Audio",t[t.Json=3]="Json",t}({}),r=class{_batchAsset;constructor(t){this._batchAsset=t}url(){return"string"==typeof this._batchAsset?this._batchAsset:Object.values(this._batchAsset)[0]}name(){return"string"==typeof this._batchAsset?this.urlLastSegment().split(".")[0]:Object.keys(this._batchAsset)[0]}type(){let t;switch(this.urlLastSegment().split(".").pop()){case"woff2":case"woff":case"ttf":case"otf":t=o.Font;break;case"png":case"webp":case"jpg":case"jpeg":case"svg":case"gif":t=o.Image;break;case"webm":case"mp3":case"ogg":case"m4a":case"aac":case"wav":t=o.Audio;break;case"json":t=o.Json;break;default:t=void 0}return t}urlLastSegment(){return this.url().split("?")[0].split("/").filter(t=>""!==t).pop()||""}},a=class extends t{load(t,s,e){const o=new FontFace(t.name(),"url("+t.url()+")");document.fonts.add(o),o.load().then(()=>{s(t.name())}).catch(t=>{e(t instanceof Error?t:/* @__PURE__ */new Error("Failed to load font!"))})}},n=class extends Error{constructor(t){super(`MediaError ${t.code}`),this.mediaError=t}},i=class extends t{load(t,s,e){const o=new Audio(t.url());o.oncanplaythrough=()=>{s(o)},o.onerror=()=>{e(o.error?new n(o.error):/* @__PURE__ */new Error("Failed to load audio!"))},o.load()}},l=class extends Error{constructor(t){super(`HTTP ${t.status}`),this.response=t}},c=class extends Error{constructor(t,s){super("Response is not valid JSON"),this.response=t,this.body=s}},h=class extends t{load(t,s,e){fetch(t.url()).then(t=>{if(!t.ok)throw new l(t);return t.text().then(s=>{try{return JSON.parse(s)}catch{throw new c(t,s)}})}).then(t=>{s(t)}).catch(t=>{e(t)})}},d=class{_total=0;_loaded=0;_failed=0;_batchAssets=[];_onLoad;_onComplete;_onError;constructor(t,s,e,o){this._batchAssets=t,this._total=this._batchAssets.length,this._onLoad=s,this._onComplete=e,this._onError=o}loadAsset(t,s,e){const o=new r(t),a=this.loader(o);a?a.load(o,t=>{s({name:o.name(),url:o.url(),asset:t,type:o.type()})},t=>e(o.url(),t)):e(o.url(),/* @__PURE__ */new Error("Cant find loader for `"+o.url()+"`"))}progress(){return new e(this._total,this._loaded,this._failed)}loader(t){let e;switch(t.type()){case o.Font:e=new a;break;case o.Image:e=new s;break;case o.Audio:e=new i;break;case o.Json:e=new h}return e}},u=class extends d{fetch(){this._batchAssets.forEach(t=>{this.loadAsset(t,this.onLoadAssetSuccessCallback.bind(this),this.onLoadAssetErrorCallback.bind(this))})}onLoadAssetSuccessCallback(t){this._loaded++;const s=this.progress();this._onLoad(t,s),this.checkAndCompleteBatch(s)}onLoadAssetErrorCallback(t,s){this._failed++;const e=this.progress();this._onError(t,s),this.checkAndCompleteBatch(e)}checkAndCompleteBatch(t){t.isCompleted()&&this._onComplete(t)}},p=class{_store={[o.Font]:{},[o.Image]:{},[o.Audio]:{},[o.Json]:{}};font(t){return this._store[o.Font][t]?.asset}image(t){return this._store[o.Image][t]?.asset}audio(t){return this._store[o.Audio][t]?.asset}json(t){return this._store[o.Json][t]?.asset}asset(t){return this.image(t)??this.audio(t)??this.json(t)??this.font(t)}fetch(t){new u(t.assets(),(s,e)=>{this._store[s.type][s.name]=s,t.loaded(s.name,e)},s=>{t.completed(s)},(s,e)=>{t.error(s,e)}).fetch()}},_=class{constructor(t){this.options=t,t.assets=this.uniqueAssets(t.assets)}assets(){return this.options.assets}loaded(t,s){this.options.onLoad&&this.options.onLoad(t,s)}completed(t){this.options.onComplete&&this.options.onComplete(t)}error(t,s){this.options.onError&&this.options.onError(t,s)}uniqueAssets(t){return t.filter((t,s,e)=>s===e.indexOf(t))}};export{p as Assets,_ as AssetsBatch,e as Progress}; |
@@ -1,1 +0,1 @@ | ||
| (function(i,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(i=typeof globalThis<"u"?globalThis:i||self,l(i.Assets={}))})(this,(function(i){"use strict";class l{}class h extends l{load(t,e,o){const s=new Image;s.crossOrigin="Anonymous",s.onload=()=>{e(s)},s.onerror=n=>{typeof n=="string"&&o(new Error(n))},s.src=t.url()}}class c{_total;_loaded;_failed;constructor(t,e,o){this._total=t,this._loaded=e,this._failed=o}total(){return this._total}loaded(){return this._loaded}failed(){return this._failed}isCompleted(){return this.loaded()+this.failed()>=this.total()}percents(){return Math.floor(100/this.total()*(this.loaded()+this.failed()))}}var r=(a=>(a[a.Image=0]="Image",a[a.Font=1]="Font",a[a.Audio=2]="Audio",a))(r||{});class u{_batchAsset;constructor(t){this._batchAsset=t}url(){return typeof this._batchAsset=="string"?this._batchAsset:Object.values(this._batchAsset)[0]}name(){return typeof this._batchAsset=="string"?this.urlLastSegment().split(".")[0]:Object.keys(this._batchAsset)[0]}type(){const t=this.urlLastSegment().split(".").pop();let e;switch(t){case"woff2":case"woff":case"ttf":case"otf":e=r.Font;break;case"png":case"webp":case"jpg":case"jpeg":case"svg":case"gif":e=r.Image;break;case"webm":case"mp3":case"ogg":case"m4a":case"aac":case"wav":e=r.Audio;break;default:e=void 0;break}return e}urlLastSegment(){return this.url().split("?")[0].split("/").filter(t=>t!=="").pop()||""}}class f extends l{load(t,e,o){const s=new FontFace(t.name(),"url("+t.url()+")");document.fonts.add(s),s.load().then(()=>{e(t.name())}).catch(n=>{o(n)})}}class p extends l{load(t,e,o){const s=new Audio(t.url());s.oncanplaythrough=()=>{e(s)},s.onerror=()=>{const n=s.error?s.error.message:"Failed to load audio!";o(new Error(n))},s.load()}}class _{_total=0;_loaded=0;_failed=0;_batchAssets=[];_onLoad;_onComplete;_onError;constructor(t,e,o,s){this._batchAssets=t,this._total=this._batchAssets.length,this._onLoad=e,this._onComplete=o,this._onError=s}loadAsset(t,e,o){const s=new u(t),n=this.loader(s);n?n.load(s,d=>{e({name:s.name(),url:s.url(),asset:d,type:s.type()})},d=>o(s.url(),d)):o(s.url(),new Error("Cant find loader for `"+s.url()+"`"))}progress(){return new c(this._total,this._loaded,this._failed)}loader(t){let e;switch(t.type()){case r.Font:e=new f;break;case r.Image:e=new h;break;case r.Audio:e=new p;break}return e}}class m extends _{fetch(){this._batchAssets.forEach(t=>{this.loadAsset(t,this.onLoadAssetSuccessCallback.bind(this),this.onLoadAssetErrorCallback.bind(this))})}onLoadAssetSuccessCallback(t){this._loaded++;const e=this.progress();this._onLoad(t,e),this.checkAndCompleteBatch(e)}onLoadAssetErrorCallback(t,e){this._failed++;const o=this.progress();this._onError(t,e),this.checkAndCompleteBatch(o)}checkAndCompleteBatch(t){t.isCompleted()&&this._onComplete(t)}}class g{_store={[r.Font]:{},[r.Image]:{},[r.Audio]:{}};font(t){return this._store[r.Font][t]?.asset}image(t){return this._store[r.Image][t]?.asset}audio(t){return this._store[r.Audio][t]?.asset}fetch(t){new m(t.assets(),(e,o)=>{const s=this._store[e.type];s[e.name]=e,t.loaded(e.name,o)},e=>{t.completed(e)},(e,o)=>{t.error(e,o)}).fetch()}}class A{constructor(t){this.options=t,t.assets=this.uniqueAssets(t.assets)}assets(){return this.options.assets}loaded(t,e){this.options.onLoad&&this.options.onLoad(t,e)}completed(t){this.options.onComplete&&this.options.onComplete(t)}error(t,e){this.options.onError&&this.options.onError(t,e)}uniqueAssets(t){return t.filter((e,o,s)=>o===s.indexOf(e))}}i.Assets=g,i.AssetsBatch=A,i.Progress=c,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})})); | ||
| !function(t,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports):"function"==typeof define&&define.amd?define(["exports"],s):s((t="undefined"!=typeof globalThis?globalThis:t||self).Assets={})}(this,function(t){Object.defineProperty(t,Symbol.toStringTag,{value:"Module"});var s=class{},e=class extends s{load(t,s,e){const o=new Image;o.crossOrigin="Anonymous",o.onload=()=>{s(o)},o.onerror=t=>{e(new Error("string"==typeof t?t:"Failed to load image!"))},o.src=t.url()}},o=class{_total;_loaded;_failed;constructor(t,s,e){this._total=t,this._loaded=s,this._failed=e}total(){return this._total}loaded(){return this._loaded}failed(){return this._failed}isCompleted(){return this.loaded()+this.failed()>=this.total()}percents(){return Math.floor(100/this.total()*(this.loaded()+this.failed()))}},r=function(t){return t[t.Image=0]="Image",t[t.Font=1]="Font",t[t.Audio=2]="Audio",t[t.Json=3]="Json",t}({}),a=class{_batchAsset;constructor(t){this._batchAsset=t}url(){return"string"==typeof this._batchAsset?this._batchAsset:Object.values(this._batchAsset)[0]}name(){return"string"==typeof this._batchAsset?this.urlLastSegment().split(".")[0]:Object.keys(this._batchAsset)[0]}type(){let t;switch(this.urlLastSegment().split(".").pop()){case"woff2":case"woff":case"ttf":case"otf":t=r.Font;break;case"png":case"webp":case"jpg":case"jpeg":case"svg":case"gif":t=r.Image;break;case"webm":case"mp3":case"ogg":case"m4a":case"aac":case"wav":t=r.Audio;break;case"json":t=r.Json;break;default:t=void 0}return t}urlLastSegment(){return this.url().split("?")[0].split("/").filter(t=>""!==t).pop()||""}},n=class extends s{load(t,s,e){const o=new FontFace(t.name(),"url("+t.url()+")");document.fonts.add(o),o.load().then(()=>{s(t.name())}).catch(t=>{e(t instanceof Error?t:new Error("Failed to load font!"))})}},i=class extends Error{constructor(t){super(`MediaError ${t.code}`),this.mediaError=t}},l=class extends s{load(t,s,e){const o=new Audio(t.url());o.oncanplaythrough=()=>{s(o)},o.onerror=()=>{e(o.error?new i(o.error):new Error("Failed to load audio!"))},o.load()}},c=class extends Error{constructor(t){super(`HTTP ${t.status}`),this.response=t}},d=class extends Error{constructor(t,s){super("Response is not valid JSON"),this.response=t,this.body=s}},h=class extends s{load(t,s,e){fetch(t.url()).then(t=>{if(!t.ok)throw new c(t);return t.text().then(s=>{try{return JSON.parse(s)}catch{throw new d(t,s)}})}).then(t=>{s(t)}).catch(t=>{e(t)})}},u=class{_total=0;_loaded=0;_failed=0;_batchAssets=[];_onLoad;_onComplete;_onError;constructor(t,s,e,o){this._batchAssets=t,this._total=this._batchAssets.length,this._onLoad=s,this._onComplete=e,this._onError=o}loadAsset(t,s,e){const o=new a(t),r=this.loader(o);r?r.load(o,t=>{s({name:o.name(),url:o.url(),asset:t,type:o.type()})},t=>e(o.url(),t)):e(o.url(),new Error("Cant find loader for `"+o.url()+"`"))}progress(){return new o(this._total,this._loaded,this._failed)}loader(t){let s;switch(t.type()){case r.Font:s=new n;break;case r.Image:s=new e;break;case r.Audio:s=new l;break;case r.Json:s=new h}return s}},p=class extends u{fetch(){this._batchAssets.forEach(t=>{this.loadAsset(t,this.onLoadAssetSuccessCallback.bind(this),this.onLoadAssetErrorCallback.bind(this))})}onLoadAssetSuccessCallback(t){this._loaded++;const s=this.progress();this._onLoad(t,s),this.checkAndCompleteBatch(s)}onLoadAssetErrorCallback(t,s){this._failed++;const e=this.progress();this._onError(t,s),this.checkAndCompleteBatch(e)}checkAndCompleteBatch(t){t.isCompleted()&&this._onComplete(t)}};t.Assets=class{_store={[r.Font]:{},[r.Image]:{},[r.Audio]:{},[r.Json]:{}};font(t){return this._store[r.Font][t]?.asset}image(t){return this._store[r.Image][t]?.asset}audio(t){return this._store[r.Audio][t]?.asset}json(t){return this._store[r.Json][t]?.asset}asset(t){return this.image(t)??this.audio(t)??this.json(t)??this.font(t)}fetch(t){new p(t.assets(),(s,e)=>{this._store[s.type][s.name]=s,t.loaded(s.name,e)},s=>{t.completed(s)},(s,e)=>{t.error(s,e)}).fetch()}},t.AssetsBatch=class{constructor(t){this.options=t,t.assets=this.uniqueAssets(t.assets)}assets(){return this.options.assets}loaded(t,s){this.options.onLoad&&this.options.onLoad(t,s)}completed(t){this.options.onComplete&&this.options.onComplete(t)}error(t,s){this.options.onError&&this.options.onError(t,s)}uniqueAssets(t){return t.filter((t,s,e)=>s===e.indexOf(t))}},t.Progress=o}); |
| import { Loader } from './loader'; | ||
| import { LoadableAsset } from '../core/loadable-asset'; | ||
| import { AssetType } from '../enums/asset-type'; | ||
| import { AssetType } from '../enums'; | ||
| export declare class AudioLoader extends Loader<AssetType.Audio> { | ||
| load(loadableAsset: LoadableAsset, onSuccess: (audio: HTMLAudioElement) => void, onError: (error: Error) => void): void; | ||
| } |
| import { Loader } from './loader'; | ||
| import { LoadableAsset } from '../core/loadable-asset'; | ||
| import { AssetType } from '../enums/asset-type'; | ||
| import { AssetType } from '../enums'; | ||
| export declare class FontLoader extends Loader<AssetType.Font> { | ||
| load(loadableAsset: LoadableAsset, onSuccess: (font: string) => void, onError: (error: Error) => void): void; | ||
| } |
| import { Loader } from './loader'; | ||
| import { LoadableAsset } from '../core/loadable-asset'; | ||
| import { AssetType } from '../enums/asset-type'; | ||
| import { AssetType } from '../enums'; | ||
| export declare class ImageLoader extends Loader<AssetType.Image> { | ||
| load(loadableAsset: LoadableAsset, onSuccess: (image: HTMLImageElement) => void, onError: (error: Error) => void): void; | ||
| } |
| import { LoadableAsset } from '../core/loadable-asset'; | ||
| import { AssetResources } from '../interfaces/asset-resources.interface'; | ||
| import { AssetType } from '../enums/asset-type'; | ||
| export declare abstract class Loader<T extends AssetType = AssetType> { | ||
| abstract load(loadableAsset: LoadableAsset, onSuccess: (asset: AssetResources[T]) => void, onError: (error: Error) => void): void; | ||
| import { AssetResources } from '../types'; | ||
| import { AssetType } from '../enums'; | ||
| export declare abstract class Loader<Type extends AssetType = AssetType> { | ||
| abstract load<T = any>(loadableAsset: LoadableAsset, onSuccess: (asset: AssetResources<T>[Type]) => void, onError: (error: Error) => void): void; | ||
| } |
+20
-0
@@ -0,3 +1,23 @@ | ||
| import { AssetType } from './enums'; | ||
| import { Progress } from './core/progress'; | ||
| export type BatchAsset = string | { | ||
| [name: string]: string; | ||
| }; | ||
| export interface AssetRecord<Type extends AssetType = AssetType> { | ||
| url: string; | ||
| name: string; | ||
| type: Type; | ||
| asset: AssetResources[Type]; | ||
| } | ||
| export interface AssetResources<T = any> { | ||
| [AssetType.Font]: string; | ||
| [AssetType.Image]: HTMLImageElement; | ||
| [AssetType.Audio]: HTMLAudioElement; | ||
| [AssetType.Json]: T; | ||
| } | ||
| export interface AssetsBatchOptions { | ||
| assets: BatchAsset[]; | ||
| onLoad?: (name: string, progress: Progress) => void; | ||
| onComplete?: (progress: Progress) => void; | ||
| onError?: (url: string, error: Error) => void; | ||
| } |
+16
-14
| { | ||
| "name": "@armniko/assets", | ||
| "version": "1.3.0", | ||
| "version": "1.4.0", | ||
| "description": "Javascript/typescript library for fetching, storing and retrieving assets!", | ||
@@ -19,3 +19,4 @@ "author": "Armīns Nikolajevs <armins.nikolajevs@gmail.com>", | ||
| "images", | ||
| "audio" | ||
| "audio", | ||
| "json" | ||
| ], | ||
@@ -32,4 +33,4 @@ "main": "./dist/index.umd.js", | ||
| "dev": "vite build --watch", | ||
| "lint": "tsc --noEmit && eslint ./src --max-warnings=0 && npm run lint --prefix=demo", | ||
| "lint:fix": "npm run lint -- --fix && npm run lint:fix --prefix=demo", | ||
| "lint": "tsc --noEmit && eslint ./src ./test --max-warnings=0", | ||
| "lint:fix": "npm run lint -- --fix", | ||
| "test": "vitest", | ||
@@ -39,15 +40,16 @@ "test:coverage": "vitest run --coverage" | ||
| "devDependencies": { | ||
| "@eslint/js": "^9.39.2", | ||
| "@vitest/coverage-v8": "^4.0.16", | ||
| "eslint": "^9.39.2", | ||
| "@eslint/js": "^10.0.1", | ||
| "@vitest/coverage-v8": "^4.1.4", | ||
| "eslint": "^10.2.1", | ||
| "eslint-config-prettier": "^10.1.8", | ||
| "eslint-plugin-prettier": "^5.5.4", | ||
| "happy-dom": "^20.0.11", | ||
| "prettier": "^3.7.4", | ||
| "typescript": "^5.9.3", | ||
| "typescript-eslint": "^8.50.1", | ||
| "vite": "^7.3.0", | ||
| "eslint-plugin-prettier": "^5.5.5", | ||
| "happy-dom": "^20.9.0", | ||
| "prettier": "^3.8.3", | ||
| "terser": "^5.46.1", | ||
| "typescript": "^6.0.3", | ||
| "typescript-eslint": "^8.58.2", | ||
| "vite": "^8.0.8", | ||
| "vite-plugin-dts": "^4.5.4", | ||
| "vitest": "^4.0.16" | ||
| "vitest": "^4.1.4" | ||
| } | ||
| } |
+11
-3
@@ -12,2 +12,3 @@ <h1 align="center">Assets</h1> | ||
| Supported audio formats: webm, mp3, ogg, m4a, aac, wav<br> | ||
| Supported data formats: json<br> | ||
| </p> | ||
@@ -51,2 +52,9 @@ <hr> | ||
| <tr> | ||
| <td>v1.4.0</td> | ||
| <td> | ||
| Added JSON loader.<br> | ||
| Added asset() method to get assets without knowing type. | ||
| </td> | ||
| </tr> | ||
| <tr> | ||
| <td>v1.3.0</td> | ||
@@ -63,3 +71,3 @@ <td> | ||
| Option to specify custom asset name.<br> | ||
| Added font loader.<br> | ||
| Added font loader. | ||
| </td> | ||
@@ -70,3 +78,3 @@ </tr> | ||
| <td> | ||
| Precompile UMD and ESM<br> | ||
| Precompile UMD and ESM. | ||
| </td> | ||
@@ -77,5 +85,5 @@ </tr> | ||
| <td> | ||
| Initial version | ||
| Initial version. | ||
| </td> | ||
| </tr> | ||
| </table> |
| export declare enum AssetType { | ||
| Image = 0, | ||
| Font = 1, | ||
| Audio = 2 | ||
| } |
| import { AssetType } from '../enums/asset-type'; | ||
| import { AssetResources } from './asset-resources.interface'; | ||
| export interface AssetRecord<T extends AssetType = AssetType> { | ||
| url: string; | ||
| name: string; | ||
| type: T; | ||
| asset: AssetResources[T]; | ||
| } |
| import { AssetType } from '../enums/asset-type'; | ||
| export interface AssetResources { | ||
| [AssetType.Font]: string; | ||
| [AssetType.Image]: HTMLImageElement; | ||
| [AssetType.Audio]: HTMLAudioElement; | ||
| } |
| import { Progress } from '../core/progress'; | ||
| import { BatchAsset } from '../types'; | ||
| export interface AssetsBatchOptions { | ||
| assets: BatchAsset[]; | ||
| onLoad?: (name: string, progress: Progress) => void; | ||
| onComplete?: (progress: Progress) => void; | ||
| onError?: (url: string, error: Error) => void; | ||
| } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19050
4.82%22
4.76%85
10.39%13
8.33%144
-60.98%3
50%12
20%