MCPcopy Create free account
hub / github.com/melonjs/melonJS / preload

Function preload

packages/melonjs/src/loader/loader.js:420–459  ·  view source on GitHub ↗
(assets, onloadcb, switchToLoadState = true)

Source from the content-addressed store, hash-verified

418 * @category Assets
419 */
420export function preload(assets, onloadcb, switchToLoadState = true) {
421 // set the onload callback if defined
422 if (typeof onloadcb !== "undefined") {
423 onload = onloadcb;
424 }
425
426 // parse the resources and collect a promise for each asset. `load()` now
427 // returns a Promise in its no-callback form, so there's no need to wrap a
428 // callback in `new Promise` here — just layer the progress / error reporting
429 // on top of it. Each asset counts as one toward the progress total (parsers
430 // load 0 or 1 resource; a cached asset resolves immediately and still
431 // reports progress, keeping `loadCount`/`resourceCount` balanced).
432 const promises = assets.map((asset) => {
433 resourceCount += 1;
434 return load(asset)
435 .then(() => {
436 onResourceLoaded(asset);
437 })
438 .catch((err) => {
439 // onLoadingError emits LOADER_ERROR and throws by design, so this
440 // re-rejects → Promise.all (and the returned promise) reject and
441 // `await loader.preload(...)` surfaces the failure. The trailing
442 // throw keeps the rejection meaningful should it ever stop throwing.
443 onLoadingError.call(this, asset);
444 throw err;
445 });
446 });
447
448 if (switchToLoadState === true) {
449 // switch to the loading screen
450 state.change(state.LOADING);
451 }
452
453 // Resolve once every asset has loaded; call the completion callback on
454 // success (back-compat). Returned so callers can `await loader.preload(...)`
455 // instead of (or as well as) passing a callback.
456 return Promise.all(promises).then(() => {
457 completeLoading(onload);
458 });
459}
460
461/**
462 * retry loading assets after a loading failure

Callers

nothing calls this directly

Calls 3

onResourceLoadedFunction · 0.85
completeLoadingFunction · 0.85
loadFunction · 0.70

Tested by

no test coverage detected