@mlightcad/common
Version:
The common package provides shared utilities and base classes that are used across the RealDWG-Web ecosystem. This package contains fundamental components for color management, event handling, logging, performance monitoring, and file loading operations.
133 lines • 5.13 kB
JavaScript
/**
* Handles and keeps track of loaded and pending data. A default global instance of this class is
* created and used by loaders if not supplied manually. In general that should be sufficient,
* however there are times when it can be useful to have separate loaders - for example if you want
* to show separate loading bars for objects and textures.
*/
var AcCmLoadingManager = /** @class */ (function () {
/**
* Create a new AcCmLoadingManager instance
* @param onLoad this function will be called when all loaders are done.
* @param onProgress this function will be called when an item is complete.
* @param onError this function will be called a loader encounters errors.
*/
function AcCmLoadingManager(onLoad, onProgress, onError) {
this.isLoading = false;
this.itemsLoaded = 0;
this.itemsTotal = 0;
this.urlModifier = undefined;
this.handlers = [];
// Refer to #5689 for the reason why we don't set .onStart
// in the constructor
this.onStart = undefined;
this.onLoad = onLoad;
this.onProgress = onProgress;
this.onError = onError;
}
/**
* This should be called by any loader using the manager when the loader starts loading an url.
* @param url The loaded url
*/
AcCmLoadingManager.prototype.itemStart = function (url) {
this.itemsTotal++;
if (this.isLoading === false) {
if (this.onStart !== undefined) {
this.onStart(url, this.itemsLoaded, this.itemsTotal);
}
}
this.isLoading = true;
};
/**
* This should be called by any loader using the manager when the loader ended loading an url.
* @param url The loaded url
*/
AcCmLoadingManager.prototype.itemEnd = function (url) {
this.itemsLoaded++;
if (this.onProgress !== undefined) {
this.onProgress(url, this.itemsLoaded, this.itemsTotal);
}
if (this.itemsLoaded === this.itemsTotal) {
this.isLoading = false;
if (this.onLoad !== undefined) {
this.onLoad();
}
}
};
/**
* This should be called by any loader using the manager when the loader errors loading an url.
* @param url The loaded url
*/
AcCmLoadingManager.prototype.itemError = function (url) {
if (this.onError !== undefined) {
this.onError(url);
}
};
/**
* Given a URL, uses the URL modifier callback (if any) and returns a resolved URL. If no URL
* modifier is set, returns the original URL.
* @param url The url to load
* @returns Return resolved URL
*/
AcCmLoadingManager.prototype.resolveURL = function (url) {
if (this.urlModifier) {
return this.urlModifier(url);
}
return url;
};
/**
* If provided, the callback will be passed each resource URL before a request is sent. The callback
* may return the original URL, or a new URL to override loading behavior. This behavior can be used
* to load assets from .ZIP files, drag-and-drop APIs, and Data URIs.
* @param transform URL modifier callback. Called with url argument, and must return resolvedURL.
* @returns Return this object
*/
AcCmLoadingManager.prototype.setURLModifier = function (transform) {
this.urlModifier = transform;
return this;
};
/**
* Register a loader with the given regular expression. Can be used to define what loader should
* be used in order to load specific files. A typical use case is to overwrite the default loader
* for textures.
* @param regex A regular expression.
* @param loader The loader.
* @returns Return this object
*/
AcCmLoadingManager.prototype.addHandler = function (regex, loader) {
this.handlers.push(regex, loader);
return this;
};
/**
* Remove the loader for the given regular expression.
* @param regex A regular expression.
* @returns Return this object
*/
AcCmLoadingManager.prototype.removeHandler = function (regex) {
var index = this.handlers.indexOf(regex);
if (index !== -1) {
this.handlers.splice(index, 2);
}
return this;
};
/**
* Retrieve the registered loader for the given file path.
* @param file The file path.
* @returns Return the registered loader for the given file path.
*/
AcCmLoadingManager.prototype.getHandler = function (file) {
for (var i = 0, l = this.handlers.length; i < l; i += 2) {
var regex = this.handlers[i];
var loader = this.handlers[i + 1];
if (regex.global)
regex.lastIndex = 0; // see #17920
if (regex.test(file)) {
return loader;
}
}
return null;
};
return AcCmLoadingManager;
}());
export { AcCmLoadingManager };
export var DefaultLoadingManager = /*@__PURE__*/ new AcCmLoadingManager();
//# sourceMappingURL=AcCmLoadingManager.js.map