mimicry/loadingFile.js

68 lines
2.4 KiB
JavaScript

"use strict";
(function loadingFilePrivateClosure () {
/**
* A small data structure to give shape to an entity that represents a loading file
* */
class LoadingFile {
/**
* @param {Mimicry.Type} contentType - what kind of file is loading
* @param {String} originalName - what was the original name before refining
* @param {Function} [callback] - optional success handler
* @param {Function} [error] - optional error handler
* */
constructor (contentType, originalName, callback, error) {
this.contentType = contentType;
this.originalName = originalName;
this.success = [];
this.error = [];
this.addSuccessHandler(callback);
this.addErrorHandler(error);
}
/**
* Finalizes the object
* */
destructor () {
delete this.contentType;
delete this.originalName;
delete this.success;
delete this.error;
}
/**
* Adds a handler for the case of success
* */
addSuccessHandler (/*Function*/callback) {
if (callback instanceof Function)
this.success.push(callback);
}
/**
* Adds a handler for the case of error
* */
addErrorHandler (/*Function*/callback) {
if (callback instanceof Function)
this.error.push(callback);
}
/**
* Notifies about success
*
* @param {Function} callback - a wrapper caller function, to call the actual callback (callCallback function in this file)
* @param {Object} context - a context for the wrapper caller function (Mimicry instance in this file)
* */
callSuccessHandlers (callback, context) {
for (let i = 0; i < this.success.length; ++i)
callback.call(context, this.success[i], this.originalName);
}
/**
* @param {BasicError} error - what happened
* */
callErrorHandlers (error) {
for (let i = 0; i < this.error.length; ++i)
this.error[i](error);
}
}
if (typeof module === 'object' && module.exports)
module.exports = LoadingFile;
else
window.__MimicryLoadingFile__ = LoadingFile;
})();