@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
44 lines (43 loc) • 1.42 kB
JavaScript
class GirafeSingleton {
constructor(type) {
if (!(type in GirafeSingleton.initializingSingletons) || !GirafeSingleton.initializingSingletons[type]) {
throw new Error('This is a singleton. Please use the getInstance() method.');
}
}
static getInstance() {
const type = this.name;
if (!(type in GirafeSingleton.instances)) {
// Singleton do not exists
// => create it
GirafeSingleton.initializingSingletons[type] = true;
try {
// Get the child constructor, and create and instance of the child
const singleton = new this(type);
GirafeSingleton.instances[type] = singleton;
}
finally {
GirafeSingleton.initializingSingletons[type] = false;
}
}
return GirafeSingleton.instances[type];
}
isNullOrUndefined(val) {
return val === undefined || val === null;
}
isNullOrUndefinedOrBlank(val) {
return val === undefined || val === null || val === '';
}
}
Object.defineProperty(GirafeSingleton, "instances", {
enumerable: true,
configurable: true,
writable: true,
value: {}
});
Object.defineProperty(GirafeSingleton, "initializingSingletons", {
enumerable: true,
configurable: true,
writable: true,
value: {}
});
export default GirafeSingleton;